| | | 1 | | using FAU.DataAccess.Repositories; |
| | | 2 | | using FAU.Entidades; |
| | | 3 | | |
| | | 4 | | namespace FAU.Logica.Services; |
| | | 5 | | |
| | | 6 | | public class RolService : IRolService |
| | | 7 | | { |
| | | 8 | | private readonly IRolRepository _rolRepository; |
| | | 9 | | private readonly IPermisoRepository _permisoRepository; |
| | | 10 | | private readonly IAuditoriaService _auditoriaService; |
| | | 11 | | private readonly ICurrentUserContext _currentUser; |
| | | 12 | | |
| | 19 | 13 | | public RolService(IRolRepository rolRepository, IPermisoRepository permisoRepository, IAuditoriaService auditoriaSer |
| | | 14 | | { |
| | 19 | 15 | | _rolRepository = rolRepository; |
| | 19 | 16 | | _permisoRepository = permisoRepository; |
| | 19 | 17 | | _auditoriaService = auditoriaService; |
| | 19 | 18 | | _currentUser = currentUser; |
| | 19 | 19 | | } |
| | | 20 | | |
| | | 21 | | public async Task<Rol?> GetByIdAsync(long id) |
| | | 22 | | { |
| | 2 | 23 | | return await _rolRepository.GetByIdAsync(id); |
| | 2 | 24 | | } |
| | | 25 | | |
| | | 26 | | public async Task<Rol?> GetByNombreAsync(string nombre) |
| | | 27 | | { |
| | 2 | 28 | | return await _rolRepository.GetByNombreAsync(nombre); |
| | 2 | 29 | | } |
| | | 30 | | |
| | | 31 | | public async Task<IEnumerable<Rol>> GetAllAsync() |
| | | 32 | | { |
| | 2 | 33 | | return await _rolRepository.GetAllAsync(); |
| | 2 | 34 | | } |
| | | 35 | | |
| | | 36 | | public async Task<(bool Success, Rol? Rol, string? Error)> CreateAsync(string nombre, string? descripcion = null) |
| | | 37 | | { |
| | | 38 | | try |
| | | 39 | | { |
| | 2 | 40 | | if (await _rolRepository.ExistsAsync(nombre)) |
| | | 41 | | { |
| | 1 | 42 | | return (false, null, "Ya existe un rol con ese nombre"); |
| | | 43 | | } |
| | | 44 | | |
| | 1 | 45 | | var nuevoRol = new Rol |
| | 1 | 46 | | { |
| | 1 | 47 | | Nombre = nombre, |
| | 1 | 48 | | Descripcion = descripcion |
| | 1 | 49 | | }; |
| | | 50 | | |
| | 1 | 51 | | var rolCreado = await _rolRepository.CreateAsync(nuevoRol); |
| | 1 | 52 | | await _auditoriaService.LogAuditoriaAsync(_currentUser.UserId ?? 0, AccionEnum.CrearRol, ContextoEnum.Roles, |
| | 1 | 53 | | return (true, rolCreado, null); |
| | | 54 | | } |
| | 0 | 55 | | catch (Exception ex) |
| | | 56 | | { |
| | 0 | 57 | | return (false, null, $"Error al crear rol: {ex.Message}"); |
| | | 58 | | } |
| | 2 | 59 | | } |
| | | 60 | | |
| | | 61 | | public async Task<(bool Success, Rol? Rol, string? Error)> UpdateAsync(long id, string nombre, string? descripcion = |
| | | 62 | | { |
| | | 63 | | try |
| | | 64 | | { |
| | 3 | 65 | | var rol = await _rolRepository.GetByIdAsync(id); |
| | 3 | 66 | | if (rol == null) |
| | | 67 | | { |
| | 1 | 68 | | return (false, null, "Rol no encontrado"); |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | // Verificar si el nuevo nombre ya existe (si es diferente al actual) |
| | 2 | 72 | | if (rol.Nombre != nombre && await _rolRepository.ExistsAsync(nombre)) |
| | | 73 | | { |
| | 1 | 74 | | return (false, null, "Ya existe un rol con ese nombre"); |
| | | 75 | | } |
| | | 76 | | |
| | 1 | 77 | | rol.Nombre = nombre; |
| | 1 | 78 | | rol.Descripcion = descripcion; |
| | | 79 | | |
| | 1 | 80 | | var rolActualizado = await _rolRepository.UpdateAsync(rol); |
| | 1 | 81 | | await _auditoriaService.LogAuditoriaAsync(_currentUser.UserId ?? 0, AccionEnum.ActualizarRol, ContextoEnum.R |
| | 1 | 82 | | return (true, rolActualizado, null); |
| | | 83 | | } |
| | 0 | 84 | | catch (Exception ex) |
| | | 85 | | { |
| | 0 | 86 | | return (false, null, $"Error al actualizar rol: {ex.Message}"); |
| | | 87 | | } |
| | 3 | 88 | | } |
| | | 89 | | |
| | | 90 | | public async Task<(bool Success, string? Error)> AsignarPermisoAsync(long rolId, long permisoId) |
| | | 91 | | { |
| | | 92 | | try |
| | | 93 | | { |
| | 4 | 94 | | var rol = await _rolRepository.GetByIdAsync(rolId); |
| | 4 | 95 | | if (rol == null) |
| | | 96 | | { |
| | 1 | 97 | | return (false, "Rol no encontrado"); |
| | | 98 | | } |
| | | 99 | | |
| | 3 | 100 | | var permiso = await _permisoRepository.GetByIdAsync(permisoId); |
| | 3 | 101 | | if (permiso == null) |
| | | 102 | | { |
| | 1 | 103 | | return (false, "Permiso no encontrado"); |
| | | 104 | | } |
| | | 105 | | |
| | 3 | 106 | | if (rol.Permisos.Any(p => p.Id == permisoId)) |
| | | 107 | | { |
| | 1 | 108 | | return (false, "El rol ya tiene este permiso asignado"); |
| | | 109 | | } |
| | | 110 | | |
| | 1 | 111 | | rol.Permisos.Add(permiso); |
| | 1 | 112 | | await _rolRepository.UpdateAsync(rol); |
| | 1 | 113 | | await _auditoriaService.LogAuditoriaAsync(_currentUser.UserId ?? 0, AccionEnum.ActualizarRol, ContextoEnum.R |
| | | 114 | | |
| | 1 | 115 | | return (true, null); |
| | | 116 | | } |
| | 0 | 117 | | catch (Exception ex) |
| | | 118 | | { |
| | 0 | 119 | | return (false, $"Error al asignar permiso: {ex.Message}"); |
| | | 120 | | } |
| | 4 | 121 | | } |
| | | 122 | | |
| | | 123 | | public async Task<(bool Success, string? Error)> RemoverPermisoAsync(long rolId, long permisoId) |
| | | 124 | | { |
| | | 125 | | try |
| | | 126 | | { |
| | 3 | 127 | | var rol = await _rolRepository.GetByIdAsync(rolId); |
| | 3 | 128 | | if (rol == null) |
| | | 129 | | { |
| | 1 | 130 | | return (false, "Rol no encontrado"); |
| | | 131 | | } |
| | | 132 | | |
| | 3 | 133 | | var permiso = rol.Permisos.FirstOrDefault(p => p.Id == permisoId); |
| | 2 | 134 | | if (permiso == null) |
| | | 135 | | { |
| | 1 | 136 | | return (false, "El rol no tiene este permiso asignado"); |
| | | 137 | | } |
| | | 138 | | |
| | 1 | 139 | | rol.Permisos.Remove(permiso); |
| | 1 | 140 | | await _rolRepository.UpdateAsync(rol); |
| | 1 | 141 | | await _auditoriaService.LogAuditoriaAsync(_currentUser.UserId ?? 0, AccionEnum.ActualizarRol, ContextoEnum.R |
| | | 142 | | |
| | 1 | 143 | | return (true, null); |
| | | 144 | | } |
| | 0 | 145 | | catch (Exception ex) |
| | | 146 | | { |
| | 0 | 147 | | return (false, $"Error al remover permiso: {ex.Message}"); |
| | | 148 | | } |
| | 3 | 149 | | } |
| | | 150 | | |
| | | 151 | | public async Task DeleteAsync(long id) |
| | | 152 | | { |
| | 1 | 153 | | await _auditoriaService.LogAuditoriaAsync(_currentUser.UserId ?? 0, AccionEnum.ActualizarRol, ContextoEnum.Roles |
| | 1 | 154 | | await _rolRepository.DeleteAsync(id); |
| | 1 | 155 | | } |
| | | 156 | | } |
| | | 157 | | |