Ya perdí el hilo de los cambios pero ahi van.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using GestionIntegral.Api.Dtos.Usuarios;
|
||||
using GestionIntegral.Api.Dtos.Usuarios.Auditoria;
|
||||
using GestionIntegral.Api.Services.Usuarios;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
@@ -141,18 +142,63 @@ namespace GestionIntegral.Api.Controllers.Usuarios
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> ToggleHabilitado(int id, [FromBody] bool habilitar)
|
||||
{
|
||||
if (!TienePermiso(PermisoModificarUsuarios)) return Forbid();
|
||||
if (!TienePermiso(PermisoModificarUsuarios)) return Forbid();
|
||||
|
||||
var idAdmin = GetCurrentUserId();
|
||||
if (idAdmin == null) return Unauthorized("Token inválido.");
|
||||
|
||||
var (exito, error) = await _usuarioService.CambiarEstadoHabilitadoAsync(id, habilitar, idAdmin.Value);
|
||||
if (!exito)
|
||||
if (!exito)
|
||||
{
|
||||
if (error == "Usuario no encontrado.") return NotFound(new { message = error });
|
||||
return BadRequest(new { message = error });
|
||||
}
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
// GET: api/usuarios/{idUsuarioAfectado}/historial
|
||||
[HttpGet("{idUsuarioAfectado:int}/historial")]
|
||||
[ProducesResponseType(typeof(IEnumerable<UsuarioHistorialDto>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> GetHistorialDeUsuario(int idUsuarioAfectado, [FromQuery] DateTime? fechaDesde, [FromQuery] DateTime? fechaHasta)
|
||||
{
|
||||
// Necesitas un permiso para ver historial, ej: "AU001" o uno más granular "AU_USR_VIEW_SINGLE"
|
||||
// O si solo SuperAdmin puede ver historiales específicos.
|
||||
if (!TienePermiso("AU001")) // Asumiendo AU001 = Ver Historial de Auditoría (General o Usuarios)
|
||||
{
|
||||
_logger.LogWarning("Acceso denegado a GetHistorialDeUsuario para Usuario ID {UserId}", GetCurrentUserId() ?? 0);
|
||||
return Forbid();
|
||||
}
|
||||
|
||||
var usuarioExiste = await _usuarioService.ObtenerPorIdAsync(idUsuarioAfectado);
|
||||
if (usuarioExiste == null)
|
||||
{
|
||||
return NotFound(new { message = $"Usuario con ID {idUsuarioAfectado} no encontrado." });
|
||||
}
|
||||
|
||||
var historial = await _usuarioService.ObtenerHistorialPorUsuarioIdAsync(idUsuarioAfectado, fechaDesde, fechaHasta);
|
||||
return Ok(historial);
|
||||
}
|
||||
|
||||
// GET: api/usuarios/historial (Para todos los usuarios, con filtros)
|
||||
[HttpGet("historial")]
|
||||
[ProducesResponseType(typeof(IEnumerable<UsuarioHistorialDto>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||||
public async Task<IActionResult> GetTodoElHistorialDeUsuarios(
|
||||
[FromQuery] DateTime? fechaDesde,
|
||||
[FromQuery] DateTime? fechaHasta,
|
||||
[FromQuery] int? idUsuarioModifico,
|
||||
[FromQuery] string? tipoModificacion)
|
||||
{
|
||||
if (!TienePermiso("AU001")) // Mismo permiso general de auditoría
|
||||
{
|
||||
_logger.LogWarning("Acceso denegado a GetTodoElHistorialDeUsuarios para Usuario ID {UserId}", GetCurrentUserId() ?? 0);
|
||||
return Forbid();
|
||||
}
|
||||
|
||||
var historial = await _usuarioService.ObtenerTodoElHistorialAsync(fechaDesde, fechaHasta, idUsuarioModifico, tipoModificacion);
|
||||
return Ok(historial);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user