45 lines
1.7 KiB
C#
45 lines
1.7 KiB
C#
using Heimdall.Infrastructure.Security;
|
|||
|
|
using Microsoft.AspNetCore.Authorization;
|
||
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
using Microsoft.AspNetCore.RateLimiting;
|
||
|
|
|
||
|
|
namespace Heimdall.Api.Controllers;
|
||
|
|
|
||
|
|
/// <summary>Response DTO for a role with its permissions.</summary>
|
||
|
|
public record RoleResponse(string Name, IReadOnlyList<string> Permissions);
|
||
|
|
|
||
|
|
/// <summary>Response DTO for a list of roles.</summary>
|
||
|
|
public record RolesListResponse(IReadOnlyList<RoleResponse> Roles);
|
||
|
|
|
||
|
|
[ApiController]
|
||
|
|
[Route("api/roles")]
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.AdminUsers)]
|
||
|
|
[EnableRateLimiting("Api")]
|
||
|
|
public class RolesController : ControllerBase
|
||
|
|
{
|
||
|
|
/// <summary>Returns all roles with their associated permissions.</summary>
|
||
|
|
[HttpGet]
|
||
|
|
[ProducesResponseType(typeof(RolesListResponse), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||
|
|
public IActionResult List()
|
||
|
|
{
|
||
|
|
var roles = AppPermissions.RolePermissions
|
||
|
|
.Select(kvp => new RoleResponse(kvp.Key, kvp.Value))
|
||
|
|
.ToList();
|
||
|
|
|
||
|
|
return Ok(new RolesListResponse(roles));
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Returns a single role with its permissions.</summary>
|
||
|
|
[HttpGet("{name}")]
|
||
|
|
[ProducesResponseType(typeof(RoleResponse), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
|
||
|
|
public IActionResult GetByName(string name)
|
||
|
|
{
|
||
|
|
if (!AppPermissions.RolePermissions.TryGetValue(name, out var permissions))
|
||
|
|
return NotFound(new { error = $"Role '{name}' not found" });
|
||
|
|
|
||
|
|
return Ok(new RoleResponse(name, permissions));
|
||
|
|
}
|
||
|
|
}
|