76 lines
2.9 KiB
C#
76 lines
2.9 KiB
C#
using System.Globalization;
|
|||
|
|
using System.Net;
|
||
|
|
using System.Security.Claims;
|
||
|
|
using Helpdesk.Web.Api;
|
||
|
|
using Helpdesk.Web.Auth;
|
||
|
|
using Helpdesk.Web.Infrastructure;
|
||
|
|
using Helpdesk.Web.Models;
|
||
|
|
using Microsoft.AspNetCore.Authorization;
|
||
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
|
||
|
|
namespace Helpdesk.Web.Controllers;
|
||
|
|
|
||
|
|
[Authorize(Roles = UserRoles.Admin)]
|
||
|
|
[ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)]
|
||
|
|
[Route("admin/users")]
|
||
|
|
public sealed class AdminUsersController(HelpdeskApiClient api) : Controller
|
||
|
|
{
|
||
|
|
[HttpGet("")]
|
||
|
|
public async Task<IActionResult> Index([FromQuery] AdminUsersQuery query, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var q = string.IsNullOrWhiteSpace(query.Q) ? null : query.Q.Trim();
|
||
|
|
var role = query.Role is { } filter && UserRoles.All.Contains(filter) ? filter : null;
|
||
|
|
var users = await api.GetAdminUsersAsync(q, role, Math.Max(1, query.Page), ct);
|
||
|
|
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));
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPost("{id:int}/role")]
|
||
|
|
public async Task<IActionResult> ChangeRole([FromRoute] int id, [FromForm] string? newRole, [FromQuery] AdminUsersQuery query,
|
||
|
|
CancellationToken ct)
|
||
|
|
{
|
||
|
|
if (newRole is null || !UserRoles.All.Contains(newRole))
|
||
|
|
{
|
||
|
|
TempData.SetError("Vyberte platnou roli.");
|
||
|
|
return BackToList(query);
|
||
|
|
}
|
||
|
|
|
||
|
|
return await UpdateAsync(query, () => api.ChangeUserRoleAsync(id, newRole, ct), "Role uživatele byla uložena.");
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpPost("{id:int}/active")]
|
||
|
|
public async Task<IActionResult> SetActive([FromRoute] int id, [FromForm] bool? isActive, [FromQuery] AdminUsersQuery query,
|
||
|
|
CancellationToken ct)
|
||
|
|
{
|
||
|
|
if (isActive is not bool active)
|
||
|
|
{
|
||
|
|
TempData.SetError("Vyberte stav účtu.");
|
||
|
|
return BackToList(query);
|
||
|
|
}
|
||
|
|
|
||
|
|
return await UpdateAsync(query, () => api.SetUserActiveAsync(id, active, ct),
|
||
|
|
active ? "Účet byl aktivován." : "Účet byl deaktivován.");
|
||
|
|
}
|
||
|
|
|
||
|
|
private async Task<IActionResult> UpdateAsync(AdminUsersQuery query, Func<Task> update, string success)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
await update();
|
||
|
|
TempData.SetSuccess(success);
|
||
|
|
}
|
||
|
|
catch (ApiException ex) when (ex.StatusCode is HttpStatusCode.BadRequest or HttpStatusCode.Conflict)
|
||
|
|
{
|
||
|
|
TempData.SetError(ex.AllMessages);
|
||
|
|
}
|
||
|
|
|
||
|
|
return BackToList(query);
|
||
|
|
}
|
||
|
|
|
||
|
|
private RedirectToActionResult BackToList(AdminUsersQuery query) =>
|
||
|
|
RedirectToAction(nameof(Index), new { q = query.Q, role = query.Role, page = query.Page });
|
||
|
|
}
|