Skip to content

Commit

Permalink
Role kontrolü tamamlandı ajax loader ve datepicker kaldı
Browse files Browse the repository at this point in the history
  • Loading branch information
batuhan çiftçi committed May 21, 2022
1 parent e72070f commit 2fa253c
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 26 deletions.
19 changes: 9 additions & 10 deletions UserManagement/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public ActionResult Details(int? id)
// GET: Users/Create
public ActionResult Create()
{
return View();
User user = new User();
user.Role = Enums.Roles.User;
return View(user);
}

// POST: Users/Create
Expand All @@ -63,7 +65,7 @@ public ActionResult Create([Bind(Include = "Id,Email,Password,Name,Surname,Phone
return View(user);
}

// GET: Users/Edit/5
[Authorize(Roles = "Admin")]
public ActionResult Edit(int? id)
{
if (id == null)
Expand All @@ -77,20 +79,17 @@ public ActionResult Edit(int? id)
}
EditDto editUser = new EditDto();
editUser.user = user;
editUser.roles = (Enums.Roles)editUser.user.Role;
return View(editUser);
}

// POST: Users/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.

[Authorize(Roles = "Admin")]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(EditDto editUser)
{
if (ModelState.IsValid)
{
editUser.user.Role = editUser.roles;
if(editUser.newPassword != null)
editUser.user.Password = editUser.newPassword.ToMD5();
db.Entry(editUser.user).State = EntityState.Modified;
Expand All @@ -99,8 +98,8 @@ public ActionResult Edit(EditDto editUser)
}
return View(editUser);
}

// GET: Users/Delete/5
[Authorize(Roles = "Admin")]
public ActionResult Delete(int? id)
{
if (id == null)
Expand All @@ -115,7 +114,7 @@ public ActionResult Delete(int? id)
return View(user);
}

// POST: Users/Delete/5
[Authorize(Roles = "Admin")]
[HttpPost, ActionName("Delete")]
public JsonResult DeleteConfirmed(int id)
{
Expand Down
1 change: 0 additions & 1 deletion UserManagement/Dtos/UserDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public class UserDto
public class EditDto
{
public User user { get; set; }
public Roles roles { get; set; }
public string newPassword { get; set; }
}
}
Expand Down
66 changes: 66 additions & 0 deletions UserManagement/Helper/Security/UserRoleProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using UserManagement.Models;

namespace UserManagement.Helper.Security
{
public class UserRoleProvider : RoleProvider
{
public override string ApplicationName { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

public override void AddUsersToRoles(string[] usernames, string[] roleNames)
{
throw new NotImplementedException();
}

public override void CreateRole(string roleName)
{
throw new NotImplementedException();
}

public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
{
throw new NotImplementedException();
}

public override string[] FindUsersInRole(string roleName, string usernameToMatch)
{
throw new NotImplementedException();
}

public override string[] GetAllRoles()
{
throw new NotImplementedException();
}

public override string[] GetRolesForUser(string username)
{
UserManagementDbContext db = new UserManagementDbContext();
Enums.Roles userRole = db.Users.FirstOrDefault(f => f.Email == username).Role;
return new string[] { userRole.ToString()};
}

public override string[] GetUsersInRole(string roleName)
{
throw new NotImplementedException();
}

public override bool IsUserInRole(string username, string roleName)
{
throw new NotImplementedException();
}

public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
{
throw new NotImplementedException();
}

public override bool RoleExists(string roleName)
{
throw new NotImplementedException();
}
}
}
8 changes: 4 additions & 4 deletions UserManagement/Views/Users/Create.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
<h2>Create</h2>


@using (Html.BeginForm())
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
<h4>User</h4>
<hr />
Expand Down Expand Up @@ -59,15 +59,15 @@
<div class="form-group">
@Html.LabelFor(model => model.BirthDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BirthDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.TextBoxFor(model => model.BirthDate, new { @Value = DateTime.Now.ToString("yyyy/MM/dd"), @class = "form-control" })
@Html.ValidationMessageFor(model => model.BirthDate, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Role, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Role, new { htmlAttributes = new { @class = "form-control" } })
@Html.EnumDropDownListFor(model => model.Role, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Role, "", new { @class = "text-danger" })
</div>
</div>
Expand Down
7 changes: 5 additions & 2 deletions UserManagement/Views/Users/Details.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.Id }) |
@Html.ActionLink("Back to List", "Index")
@if (User.IsInRole("Admin"))
{
@Html.ActionLink("Edit", "Edit", new { id = Model.Id }, new { @class = "btn btn-warning" })
}
@Html.ActionLink("Back to List", "Index", null, new { @class = "btn btn-light" })
</p>
8 changes: 3 additions & 5 deletions UserManagement/Views/Users/Edit.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.user.Id)
@Html.HiddenFor(model => model.user.Password)
@Html.HiddenFor(model => model.user.Role)

<div class="form-group">
@Html.LabelFor(model => model.user.Email, htmlAttributes: new { @class = "control-label col-md-2" })
Expand Down Expand Up @@ -62,16 +61,15 @@
<div class="form-group">
@Html.LabelFor(model => model.user.BirthDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.user.BirthDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.EditorFor(model => model.user.BirthDate.Date, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.user.BirthDate, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.user.Role, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EnumDropDownListFor(e => e.roles,
new { @class = "form-control" })
@Html.EnumDropDownListFor(model => model.user.Role, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.user.Role, "", new { @class = "text-danger" })
</div>
</div>
Expand All @@ -85,7 +83,7 @@ new { @class = "form-control" })
}

<div>
@Html.ActionLink("Back to List", "Index")
@Html.ActionLink("Back to List", "Index", null, new { @class = "btn btn-light" })
</div>

@section Scripts {
Expand Down
11 changes: 7 additions & 4 deletions UserManagement/Views/Users/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@
@Html.DisplayFor(modelItem => item.Role)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Details", "Details", new { id = item.Id }) |
<button data-key=@item.Id class="btn btn-danger deleteUser">Delete</button>
@Html.ActionLink("Details", "Details", new { id = item.Id }, new { @class= "btn btn-success" })
@if (User.IsInRole("Admin"))
{
@Html.ActionLink("Edit", "Edit", new { id = item.Id }, new { @class = "btn btn-warning" })
<button data-key=@item.Id class="btn btn-danger deleteUser">Delete</button>
}
</td>
</tr>
}
Expand All @@ -75,7 +78,7 @@
if (confirmation) {
//$("#spinner-div").show(); //Load button clicked show spinner
$.ajax({
url: '/Users/Delete/'+ id,
url: '/Users/Delete/' + id,
type: 'POST',
dataType: 'json',
success: function (data) {
Expand Down
5 changes: 5 additions & 0 deletions UserManagement/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
<authentication mode="Forms">
<forms loginUrl="Auth/Login"></forms>
</authentication>
<roleManager enabled="true" defaultProvider="userRoleProvider">
<providers>
<add name="userRoleProvider" type="UserManagement.Helper.Security.UserRoleProvider" />
</providers>
</roleManager>
</system.web>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
Expand Down

0 comments on commit 2fa253c

Please sign in to comment.