backend + web: Add admin account lifecycle management
This commit is contained in:
1 parent
fcf7cd0596
commit
91d0c06c83
25 files changed
+1148
-97
No files matched your search
@@ -24,9 +24,95 @@ public sealed class AdminUsersController(HelpdeskApiClient api) : Controller
|
||||
if (users.Items.Count == 0 && users.Page > 1)
|
||||
return BackToList(new AdminUsersQuery(q, role, Math.Max(1, users.PageCount)));
|
||||
|
||||
int? currentUserId = int.TryParse(User.FindFirstValue(ClaimTypes.NameIdentifier), CultureInfo.InvariantCulture, out var id)
|
||||
? id : null;
|
||||
return View(new AdminUsersViewModel(users, new AdminUsersQuery(q, role, users.Page), currentUserId));
|
||||
return View(new AdminUsersViewModel(users, new AdminUsersQuery(q, role, users.Page), CurrentUserId));
|
||||
}
|
||||
|
||||
[HttpGet("new")]
|
||||
public IActionResult New([FromQuery] AdminUsersQuery query) => View("Form", new AdminUserForm { Query = query });
|
||||
|
||||
[HttpPost("new")]
|
||||
public Task<IActionResult> New([FromForm] AdminUserForm model, [FromQuery] AdminUsersQuery query, CancellationToken ct) =>
|
||||
SaveAsync(null, model, query, ct);
|
||||
|
||||
[HttpGet("{id:int}/edit")]
|
||||
public async Task<IActionResult> Edit([FromRoute] int id, [FromQuery] AdminUsersQuery query, CancellationToken ct)
|
||||
{
|
||||
if (id == CurrentUserId)
|
||||
return RedirectToAction("Index", "Profile");
|
||||
|
||||
var user = await api.GetAdminUserAsync(id, ct);
|
||||
return View("Form", new AdminUserForm
|
||||
{
|
||||
Id = user.Id,
|
||||
Query = query,
|
||||
Email = user.Email,
|
||||
FirstName = user.FirstName,
|
||||
LastName = user.LastName,
|
||||
Phone = user.Phone,
|
||||
Role = user.Role,
|
||||
IsActive = user.IsActive,
|
||||
EmailConfirmed = user.EmailConfirmed,
|
||||
EmailNotificationsEnabled = user.EmailNotificationsEnabled,
|
||||
});
|
||||
}
|
||||
|
||||
[HttpPost("{id:int}/edit")]
|
||||
public Task<IActionResult> Edit([FromRoute] int id, [FromForm] AdminUserForm model, [FromQuery] AdminUsersQuery query,
|
||||
CancellationToken ct) => SaveAsync(id, model, query, ct);
|
||||
|
||||
[HttpPost("{id:int}/email-confirmation")]
|
||||
public async Task<IActionResult> SendEmailConfirmation([FromRoute] int id, [FromQuery] AdminUsersQuery query, CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
await api.SendAdminUserEmailConfirmationAsync(id, ct);
|
||||
TempData.SetSuccess("Ověřovací e-mail byl odeslán.");
|
||||
}
|
||||
catch (ApiException ex) when (ex.StatusCode is HttpStatusCode.Conflict or HttpStatusCode.ServiceUnavailable)
|
||||
{
|
||||
TempData.SetError(ex.AllMessages);
|
||||
}
|
||||
|
||||
return BackToEdit(id, query);
|
||||
}
|
||||
|
||||
[HttpPost("{id:int}/delete")]
|
||||
public async Task<IActionResult> Delete([FromRoute] int id, [FromForm] bool confirmDelete, [FromQuery] AdminUsersQuery query,
|
||||
CancellationToken ct)
|
||||
{
|
||||
if (!confirmDelete)
|
||||
{
|
||||
TempData.SetError("Potvrďte smazání účtu.");
|
||||
return BackToEdit(id, query);
|
||||
}
|
||||
|
||||
return await UpdateAsync(query, () => api.DeleteAdminUserAsync(id, ct), "Účet byl smazán a jeho profil anonymizován.");
|
||||
}
|
||||
|
||||
[HttpPost("bulk")]
|
||||
public async Task<IActionResult> Bulk([FromForm] int[] userIds, [FromForm] string? action, [FromForm] string? newRole,
|
||||
[FromForm] bool confirmDelete, [FromQuery] AdminUsersQuery query, CancellationToken ct)
|
||||
{
|
||||
if (!ModelState.IsValid || userIds is not { Length: >= 1 } ||
|
||||
action is not ("activate" or "deactivate" or "role" or "delete"))
|
||||
{
|
||||
TempData.SetError("Označte účty a vyberte hromadnou akci.");
|
||||
return BackToList(query);
|
||||
}
|
||||
|
||||
if (action == "delete" && !confirmDelete)
|
||||
{
|
||||
TempData.SetError("Potvrďte smazání vybraných účtů.");
|
||||
return BackToList(query);
|
||||
}
|
||||
|
||||
if (action == "role" && (newRole is null || !UserRoles.All.Contains(newRole)))
|
||||
{
|
||||
TempData.SetError("Vyberte roli pro označené účty.");
|
||||
return BackToList(query);
|
||||
}
|
||||
|
||||
return await UpdateAsync(query, () => api.UpdateAdminUsersAsync(userIds, action, newRole, ct), "Hromadná akce byla provedena.");
|
||||
}
|
||||
|
||||
[HttpPost("{id:int}/role")]
|
||||
@@ -63,7 +149,7 @@ public sealed class AdminUsersController(HelpdeskApiClient api) : Controller
|
||||
await update();
|
||||
TempData.SetSuccess(success);
|
||||
}
|
||||
catch (ApiException ex) when (ex.StatusCode is HttpStatusCode.BadRequest or HttpStatusCode.Conflict)
|
||||
catch (ApiException ex) when (ex.StatusCode is HttpStatusCode.BadRequest or HttpStatusCode.Conflict or HttpStatusCode.NotFound)
|
||||
{
|
||||
TempData.SetError(ex.AllMessages);
|
||||
}
|
||||
@@ -71,6 +157,44 @@ public sealed class AdminUsersController(HelpdeskApiClient api) : Controller
|
||||
return BackToList(query);
|
||||
}
|
||||
|
||||
private async Task<IActionResult> SaveAsync(int? id, AdminUserForm model, AdminUsersQuery query, CancellationToken ct)
|
||||
{
|
||||
if (id is not null && id == CurrentUserId)
|
||||
return RedirectToAction("Index", "Profile");
|
||||
|
||||
model.Id = id;
|
||||
model.Query = query;
|
||||
if (id is null && string.IsNullOrWhiteSpace(model.Password))
|
||||
ModelState.AddModelError(nameof(model.Password), "Zadejte počáteční heslo.");
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await api.SaveAdminUserAsync(id, model.ToRequest(), ct);
|
||||
var message = id is null ? "Účet byl založen." : "Změny účtu byly uloženy.";
|
||||
TempData.SetSuccess(result?.VerificationEmailSent == true ? message + " Ověřovací e-mail byl odeslán." : message);
|
||||
if (result is { VerificationEmailSent: false })
|
||||
{
|
||||
TempData.SetError("Ověřovací e-mail se nepodařilo odeslat. Můžete ho poslat znovu.");
|
||||
return BackToEdit(result.Id, query);
|
||||
}
|
||||
return BackToList(query);
|
||||
}
|
||||
catch (ApiException ex) when (ex.StatusCode is HttpStatusCode.BadRequest or HttpStatusCode.Conflict)
|
||||
{
|
||||
ModelState.AddApiErrors(ex, typeof(AdminUserForm));
|
||||
}
|
||||
}
|
||||
|
||||
return View("Form", model);
|
||||
}
|
||||
|
||||
private int? CurrentUserId =>
|
||||
int.TryParse(User.FindFirstValue(ClaimTypes.NameIdentifier), CultureInfo.InvariantCulture, out var id) ? id : null;
|
||||
|
||||
private RedirectToActionResult BackToList(AdminUsersQuery query) =>
|
||||
RedirectToAction(nameof(Index), new { q = query.Q, role = query.Role, page = query.Page });
|
||||
|
||||
private RedirectToActionResult BackToEdit(int id, AdminUsersQuery query) =>
|
||||
RedirectToAction(nameof(Edit), new { id, q = query.Q, role = query.Role, page = query.Page });
|
||||
}
|
||||
Reference in new issue
Block a user