UDT-010: Infraestructura de Auditoría y Trazabilidad — Closes #6 #14
@@ -1,5 +1,7 @@
|
|||||||
|
using System.Transactions;
|
||||||
using SIGCM2.Application.Abstractions;
|
using SIGCM2.Application.Abstractions;
|
||||||
using SIGCM2.Application.Abstractions.Persistence;
|
using SIGCM2.Application.Abstractions.Persistence;
|
||||||
|
using SIGCM2.Application.Audit;
|
||||||
using SIGCM2.Application.Permisos.Dtos;
|
using SIGCM2.Application.Permisos.Dtos;
|
||||||
using SIGCM2.Domain.Exceptions;
|
using SIGCM2.Domain.Exceptions;
|
||||||
|
|
||||||
@@ -10,15 +12,18 @@ public sealed class AssignPermisosToRolCommandHandler : ICommandHandler<AssignPe
|
|||||||
private readonly IRolRepository _rolRepository;
|
private readonly IRolRepository _rolRepository;
|
||||||
private readonly IPermisoRepository _permisoRepository;
|
private readonly IPermisoRepository _permisoRepository;
|
||||||
private readonly IRolPermisoRepository _rolPermisoRepository;
|
private readonly IRolPermisoRepository _rolPermisoRepository;
|
||||||
|
private readonly IAuditLogger _audit;
|
||||||
|
|
||||||
public AssignPermisosToRolCommandHandler(
|
public AssignPermisosToRolCommandHandler(
|
||||||
IRolRepository rolRepository,
|
IRolRepository rolRepository,
|
||||||
IPermisoRepository permisoRepository,
|
IPermisoRepository permisoRepository,
|
||||||
IRolPermisoRepository rolPermisoRepository)
|
IRolPermisoRepository rolPermisoRepository,
|
||||||
|
IAuditLogger audit)
|
||||||
{
|
{
|
||||||
_rolRepository = rolRepository;
|
_rolRepository = rolRepository;
|
||||||
_permisoRepository = permisoRepository;
|
_permisoRepository = permisoRepository;
|
||||||
_rolPermisoRepository = rolPermisoRepository;
|
_rolPermisoRepository = rolPermisoRepository;
|
||||||
|
_audit = audit;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IReadOnlyList<PermisoDto>> Handle(AssignPermisosToRolCommand command)
|
public async Task<IReadOnlyList<PermisoDto>> Handle(AssignPermisosToRolCommand command)
|
||||||
@@ -40,10 +45,29 @@ public sealed class AssignPermisosToRolCommandHandler : ICommandHandler<AssignPe
|
|||||||
throw new PermisoNotFoundException(missing);
|
throw new PermisoNotFoundException(missing);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Capture "before" snapshot for audit diff
|
||||||
|
var previousPermisos = await _rolPermisoRepository.GetByRolCodigoAsync(rol.Codigo);
|
||||||
|
var beforeCodigos = previousPermisos.Select(p => p.Codigo).OrderBy(c => c, StringComparer.Ordinal).ToArray();
|
||||||
|
var afterCodigos = permisos.Select(p => p.Codigo).OrderBy(c => c, StringComparer.Ordinal).ToArray();
|
||||||
|
|
||||||
|
using (var tx = new TransactionScope(
|
||||||
|
TransactionScopeOption.Required,
|
||||||
|
new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted },
|
||||||
|
TransactionScopeAsyncFlowOption.Enabled))
|
||||||
|
{
|
||||||
// 3. Reemplazar el set (DELETE+INSERT en transacción dentro del repo)
|
// 3. Reemplazar el set (DELETE+INSERT en transacción dentro del repo)
|
||||||
var permisoIds = permisos.Select(p => p.Id);
|
var permisoIds = permisos.Select(p => p.Id);
|
||||||
await _rolPermisoRepository.ReplaceForRolAsync(rol.Id, permisoIds);
|
await _rolPermisoRepository.ReplaceForRolAsync(rol.Id, permisoIds);
|
||||||
|
|
||||||
|
await _audit.LogAsync(
|
||||||
|
action: "rol.permisos_update",
|
||||||
|
targetType: "Rol",
|
||||||
|
targetId: rol.Id.ToString(),
|
||||||
|
metadata: new { before = beforeCodigos, after = afterCodigos });
|
||||||
|
|
||||||
|
tx.Complete();
|
||||||
|
}
|
||||||
|
|
||||||
// 4. Retornar el nuevo set asignado
|
// 4. Retornar el nuevo set asignado
|
||||||
return permisos
|
return permisos
|
||||||
.Select(p => new PermisoDto(p.Id, p.Codigo, p.Nombre, p.Descripcion, p.Modulo))
|
.Select(p => new PermisoDto(p.Id, p.Codigo, p.Nombre, p.Descripcion, p.Modulo))
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
|
using System.Transactions;
|
||||||
using SIGCM2.Application.Abstractions;
|
using SIGCM2.Application.Abstractions;
|
||||||
using SIGCM2.Application.Abstractions.Persistence;
|
using SIGCM2.Application.Abstractions.Persistence;
|
||||||
|
using SIGCM2.Application.Audit;
|
||||||
using SIGCM2.Application.Roles.Dtos;
|
using SIGCM2.Application.Roles.Dtos;
|
||||||
using SIGCM2.Domain.Entities;
|
using SIGCM2.Domain.Entities;
|
||||||
using SIGCM2.Domain.Exceptions;
|
using SIGCM2.Domain.Exceptions;
|
||||||
@@ -9,10 +11,12 @@ namespace SIGCM2.Application.Roles.Create;
|
|||||||
public sealed class CreateRolCommandHandler : ICommandHandler<CreateRolCommand, RolCreatedDto>
|
public sealed class CreateRolCommandHandler : ICommandHandler<CreateRolCommand, RolCreatedDto>
|
||||||
{
|
{
|
||||||
private readonly IRolRepository _repository;
|
private readonly IRolRepository _repository;
|
||||||
|
private readonly IAuditLogger _audit;
|
||||||
|
|
||||||
public CreateRolCommandHandler(IRolRepository repository)
|
public CreateRolCommandHandler(IRolRepository repository, IAuditLogger audit)
|
||||||
{
|
{
|
||||||
_repository = repository;
|
_repository = repository;
|
||||||
|
_audit = audit;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<RolCreatedDto> Handle(CreateRolCommand command)
|
public async Task<RolCreatedDto> Handle(CreateRolCommand command)
|
||||||
@@ -24,7 +28,23 @@ public sealed class CreateRolCommandHandler : ICommandHandler<CreateRolCommand,
|
|||||||
throw new RolAlreadyExistsException(command.Codigo);
|
throw new RolAlreadyExistsException(command.Codigo);
|
||||||
|
|
||||||
var rol = Rol.ForCreation(command.Codigo, command.Nombre, command.Descripcion);
|
var rol = Rol.ForCreation(command.Codigo, command.Nombre, command.Descripcion);
|
||||||
var newId = await _repository.AddAsync(rol);
|
|
||||||
|
int newId;
|
||||||
|
using (var tx = new TransactionScope(
|
||||||
|
TransactionScopeOption.Required,
|
||||||
|
new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted },
|
||||||
|
TransactionScopeAsyncFlowOption.Enabled))
|
||||||
|
{
|
||||||
|
newId = await _repository.AddAsync(rol);
|
||||||
|
|
||||||
|
await _audit.LogAsync(
|
||||||
|
action: "rol.create",
|
||||||
|
targetType: "Rol",
|
||||||
|
targetId: newId.ToString(),
|
||||||
|
metadata: new { after = new { rol.Codigo, rol.Nombre, rol.Descripcion } });
|
||||||
|
|
||||||
|
tx.Complete();
|
||||||
|
}
|
||||||
|
|
||||||
return new RolCreatedDto(
|
return new RolCreatedDto(
|
||||||
Id: newId,
|
Id: newId,
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
|
using System.Transactions;
|
||||||
using SIGCM2.Application.Abstractions;
|
using SIGCM2.Application.Abstractions;
|
||||||
using SIGCM2.Application.Abstractions.Persistence;
|
using SIGCM2.Application.Abstractions.Persistence;
|
||||||
|
using SIGCM2.Application.Audit;
|
||||||
using SIGCM2.Application.Roles.Dtos;
|
using SIGCM2.Application.Roles.Dtos;
|
||||||
using SIGCM2.Domain.Exceptions;
|
using SIGCM2.Domain.Exceptions;
|
||||||
|
|
||||||
@@ -8,10 +10,12 @@ namespace SIGCM2.Application.Roles.Deactivate;
|
|||||||
public sealed class DeactivateRolCommandHandler : ICommandHandler<DeactivateRolCommand, RolDto>
|
public sealed class DeactivateRolCommandHandler : ICommandHandler<DeactivateRolCommand, RolDto>
|
||||||
{
|
{
|
||||||
private readonly IRolRepository _repository;
|
private readonly IRolRepository _repository;
|
||||||
|
private readonly IAuditLogger _audit;
|
||||||
|
|
||||||
public DeactivateRolCommandHandler(IRolRepository repository)
|
public DeactivateRolCommandHandler(IRolRepository repository, IAuditLogger audit)
|
||||||
{
|
{
|
||||||
_repository = repository;
|
_repository = repository;
|
||||||
|
_audit = audit;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<RolDto> Handle(DeactivateRolCommand command)
|
public async Task<RolDto> Handle(DeactivateRolCommand command)
|
||||||
@@ -23,11 +27,24 @@ public sealed class DeactivateRolCommandHandler : ICommandHandler<DeactivateRolC
|
|||||||
if (await _repository.HasActiveUsuariosAsync(command.Codigo))
|
if (await _repository.HasActiveUsuariosAsync(command.Codigo))
|
||||||
throw new RolInUseException(command.Codigo);
|
throw new RolInUseException(command.Codigo);
|
||||||
|
|
||||||
|
using (var tx = new TransactionScope(
|
||||||
|
TransactionScopeOption.Required,
|
||||||
|
new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted },
|
||||||
|
TransactionScopeAsyncFlowOption.Enabled))
|
||||||
|
{
|
||||||
var updated = await _repository.UpdateAsync(
|
var updated = await _repository.UpdateAsync(
|
||||||
existing.Codigo, existing.Nombre, existing.Descripcion, activo: false);
|
existing.Codigo, existing.Nombre, existing.Descripcion, activo: false);
|
||||||
if (!updated)
|
if (!updated)
|
||||||
throw new RolNotFoundException(command.Codigo);
|
throw new RolNotFoundException(command.Codigo);
|
||||||
|
|
||||||
|
await _audit.LogAsync(
|
||||||
|
action: "rol.deactivate",
|
||||||
|
targetType: "Rol",
|
||||||
|
targetId: existing.Id.ToString());
|
||||||
|
|
||||||
|
tx.Complete();
|
||||||
|
}
|
||||||
|
|
||||||
var rol = await _repository.GetByCodigoAsync(command.Codigo)
|
var rol = await _repository.GetByCodigoAsync(command.Codigo)
|
||||||
?? throw new RolNotFoundException(command.Codigo);
|
?? throw new RolNotFoundException(command.Codigo);
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
|
using System.Transactions;
|
||||||
using SIGCM2.Application.Abstractions;
|
using SIGCM2.Application.Abstractions;
|
||||||
using SIGCM2.Application.Abstractions.Persistence;
|
using SIGCM2.Application.Abstractions.Persistence;
|
||||||
|
using SIGCM2.Application.Audit;
|
||||||
using SIGCM2.Application.Roles.Dtos;
|
using SIGCM2.Application.Roles.Dtos;
|
||||||
using SIGCM2.Domain.Exceptions;
|
using SIGCM2.Domain.Exceptions;
|
||||||
|
|
||||||
@@ -8,13 +10,23 @@ namespace SIGCM2.Application.Roles.Update;
|
|||||||
public sealed class UpdateRolCommandHandler : ICommandHandler<UpdateRolCommand, RolDto>
|
public sealed class UpdateRolCommandHandler : ICommandHandler<UpdateRolCommand, RolDto>
|
||||||
{
|
{
|
||||||
private readonly IRolRepository _repository;
|
private readonly IRolRepository _repository;
|
||||||
|
private readonly IAuditLogger _audit;
|
||||||
|
|
||||||
public UpdateRolCommandHandler(IRolRepository repository)
|
public UpdateRolCommandHandler(IRolRepository repository, IAuditLogger audit)
|
||||||
{
|
{
|
||||||
_repository = repository;
|
_repository = repository;
|
||||||
|
_audit = audit;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<RolDto> Handle(UpdateRolCommand command)
|
public async Task<RolDto> Handle(UpdateRolCommand command)
|
||||||
|
{
|
||||||
|
var before = await _repository.GetByCodigoAsync(command.Codigo)
|
||||||
|
?? throw new RolNotFoundException(command.Codigo);
|
||||||
|
|
||||||
|
using (var tx = new TransactionScope(
|
||||||
|
TransactionScopeOption.Required,
|
||||||
|
new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted },
|
||||||
|
TransactionScopeAsyncFlowOption.Enabled))
|
||||||
{
|
{
|
||||||
var updated = await _repository.UpdateAsync(
|
var updated = await _repository.UpdateAsync(
|
||||||
command.Codigo, command.Nombre, command.Descripcion, command.Activo);
|
command.Codigo, command.Nombre, command.Descripcion, command.Activo);
|
||||||
@@ -22,6 +34,19 @@ public sealed class UpdateRolCommandHandler : ICommandHandler<UpdateRolCommand,
|
|||||||
if (!updated)
|
if (!updated)
|
||||||
throw new RolNotFoundException(command.Codigo);
|
throw new RolNotFoundException(command.Codigo);
|
||||||
|
|
||||||
|
await _audit.LogAsync(
|
||||||
|
action: "rol.update",
|
||||||
|
targetType: "Rol",
|
||||||
|
targetId: before.Id.ToString(),
|
||||||
|
metadata: new
|
||||||
|
{
|
||||||
|
before = new { before.Nombre, before.Descripcion, before.Activo },
|
||||||
|
after = new { command.Nombre, command.Descripcion, command.Activo },
|
||||||
|
});
|
||||||
|
|
||||||
|
tx.Complete();
|
||||||
|
}
|
||||||
|
|
||||||
var rol = await _repository.GetByCodigoAsync(command.Codigo)
|
var rol = await _repository.GetByCodigoAsync(command.Codigo)
|
||||||
?? throw new RolNotFoundException(command.Codigo);
|
?? throw new RolNotFoundException(command.Codigo);
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using NSubstitute;
|
using NSubstitute;
|
||||||
using SIGCM2.Application.Abstractions.Persistence;
|
using SIGCM2.Application.Abstractions.Persistence;
|
||||||
|
using SIGCM2.Application.Audit;
|
||||||
using SIGCM2.Application.Permisos.Assign;
|
using SIGCM2.Application.Permisos.Assign;
|
||||||
using SIGCM2.Domain.Entities;
|
using SIGCM2.Domain.Entities;
|
||||||
using SIGCM2.Domain.Exceptions;
|
using SIGCM2.Domain.Exceptions;
|
||||||
@@ -11,11 +12,12 @@ public class AssignPermisosToRolCommandHandlerTests
|
|||||||
private readonly IRolRepository _rolRepository = Substitute.For<IRolRepository>();
|
private readonly IRolRepository _rolRepository = Substitute.For<IRolRepository>();
|
||||||
private readonly IPermisoRepository _permisoRepository = Substitute.For<IPermisoRepository>();
|
private readonly IPermisoRepository _permisoRepository = Substitute.For<IPermisoRepository>();
|
||||||
private readonly IRolPermisoRepository _rolPermisoRepository = Substitute.For<IRolPermisoRepository>();
|
private readonly IRolPermisoRepository _rolPermisoRepository = Substitute.For<IRolPermisoRepository>();
|
||||||
|
private readonly IAuditLogger _audit = Substitute.For<IAuditLogger>();
|
||||||
private readonly AssignPermisosToRolCommandHandler _handler;
|
private readonly AssignPermisosToRolCommandHandler _handler;
|
||||||
|
|
||||||
public AssignPermisosToRolCommandHandlerTests()
|
public AssignPermisosToRolCommandHandlerTests()
|
||||||
{
|
{
|
||||||
_handler = new AssignPermisosToRolCommandHandler(_rolRepository, _permisoRepository, _rolPermisoRepository);
|
_handler = new AssignPermisosToRolCommandHandler(_rolRepository, _permisoRepository, _rolPermisoRepository, _audit);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Rol MakeRol(int id, string codigo) =>
|
private static Rol MakeRol(int id, string codigo) =>
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using NSubstitute;
|
using NSubstitute;
|
||||||
using SIGCM2.Application.Abstractions.Persistence;
|
using SIGCM2.Application.Abstractions.Persistence;
|
||||||
|
using SIGCM2.Application.Audit;
|
||||||
using SIGCM2.Application.Roles.Create;
|
using SIGCM2.Application.Roles.Create;
|
||||||
using SIGCM2.Domain.Entities;
|
using SIGCM2.Domain.Entities;
|
||||||
using SIGCM2.Domain.Exceptions;
|
using SIGCM2.Domain.Exceptions;
|
||||||
@@ -9,13 +10,14 @@ namespace SIGCM2.Application.Tests.Roles.Create;
|
|||||||
public class CreateRolCommandHandlerTests
|
public class CreateRolCommandHandlerTests
|
||||||
{
|
{
|
||||||
private readonly IRolRepository _repository = Substitute.For<IRolRepository>();
|
private readonly IRolRepository _repository = Substitute.For<IRolRepository>();
|
||||||
|
private readonly IAuditLogger _audit = Substitute.For<IAuditLogger>();
|
||||||
private readonly CreateRolCommandHandler _handler;
|
private readonly CreateRolCommandHandler _handler;
|
||||||
|
|
||||||
private static CreateRolCommand ValidCommand() => new("cajero_senior", "Cajero Senior", "Con más permisos");
|
private static CreateRolCommand ValidCommand() => new("cajero_senior", "Cajero Senior", "Con más permisos");
|
||||||
|
|
||||||
public CreateRolCommandHandlerTests()
|
public CreateRolCommandHandlerTests()
|
||||||
{
|
{
|
||||||
_handler = new CreateRolCommandHandler(_repository);
|
_handler = new CreateRolCommandHandler(_repository, _audit);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using NSubstitute;
|
using NSubstitute;
|
||||||
using SIGCM2.Application.Abstractions.Persistence;
|
using SIGCM2.Application.Abstractions.Persistence;
|
||||||
|
using SIGCM2.Application.Audit;
|
||||||
using SIGCM2.Application.Roles.Deactivate;
|
using SIGCM2.Application.Roles.Deactivate;
|
||||||
using SIGCM2.Domain.Entities;
|
using SIGCM2.Domain.Entities;
|
||||||
using SIGCM2.Domain.Exceptions;
|
using SIGCM2.Domain.Exceptions;
|
||||||
@@ -9,6 +10,7 @@ namespace SIGCM2.Application.Tests.Roles.Deactivate;
|
|||||||
public class DeactivateRolCommandHandlerTests
|
public class DeactivateRolCommandHandlerTests
|
||||||
{
|
{
|
||||||
private readonly IRolRepository _repository = Substitute.For<IRolRepository>();
|
private readonly IRolRepository _repository = Substitute.For<IRolRepository>();
|
||||||
|
private readonly IAuditLogger _audit = Substitute.For<IAuditLogger>();
|
||||||
private readonly DeactivateRolCommandHandler _handler;
|
private readonly DeactivateRolCommandHandler _handler;
|
||||||
|
|
||||||
private static Rol RolActive(string codigo, int id = 10)
|
private static Rol RolActive(string codigo, int id = 10)
|
||||||
@@ -19,7 +21,7 @@ public class DeactivateRolCommandHandlerTests
|
|||||||
|
|
||||||
public DeactivateRolCommandHandlerTests()
|
public DeactivateRolCommandHandlerTests()
|
||||||
{
|
{
|
||||||
_handler = new DeactivateRolCommandHandler(_repository);
|
_handler = new DeactivateRolCommandHandler(_repository, _audit);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using NSubstitute;
|
using NSubstitute;
|
||||||
using SIGCM2.Application.Abstractions.Persistence;
|
using SIGCM2.Application.Abstractions.Persistence;
|
||||||
|
using SIGCM2.Application.Audit;
|
||||||
using SIGCM2.Application.Roles.Update;
|
using SIGCM2.Application.Roles.Update;
|
||||||
using SIGCM2.Domain.Entities;
|
using SIGCM2.Domain.Entities;
|
||||||
using SIGCM2.Domain.Exceptions;
|
using SIGCM2.Domain.Exceptions;
|
||||||
@@ -9,11 +10,12 @@ namespace SIGCM2.Application.Tests.Roles.Update;
|
|||||||
public class UpdateRolCommandHandlerTests
|
public class UpdateRolCommandHandlerTests
|
||||||
{
|
{
|
||||||
private readonly IRolRepository _repository = Substitute.For<IRolRepository>();
|
private readonly IRolRepository _repository = Substitute.For<IRolRepository>();
|
||||||
|
private readonly IAuditLogger _audit = Substitute.For<IAuditLogger>();
|
||||||
private readonly UpdateRolCommandHandler _handler;
|
private readonly UpdateRolCommandHandler _handler;
|
||||||
|
|
||||||
public UpdateRolCommandHandlerTests()
|
public UpdateRolCommandHandlerTests()
|
||||||
{
|
{
|
||||||
_handler = new UpdateRolCommandHandler(_repository);
|
_handler = new UpdateRolCommandHandler(_repository, _audit);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|||||||
Reference in New Issue
Block a user