Skip to content

Commit 11172c1

Browse files
Roles management functional.
1 parent 3926bfb commit 11172c1

File tree

11 files changed

+284
-11
lines changed

11 files changed

+284
-11
lines changed

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

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ public ActionResult UserHome()
3939
{
4040
return View();
4141
}
42+
43+
public ActionResult RoleHome()
44+
{
45+
return View();
46+
}
47+
48+
4249
public async Task<IActionResult> GetAllUsers(bool isSuccess = false, bool error = false)
4350
{
4451
ViewBag.isSuccess = isSuccess;
@@ -55,6 +62,21 @@ public async Task<IActionResult> GetAllUsers(bool isSuccess = false, bool error
5562
return View(vm);
5663
}
5764

65+
public async Task<IActionResult> GetAllRoles()
66+
{
67+
var roles = await _roleMgr.Roles.ToListAsync();
68+
var vm = new List<RoleViewModel>();
69+
foreach (var role in roles)
70+
{
71+
vm.Add(new RoleViewModel
72+
{
73+
Id = role.Id,
74+
Name = role.Name
75+
});
76+
}
77+
return View(vm);
78+
}
79+
5880
[HttpGet]
5981
public ActionResult CreateUser(bool isSuccess = false)
6082
{
@@ -65,6 +87,14 @@ public ActionResult CreateUser(bool isSuccess = false)
6587
return View(vm);
6688
}
6789

90+
[HttpGet]
91+
public ActionResult CreateRole(bool isSuccess = false)
92+
{
93+
ViewBag.isSuccess = isSuccess;
94+
var vm = new CreateRoleViewModel();
95+
return View(vm);
96+
}
97+
6898
[HttpPost]
6999
[ValidateAntiForgeryToken]
70100
public async Task<IActionResult> CreateUser(CreateUserWithRoleWithViewModel model)
@@ -76,8 +106,9 @@ public async Task<IActionResult> CreateUser(CreateUserWithRoleWithViewModel mode
76106
var user = _mapper.Map<ApplicationUser>(model);
77107
var EmailExist = await _userMgr.FindByEmailAsync(user.Email);
78108
var nameExist = await _userMgr.FindByNameAsync(user.UserName);
79-
if (EmailExist!= null || nameExist != null)
109+
if (EmailExist != null || nameExist != null)
80110
{
111+
ViewBag.error = true;
81112
ModelState.AddModelError(string.Empty, "User with given Username/Email already exist.");
82113
model.RoleChoices = _roleMgr.Roles.Select(x => x.Name).ToList();
83114
return View(model);
@@ -98,7 +129,30 @@ public async Task<IActionResult> CreateUser(CreateUserWithRoleWithViewModel mode
98129
return RedirectToAction(nameof(CreateUser), new { isSuccess = true });
99130
}
100131

101-
// GET: UserManagementController/Edit/5
132+
[HttpPost]
133+
[ValidateAntiForgeryToken]
134+
public async Task<IActionResult> CreateRole(CreateRoleViewModel model)
135+
{
136+
if (!ModelState.IsValid)
137+
{
138+
return View(model);
139+
}
140+
var roleExist = await _roleMgr.RoleExistsAsync(model.Name);
141+
if (!roleExist)
142+
{
143+
var result = await _roleMgr.CreateAsync(new IdentityRole(model.Name));
144+
if (result.Succeeded)
145+
{
146+
return RedirectToAction(nameof(CreateRole), new { isSuccess = true });
147+
}
148+
return RedirectToAction(nameof(GetAllRoles), new { error = true });
149+
150+
}
151+
ViewBag.error = true;
152+
ModelState.AddModelError(string.Empty, "A role with this Name already exist");
153+
return View(model);
154+
155+
}
102156
public async Task<IActionResult> EditUserRoles(string id, bool isSuccess = false, bool error = false)
103157
{
104158
ViewBag.isSuccess = isSuccess;
@@ -140,6 +194,7 @@ public async Task<IActionResult> EditUserRoles(string id, UpdateUseRolesViewMode
140194

141195
}
142196

197+
[HttpGet]
143198
public async Task<IActionResult> DeleteUser(string id)
144199
{
145200
var entity = await _userMgr.FindByIdAsync(id);
@@ -151,6 +206,23 @@ public async Task<IActionResult> DeleteUser(string id)
151206
return View(vm);
152207
}
153208

209+
[HttpGet]
210+
public async Task<IActionResult> DeleteRole(string id)
211+
{
212+
var entity = await _roleMgr.FindByIdAsync(id);
213+
if (entity == null)
214+
{
215+
return NotFound();
216+
}
217+
218+
var vm = new RoleViewModel()
219+
{
220+
Id = entity.Id,
221+
Name = entity.Name
222+
};
223+
return View(vm);
224+
}
225+
154226
[HttpPost, ActionName("DeleteUser")]
155227
[ValidateAntiForgeryToken]
156228
public async Task<IActionResult> DeleteUserConfirmed(string id)
@@ -167,5 +239,23 @@ public async Task<IActionResult> DeleteUserConfirmed(string id)
167239
}
168240
return RedirectToAction(nameof(GetAllUsers), new { error = true });
169241
}
242+
243+
[HttpPost, ActionName("DeleteRole")]
244+
[ValidateAntiForgeryToken]
245+
public async Task<IActionResult> DeleteRoleConfirmed(string id)
246+
{
247+
var entity = await _roleMgr.FindByIdAsync(id);
248+
if (entity == null)
249+
{
250+
return NotFound();
251+
}
252+
var result = await _roleMgr.DeleteAsync(entity);
253+
if (result.Succeeded)
254+
{
255+
return RedirectToAction(nameof(GetAllRoles), new { isSuccess = true });
256+
}
257+
return RedirectToAction(nameof(GetAllRoles), new { error = true });
258+
259+
}
170260
}
171261
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace IdentityServer.Areas.HeliosAdminUI.Models.UserManagement
4+
{
5+
public class CreateRoleViewModel
6+
{
7+
[Required]
8+
[StringLength(100)]
9+
public string Name { get; set; }
10+
}
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace IdentityServer.Areas.HeliosAdminUI.Models.UserManagement
2+
{
3+
public class RoleViewModel
4+
{
5+
public string Id { get; set; }
6+
public string Name { get; set; }
7+
}
8+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
@model CreateRoleViewModel
2+
@section style{
3+
4+
<link href="~/css/TagsInput.css" rel="stylesheet" />
5+
}
6+
@{
7+
if (ViewBag.isSuccess == true)
8+
{
9+
<div class="alert alert-success alert-dismissible fade show" role="alert">
10+
Your new Role has been added successfully. <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+
if (ViewBag.error == true)
17+
{
18+
<div>
19+
<div class="alert alert-danger alert-dismissible fade show" role="alert" asp-validation-summary="All">
20+
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
21+
<span aria-hidden="true">&times;</span>
22+
</button>
23+
</div>
24+
</div>
25+
}
26+
}
27+
28+
<a asp-action="GetAllRoles" class="btn btn-primary mb-3"> <i class="bi bi-arrow-left-circle-fill"></i> Go back to List </a>
29+
30+
<div class="card">
31+
<div class="card-header sub-role-theme"></div>
32+
<div class="card-body">
33+
<h4 class="card-title">Create new Role</h4>
34+
<hr />
35+
<form method="post" asp-action="CreateRole">
36+
<div class="row">
37+
<div class="form-group col-6">
38+
<label asp-for="Name" class="col-form-label"></label>
39+
<div>
40+
<input asp-for="Name" class="form-control" />
41+
</div>
42+
<span class="text-danger" asp-validation-for="Name"></span>
43+
</div>
44+
</div>
45+
46+
<div class="form-group row">
47+
48+
<div class="col-2">
49+
<button class="btn btn-success btn-block">Create <i class="bi bi-check-square ml-1"></i></button>
50+
</div>
51+
52+
</div>
53+
</form>
54+
</div>
55+
</div>
56+

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,20 @@
1313
</button>
1414
</div>
1515
}
16+
if (ViewBag.error == true)
17+
{
18+
<div class="alert alert-danger alert-dismissible fade show" role="alert" asp-validation-summary="All">
19+
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
20+
<span aria-hidden="true">&times;</span>
21+
</button>
22+
</div>
23+
}
1624
}
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>
25+
2226
<a asp-action="GetAllUsers" class="btn btn-primary mb-3"> <i class="bi bi-arrow-left-circle-fill"></i> Go back to List </a>
2327

2428
<div class="card">
25-
<div class="card-header identity-resource-theme"></div>
29+
<div class="card-header sub-user-theme"></div>
2630
<div class="card-body">
2731
<h4 class="card-title">Create new User</h4>
2832
<hr />
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@model RoleViewModel;
2+
3+
<a asp-action="GetAllRoles" class="btn btn-primary mb-3"> <i class="bi bi-arrow-left-circle-fill"></i> Go back to List</a>
4+
5+
<div class="card">
6+
<div class="card-header sub-role-theme"></div>
7+
<div class="card-body">
8+
<h4 class="card-title">Delete User</h4>
9+
<hr />
10+
<form method="post" asp-controller="UserManagement" asp-action="DeleteRole" asp-route-id="@Model.Id">
11+
<p>Are you sure you want to delete the role <strong>@Model.Name</strong>? <br /> The action will be irreversible.</p>
12+
<button class="btn btn-danger btn-block">Delete <i class="bi bi-trash ml-1"></i></button>
13+
</form>
14+
</div>
15+
</div>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
@model List<RoleViewModel>;
2+
@{
3+
if (ViewBag.error == true)
4+
{
5+
<div class="alert alert-danger alert-dismissible fade show" role="alert">
6+
An error occured while processing your request. Contact your administrator. <br />
7+
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
8+
<span aria-hidden="true">&times;</span>
9+
</button>
10+
</div>
11+
}
12+
if (ViewBag.isSuccess == true)
13+
{
14+
<div class="alert alert-success alert-dismissible fade show" role="alert">
15+
Your operation was a success. <br />
16+
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
17+
<span aria-hidden="true">&times;</span>
18+
</button>
19+
</div>
20+
}
21+
}
22+
<div class="row p-3 m-3">
23+
<a class="btn btn-primary" asp-action="RoleHome"> <i class="bi bi-arrow-left-circle-fill"></i> Go back</a>
24+
25+
<a class="btn btn-success ml-auto" asp-action="CreateRole">Create Role <i class="bi bi-lightning"></i></a>
26+
27+
</div>
28+
29+
<div class="card">
30+
<div class="card-header sub-role-theme"></div>
31+
<div class="card-body">
32+
<h4 class="card-title">Roles list</h4>
33+
<hr />
34+
<table class="table table-bordered table-hover">
35+
<thead class="thead-dark">
36+
<tr>
37+
<th scope="col">Name</th>
38+
<th scope="col">Actions</th>
39+
</tr>
40+
</thead>
41+
<tbody>
42+
@foreach (var role in Model)
43+
{
44+
<tr>
45+
<td>@role.Name</td>
46+
47+
<td class="text-center">
48+
<a asp-action="DeleteRole" asp-route-id="@role.Id" class="btn btn-danger text-white">
49+
<i class="bi bi-trash"></i>
50+
</a>
51+
</td>
52+
</tr>
53+
}
54+
</tbody>
55+
</table>
56+
57+
</div>
58+
</div>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<th scope="col">Email</th>
3939
<th scope="col">EmailConfirmed</th>
4040
<th scope="col">LockoutEnabled</th>
41-
<th scope="col">Roles</th>
41+
<th scope="col">Role(s)</th>
4242
<th scope="col">Actions</th>
4343
</tr>
4444
</thead>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<div class="card-body">
2424
<h5 class="card-title">Roles</h5>
2525
<p class="card-text">Manage the Roles.</p>
26-
<a asp-controller="UserManagement" asp-action="Index" class="btn btn-primary">Go </a>
26+
<a asp-controller="UserManagement" asp-action="RoleHome" class="btn btn-primary">Go </a>
2727
</div>
2828
<div class="card-footer sub-role-theme"></div>
2929
</div>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<a asp-action="Index" class="btn btn-primary mb-3"> <i class="bi bi-arrow-left-circle-fill"></i> Go back to User Management home</a>
2+
3+
<div class="row d-flex justify-content-center mt-3">
4+
<div class="col-lg-6">
5+
<div class="card text-center">
6+
<div class="card-header sub-role-theme">
7+
8+
</div>
9+
<div class="card-body">
10+
<h5 class="card-title">List</h5>
11+
<p class="card-text">Access a list of all roles stored on the server.</p>
12+
<a asp-controller="UserManagement" asp-action="GetAllRoles" class="btn btn-primary">Go </a>
13+
</div>
14+
<div class="card-footer sub-role-theme"></div>
15+
</div>
16+
</div>
17+
18+
<div class="col-lg-6">
19+
<div class="card text-center">
20+
<div class="card-header sub-role-theme">
21+
22+
</div>
23+
<div class="card-body">
24+
<h5 class="card-title">Create Role</h5>
25+
<p class="card-text">Create a new Role</p>
26+
<a asp-controller="UserManagement" asp-action="CreateRole" class="btn btn-primary">Go </a>
27+
</div>
28+
<div class="card-footer sub-role-theme"></div>
29+
</div>
30+
</div>
31+
</div>

0 commit comments

Comments
 (0)