feat: add user and role view sheets with detailed information display

- 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.
This commit is contained in:
Kalana B. Thilakarathna
2026-10-18 23:11:01 +05:30
parent 558afc6f9d
commit 0052725b88
86 changed files with 41117 additions and 939 deletions
@@ -5,4 +5,8 @@ public interface IRoleRepository
Task<Models.Role?> GetByIdAsync(Guid id);
Task<List<Models.Role>> GetAllAsync();
Task<List<Models.RolePermission>> GetPermissionsAsync(Guid roleId);
Task AddAsync(Models.Role role);
void Update(Models.Role role);
void Remove(Models.Role role);
Task ReplacePermissionsAsync(Guid roleId, List<Guid> permissionIds);
}
@@ -23,4 +23,32 @@ public class RoleController(RoleService service) : ControllerBase
var result = await service.GetPermissionsAsync(id);
return result is null ? NotFound() : Ok(result);
}
[HttpPost]
public async Task<IActionResult> Create(CreateRoleRequest request, CancellationToken ct)
{
var result = await service.CreateAsync(request, ct);
return CreatedAtAction(nameof(GetById), new { id = result.Id }, result);
}
[HttpPut("{id:guid}")]
public async Task<IActionResult> Update(Guid id, UpdateRoleRequest request, CancellationToken ct)
{
var result = await service.UpdateAsync(id, request, ct);
return result is null ? NotFound() : Ok(result);
}
[HttpPut("{id:guid}/permissions")]
public async Task<IActionResult> AssignPermissions(Guid id, AssignRolePermissionsRequest request, CancellationToken ct)
{
var result = await service.AssignPermissionsAsync(id, request, ct);
return result is null ? NotFound() : Ok(result);
}
[HttpDelete("{id:guid}")]
public async Task<IActionResult> Deactivate(Guid id, CancellationToken ct)
{
var deactivated = await service.DeactivateAsync(id, ct);
return deactivated ? NoContent() : NotFound();
}
}
+3 -1
View File
@@ -1,5 +1,7 @@
namespace Distribution.Features.Role;
public record RoleResponse(Guid Id, string Name, int Level, bool IsActive, DateTime CreatedAt);
public record RolePermissionResponse(Guid PermissionId, string Code, string Description);
public record CreateRoleRequest(string Name, int Level);
public record UpdateRoleRequest(string Name, int Level);
public record AssignRolePermissionsRequest(List<Guid> PermissionIds);
@@ -1,4 +1,5 @@
using Distribution.Data;
using Distribution.Models;
using Microsoft.EntityFrameworkCore;
namespace Distribution.Features.Role;
@@ -16,4 +17,29 @@ public class RoleRepository(AppDBContext context) : IRoleRepository
.Include(rp => rp.Permission)
.Where(rp => rp.FK_RoleId == roleId && rp.IsActive)
.ToListAsync();
public async Task AddAsync(Models.Role role) =>
await context.Roles.AddAsync(role);
public void Update(Models.Role role) =>
context.Roles.Update(role);
public void Remove(Models.Role role)
{
role.IsActive = false;
context.Roles.Update(role);
}
public async Task ReplacePermissionsAsync(Guid roleId, List<Guid> permissionIds)
{
var existing = await context.RolePermissions
.Where(rp => rp.FK_RoleId == roleId)
.ToListAsync();
context.RolePermissions.RemoveRange(existing);
context.RolePermissions.AddRange(permissionIds.Select(pid => new RolePermission
{
FK_RoleId = roleId,
FK_PermissionId = pid
}));
}
}
+81 -3
View File
@@ -1,18 +1,23 @@
using Distribution.Infra.UoW;
namespace Distribution.Features.Role;
public class RoleService(IRoleRepository repo)
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(r => new RoleResponse(r.Id, r.Name, r.Level, r.IsActive, r.CreatedAt))
.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 new RoleResponse(role.Id, role.Name, role.Level, role.IsActive, role.CreatedAt);
return ToResponse(role);
}
public async Task<List<RolePermissionResponse>?> GetPermissionsAsync(Guid roleId)
@@ -23,4 +28,77 @@ public class RoleService(IRoleRepository repo)
.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);
}
}