Skip to content

Commit 3926bfb

Browse files
User roles Editable.
1 parent c61af48 commit 3926bfb

File tree

6 files changed

+142
-23
lines changed

6 files changed

+142
-23
lines changed

src/IdentityServer/Areas/HeliosAdminUI/Controllers/UserManagementController.cs

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using IdentityServer.Areas.HeliosAdminUI.Models.UserManagement;
33
using IdentityServer.Models;
44
using Microsoft.AspNetCore.Authorization;
5-
using Microsoft.AspNetCore.Http;
65
using Microsoft.AspNetCore.Identity;
76
using Microsoft.AspNetCore.Mvc;
87
using Microsoft.EntityFrameworkCore;
@@ -13,7 +12,7 @@
1312
namespace IdentityServer.Areas.HeliosAdminUI.Controllers
1413
{
1514
[Area("HeliosAdminUI")]
16-
[Authorize(Roles ="IsAdmin")]
15+
[Authorize(Roles = "IsAdmin")]
1716
public class UserManagementController : Controller
1817
{
1918
private readonly UserManager<ApplicationUser> _userMgr;
@@ -60,13 +59,12 @@ public async Task<IActionResult> GetAllUsers(bool isSuccess = false, bool error
6059
public ActionResult CreateUser(bool isSuccess = false)
6160
{
6261
ViewBag.isSuccess = isSuccess;
63-
var vm = new CreateUserWithRoleWithViewModel();
64-
vm.RoleChoices = _roleMgr.Roles.Select(x => x.Name).ToList();
62+
var vm = new CreateUserWithRoleWithViewModel();
63+
vm.RoleChoices = _roleMgr.Roles.Select(x => x.Name).ToList();
6564

66-
return View(vm);
65+
return View(vm);
6766
}
6867

69-
// POST: UserManagementController/Create
7068
[HttpPost]
7169
[ValidateAntiForgeryToken]
7270
public async Task<IActionResult> CreateUser(CreateUserWithRoleWithViewModel model)
@@ -76,6 +74,14 @@ public async Task<IActionResult> CreateUser(CreateUserWithRoleWithViewModel mode
7674
return View(model);
7775
}
7876
var user = _mapper.Map<ApplicationUser>(model);
77+
var EmailExist = await _userMgr.FindByEmailAsync(user.Email);
78+
var nameExist = await _userMgr.FindByNameAsync(user.UserName);
79+
if (EmailExist!= null || nameExist != null)
80+
{
81+
ModelState.AddModelError(string.Empty, "User with given Username/Email already exist.");
82+
model.RoleChoices = _roleMgr.Roles.Select(x => x.Name).ToList();
83+
return View(model);
84+
}
7985
var result = await _userMgr.CreateAsync(user, model.Password);
8086
if (result.Succeeded)
8187
{
@@ -93,33 +99,49 @@ public async Task<IActionResult> CreateUser(CreateUserWithRoleWithViewModel mode
9399
}
94100

95101
// GET: UserManagementController/Edit/5
96-
public ActionResult Edit(int id)
102+
public async Task<IActionResult> EditUserRoles(string id, bool isSuccess = false, bool error = false)
97103
{
98-
return View();
104+
ViewBag.isSuccess = isSuccess;
105+
ViewBag.error = error;
106+
107+
var user = await _userMgr.FindByIdAsync(id);
108+
var vm = _mapper.Map<UpdateUseRolesViewModel>(user);
109+
110+
var roles = _roleMgr.Roles.Select(x => x.Name);
111+
vm.RoleChoices = _roleMgr.Roles.Select(x => x.Name).ToList();
112+
vm.RolesString = string.Join(",", await _userMgr.GetRolesAsync(user));
113+
114+
return View(vm);
99115
}
100116

101-
// POST: UserManagementController/Edit/5
102117
[HttpPost]
103118
[ValidateAntiForgeryToken]
104-
public ActionResult Edit(int id, IFormCollection collection)
119+
public async Task<IActionResult> EditUserRoles(string id, UpdateUseRolesViewModel model)
105120
{
106-
try
121+
122+
var user = await _userMgr.FindByIdAsync(id);
123+
if (user == null)
107124
{
108-
return RedirectToAction(nameof(Index));
125+
return NotFound();
109126
}
110-
catch
127+
var DBroles = await _userMgr.GetRolesAsync(user);
128+
var roleRemoveResult = await _userMgr.RemoveFromRolesAsync(user, DBroles);
129+
if (!roleRemoveResult.Succeeded)
111130
{
112-
return View();
131+
return RedirectToAction(nameof(EditUserRoles), new { error = true });
113132
}
114-
}
115133

116-
public async Task<IActionResult> DeleteUser(string? id)
117-
{
118-
if (id == null)
134+
var addToRoleResult = await _userMgr.AddToRolesAsync(user, model.Roles);
135+
if (!roleRemoveResult.Succeeded)
119136
{
120-
return NotFound();
137+
return RedirectToAction(nameof(EditUserRoles), new { error = true });
121138
}
139+
return RedirectToAction(nameof(GetAllUsers), new { isSuccess = true });
122140

141+
}
142+
143+
public async Task<IActionResult> DeleteUser(string id)
144+
{
123145
var entity = await _userMgr.FindByIdAsync(id);
124146
if (entity == null)
125147
{
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using IdentityServer.Areas.HeliosAdminUI.Helpers;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
4+
5+
namespace IdentityServer.Areas.HeliosAdminUI.Models.UserManagement
6+
{
7+
public class UpdateUseRolesViewModel
8+
{
9+
[Editable(false)]
10+
public string Id { get; set; }
11+
public List<string> Roles
12+
{
13+
get { return string.IsNullOrEmpty(RolesString) ? new List<string>() : UserRolesHelper.CreateRoles(RolesString); }
14+
set { }
15+
}
16+
17+
[Display(Name = "Roles")]
18+
[StringLength(250)]
19+
public string RolesString { get; set; }
20+
public List<string> RoleChoices { get; set; }
21+
}
22+
}

src/IdentityServer/Areas/HeliosAdminUI/Profiles/MappingProfiles.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public MappingProfiles()
5757

5858
#region Users
5959
CreateMap<ApplicationUser, UserWithRoles>();
60+
CreateMap<ApplicationUser, UpdateUseRolesViewModel>().ReverseMap();
6061
CreateMap<CreateUserWithRoleWithViewModel, ApplicationUser>();
6162
#endregion
6263

src/IdentityServer/Areas/HeliosAdminUI/Views/UserManagement/CreateUser.cshtml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
</div>
1515
}
1616
}
17+
<div class="alert alert-danger alert-dismissible fade show" role="alert" asp-validation-summary="All">
18+
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
19+
<span aria-hidden="true">&times;</span>
20+
</button>
21+
</div>
1722
<a asp-action="GetAllUsers" class="btn btn-primary mb-3"> <i class="bi bi-arrow-left-circle-fill"></i> Go back to List </a>
1823

1924
<div class="card">
@@ -62,7 +67,7 @@
6267
<div class="form-group col-6">
6368
<div class="form-control-plaintext">
6469
<div class="custom-control custom-checkbox">
65-
<input asp-for="EmailConfirmed" class="custom-control-input" />
70+
<input asp-for="EmailConfirmed" class="custom-control-input" />
6671
<label asp-for="EmailConfirmed" class="custom-control-label"></label>
6772
</div>
6873
</div>
@@ -97,8 +102,7 @@
97102
@section Scripts
98103
{
99104
<script>
100-
$(document).ready(function ()
101-
{
105+
$(document).ready(function () {
102106
$('input[name="roles"]').click(function () {
103107
104108
var newVal = $('input[name="roles"]:checked').map(function () {
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
@model UpdateUseRolesViewModel
2+
@section style{
3+
4+
@*<link href="~/css/TagsInput.css" rel="stylesheet" />*@
5+
}
6+
@{
7+
if (ViewBag.error == true)
8+
{
9+
<div class="alert alert-danger alert-dismissible fade show" role="alert">
10+
There was an error while updating your user, if the problem persist, contact your administrator. <br />
11+
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
12+
<span aria-hidden="true">&times;</span>
13+
</button>
14+
</div>
15+
}
16+
}
17+
<a asp-action="GetAllUsers" class="btn btn-primary mb-3"> <i class="bi bi-arrow-left-circle-fill"></i> Go back to List </a>
18+
19+
<div class="card">
20+
<div class="card-header identity-resource-theme"></div>
21+
<div class="card-body">
22+
<h4 class="card-title">Update User Roles</h4>
23+
<hr />
24+
<form method="post" asp-action="EditUserRoles">
25+
<input type="hidden" asp-for="Id" />
26+
<div class="row">
27+
<div class="form-group col-6">
28+
<label asp-for="RolesString" class="col-form-label"></label>
29+
<div>
30+
@foreach (var role in Model.RoleChoices)
31+
{
32+
<input type="checkbox" name="roles" value="@role"
33+
@(Model.Roles.Contains(role) ? "checked='checked'":"")>
34+
<label for="@role">@role</label>
35+
}
36+
</div>
37+
<input id="tag-holder" asp-for="RolesString" type="hidden">
38+
</div>
39+
</div>
40+
<hr />
41+
42+
<div class="form-group row">
43+
44+
<div class="col-2">
45+
<button class="btn btn-success btn-block">Update <i class="bi bi-check-square ml-1"></i></button>
46+
</div>
47+
48+
</div>
49+
</form>
50+
</div>
51+
</div>
52+
53+
@section Scripts
54+
{
55+
<script>
56+
$(document).ready(function () {
57+
$('input[name="roles"]').click(function () {
58+
59+
setCheck();
60+
});
61+
});
62+
63+
function setCheck() {
64+
var newVal = $('input[name="roles"]:checked').map(function () {
65+
return this.value;
66+
}).get().join();
67+
$("#tag-holder").val(newVal);
68+
}
69+
</script>
70+
}

src/IdentityServer/Areas/HeliosAdminUI/Views/UserManagement/GetAllUsers.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
}
6767
</td>
6868
<td>
69-
<a asp-action="EditUser" asp-route-id="@user.Id" class="btn btn-success text-white">
69+
<a asp-action="EditUserRoles" asp-route-id="@user.Id" class="btn btn-success text-white">
7070
<i class="bi bi-pen"></i>
7171
</a>
7272
<a asp-action="DeleteUser" asp-route-id="@user.Id" class="btn btn-danger text-white">

0 commit comments

Comments
 (0)