0052725b88
- Created UserViewSheet component to display user details including username, full name, role, status, and timestamps. - Created RoleViewSheet component to display role details including name, level, status, creation date, and associated permissions. - Introduced permissions configuration in a new permissions.ts file, defining categories and permission codes for better management. - Implemented middleware for authentication, ensuring access control for protected routes.
105 lines
3.1 KiB
C#
105 lines
3.1 KiB
C#
using Distribution.Infra.UoW;
|
|
|
|
namespace Distribution.Features.Role;
|
|
|
|
public class RoleService(IRoleRepository repo, IUnitOfWork uow)
|
|
{
|
|
private static RoleResponse ToResponse(Models.Role r) =>
|
|
new(r.Id, r.Name, r.Level, r.IsActive, r.CreatedAt);
|
|
|
|
public async Task<List<RoleResponse>> GetAllAsync() =>
|
|
(await repo.GetAllAsync())
|
|
.Where(r => r.IsActive)
|
|
.Select(ToResponse)
|
|
.ToList();
|
|
|
|
public async Task<RoleResponse?> GetByIdAsync(Guid id)
|
|
{
|
|
var role = await repo.GetByIdAsync(id);
|
|
if (role is null || !role.IsActive) return null;
|
|
return ToResponse(role);
|
|
}
|
|
|
|
public async Task<List<RolePermissionResponse>?> GetPermissionsAsync(Guid roleId)
|
|
{
|
|
var role = await repo.GetByIdAsync(roleId);
|
|
if (role is null || !role.IsActive) return null;
|
|
return (await repo.GetPermissionsAsync(roleId))
|
|
.Select(rp => new RolePermissionResponse(rp.FK_PermissionId, rp.Permission.Code, rp.Permission.Description))
|
|
.ToList();
|
|
}
|
|
|
|
public async Task<RoleResponse> CreateAsync(CreateRoleRequest request, CancellationToken ct = default)
|
|
{
|
|
var role = new Models.Role { Name = request.Name, Level = request.Level };
|
|
await uow.BeginAsync(ct);
|
|
try
|
|
{
|
|
await repo.AddAsync(role);
|
|
await uow.CommitAsync(ct);
|
|
}
|
|
catch
|
|
{
|
|
await uow.RollbackAsync(ct);
|
|
throw;
|
|
}
|
|
return ToResponse(role);
|
|
}
|
|
|
|
public async Task<RoleResponse?> UpdateAsync(Guid id, UpdateRoleRequest request, CancellationToken ct = default)
|
|
{
|
|
var role = await repo.GetByIdAsync(id);
|
|
if (role is null || !role.IsActive) return null;
|
|
role.Name = request.Name;
|
|
role.Level = request.Level;
|
|
await uow.BeginAsync(ct);
|
|
try
|
|
{
|
|
repo.Update(role);
|
|
await uow.CommitAsync(ct);
|
|
}
|
|
catch
|
|
{
|
|
await uow.RollbackAsync(ct);
|
|
throw;
|
|
}
|
|
return ToResponse(role);
|
|
}
|
|
|
|
public async Task<bool> DeactivateAsync(Guid id, CancellationToken ct = default)
|
|
{
|
|
var role = await repo.GetByIdAsync(id);
|
|
if (role is null || !role.IsActive) return false;
|
|
await uow.BeginAsync(ct);
|
|
try
|
|
{
|
|
repo.Remove(role);
|
|
await uow.CommitAsync(ct);
|
|
}
|
|
catch
|
|
{
|
|
await uow.RollbackAsync(ct);
|
|
throw;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public async Task<List<RolePermissionResponse>?> AssignPermissionsAsync(Guid id, AssignRolePermissionsRequest request, CancellationToken ct = default)
|
|
{
|
|
var role = await repo.GetByIdAsync(id);
|
|
if (role is null || !role.IsActive) return null;
|
|
await uow.BeginAsync(ct);
|
|
try
|
|
{
|
|
await repo.ReplacePermissionsAsync(id, request.PermissionIds);
|
|
await uow.CommitAsync(ct);
|
|
}
|
|
catch
|
|
{
|
|
await uow.RollbackAsync(ct);
|
|
throw;
|
|
}
|
|
return await GetPermissionsAsync(id);
|
|
}
|
|
}
|