Refactors current user identity handling in handlers

This commit is contained in:
BinaryAlley committed 2026-08-16 04:59:44 -07:00
1 parent a7dfcbe926
commit f0a1942691
28 files changed
+336 -30

No files matched your search

@@ -10,6 +10,7 @@ using Lumina.Application.Common.Infrastructure.Authorization;
using Lumina.Application.Common.Mapping.Authorization;
using Lumina.Application.Common.Mapping.UsersManagement.Users;
using Lumina.Contracts.Responses.UsersManagement.Users;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
@@ -49,8 +50,14 @@ public class GetUsersQueryHandler : IQueryHandler<GetUsersQuery, Result<IEnumera
/// </returns>
public async Task<Result<IEnumerable<UserResponse>>> HandleAsync(GetUsersQuery query, CancellationToken cancellationToken)
{
// an authenticated request must always carry a user identity
Guid? currentUserId = _currentUserService.UserId;
if (currentUserId is null)
return Errors.Authorization.NotAuthorized;
Guid userId = currentUserId.Value;
// only admins can see the list of users
bool isAdmin = await _authorizationService.IsInRoleAsync(_currentUserService.UserId!.Value, "Admin", cancellationToken).ConfigureAwait(false);
bool isAdmin = await _authorizationService.IsInRoleAsync(userId, "Admin", cancellationToken).ConfigureAwait(false);
if (!isAdmin)
return Errors.Authorization.NotAuthorized;
Result<IEnumerable<UserEntity>> getRolesResult = await _unitOfWork.UserRepository.GetAllAsync(cancellationToken).ConfigureAwait(false);
@@ -60,8 +60,14 @@ public class UpdateUserRoleAndPermissionsCommandHandler : ICommandHandler<Update
if (validationResult.Count > 0)
return validationResult;
// an authenticated request must always carry a user identity
Guid? currentUserId = _currentUserService.UserId;
if (currentUserId is null)
return ApplicationErrors.Authorization.NotAuthorized;
Guid userId = currentUserId.Value;
// only admins can update user authorizations
bool isAdmin = await _authorizationService.IsInRoleAsync(_currentUserService.UserId!.Value, "Admin", cancellationToken).ConfigureAwait(false);
bool isAdmin = await _authorizationService.IsInRoleAsync(userId, "Admin", cancellationToken).ConfigureAwait(false);
if (!isAdmin)
return ApplicationErrors.Authorization.NotAuthorized;
@@ -8,6 +8,7 @@ using Lumina.Application.Common.Infrastructure.Authorization;
using Lumina.Application.Common.Infrastructure.Validation;
using Lumina.Application.Common.Mapping.Authorization;
using Lumina.Contracts.Responses.Authorization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
@@ -52,11 +53,17 @@ public class GetAuthorizationQueryHandler : IQueryHandler<GetAuthorizationQuery,
if (validationResult.Count > 0)
return validationResult;
// an authenticated request must always carry a user identity
Guid? currentUserId = _currentUserService.UserId;
if (currentUserId is null)
return Errors.Authorization.NotAuthorized;
Guid userId = currentUserId.Value;
// first, check if the Id of the user for whom to get the permission list is different from the Id currently making the request
if (_currentUserService.UserId != query.UserId)
if (userId != query.UserId)
{
// if it is, get the role of the current user, and see if they are Admin
Result<UserAuthorizationEntity> getCurrentUserPermissionResult = await _authorizationService.GetUserAuthorizationAsync(_currentUserService.UserId!.Value, cancellationToken).ConfigureAwait(false);
Result<UserAuthorizationEntity> getCurrentUserPermissionResult = await _authorizationService.GetUserAuthorizationAsync(userId, cancellationToken).ConfigureAwait(false);
if (getCurrentUserPermissionResult.IsFailure)
return getCurrentUserPermissionResult.Errors;
// if the current user is not an Admin, and the account for whom they request the permissions list is not theirs, deny the request
@@ -10,6 +10,7 @@ using Lumina.Application.Common.Infrastructure.Authorization;
using Lumina.Application.Common.Infrastructure.Validation;
using Lumina.Application.Common.Mapping.Authorization;
using Lumina.Contracts.Responses.Authorization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
@@ -57,8 +58,14 @@ public class GetUserPermissionsQueryHandler : IQueryHandler<GetUserPermissionsQu
if (validationResult.Count > 0)
return validationResult;
// an authenticated request must always carry a user identity
Guid? currentUserId = _currentUserService.UserId;
if (currentUserId is null)
return Errors.Authorization.NotAuthorized;
Guid userId = currentUserId.Value;
// only admins can see the list of authorization permissions
bool isAdmin = await _authorizationService.IsInRoleAsync(_currentUserService.UserId!.Value, "Admin", cancellationToken).ConfigureAwait(false);
bool isAdmin = await _authorizationService.IsInRoleAsync(userId, "Admin", cancellationToken).ConfigureAwait(false);
if (!isAdmin)
return Errors.Authorization.NotAuthorized;
// get the user from the repository and return its permissions
@@ -10,6 +10,7 @@ using Lumina.Application.Common.Infrastructure.Authorization;
using Lumina.Application.Common.Infrastructure.Validation;
using Lumina.Application.Common.Mapping.Authorization;
using Lumina.Contracts.Responses.Authorization;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
@@ -56,8 +57,14 @@ public class GetUserRoleQueryHandler : IQueryHandler<GetUserRoleQuery, Result<Ro
if (validationResult.Count > 0)
return validationResult;
// an authenticated request must always carry a user identity
Guid? currentUserId = _currentUserService.UserId;
if (currentUserId is null)
return Errors.Authorization.NotAuthorized;
Guid userId = currentUserId.Value;
// only admins can see the authorization role
bool isAdmin = await _authorizationService.IsInRoleAsync(_currentUserService.UserId!.Value, "Admin", cancellationToken).ConfigureAwait(false);
bool isAdmin = await _authorizationService.IsInRoleAsync(userId, "Admin", cancellationToken).ConfigureAwait(false);
if (!isAdmin)
return Errors.Authorization.NotAuthorized;
// get the user from the repository and return its roles