Skip to content

Commit c61af48

Browse files
User deletion functional.
1 parent 8879b6f commit c61af48

File tree

3 files changed

+44
-14
lines changed

3 files changed

+44
-14
lines changed

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

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ public ActionResult UserHome()
4040
{
4141
return View();
4242
}
43-
public async Task<IActionResult> GetAllUsers()
43+
public async Task<IActionResult> GetAllUsers(bool isSuccess = false, bool error = false)
4444
{
45+
ViewBag.isSuccess = isSuccess;
46+
ViewBag.error = error;
47+
4548
var users = await _userMgr.Users.ToListAsync();
4649
var vm = _mapper.Map<List<UserWithRoles>>(users);
4750
foreach (var user in vm)
@@ -110,25 +113,37 @@ public ActionResult Edit(int id, IFormCollection collection)
110113
}
111114
}
112115

113-
// GET: UserManagementController/Delete/5
114-
public ActionResult Delete(int id)
116+
public async Task<IActionResult> DeleteUser(string? id)
115117
{
116-
return View();
118+
if (id == null)
119+
{
120+
return NotFound();
121+
}
122+
123+
var entity = await _userMgr.FindByIdAsync(id);
124+
if (entity == null)
125+
{
126+
return NotFound();
127+
}
128+
var vm = _mapper.Map<UserWithRoles>(entity);
129+
return View(vm);
117130
}
118131

119-
// POST: UserManagementController/Delete/5
120-
[HttpPost]
132+
[HttpPost, ActionName("DeleteUser")]
121133
[ValidateAntiForgeryToken]
122-
public ActionResult Delete(int id, IFormCollection collection)
134+
public async Task<IActionResult> DeleteUserConfirmed(string id)
123135
{
124-
try
136+
var user = await _userMgr.FindByIdAsync(id);
137+
if (user == null)
125138
{
126-
return RedirectToAction(nameof(Index));
139+
return NotFound();
127140
}
128-
catch
141+
var deleted = await _userMgr.DeleteAsync(user);
142+
if (deleted.Succeeded)
129143
{
130-
return View();
144+
return RedirectToAction(nameof(GetAllUsers), new { isSuccess = true });
131145
}
146+
return RedirectToAction(nameof(GetAllUsers), new { error = true });
132147
}
133148
}
134149
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@model UserWithRoles;
2+
3+
<a asp-action="GetAllUsers" 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-user-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="DeleteUser" asp-route-id="@Model.Id">
11+
<p>Are you sure you want to delete <strong>@Model.UserName</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>

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@model List<UserWithRoles>;
22
@{
3-
if (ViewBag.Error == true)
3+
if (ViewBag.error == true)
44
{
55
<div class="alert alert-danger alert-dismissible fade show" role="alert">
66
An error occured while processing your request. Contact your administrator. <br />
@@ -66,10 +66,10 @@
6666
}
6767
</td>
6868
<td>
69-
<a asp-action="Edit" asp-route-id="@user.Id" class="btn btn-success text-white">
69+
<a asp-action="EditUser" asp-route-id="@user.Id" class="btn btn-success text-white">
7070
<i class="bi bi-pen"></i>
7171
</a>
72-
<a asp-action="Delete" asp-route-id="@user.Id" class="btn btn-danger text-white">
72+
<a asp-action="DeleteUser" asp-route-id="@user.Id" class="btn btn-danger text-white">
7373
<i class="bi bi-trash"></i>
7474
</a>
7575
</td>

0 commit comments

Comments
 (0)