using Heimdall.Infrastructure.Security; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.RateLimiting; namespace Heimdall.Api.Controllers; /// Response DTO for a role with its permissions. public record RoleResponse(string Name, IReadOnlyList Permissions); /// Response DTO for a list of roles. public record RolesListResponse(IReadOnlyList Roles); [ApiController] [Route("api/roles")] [Authorize(AuthenticationSchemes = "JwtBearer,Cookies", Policy = AppPermissions.AdminUsers)] [EnableRateLimiting("Api")] public class RolesController : ControllerBase { /// Returns all roles with their associated permissions. [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)); } /// Returns a single role with its permissions. [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)); } }