46 lines
2.0 KiB
C#
46 lines
2.0 KiB
C#
using Helpdesk.Api.Infrastructure;
|
||||
|
|
using Helpdesk.Application.Abstractions;
|
|||
|
|
using Helpdesk.Application.Common;
|
|||
|
|
using Helpdesk.Application.Users;
|
|||
|
|
using Microsoft.AspNetCore.Authorization;
|
|||
|
|
using Microsoft.AspNetCore.Mvc;
|
|||
|
|
|
|||
|
|
namespace Helpdesk.Api.Controllers;
|
|||
|
|
|
|||
|
|
/// <summary>Správa uživatelů a rolí (jen administrátor).</summary>
|
|||
|
|
[ApiController]
|
|||
|
|
[Authorize(Roles = Roles.Admin)]
|
|||
|
|
[Route("api/admin/users")]
|
|||
|
|
[Produces("application/json")]
|
|||
|
|
public sealed class AdminUsersController(UserService users, ICurrentUser currentUser) : ControllerBase
|
|||
|
|
{
|
|||
|
|
/// <summary>Vyhledání uživatelů podle jména / e-mailu a role.</summary>
|
|||
|
|
[HttpGet]
|
|||
|
|
public Task<PagedResult<AdminUserDto>> Search(
|
|||
|
|
[FromQuery] string? q, [FromQuery] string? role,
|
|||
|
|
[FromQuery] int page = 1, [FromQuery] int pageSize = 20,
|
|||
|
|
CancellationToken ct = default) =>
|
|||
|
|
users.SearchAsync(q, role, page, pageSize, ct);
|
|||
|
|
|
|||
|
|
/// <summary>Změna role – uživatel bude odhlášen a po přihlášení dostane token s novou rolí.</summary>
|
|||
|
|
[HttpPut("{id:int}/role")]
|
|||
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|||
|
|
[ProducesResponseType<ProblemDetails>(StatusCodes.Status404NotFound)]
|
|||
|
|
[ProducesResponseType<ProblemDetails>(StatusCodes.Status409Conflict)]
|
|||
|
|
public async Task<IActionResult> ChangeRole(int id, ChangeRoleRequest request, CancellationToken ct)
|
|||
|
|
{
|
|||
|
|
await users.ChangeRoleAsync(currentUser, id, request, ct);
|
|||
|
|
return NoContent();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>Aktivace / deaktivace účtu. Deaktivace zneplatní všechny tokeny uživatele.</summary>
|
|||
|
|
[HttpPut("{id:int}/active")]
|
|||
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|||
|
|
[ProducesResponseType<ProblemDetails>(StatusCodes.Status404NotFound)]
|
|||
|
|
[ProducesResponseType<ProblemDetails>(StatusCodes.Status409Conflict)]
|
|||
|
|
public async Task<IActionResult> SetActive(int id, SetActiveRequest request, CancellationToken ct)
|
|||
|
|
{
|
|||
|
|
await users.SetActiveAsync(currentUser, id, request, ct);
|
|||
|
|
return NoContent();
|
|||
|
|
}
|
|||
|
|
}
|