Implements authorization caching and check in the web client

This commit is contained in:
BinaryAlley committed 2024-12-06 18:46:48 +02:00
1 parent 7bca5b1ba3
commit 8eb1b9f13c
108 files changed
+5931 -73

No files matched your search

@@ -1,5 +1,6 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Common;
using Lumina.Domain.Common.Enums.Authorization;
using System;
using System.Collections.Generic;
using System.Diagnostics;
@@ -21,17 +22,22 @@ public class PermissionEntity : IStorageEntity, IAuditableEntity
/// <summary>
/// Gets the name of the permission.
/// </summary>
public required string PermissionName { get; init; }
public required AuthorizationPermission PermissionName { get; init; }
/// <summary>
/// Gets the collection of role permission associations that include this permission.
/// Gets the description of the permission.
/// </summary>
public required ICollection<RolePermissionEntity> RolePermissions { get; init; } = [];
public string? PermissionDescription { get; set; }
/// <summary>
/// Gets the collection of user permission associations that include this permission.
/// Gets or sets the collection of role permission associations that include this permission.
/// </summary>
public required ICollection<UserPermissionEntity> UserPermissions { get; init; } = [];
public ICollection<RolePermissionEntity> RolePermissions { get; set; } = [];
/// <summary>
/// Gets or sets the collection of user permission associations that include this permission.
/// </summary>
public ICollection<UserPermissionEntity> UserPermissions { get; set; } = [];
/// <summary>
/// Gets or sets the time and date when the entity was added.
@@ -1,5 +1,6 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Common;
using Lumina.Domain.Common.Enums.Authorization;
using System;
using System.Collections.Generic;
using System.Diagnostics;
@@ -21,17 +22,22 @@ public class RoleEntity : IStorageEntity
/// <summary>
/// Gets the name of the role.
/// </summary>
public required string RoleName { get; init; }
public required AuthorizationRole RoleName { get; init; }
/// <summary>
/// Gets the collection of user roles associations that include this role.
/// Gets or sets the description of the role.
/// </summary>
public required ICollection<UserRoleEntity> UserRoles { get; init; } = [];
public string? RoleDescription { get; set; }
/// <summary>
/// Gets the collection of role permission associations that include this role.
/// Gets or sets the collection of user roles associations that include this role.
/// </summary>
public required ICollection<RolePermissionEntity> RolePermissions { get; init; } = [];
public ICollection<UserRoleEntity> UserRoles { get; set; } = [];
/// <summary>
/// Gets or sets the collection of role permission associations that include this role.
/// </summary>
public ICollection<RolePermissionEntity> RolePermissions { get; set; } = [];
/// <summary>
/// Gets or sets the time and date when the entity was added.
@@ -0,0 +1,30 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Enums.Authorization;
using System;
using System.Collections.Generic;
using System.Diagnostics;
#endregion
namespace Lumina.Application.Common.DataAccess.Entities.Authorization;
/// <summary>
/// Data Transfer object containing all the authorization info of a user.
/// </summary>
[DebuggerDisplay("UserId: {UserId}")]
public class UserAuthorizationEntity
{
/// <summary>
/// Gets the Id of the user.
/// </summary>
public required Guid UserId { get; init; }
/// <summary>
/// Gets the collection of roles associated to the user.
/// </summary>
public required IReadOnlySet<AuthorizationRole> Roles { get; init; }
/// <summary>
/// Gets the collection of permissions associated to the user.
/// </summary>
public required IReadOnlySet<AuthorizationPermission> Permissions { get; init; }
}
@@ -0,0 +1,16 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Authorization;
using Lumina.Application.Common.DataAccess.Repositories.Common.Actions;
using Lumina.Application.Common.DataAccess.Repositories.Common.Base;
#endregion
namespace Lumina.Application.Common.DataAccess.Repositories.Authorization;
/// <summary>
/// Interface for the repository for authorization permissions.
/// </summary>
public interface IPermissionRepository : IRepository<PermissionEntity>,
IInsertRepositoryAction<PermissionEntity>,
IGetAllRepositoryAction<PermissionEntity>
{
}
@@ -0,0 +1,15 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Authorization;
using Lumina.Application.Common.DataAccess.Repositories.Common.Actions;
using Lumina.Application.Common.DataAccess.Repositories.Common.Base;
#endregion
namespace Lumina.Application.Common.DataAccess.Repositories.Authorization;
/// <summary>
/// Interface for the repository for authorization role permissions.
/// </summary>
public interface IRolePermissionRepository : IRepository<RolePermissionEntity>,
IInsertRepositoryAction<RolePermissionEntity>
{
}
@@ -0,0 +1,27 @@
#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 Lumina.Domain.Common.Enums.Authorization;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.Common.DataAccess.Repositories.Authorization;
/// <summary>
/// Interface for the repository for authorization roles.
/// </summary>
public interface IRoleRepository : IRepository<RoleEntity>,
IInsertRepositoryAction<RoleEntity>,
IGetAllRepositoryAction<RoleEntity>
{
/// <summary>
/// Gets a role identified by <paramref name="roleType"/> from the repository, if it exists.
/// </summary>
/// <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);
}
@@ -0,0 +1,15 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Authorization;
using Lumina.Application.Common.DataAccess.Repositories.Common.Actions;
using Lumina.Application.Common.DataAccess.Repositories.Common.Base;
#endregion
namespace Lumina.Application.Common.DataAccess.Repositories.Authorization;
/// <summary>
/// Interface for the repository for authorization user roles.
/// </summary>
public interface IUserRoleRepository : IRepository<UserRoleEntity>,
IInsertRepositoryAction<UserRoleEntity>
{
}
@@ -20,7 +20,7 @@ public interface IUserRepository : IRepository<UserEntity>,
IUpdateRepositoryAction<UserEntity>
{
/// <summary>
/// Gets a username identified by <paramref name="username"/> from the repository, if it exists.
/// Gets a user identified by <paramref name="username"/> from the repository, if it exists.
/// </summary>
/// <param name="username">The username of the user to get.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
@@ -0,0 +1,46 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using System;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.Common.DataAccess.Seed;
/// <summary>
/// Interface for the service for seeding initial data in the persistence medium.
/// </summary>
public interface IDataSeedService
{
/// <summary>
/// Sets up default authorization permissions in the system.
/// </summary>
/// <param name="adminId">The Id of the admin admin user who will own these permissions.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> representing either a successfull operation, or an error.</returns>
Task<ErrorOr<Created>> SetDefaultAuthorizationPermissionsAsync(Guid adminId, CancellationToken cancellationToken);
/// <summary>
/// Sets up default authorization roles in the system.
/// </summary>
/// <param name="userId">The Id of the admin user for whom roles will be set.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> representing either a successfull operation, or an error.</returns>
Task<ErrorOr<Created>> SetDefaultAuthorizationRolesAsync(Guid userId, CancellationToken cancellationToken);
/// <summary>
/// Assigns admin role permissions to the admin user.
/// </summary>
/// <param name="userId">The Id of the admin user to receive admin role permissions.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> representing either a successfull operation, or an error.</returns>
Task<ErrorOr<Created>> SetAdminRolePermissionsAsync(Guid userId, CancellationToken cancellationToken);
/// <summary>
/// Assigns admin role to the admin user.
/// </summary>
/// <param name="userId">The Id of the admin user to receive the admin role.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> representing either a successfull operation, or an error.</returns>
Task<ErrorOr<Created>> SetAdminRoleToAdministratorAccount(Guid userId, CancellationToken cancellationToken);
}
@@ -13,5 +13,9 @@ public static partial class Errors
{
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 PermissionAlreadyExists => Error.Unauthorized(description: nameof(PermissionAlreadyExists));
public static Error RoleAlreadyExists => Error.Unauthorized(description: nameof(RoleAlreadyExists));
}
}
@@ -1,5 +1,8 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Entities.Authorization;
using Lumina.Application.Common.Infrastructure.Authorization.Policies.Common.Base;
using Lumina.Domain.Common.Enums.Authorization;
using System;
using System.Threading;
using System.Threading.Tasks;
@@ -16,19 +19,19 @@ public interface IAuthorizationService
/// Determines whether the specified user has a specific permission.
/// </summary>
/// <param name="userId">The Id of the user for whom to check the permission.</param>
/// <param name="permission">The name of the permission to check.</param>
/// <param name="permission">The permission to check.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns><see langword="true"/> if the user has the specified permission, <see langword="false"/> otherwise.</returns>
Task<bool> HasPermissionAsync(Guid userId, string permission, CancellationToken cancellationToken);
Task<bool> HasPermissionAsync(Guid userId, AuthorizationPermission permission, CancellationToken cancellationToken);
/// <summary>
/// Determines whether the specified user belongs to a specific role.
/// </summary>
/// <param name="userId">The Id of the user for whom to check the role.</param>
/// <param name="role">The name of the role to check.</param>
/// <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, string role, CancellationToken cancellationToken);
Task<bool> IsInRoleAsync(Guid userId, AuthorizationRole role, CancellationToken cancellationToken);
/// <summary>
/// Evaluates whether the specified user meets the conditions defined in the specified authorization policy.
@@ -38,4 +41,12 @@ public interface IAuthorizationService
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns><see langword="true"/> if the user satisfies the policy, <see langword="false"/> otherwise.</returns>
Task<bool> EvaluatePolicyAsync<TAuthorizationPolicy>(Guid userId, CancellationToken cancellationToken) where TAuthorizationPolicy : IAuthorizationPolicy;
/// <summary>
/// Retrieves all authorization roles and permissions of a user identified by <paramref name="userId"/>.
/// </summary>
/// <param name="userId">The id of the user for whom to retrieve the authorization roles and permissions.</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="UserAuthorizationEntity"/>, or an error.</returns>
Task<ErrorOr<UserAuthorizationEntity>> GetUserAuthorizationAsync(Guid userId, CancellationToken cancellationToken);
}
@@ -0,0 +1,24 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.UsersManagement.Authorization.Queries.GetAuthorization;
using Lumina.Contracts.Requests.Authorization;
#endregion
namespace Lumina.Application.Common.Mapping.Authorization;
/// <summary>
/// Extension methods for converting <see cref="GetAuthorizationRequest"/>.
/// </summary>
public static class GetAuthorizationRequestMapping
{
/// <summary>
/// Converts <paramref name="request"/> to <see cref="GetAuthorizationQuery"/>.
/// </summary>
/// <param name="request">The request to be converted.</param>
/// <returns>The converted query.</returns>
public static GetAuthorizationQuery ToQuery(this GetAuthorizationRequest request)
{
return new GetAuthorizationQuery(
request.UserId
);
}
}
@@ -0,0 +1,26 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Authorization;
using Lumina.Contracts.Responses.Authorization;
#endregion
namespace Lumina.Application.Common.Mapping.Authorization;
/// <summary>
/// Extension methods for converting <see cref="UserAuthorizationEntity"/>.
/// </summary>
public static class UserAuthorizationEntityMapping
{
/// <summary>
/// Converts <paramref name="infrastructureEntity"/> to <see cref="GetAuthorizationResponse"/>.
/// </summary>
/// <param name="infrastructureEntity">The infrastructure entity to be converted.</param>
/// <returns>The converted response.</returns>
public static GetAuthorizationResponse ToResponse(this UserAuthorizationEntity infrastructureEntity)
{
return new GetAuthorizationResponse(
infrastructureEntity.UserId,
infrastructureEntity.Roles,
infrastructureEntity.Permissions
);
}
}