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.
147 lines
4.4 KiB
C#
147 lines
4.4 KiB
C#
using Distribution.Features.Auth;
|
|
using Distribution.Infra.UoW;
|
|
|
|
namespace Distribution.Features.User;
|
|
|
|
public class UserService(IUserRepository repo, IUnitOfWork uow, IAuthHexClient authHex)
|
|
{
|
|
private static UserResponse ToResponse(Models.User u) =>
|
|
new(u.Id, u.Username, u.FullName, u.FK_RoleId, u.Role?.Name ?? string.Empty,
|
|
u.FK_ReportsToUserId, u.IsActive, u.CreatedAt, u.UpdatedAt);
|
|
|
|
public async Task<List<UserResponse>> GetAllAsync() =>
|
|
(await repo.GetAllAsync())
|
|
.Where(u => u.IsActive)
|
|
.Select(ToResponse)
|
|
.ToList();
|
|
|
|
public async Task<UserResponse?> GetByIdAsync(Guid id)
|
|
{
|
|
var user = await repo.GetByIdAsync(id);
|
|
if (user is null || !user.IsActive) return null;
|
|
return ToResponse(user);
|
|
}
|
|
|
|
public async Task<UserResponse> CreateAsync(CreateUserRequest request, CancellationToken ct = default)
|
|
{
|
|
var user = new Models.User
|
|
{
|
|
Username = request.Username,
|
|
FullName = request.FullName,
|
|
FK_RoleId = request.RoleId,
|
|
FK_ReportsToUserId = request.ReportsToUserId
|
|
};
|
|
|
|
await uow.BeginAsync(ct);
|
|
try
|
|
{
|
|
await repo.AddAsync(user);
|
|
|
|
var authResult = await authHex.CallAsync("registerUser", new
|
|
{
|
|
userId = user.Id,
|
|
userName = request.Username,
|
|
email = request.Username,
|
|
fullname = request.FullName,
|
|
password = request.Password,
|
|
roleId = request.RoleId,
|
|
userTypeId = "00000001-0000-0000-0000-200000000000",
|
|
sendCredentialsEmail = false
|
|
}, reference: user.Id.ToString(), ct: ct);
|
|
|
|
if (!authResult.Success)
|
|
{
|
|
await uow.RollbackAsync(ct);
|
|
throw new InvalidOperationException($"AuthHex registration failed: {authResult.Message}");
|
|
}
|
|
|
|
await uow.CommitAsync(ct);
|
|
}
|
|
catch
|
|
{
|
|
await uow.RollbackAsync(ct);
|
|
throw;
|
|
}
|
|
return ToResponse(user);
|
|
}
|
|
|
|
public async Task<UserResponse?> UpdateAsync(Guid id, UpdateUserRequest request, CancellationToken ct = default)
|
|
{
|
|
var user = await repo.GetByIdAsync(id);
|
|
if (user is null || !user.IsActive) return null;
|
|
user.FullName = request.FullName;
|
|
user.UpdatedAt = DateTime.Now;
|
|
await uow.BeginAsync(ct);
|
|
try
|
|
{
|
|
repo.Update(user);
|
|
await uow.CommitAsync(ct);
|
|
}
|
|
catch
|
|
{
|
|
await uow.RollbackAsync(ct);
|
|
throw;
|
|
}
|
|
return ToResponse(user);
|
|
}
|
|
|
|
public async Task<UserResponse?> AssignRoleAsync(Guid id, AssignUserRoleRequest request, CancellationToken ct = default)
|
|
{
|
|
var user = await repo.GetByIdAsync(id);
|
|
if (user is null || !user.IsActive) return null;
|
|
user.FK_RoleId = request.RoleId;
|
|
user.UpdatedAt = DateTime.Now;
|
|
await uow.BeginAsync(ct);
|
|
try
|
|
{
|
|
repo.Update(user);
|
|
await uow.CommitAsync(ct);
|
|
}
|
|
catch
|
|
{
|
|
await uow.RollbackAsync(ct);
|
|
throw;
|
|
}
|
|
return ToResponse(user);
|
|
}
|
|
|
|
public async Task<bool> AssignScopeAsync(Guid id, AssignUserScopeRequest request, CancellationToken ct = default)
|
|
{
|
|
var user = await repo.GetByIdAsync(id);
|
|
if (user is null || !user.IsActive) return false;
|
|
|
|
await uow.BeginAsync(ct);
|
|
try
|
|
{
|
|
await repo.ReplaceRegionScopesAsync(id, request.RegionIds);
|
|
await repo.ReplaceRouteScopesAsync(id, request.RouteIds);
|
|
await repo.ReplaceStoreScopesAsync(id, request.StoreIds);
|
|
await uow.CommitAsync(ct);
|
|
}
|
|
catch
|
|
{
|
|
await uow.RollbackAsync(ct);
|
|
throw;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public async Task<bool> DeactivateAsync(Guid id, CancellationToken ct = default)
|
|
{
|
|
var user = await repo.GetByIdAsync(id);
|
|
if (user is null || !user.IsActive) return false;
|
|
await uow.BeginAsync(ct);
|
|
try
|
|
{
|
|
repo.Remove(user);
|
|
await uow.CommitAsync(ct);
|
|
}
|
|
catch
|
|
{
|
|
await uow.RollbackAsync(ct);
|
|
throw;
|
|
}
|
|
return true;
|
|
}
|
|
}
|