fix(app): validar formato codigo rol en GetRolPermisos [UDT-005]
Agrega GetRolPermisosQueryValidator con regex ^[a-z][a-z0-9_]*$ para
rechazar codigos invalidos con 400 en GET /api/v1/roles/{codigo}/permisos.
This commit is contained in:
@@ -16,13 +16,16 @@ public sealed class PermisosController : ControllerBase
|
||||
{
|
||||
private readonly IDispatcher _dispatcher;
|
||||
private readonly IValidator<AssignPermisosToRolCommand> _assignValidator;
|
||||
private readonly IValidator<GetRolPermisosQuery> _getRolPermisosValidator;
|
||||
|
||||
public PermisosController(
|
||||
IDispatcher dispatcher,
|
||||
IValidator<AssignPermisosToRolCommand> assignValidator)
|
||||
IValidator<AssignPermisosToRolCommand> assignValidator,
|
||||
IValidator<GetRolPermisosQuery> getRolPermisosValidator)
|
||||
{
|
||||
_dispatcher = dispatcher;
|
||||
_assignValidator = assignValidator;
|
||||
_getRolPermisosValidator = getRolPermisosValidator;
|
||||
}
|
||||
|
||||
/// <summary>Lists all permisos in the canonical catalog. Requires admin role.</summary>
|
||||
@@ -39,13 +42,23 @@ public sealed class PermisosController : ControllerBase
|
||||
/// <summary>Gets all permisos assigned to a rol. Requires admin role.</summary>
|
||||
[HttpGet("roles/{codigo}/permisos")]
|
||||
[ProducesResponseType(typeof(IReadOnlyList<PermisoDto>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> GetRolPermisos(string codigo)
|
||||
{
|
||||
var result = await _dispatcher.Send<GetRolPermisosQuery, IReadOnlyList<PermisoDto>>(
|
||||
new GetRolPermisosQuery(codigo));
|
||||
var query = new GetRolPermisosQuery(codigo);
|
||||
var validation = await _getRolPermisosValidator.ValidateAsync(query);
|
||||
if (!validation.IsValid)
|
||||
{
|
||||
var errors = validation.Errors
|
||||
.GroupBy(e => e.PropertyName)
|
||||
.ToDictionary(g => g.Key, g => g.Select(e => e.ErrorMessage).ToArray());
|
||||
return BadRequest(new { errors });
|
||||
}
|
||||
|
||||
var result = await _dispatcher.Send<GetRolPermisosQuery, IReadOnlyList<PermisoDto>>(query);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace SIGCM2.Application.Permisos.GetByRol;
|
||||
|
||||
public sealed class GetRolPermisosQueryValidator : AbstractValidator<GetRolPermisosQuery>
|
||||
{
|
||||
public GetRolPermisosQueryValidator()
|
||||
{
|
||||
RuleFor(x => x.RolCodigo)
|
||||
.NotEmpty().WithMessage("El código del rol es requerido.")
|
||||
.Matches(@"^[a-z][a-z0-9_]*$")
|
||||
.WithMessage("El código del rol debe empezar con una letra minúscula y contener solo minúsculas, dígitos o guion bajo.");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user