mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-01 18:57:24 +00:00
Reports a missing permission when updating a user's role and permissions
This commit is contained in:
1 parent
cd1386b9e7
commit
ab4d596500
3 files changed
+21
-16
No files matched your search
@@ -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));
|
||||
|
||||
+17
-13
@@ -78,16 +78,17 @@ public class UpdateUserRoleAndPermissionsCommandHandler : ICommandHandler<Update
|
||||
if (getUserResult.IsFailure || getUserResult.Value is null)
|
||||
return DomainErrors.Users.UserDoesNotExist;
|
||||
|
||||
Result<RoleEntity?> 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<RoleEntity?> 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<PaginatedResultDto<UserEntity>> getAllUsersResult = await _unitOfWork.UserRepository.GetAllAsync<BaseFilterDto>(cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
@@ -106,22 +107,25 @@ public class UpdateUserRoleAndPermissionsCommandHandler : ICommandHandler<Update
|
||||
if (getPermissionsResult.IsFailure)
|
||||
return getPermissionsResult.Errors;
|
||||
|
||||
// Every requested permission must exist, otherwise the requested permission set cannot be built and an unknown Id would throw instead of returning a result.
|
||||
Dictionary<Guid, PermissionEntity> 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<Update
|
||||
{
|
||||
UserId = userToUpdate.Id,
|
||||
PermissionId = permissionId,
|
||||
Permission = getPermissionsResult.Value.First(permission => permission.Id == permissionId),
|
||||
Permission = permissionsById[permissionId],
|
||||
User = userToUpdate
|
||||
})],
|
||||
LibraryScans = userToUpdate.LibraryScans
|
||||
@@ -154,7 +158,7 @@ public class UpdateUserRoleAndPermissionsCommandHandler : ICommandHandler<Update
|
||||
|
||||
return new AuthorizationResponse(
|
||||
userToUpdate.Id,
|
||||
getRoleResult.Value?.RoleName,
|
||||
newRole?.RoleName ?? userToUpdate.UserRole?.Role.RoleName,
|
||||
userToUpdate.UserPermissions
|
||||
.Select(up => up.Permission.PermissionName)
|
||||
.ToHashSet()
|
||||
|
||||
+2
-2
@@ -92,13 +92,13 @@ public class UpdateUserRoleAndPermissionsEndpointSummary : Summary<UpdateUserRol
|
||||
}
|
||||
);
|
||||
|
||||
Response(404, "The request failed because the provided role or user do not exist.", "application/problem+json",
|
||||
Response(404, "The request failed because the provided role, user or permission do not exist.", "application/problem+json",
|
||||
example: new
|
||||
{
|
||||
type = "https://tools.ietf.org/html/rfc9110#section-15.5.5",
|
||||
title = "General.NotFound",
|
||||
status = 404,
|
||||
detail = "RoleNotFound | UserDoesNotExist",
|
||||
detail = "RoleNotFound | UserDoesNotExist | PermissionNotFound",
|
||||
instance = "/api/v1/auth/users/{userId}/role-and-permissions",
|
||||
traceId = "00-57d15dadd702dbd4aeb5dc9b7cee68ee-9330237dbb2ce0e5-00"
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user