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

@@ -24,11 +24,6 @@ public class PermissionEntity : IStorageEntity, IAuditableEntity
/// </summary>
public required AuthorizationPermission PermissionName { get; init; }
/// <summary>
/// Gets the description of the permission.
/// </summary>
public string? PermissionDescription { get; set; }
/// <summary>
/// Gets or sets the collection of role permission associations that include this permission.
/// </summary>
@@ -42,12 +37,12 @@ public class PermissionEntity : IStorageEntity, IAuditableEntity
/// <summary>
/// Gets or sets the time and date when the entity was added.
/// </summary>
public required DateTime CreatedOnUtc { get; set; }
public DateTime CreatedOnUtc { get; set; }
/// <summary>
/// Gets or sets the Id of the user that created the entity.
/// </summary>
public required Guid CreatedBy { get; set; }
public Guid CreatedBy { get; set; }
/// <summary>
/// Gets or sets the optional time and date when the entity was updated.
@@ -12,7 +12,7 @@ namespace Lumina.Application.Common.DataAccess.Entities.Authorization;
/// Repository entity for a permission in the authorization system.
/// </summary>
[DebuggerDisplay("RoleName: {RoleName}")]
public class RoleEntity : IStorageEntity
public class RoleEntity : IStorageEntity, IAuditableEntity
{
/// <summary>
/// Gets the Id of the role.
@@ -22,12 +22,7 @@ public class RoleEntity : IStorageEntity
/// <summary>
/// Gets the name of the role.
/// </summary>
public required AuthorizationRole RoleName { get; init; }
/// <summary>
/// Gets or sets the description of the role.
/// </summary>
public string? RoleDescription { get; set; }
public required string RoleName { get; init; }
/// <summary>
/// Gets or sets the collection of user roles associations that include this role.
@@ -42,12 +37,12 @@ public class RoleEntity : IStorageEntity
/// <summary>
/// Gets or sets the time and date when the entity was added.
/// </summary>
public required DateTime CreatedOnUtc { get; set; }
public DateTime CreatedOnUtc { get; set; }
/// <summary>
/// Gets or sets the Id of the user that created the entity.
/// </summary>
public required Guid CreatedBy { get; set; }
public Guid CreatedBy { get; set; }
/// <summary>
/// Gets or sets the optional time and date when the entity was updated.
@@ -40,12 +40,12 @@ public class RolePermissionEntity : IStorageEntity, IAuditableEntity
/// <summary>
/// Gets or sets the time and date when the entity was added.
/// </summary>
public required DateTime CreatedOnUtc { get; set; }
public DateTime CreatedOnUtc { get; set; }
/// <summary>
/// Gets or sets the Id of the user that created the entity.
/// </summary>
public required Guid CreatedBy { get; set; }
public Guid CreatedBy { get; set; }
/// <summary>
/// Gets or sets the optional time and date when the entity was updated.
@@ -8,7 +8,7 @@ using System.Diagnostics;
namespace Lumina.Application.Common.DataAccess.Entities.Authorization;
/// <summary>
/// Data Transfer object containing all the authorization info of a user.
/// Data Transfer Object containing all the authorization info of a user.
/// </summary>
[DebuggerDisplay("UserId: {UserId}")]
public class UserAuthorizationEntity
@@ -21,7 +21,7 @@ public class UserAuthorizationEntity
/// <summary>
/// Gets the collection of roles associated to the user.
/// </summary>
public required IReadOnlySet<AuthorizationRole> Roles { get; init; }
public required IReadOnlySet<string> Roles { get; init; }
/// <summary>
/// Gets the collection of permissions associated to the user.
@@ -41,12 +41,12 @@ public class UserPermissionEntity : IStorageEntity, IAuditableEntity
/// <summary>
/// Gets or sets the time and date when the entity was added.
/// </summary>
public required DateTime CreatedOnUtc { get; set; }
public DateTime CreatedOnUtc { get; set; }
/// <summary>
/// Gets or sets the Id of the user that created the entity.
/// </summary>
public required Guid CreatedBy { get; set; }
public Guid CreatedBy { get; set; }
/// <summary>
/// Gets or sets the optional time and date when the entity was updated.
@@ -41,12 +41,12 @@ public class UserRoleEntity : IStorageEntity, IAuditableEntity
/// <summary>
/// Gets or sets the time and date when the entity was added.
/// </summary>
public required DateTime CreatedOnUtc { get; set; }
public DateTime CreatedOnUtc { get; set; }
/// <summary>
/// Gets or sets the Id of the user that created the entity.
/// </summary>
public required Guid CreatedBy { get; set; }
public Guid CreatedBy { get; set; }
/// <summary>
/// Gets or sets the optional time and date when the entity was updated.
@@ -63,12 +63,12 @@ public class UserEntity : IStorageEntity, IAuditableEntity
/// <summary>
/// Gets the time and date when the entity was added.
/// </summary>
public required DateTime CreatedOnUtc { get; set; }
public DateTime CreatedOnUtc { get; set; }
/// <summary>
/// Gets or sets the Id of the user that created the entity.
/// </summary>
public required Guid CreatedBy { get; set; }
public Guid CreatedBy { get; set; }
/// <summary>
/// Gets the optional time and date when the entity was updated.
@@ -1,7 +1,12 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Entities.Authorization;
using Lumina.Application.Common.DataAccess.Repositories.Common.Actions;
using Lumina.Application.Common.DataAccess.Repositories.Common.Base;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.Common.DataAccess.Repositories.Authorization;
@@ -13,4 +18,11 @@ public interface IPermissionRepository : IRepository<PermissionEntity>,
IInsertRepositoryAction<PermissionEntity>,
IGetAllRepositoryAction<PermissionEntity>
{
/// <summary>
/// Gets all permissions that have their Id match an Id in <paramref name="ids"/> from the storage medium.
/// </summary>
/// <param name="ids">The list of Ids of the permissions to get.</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="PermissionEntity"/>, or an error.</returns>
Task<ErrorOr<IEnumerable<PermissionEntity>>> GetByIdsAsync(IEnumerable<Guid> ids, CancellationToken cancellationToken);
}
@@ -3,7 +3,7 @@ using ErrorOr;
using Lumina.Application.Common.DataAccess.Entities.Authorization;
using Lumina.Application.Common.DataAccess.Repositories.Common.Actions;
using Lumina.Application.Common.DataAccess.Repositories.Common.Base;
using Lumina.Domain.Common.Enums.Authorization;
using System;
using System.Threading;
using System.Threading.Tasks;
#endregion
@@ -14,8 +14,11 @@ namespace Lumina.Application.Common.DataAccess.Repositories.Authorization;
/// Interface for the repository for authorization roles.
/// </summary>
public interface IRoleRepository : IRepository<RoleEntity>,
IGetByIdRepositoryAction<RoleEntity, Guid>,
IGetAllRepositoryAction<RoleEntity>,
IInsertRepositoryAction<RoleEntity>,
IGetAllRepositoryAction<RoleEntity>
IUpdateRepositoryAction<RoleEntity>,
IDeleteByIdRepositoryAction<Guid>
{
/// <summary>
/// Gets a role identified by <paramref name="roleType"/> from the repository, if it exists.
@@ -23,5 +26,5 @@ public interface IRoleRepository : IRepository<RoleEntity>,
/// <param name="roleType">The type of role to get.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> containing either a <see cref="RoleEntity"/> if found, or an error.</returns>
Task<ErrorOr<RoleEntity?>> GetByNameAsync(AuthorizationRole roleType, CancellationToken cancellationToken);
Task<ErrorOr<RoleEntity?>> GetByNameAsync(string roleType, CancellationToken cancellationToken);
}
@@ -12,10 +12,17 @@ public static partial class Errors
public static class Authorization
{
public static Error NotAuthorized => Error.Unauthorized(description: nameof(NotAuthorized));
public static Error AdminAccountAlreadyCreated => Error.Unauthorized(description: nameof(AdminAccountAlreadyCreated));
public static Error AdminAccountNotFound => Error.Unauthorized(description: nameof(AdminAccountNotFound));
public static Error AdminRoleNotFound => Error.Unauthorized(description: nameof(AdminRoleNotFound));
public static Error AdminAccountAlreadyCreated => Error.Conflict(description: nameof(AdminAccountAlreadyCreated));
public static Error AdminAccountNotFound => Error.NotFound(description: nameof(AdminAccountNotFound));
public static Error AdminRoleNotFound => Error.NotFound(description: nameof(AdminRoleNotFound));
public static Error RoleNotFound => Error.NotFound(description: nameof(RoleNotFound));
public static Error PermissionAlreadyExists => Error.Unauthorized(description: nameof(PermissionAlreadyExists));
public static Error RoleAlreadyExists => Error.Unauthorized(description: nameof(RoleAlreadyExists));
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));
public static Error PermissionsListCannotBeEmpty => Error.Validation(description: nameof(PermissionsListCannotBeEmpty));
public static Error PermissionIdCannotBeEmpty => Error.Validation(description: nameof(PermissionIdCannotBeEmpty));
public static Error RoleNameCannotBeEmpty => Error.Validation(description: nameof(RoleNameCannotBeEmpty));
public static Error RoleNameCannotBeNull => Error.Validation(description: nameof(RoleNameCannotBeNull));
}
}
@@ -12,5 +12,6 @@ public static partial class Errors
public static class Persistence
{
public static Error ErrorPersistingMediaLibrary => Error.Failure(description: nameof(ErrorPersistingMediaLibrary));
public static Error ErrorPersistingAuthorizationRole => Error.Failure(description: nameof(ErrorPersistingAuthorizationRole));
}
}
@@ -31,7 +31,7 @@ public interface IAuthorizationService
/// <param name="role">The role to check.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns><see langword="true"/> if the user is in the specified role, <see langword="false"/> otherwise.</returns>
Task<bool> IsInRoleAsync(Guid userId, AuthorizationRole role, CancellationToken cancellationToken);
Task<bool> IsInRoleAsync(Guid userId, string role, CancellationToken cancellationToken);
/// <summary>
/// Evaluates whether the specified user meets the conditions defined in the specified authorization policy.
@@ -0,0 +1,25 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.Admin.Authorization.Roles.Commands.AddRole;
using Lumina.Contracts.Requests.Authorization;
#endregion
namespace Lumina.Application.Common.Mapping.Authorization;
/// <summary>
/// Extension methods for converting <see cref="AddRoleRequest"/>.
/// </summary>
public static class AddRoleRequestMapping
{
/// <summary>
/// Converts <paramref name="request"/> to <see cref="AddRoleCommand"/>.
/// </summary>
/// <param name="request">The request to be converted.</param>
/// <returns>The converted command.</returns>
public static AddRoleCommand ToCommand(this AddRoleRequest request)
{
return new AddRoleCommand(
request.RoleName!,
request.Permissions!
);
}
}
@@ -0,0 +1,24 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.Admin.Authorization.Roles.Commands.DeleteRole;
using Lumina.Contracts.Requests.Authorization;
#endregion
namespace Lumina.Application.Common.Mapping.Authorization;
/// <summary>
/// Extension methods for converting <see cref="UpdateRoleRequest"/>.
/// </summary>
public static class DeleteRoleRequestMapping
{
/// <summary>
/// Converts <paramref name="request"/> to <see cref="DeleteRoleCommand"/>.
/// </summary>
/// <param name="request">The request to be converted.</param>
/// <returns>The converted command.</returns>
public static DeleteRoleCommand ToCommand(this DeleteRoleRequest request)
{
return new DeleteRoleCommand(
request.RoleId!.Value
);
}
}
@@ -0,0 +1,24 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.Admin.Authorization.Roles.Queries.GetRolePermissions;
using Lumina.Contracts.Requests.Authorization;
#endregion
namespace Lumina.Application.Common.Mapping.Authorization;
/// <summary>
/// Extension methods for converting <see cref="GetRolePermissionsRequest"/>.
/// </summary>
public static class GetRolePermissionsRequestMapping
{
/// <summary>
/// Converts <paramref name="request"/> to <see cref="GetRolePermissionsQuery"/>.
/// </summary>
/// <param name="request">The request to be converted.</param>
/// <returns>The converted query.</returns>
public static GetRolePermissionsQuery ToQuery(this GetRolePermissionsRequest request)
{
return new GetRolePermissionsQuery(
request.RoleId
);
}
}
@@ -0,0 +1,37 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Authorization;
using Lumina.Contracts.Responses.Authorization;
using System.Collections.Generic;
using System.Linq;
#endregion
namespace Lumina.Application.Common.Mapping.Authorization;
/// <summary>
/// Extension methods for converting <see cref="PermissionEntity"/>.
/// </summary>
public static class PermissionEntityMapping
{
/// <summary>
/// Converts <paramref name="repositoryEntity"/> to <see cref="PermissionResponse"/>.
/// </summary>
/// <param name="repositoryEntity">The repository entity to be converted.</param>
/// <returns>The converted response.</returns>
public static PermissionResponse ToResponse(this PermissionEntity repositoryEntity)
{
return new PermissionResponse(
repositoryEntity.Id,
repositoryEntity.PermissionName
);
}
/// <summary>
/// Converts <paramref name="repositoryEntities"/> to a collection of <see cref="PermissionResponse"/>.
/// </summary>
/// <param name="repositoryEntities">The repository entities to be converted.</param>
/// <returns>The converted responses.</returns>
public static IEnumerable<PermissionResponse> ToResponses(this IEnumerable<PermissionEntity> repositoryEntities)
{
return repositoryEntities.Select(permission => permission.ToResponse());
}
}
@@ -0,0 +1,51 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Authorization;
using Lumina.Contracts.DTO.Authentication;
using Lumina.Contracts.Responses.Authorization;
using System.Collections.Generic;
using System.Linq;
#endregion
namespace Lumina.Application.Common.Mapping.Authorization;
/// <summary>
/// Extension methods for converting <see cref="RoleEntity"/>.
/// </summary>
public static class RoleEntityMapping
{
/// <summary>
/// Converts <paramref name="repositoryEntity"/> to <see cref="RoleResponse"/>.
/// </summary>
/// <param name="repositoryEntity">The repository entity to be converted.</param>
/// <returns>The converted response.</returns>
public static RoleResponse ToResponse(this RoleEntity repositoryEntity)
{
return new RoleResponse(
repositoryEntity.Id,
repositoryEntity.RoleName
);
}
/// <summary>
/// Converts <paramref name="repositoryEntity"/> to <see cref="RoleResponse"/>.
/// </summary>
/// <param name="repositoryEntity">The repository entity to be converted.</param>
/// <returns>The converted response.</returns>
public static RolePermissionsResponse ToRolePermissionsResponse(this RoleEntity repositoryEntity)
{
return new RolePermissionsResponse(
new RoleDto(repositoryEntity.Id, repositoryEntity.RoleName),
repositoryEntity.RolePermissions.Select(rolePermission => rolePermission.ToResponse()).ToArray()
);
}
/// <summary>
/// Converts <paramref name="repositoryEntities"/> to a collection of <see cref="RoleResponse"/>.
/// </summary>
/// <param name="repositoryEntities">The repository entities to be converted.</param>
/// <returns>The converted responses.</returns>
public static IEnumerable<RoleResponse> ToResponses(this IEnumerable<RoleEntity> repositoryEntities)
{
return repositoryEntities.Select(role => role.ToResponse());
}
}
@@ -0,0 +1,26 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Authorization;
using Lumina.Contracts.DTO.Authentication;
using Lumina.Contracts.Responses.Authorization;
#endregion
namespace Lumina.Application.Common.Mapping.Authorization;
/// <summary>
/// Extension methods for converting <see cref="RolePermissionEntity"/>.
/// </summary>
public static class RolePermissionEntityMapping
{
/// <summary>
/// Converts <paramref name="repositoryEntity"/> to <see cref="RolePermissionsResponse"/>.
/// </summary>
/// <param name="repositoryEntity">The repository entity to be converted.</param>
/// <returns>The converted response.</returns>
public static PermissionDto ToResponse(this RolePermissionEntity repositoryEntity)
{
return new PermissionDto(
repositoryEntity.PermissionId,
repositoryEntity.Permission.PermissionName
);
}
}
@@ -0,0 +1,26 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.Admin.Authorization.Roles.Commands.UpdateRole;
using Lumina.Contracts.Requests.Authorization;
#endregion
namespace Lumina.Application.Common.Mapping.Authorization;
/// <summary>
/// Extension methods for converting <see cref="UpdateRoleRequest"/>.
/// </summary>
public static class UpdateRoleRequestMapping
{
/// <summary>
/// Converts <paramref name="request"/> to <see cref="UpdateRoleCommand"/>.
/// </summary>
/// <param name="request">The request to be converted.</param>
/// <returns>The converted command.</returns>
public static UpdateRoleCommand ToCommand(this UpdateRoleRequest request)
{
return new UpdateRoleCommand(
request.RoleId!.Value,
request.RoleName!,
request.Permissions!
);
}
}
@@ -11,16 +11,16 @@ namespace Lumina.Application.Common.Mapping.Authorization;
public static class UserAuthorizationEntityMapping
{
/// <summary>
/// Converts <paramref name="infrastructureEntity"/> to <see cref="GetAuthorizationResponse"/>.
/// Converts <paramref name="repositoryEntity"/> to <see cref="AuthorizationResponse"/>.
/// </summary>
/// <param name="infrastructureEntity">The infrastructure entity to be converted.</param>
/// <param name="repositoryEntity">The infrastructure entity to be converted.</param>
/// <returns>The converted response.</returns>
public static GetAuthorizationResponse ToResponse(this UserAuthorizationEntity infrastructureEntity)
public static AuthorizationResponse ToResponse(this UserAuthorizationEntity repositoryEntity)
{
return new GetAuthorizationResponse(
infrastructureEntity.UserId,
infrastructureEntity.Roles,
infrastructureEntity.Permissions
return new AuthorizationResponse(
repositoryEntity.UserId,
repositoryEntity.Roles,
repositoryEntity.Permissions
);
}
}