Adds authorization roles administrator dashboard

This commit is contained in:
BinaryAlley committed 2024-12-18 13:12:42 +02:00
1 parent 290af03c83
commit 1df31515fb
191 files changed
+6189 -806

No files matched your search

@@ -0,0 +1,13 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Contracts.Responses.Authorization;
using Mediator;
using System.Collections.Generic;
#endregion
namespace Lumina.Application.Core.Admin.Authorization.Permissions.Queries.GetPermissions;
/// <summary>
/// Query for retrieving the list of authorization permissions.
/// </summary>
public record GetPermissionsQuery : IRequest<ErrorOr<IEnumerable<PermissionResponse>>>;
@@ -0,0 +1,58 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Entities.Authorization;
using Lumina.Application.Common.DataAccess.Repositories.Authorization;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.Errors;
using Lumina.Application.Common.Infrastructure.Authentication;
using Lumina.Application.Common.Infrastructure.Authorization;
using Lumina.Application.Common.Mapping.Authorization;
using Lumina.Contracts.Responses.Authorization;
using Mediator;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.Core.Admin.Authorization.Permissions.Queries.GetPermissions;
/// <summary>
/// Handler for the query to retrieve the list of authorization permissions.
/// </summary>
public class GetPermissionsQueryHandler : IRequestHandler<GetPermissionsQuery, ErrorOr<IEnumerable<PermissionResponse>>>
{
private readonly ICurrentUserService _currentUserService;
private readonly IAuthorizationService _authorizationService;
private readonly IPermissionRepository _permissionRepository;
/// <summary>
/// Initializes a new instance of the <see cref="GetPermissionsQueryHandler"/> class.
/// </summary>
/// <param name="authorizationService">Injected service for authorization related functionality.</param>
/// <param name="currentUserService">Injected service to retrieve the current user information.</param>
/// <param name="unitOfWork">Injected unit of work for interacting with the data access layer repositories.</param>
public GetPermissionsQueryHandler(IAuthorizationService authorizationService, ICurrentUserService currentUserService, IUnitOfWork unitOfWork)
{
_authorizationService = authorizationService;
_currentUserService = currentUserService;
_permissionRepository = unitOfWork.GetRepository<IPermissionRepository>();
}
/// <summary>
/// Handles the query to retrieve the list of authorization permissions.
/// </summary>
/// <param name="request">The request to be handled.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>
/// An <see cref="ErrorOr{TValue}"/> containing either a collection of <see cref="PermissionResponse"/>, or an error message.
/// </returns>
public async ValueTask<ErrorOr<IEnumerable<PermissionResponse>>> Handle(GetPermissionsQuery request, CancellationToken cancellationToken)
{
// only admins can see the list of authorization permissions
bool isAdmin = await _authorizationService.IsInRoleAsync(_currentUserService.UserId!.Value, "Admin", cancellationToken).ConfigureAwait(false);
if (!isAdmin)
return Errors.Authorization.NotAuthorized;
ErrorOr<IEnumerable<PermissionEntity>> getPermissionsResult = await _permissionRepository.GetAllAsync(cancellationToken).ConfigureAwait(false);
return getPermissionsResult.Match(value => ErrorOrFactory.From(value.ToResponses()), errors => errors);
}
}
@@ -0,0 +1,21 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Contracts.Responses.Authorization;
using Mediator;
using System;
using System.Collections.Generic;
using System.Diagnostics;
#endregion
namespace Lumina.Application.Core.Admin.Authorization.Roles.Commands.AddRole;
/// <summary>
/// Command for adding an authorization role.
/// </summary>
/// <param name="RoleName">The name of the role.</param>
/// <param name="Permissions">The collection or permissions of the role.</param>
[DebuggerDisplay("RoleName: {RoleName}")]
public record AddRoleCommand(
string RoleName,
List<Guid> Permissions
) : IRequest<ErrorOr<RolePermissionsResponse>>;
@@ -0,0 +1,83 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Entities.Authorization;
using Lumina.Application.Common.DataAccess.Repositories.Authorization;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.Errors;
using Lumina.Application.Common.Infrastructure.Authentication;
using Lumina.Application.Common.Infrastructure.Authorization;
using Lumina.Application.Common.Mapping.Authorization;
using Lumina.Contracts.Responses.Authorization;
using Mediator;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.Core.Admin.Authorization.Roles.Commands.AddRole;
/// <summary>
/// Handler for the command to add an authorization role.
/// </summary>
public class AddRoleCommandHandler : IRequestHandler<AddRoleCommand, ErrorOr<RolePermissionsResponse>>
{
private readonly IUnitOfWork _unitOfWork;
private readonly IAuthorizationService _authorizationService;
private readonly ICurrentUserService _currentUserService;
/// <summary>
/// Initializes a new instance of the <see cref="AddRoleCommandHandler"/> class.
/// </summary>
/// <param name="authorizationService">Injected service for authorization related functionality.</param>
/// <param name="currentUserService">Injected service to retrieve the current user information.</param>
/// <param name="unitOfWork">Injected unit of work for interacting with the data access layer repositories.</param>
public AddRoleCommandHandler(IAuthorizationService authorizationService, ICurrentUserService currentUserService, IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
_authorizationService = authorizationService;
_currentUserService = currentUserService;
}
/// <summary>
/// Handles the command to add an authorization role.
/// </summary>
/// <param name="request">The request to be handled.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>
/// An <see cref="ErrorOr{TValue}"/> containing either a successfully created <see cref="RoleResponse"/>, or an error message.
/// </returns>
public async ValueTask<ErrorOr<RolePermissionsResponse>> Handle(AddRoleCommand request, CancellationToken cancellationToken)
{
// only admins can create authorization roles
bool isAdmin = await _authorizationService.IsInRoleAsync(_currentUserService.UserId!.Value, "Admin", cancellationToken).ConfigureAwait(false);
if (!isAdmin)
return Errors.Authorization.NotAuthorized;
IRoleRepository roleRepository = _unitOfWork.GetRepository<IRoleRepository>();
// create the new role, with its permissions
RoleEntity newRole = new()
{
RoleName = request.RoleName,
RolePermissions = request.Permissions.Select(permissionId => new RolePermissionEntity()
{
PermissionId = permissionId,
Permission = null!,
Role = null!,
RoleId = default
}).ToList()
};
// save the new role in the repository
ErrorOr<Created> insertRoleResult = await roleRepository.InsertAsync(newRole, cancellationToken).ConfigureAwait(false);
if (insertRoleResult.IsError)
return insertRoleResult.Errors;
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
// retrieve the newly saved authorization role from the persistence medium and return it
ErrorOr<RoleEntity?> getRoleResult = await roleRepository.GetByNameAsync(request.RoleName, cancellationToken).ConfigureAwait(false);
if (getRoleResult.IsError)
return getRoleResult.Errors;
if (getRoleResult.Value is null)
return Errors.Persistence.ErrorPersistingAuthorizationRole;
return getRoleResult.Value.ToRolePermissionsResponse();
}
}
@@ -0,0 +1,30 @@
#region ========================================================================= USING =====================================================================================
using FluentValidation;
using Lumina.Application.Common.Errors;
using System;
#endregion
namespace Lumina.Application.Core.Admin.Authorization.Roles.Commands.AddRole;
/// <summary>
/// Validates the needed validation rules for <see cref="AddRoleCommand"/>.
/// </summary>
public class AddRoleCommandValidator : AbstractValidator<AddRoleCommand>
{
/// <summary>
/// Initializes a new instance of the <see cref="AddRoleCommandValidator"/> class.
/// </summary>
public AddRoleCommandValidator()
{
RuleFor(x => x.RoleName)
.NotNull().WithMessage(Errors.Authorization.RoleNameCannotBeNull.Description)
.NotEmpty().WithMessage(Errors.Authorization.RoleNameCannotBeEmpty.Description);
RuleFor(x => x.Permissions)
.NotNull().WithMessage(Errors.Authorization.PermissionsListCannotBeNull.Description)
.NotEmpty().WithMessage(Errors.Authorization.PermissionsListCannotBeEmpty.Description);
RuleForEach(x => x.Permissions)
.NotEmpty().WithMessage(Errors.Authorization.PermissionIdCannotBeEmpty.Description)
.Must(id => id != Guid.Empty).WithMessage(Errors.Authorization.PermissionIdCannotBeEmpty.Description);
}
}
@@ -0,0 +1,17 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Mediator;
using System;
using System.Diagnostics;
#endregion
namespace Lumina.Application.Core.Admin.Authorization.Roles.Commands.DeleteRole;
/// <summary>
/// Command for deleting an authorization role.
/// </summary>
/// <param name="RoleId">The Id of the role.</param>
[DebuggerDisplay("RoleId: {RoleId}")]
public record DeleteRoleCommand(
Guid RoleId
) : IRequest<ErrorOr<Deleted>>;
@@ -0,0 +1,73 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Entities.Authorization;
using Lumina.Application.Common.DataAccess.Repositories.Authorization;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.Errors;
using Lumina.Application.Common.Infrastructure.Authentication;
using Lumina.Contracts.Responses.Authorization;
using Mediator;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Lumina.Application.Common.Mapping.Authorization;
using Lumina.Application.Common.Infrastructure.Authorization;
#endregion
namespace Lumina.Application.Core.Admin.Authorization.Roles.Commands.DeleteRole;
/// <summary>
/// Handler for the command to delete an authorization role.
/// </summary>
public class DeleteRoleCommandHandler : IRequestHandler<DeleteRoleCommand, ErrorOr<Deleted>>
{
private readonly IUnitOfWork _unitOfWork;
private readonly IAuthorizationService _authorizationService;
private readonly ICurrentUserService _currentUserService;
/// <summary>
/// Initializes a new instance of the <see cref="DeleteRoleCommandHandler"/> class.
/// </summary>
/// <param name="authorizationService">Injected service for authorization related functionality.</param>
/// <param name="currentUserService">Injected service to retrieve the current user information.</param>
/// <param name="unitOfWork">Injected unit of work for interacting with the data access layer repositories.</param>
public DeleteRoleCommandHandler(IAuthorizationService authorizationService, ICurrentUserService currentUserService, IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
_authorizationService = authorizationService;
_currentUserService = currentUserService;
}
/// <summary>
/// Handles the command to delete an authorization role.
/// </summary>
/// <param name="request">The request to be handled.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>
/// An <see cref="ErrorOr{TValue}"/> containing either a successfully deleted <see cref="RoleResponse"/>, or an error message.
/// </returns>
public async ValueTask<ErrorOr<Deleted>> Handle(DeleteRoleCommand request, CancellationToken cancellationToken)
{
// only admins can delete authorization roles
bool isAdmin = await _authorizationService.IsInRoleAsync(_currentUserService.UserId!.Value, "Admin", cancellationToken).ConfigureAwait(false);
if (!isAdmin)
return Errors.Authorization.NotAuthorized;
// check if a role with the requested Id exists
IRoleRepository roleRepository = _unitOfWork.GetRepository<IRoleRepository>();
ErrorOr<RoleEntity?> getExistingRoleResult = await roleRepository.GetByIdAsync(request.RoleId, cancellationToken).ConfigureAwait(false);
if (getExistingRoleResult.IsError)
return getExistingRoleResult.Errors;
else if (getExistingRoleResult.Value is null)
return Errors.Authorization.RoleNotFound;
// delete the role and its permissions
ErrorOr<Deleted> deleteRoleResult = await roleRepository.DeleteByIdAsync(request.RoleId, cancellationToken).ConfigureAwait(false);
if (deleteRoleResult.IsError)
return deleteRoleResult.Errors;
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
return deleteRoleResult.Value;
}
}
@@ -0,0 +1,23 @@
#region ========================================================================= USING =====================================================================================
using FluentValidation;
using Lumina.Application.Common.Errors;
using System;
#endregion
namespace Lumina.Application.Core.Admin.Authorization.Roles.Commands.DeleteRole;
/// <summary>
/// Validates the needed validation rules for <see cref="DeleteRoleCommand"/>.
/// </summary>
public class DeleteRoleCommandValidator : AbstractValidator<DeleteRoleCommand>
{
/// <summary>
/// Initializes a new instance of the <see cref="DeleteRoleCommandValidator"/> class.
/// </summary>
public DeleteRoleCommandValidator()
{
RuleFor(x => x.RoleId)
.NotEmpty().WithMessage(Errors.Authorization.RoleIdCannotBeEmpty.Description)
.Must(id => id != Guid.Empty).WithMessage(Errors.Authorization.RoleIdCannotBeEmpty.Description);
}
}
@@ -0,0 +1,23 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Contracts.Responses.Authorization;
using Mediator;
using System;
using System.Collections.Generic;
using System.Diagnostics;
#endregion
namespace Lumina.Application.Core.Admin.Authorization.Roles.Commands.UpdateRole;
/// <summary>
/// Command for updating an authorization role.
/// </summary>
/// <param name="RoleId">The Id of the role.</param>
/// <param name="RoleName">The name of the role.</param>
/// <param name="Permissions">The collection or permissions of the role.</param>
[DebuggerDisplay("RoleName: {RoleName}")]
public record UpdateRoleCommand(
Guid RoleId,
string RoleName,
List<Guid> Permissions
) : IRequest<ErrorOr<RolePermissionsResponse>>;
@@ -0,0 +1,84 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Entities.Authorization;
using Lumina.Application.Common.DataAccess.Repositories.Authorization;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.Errors;
using Lumina.Application.Common.Infrastructure.Authentication;
using Lumina.Application.Common.Infrastructure.Authorization;
using Lumina.Application.Common.Mapping.Authorization;
using Lumina.Contracts.Responses.Authorization;
using Mediator;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.Core.Admin.Authorization.Roles.Commands.UpdateRole;
/// <summary>
/// Handler for the command to update an authorization role.
/// </summary>
public class UpdateRoleCommandHandler : IRequestHandler<UpdateRoleCommand, ErrorOr<RolePermissionsResponse>>
{
private readonly IUnitOfWork _unitOfWork;
private readonly IAuthorizationService _authorizationService;
private readonly ICurrentUserService _currentUserService;
/// <summary>
/// Initializes a new instance of the <see cref="UpdateRoleCommandHandler"/> class.
/// </summary>
/// <param name="authorizationService">Injected service for authorization related functionality.</param>
/// <param name="currentUserService">Injected service to retrieve the current user information.</param>
/// <param name="unitOfWork">Injected unit of work for interacting with the data access layer repositories.</param>
public UpdateRoleCommandHandler(IAuthorizationService authorizationService, ICurrentUserService currentUserService, IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
_authorizationService = authorizationService;
_currentUserService = currentUserService;
}
/// <summary>
/// Handles the command to update an authorization role.
/// </summary>
/// <param name="request">The request to be handled.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>
/// An <see cref="ErrorOr{TValue}"/> containing either a successfully updated <see cref="RoleResponse"/>, or an error message.
/// </returns>
public async ValueTask<ErrorOr<RolePermissionsResponse>> Handle(UpdateRoleCommand request, CancellationToken cancellationToken)
{
// only admins can update authorization roles
bool isAdmin = await _authorizationService.IsInRoleAsync(_currentUserService.UserId!.Value, "Admin", cancellationToken).ConfigureAwait(false);
if (!isAdmin)
return Errors.Authorization.NotAuthorized;
IRoleRepository roleRepository = _unitOfWork.GetRepository<IRoleRepository>();
// update the role and its permissions
RoleEntity newRole = new()
{
Id = request.RoleId,
RoleName = request.RoleName,
RolePermissions = request.Permissions.Select(permissionId => new RolePermissionEntity()
{
PermissionId = permissionId,
Permission = null!,
Role = null!,
RoleId = default
}).ToList()
};
// save the updated role in the repository
ErrorOr<Updated> updateRoleResult = await roleRepository.UpdateAsync(newRole, cancellationToken).ConfigureAwait(false);
if (updateRoleResult.IsError)
return updateRoleResult.Errors;
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
// retrieve the updated authorization role from the persistence medium and return it
ErrorOr<RoleEntity?> getRoleResult = await roleRepository.GetByIdAsync(request.RoleId, cancellationToken).ConfigureAwait(false);
if (getRoleResult.IsError)
return getRoleResult.Errors;
if (getRoleResult.Value is null)
return Errors.Persistence.ErrorPersistingAuthorizationRole;
return getRoleResult.Value.ToRolePermissionsResponse();
}
}
@@ -0,0 +1,34 @@
#region ========================================================================= USING =====================================================================================
using FluentValidation;
using Lumina.Application.Common.Errors;
using System;
#endregion
namespace Lumina.Application.Core.Admin.Authorization.Roles.Commands.UpdateRole;
/// <summary>
/// Validates the needed validation rules for <see cref="UpdateRoleCommand"/>.
/// </summary>
public class UpdateRoleCommandValidator : AbstractValidator<UpdateRoleCommand>
{
/// <summary>
/// Initializes a new instance of the <see cref="UpdateRoleCommandValidator"/> class.
/// </summary>
public UpdateRoleCommandValidator()
{
RuleFor(x => x.RoleId)
.NotEmpty().WithMessage(Errors.Authorization.RoleIdCannotBeEmpty.Description)
.Must(id => id != Guid.Empty).WithMessage(Errors.Authorization.RoleIdCannotBeEmpty.Description);
RuleFor(x => x.RoleName)
.NotNull().WithMessage(Errors.Authorization.RoleNameCannotBeNull.Description)
.NotEmpty().WithMessage(Errors.Authorization.RoleNameCannotBeEmpty.Description);
RuleFor(x => x.Permissions)
.NotNull().WithMessage(Errors.Authorization.PermissionsListCannotBeNull.Description)
.NotEmpty().WithMessage(Errors.Authorization.PermissionsListCannotBeEmpty.Description);
RuleForEach(x => x.Permissions)
.NotEmpty().WithMessage(Errors.Authorization.PermissionIdCannotBeEmpty.Description)
.Must(id => id != Guid.Empty).WithMessage(Errors.Authorization.PermissionIdCannotBeEmpty.Description);
}
}
@@ -0,0 +1,18 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Contracts.Responses.Authorization;
using Mediator;
using System;
using System.Diagnostics;
#endregion
namespace Lumina.Application.Core.Admin.Authorization.Roles.Queries.GetRolePermissions;
/// <summary>
/// Query for getting the authorization permissions of a role identified by <paramref name="RoleId"/>.
/// </summary>
/// <param name="RoleId">The Id of the authorization role for which to get the list of permissions.</param>
[DebuggerDisplay("RoleId: {RoleId}")]
public record GetRolePermissionsQuery(
Guid RoleId
) : IRequest<ErrorOr<RolePermissionsResponse>>;
@@ -0,0 +1,61 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Entities.Authorization;
using Lumina.Application.Common.DataAccess.Repositories.Authorization;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.Errors;
using Lumina.Application.Common.Infrastructure.Authentication;
using Lumina.Application.Common.Infrastructure.Authorization;
using Lumina.Application.Common.Mapping.Authorization;
using Lumina.Contracts.Responses.Authorization;
using Mediator;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.Core.Admin.Authorization.Roles.Queries.GetRolePermissions;
/// <summary>
/// Handler for the query to retrieve the list of authorization permissions for a role.
/// </summary>
public class GetRolePermissionsQueryHandler : IRequestHandler<GetRolePermissionsQuery, ErrorOr<RolePermissionsResponse>>
{
private readonly ICurrentUserService _currentUserService;
private readonly IAuthorizationService _authorizationService;
private readonly IRoleRepository _roleRepository;
/// <summary>
/// Initializes a new instance of the <see cref="GetRolePermissionsQueryHandler"/> class.
/// </summary>
/// <param name="authorizationService">Injected service for authorization related functionality.</param>
/// <param name="currentUserService">Injected service to retrieve the current user information.</param>
/// <param name="unitOfWork">Injected unit of work for interacting with the data access layer repositories.</param>
public GetRolePermissionsQueryHandler(IAuthorizationService authorizationService, ICurrentUserService currentUserService, IUnitOfWork unitOfWork)
{
_authorizationService = authorizationService;
_currentUserService = currentUserService;
_roleRepository = unitOfWork.GetRepository<IRoleRepository>();
}
/// <summary>
/// Handles the query to retrieve the list of authorization permissions for a role.
/// </summary>
/// <param name="request">The request to be handled.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>
/// An <see cref="ErrorOr{TValue}"/> containing either a collection of <see cref="RoleResponse"/>, or an error message.
/// </returns>
public async ValueTask<ErrorOr<RolePermissionsResponse>> Handle(GetRolePermissionsQuery request, CancellationToken cancellationToken)
{
// only admins can see the list of authorization roles
bool isAdmin = await _authorizationService.IsInRoleAsync(_currentUserService.UserId!.Value, "Admin", cancellationToken).ConfigureAwait(false);
if (!isAdmin)
return Errors.Authorization.NotAuthorized;
ErrorOr<RoleEntity?> getRoleResult = await _roleRepository.GetByIdAsync(request.RoleId, cancellationToken).ConfigureAwait(false);
if (getRoleResult.IsError)
return getRoleResult.Errors;
else if (getRoleResult.Value is null)
return Errors.Authorization.RoleNotFound;
return getRoleResult.Value.ToRolePermissionsResponse();
}
}
@@ -0,0 +1,23 @@
#region ========================================================================= USING =====================================================================================
using FluentValidation;
using Lumina.Application.Common.Errors;
using System;
#endregion
namespace Lumina.Application.Core.Admin.Authorization.Roles.Queries.GetRolePermissions;
/// <summary>
/// Validates the needed validation rules for <see cref="GetRolePermissionsQuery"/>.
/// </summary>
public class GetRolePermissionsQueryValidator : AbstractValidator<GetRolePermissionsQuery>
{
/// <summary>
/// Initializes a new instance of the <see cref="GetRolePermissionsQueryValidator"/> class.
/// </summary>
public GetRolePermissionsQueryValidator()
{
RuleFor(x => x.RoleId)
.NotEmpty().WithMessage(Errors.Authorization.RoleIdCannotBeEmpty.Description)
.Must(id => id != Guid.Empty).WithMessage(Errors.Authorization.RoleIdCannotBeEmpty.Description);
}
}
@@ -0,0 +1,13 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Contracts.Responses.Authorization;
using Mediator;
using System.Collections.Generic;
#endregion
namespace Lumina.Application.Core.Admin.Authorization.Roles.Queries.GetRoles;
/// <summary>
/// Query for retrieving the list of authorization roles.
/// </summary>
public record GetRolesQuery() : IRequest<ErrorOr<IEnumerable<RoleResponse>>>;
@@ -0,0 +1,58 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Entities.Authorization;
using Lumina.Application.Common.DataAccess.Repositories.Authorization;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.Errors;
using Lumina.Application.Common.Infrastructure.Authentication;
using Lumina.Application.Common.Infrastructure.Authorization;
using Lumina.Application.Common.Mapping.Authorization;
using Lumina.Contracts.Responses.Authorization;
using Mediator;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.Core.Admin.Authorization.Roles.Queries.GetRoles;
/// <summary>
/// Handler for the query to retrieve the list of authorization roles.
/// </summary>
public class GetRolesQueryHandler : IRequestHandler<GetRolesQuery, ErrorOr<IEnumerable<RoleResponse>>>
{
private readonly ICurrentUserService _currentUserService;
private readonly IAuthorizationService _authorizationService;
private readonly IRoleRepository _roleRepository;
/// <summary>
/// Initializes a new instance of the <see cref="GetRolesQueryHandler"/> class.
/// </summary>
/// <param name="authorizationService">Injected service for authorization related functionality.</param>
/// <param name="currentUserService">Injected service to retrieve the current user information.</param>
/// <param name="unitOfWork">Injected unit of work for interacting with the data access layer repositories.</param>
public GetRolesQueryHandler(IAuthorizationService authorizationService, ICurrentUserService currentUserService, IUnitOfWork unitOfWork)
{
_authorizationService = authorizationService;
_currentUserService = currentUserService;
_roleRepository = unitOfWork.GetRepository<IRoleRepository>();
}
/// <summary>
/// Handles the query to retrieve the list of authorization roles.
/// </summary>
/// <param name="request">The request to be handled.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>
/// An <see cref="ErrorOr{TValue}"/> containing either a collection of <see cref="RoleResponse"/>, or an error message.
/// </returns>
public async ValueTask<ErrorOr<IEnumerable<RoleResponse>>> Handle(GetRolesQuery request, CancellationToken cancellationToken)
{
// only admins can see the list of authorization roles
bool isAdmin = await _authorizationService.IsInRoleAsync(_currentUserService.UserId!.Value, "Admin", cancellationToken).ConfigureAwait(false);
if (!isAdmin)
return Errors.Authorization.NotAuthorized;
ErrorOr<IEnumerable<RoleEntity>> getRolesResult = await _roleRepository.GetAllAsync(cancellationToken).ConfigureAwait(false);
return getRolesResult.Match(value => ErrorOrFactory.From(value.ToResponses()), errors => errors);
}
}