Replaces IRoleService/RoleService. The permission-builder logic moves to a shared RolePermissionBuilder static helper used by both the create and update handlers. RolesController keeps its inline IPermissionService checks unchanged. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
50 lines
1.8 KiB
C#
50 lines
1.8 KiB
C#
using MediatR;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Mws.Application.Permissions;
|
|
using Mws.Application.Roles;
|
|
|
|
namespace Mws.Api.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/roles")]
|
|
[Authorize]
|
|
public class RolesController(ISender sender, IPermissionService permissions) : ControllerBase
|
|
{
|
|
[HttpGet]
|
|
public async Task<ActionResult<List<RoleDto>>> GetAll(CancellationToken ct)
|
|
{
|
|
await permissions.EnsureAsync(User.GetUserId(), "roles", PermissionAction.View, ct);
|
|
return Ok(await sender.Send(new GetRolesQuery(), ct));
|
|
}
|
|
|
|
[HttpGet("{id:guid}")]
|
|
public async Task<ActionResult<RoleDto>> Get(Guid id, CancellationToken ct)
|
|
{
|
|
await permissions.EnsureAsync(User.GetUserId(), "roles", PermissionAction.View, ct);
|
|
return Ok(await sender.Send(new GetRoleQuery(id), ct));
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<ActionResult<RoleDto>> Create([FromBody] SaveRoleRequest request, CancellationToken ct)
|
|
{
|
|
await permissions.EnsureAsync(User.GetUserId(), "roles", PermissionAction.Create, ct);
|
|
return Ok(await sender.Send(new CreateRoleCommand(request), ct));
|
|
}
|
|
|
|
[HttpPut("{id:guid}")]
|
|
public async Task<ActionResult<RoleDto>> Update(Guid id, [FromBody] SaveRoleRequest request, CancellationToken ct)
|
|
{
|
|
await permissions.EnsureAsync(User.GetUserId(), "roles", PermissionAction.Edit, ct);
|
|
return Ok(await sender.Send(new UpdateRoleCommand(id, request), ct));
|
|
}
|
|
|
|
[HttpDelete("{id:guid}")]
|
|
public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
|
|
{
|
|
await permissions.EnsureAsync(User.GetUserId(), "roles", PermissionAction.Delete, ct);
|
|
await sender.Send(new DeleteRoleCommand(id), ct);
|
|
return NoContent();
|
|
}
|
|
}
|