From ab4d5965004f298a886f715d9559c19041d3a34a Mon Sep 17 00:00:00 2001 From: Binary Alley Date: Sun, 27 Sep 2026 21:21:04 +0300 Subject: [PATCH] Reports a missing permission when updating a user's role and permissions --- .../Common/Errors/Errors.Authorization.cs | 3 +- ...ateUserRoleAndPermissionsCommandHandler.cs | 30 +++++++++++-------- ...teUserRoleAndPermissionsEndpointSummary.cs | 4 +-- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/Lumina.Application/Common/Errors/Errors.Authorization.cs b/src/Lumina.Application/Common/Errors/Errors.Authorization.cs index a216fd92..1edb0511 100644 --- a/src/Lumina.Application/Common/Errors/Errors.Authorization.cs +++ b/src/Lumina.Application/Common/Errors/Errors.Authorization.cs @@ -17,7 +17,8 @@ public static partial class Errors public static Error AdminRoleNotFound => Error.NotFound(description: nameof(AdminRoleNotFound)); public static Error AdminRoleCannotBeDeleted => Error.Forbidden(description: nameof(AdminRoleCannotBeDeleted)); public static Error RoleNotFound => Error.NotFound(description: nameof(RoleNotFound)); - public static Error PermissionAlreadyExists => Error.Unauthorized(description: nameof(PermissionAlreadyExists)); + public static Error PermissionNotFound => Error.NotFound(description: nameof(PermissionNotFound)); + public static Error PermissionAlreadyExists => Error.Conflict(description: nameof(PermissionAlreadyExists)); public static Error RoleAlreadyExists => Error.Conflict(description: nameof(RoleAlreadyExists)); public static Error RoleIdCannotBeEmpty => Error.Validation(description: nameof(RoleIdCannotBeEmpty)); public static Error PermissionsListCannotBeNull => Error.Validation(description: nameof(PermissionsListCannotBeNull)); diff --git a/src/Lumina.Application/Core/UsersManagement/Authorization/Commands/UpdateUserRoleAndPermissions/UpdateUserRoleAndPermissionsCommandHandler.cs b/src/Lumina.Application/Core/UsersManagement/Authorization/Commands/UpdateUserRoleAndPermissions/UpdateUserRoleAndPermissionsCommandHandler.cs index f8207dc7..f53c150f 100644 --- a/src/Lumina.Application/Core/UsersManagement/Authorization/Commands/UpdateUserRoleAndPermissions/UpdateUserRoleAndPermissionsCommandHandler.cs +++ b/src/Lumina.Application/Core/UsersManagement/Authorization/Commands/UpdateUserRoleAndPermissions/UpdateUserRoleAndPermissionsCommandHandler.cs @@ -78,16 +78,17 @@ public class UpdateUserRoleAndPermissionsCommandHandler : ICommandHandler getRoleResult = default; + RoleEntity? newRole = null; if (command.RoleId is not null) { // Get the new role. - getRoleResult = await _unitOfWork.RoleRepository.GetByIdAsync(command.RoleId!.Value, cancellationToken: cancellationToken).ConfigureAwait(false); + Result getRoleResult = await _unitOfWork.RoleRepository.GetByIdAsync(command.RoleId!.Value, cancellationToken: cancellationToken).ConfigureAwait(false); if (getRoleResult.IsFailure || getRoleResult.Value is null) return ApplicationErrors.Authorization.RoleNotFound; + newRole = getRoleResult.Value; // Check if changing the role of an admin would leave the application without any admin. - if (getUserResult.Value.UserRole?.Role.RoleName == "Admin" && getRoleResult.Value.RoleName != "Admin") + if (getUserResult.Value.UserRole?.Role.RoleName == "Admin" && newRole.RoleName != "Admin") { // Count how many admins exist, so the last one is never demoted. Result> getAllUsersResult = await _unitOfWork.UserRepository.GetAllAsync(cancellationToken: cancellationToken).ConfigureAwait(false); @@ -106,22 +107,25 @@ public class UpdateUserRoleAndPermissionsCommandHandler : ICommandHandler permissionsById = getPermissionsResult.Value.ToDictionary(permission => permission.Id); + if (command.Permissions.Any(permissionId => !permissionsById.ContainsKey(permissionId))) + return ApplicationErrors.Authorization.PermissionNotFound; + // Update the user. UserEntity userToUpdate = getUserResult.Value; - // Build the role link for the requested role, leaving the current role untouched when no role was provided. - UserRoleEntity? userRole = default!; - if (command.RoleId is not null) - { - userRole = new() + // Build the role link for the requested role, keeping the current role untouched when no role was provided. + UserRoleEntity? userRole = command.RoleId is null + ? userToUpdate.UserRole + : new UserRoleEntity { UserId = userToUpdate.Id, RoleId = command.RoleId.Value, - Role = getRoleResult.Value!, + Role = newRole!, User = userToUpdate }; - } - + // Build the updated user by copying the unchanged fields and replacing the role and permissions with the requested ones. UserEntity updatedUser = new() { @@ -137,7 +141,7 @@ public class UpdateUserRoleAndPermissionsCommandHandler : ICommandHandler permission.Id == permissionId), + Permission = permissionsById[permissionId], User = userToUpdate })], LibraryScans = userToUpdate.LibraryScans @@ -154,7 +158,7 @@ public class UpdateUserRoleAndPermissionsCommandHandler : ICommandHandler up.Permission.PermissionName) .ToHashSet() diff --git a/src/Lumina.Presentation.Api/Core/Endpoints/UsersManagement/Authorization/UpdateUserRoleAndPermissions/UpdateUserRoleAndPermissionsEndpointSummary.cs b/src/Lumina.Presentation.Api/Core/Endpoints/UsersManagement/Authorization/UpdateUserRoleAndPermissions/UpdateUserRoleAndPermissionsEndpointSummary.cs index 06382ccb..4b4248bf 100644 --- a/src/Lumina.Presentation.Api/Core/Endpoints/UsersManagement/Authorization/UpdateUserRoleAndPermissions/UpdateUserRoleAndPermissionsEndpointSummary.cs +++ b/src/Lumina.Presentation.Api/Core/Endpoints/UsersManagement/Authorization/UpdateUserRoleAndPermissions/UpdateUserRoleAndPermissionsEndpointSummary.cs @@ -92,13 +92,13 @@ public class UpdateUserRoleAndPermissionsEndpointSummary : Summary