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

@@ -5,7 +5,9 @@ using Lumina.Application.Common.DataAccess.Repositories.Authorization;
using Lumina.Application.Common.Errors;
using Lumina.DataAccess.Core.UoW;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
#endregion
@@ -53,4 +55,15 @@ internal sealed class PermissionRepository : IPermissionRepository
{
return await _luminaDbContext.Permissions.ToListAsync(cancellationToken).ConfigureAwait(false);
}
/// <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>
public async Task<ErrorOr<IEnumerable<PermissionEntity>>> GetByIdsAsync(IEnumerable<Guid> ids, CancellationToken cancellationToken)
{
return await _luminaDbContext.Permissions.Where(permission => ids.Contains(permission.Id)).ToListAsync(cancellationToken).ConfigureAwait(false);
}
}
@@ -4,8 +4,8 @@ using Lumina.Application.Common.DataAccess.Entities.Authorization;
using Lumina.Application.Common.DataAccess.Repositories.Authorization;
using Lumina.Application.Common.Errors;
using Lumina.DataAccess.Core.UoW;
using Lumina.Domain.Common.Enums.Authorization;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
@@ -37,7 +37,7 @@ internal sealed class RoleRepository : IRoleRepository
/// <returns>An <see cref="ErrorOr{TValue}"/> representing either a successfull operation, or an error.</returns>
public async Task<ErrorOr<Created>> InsertAsync(RoleEntity role, CancellationToken cancellationToken)
{
bool roleExists = await _luminaDbContext.Roles.AnyAsync(repositoryRole => repositoryRole.Id == role.Id, cancellationToken).ConfigureAwait(false);
bool roleExists = await _luminaDbContext.Roles.AnyAsync(repositoryRole => repositoryRole.Id == role.Id || repositoryRole.RoleName == role.RoleName, cancellationToken).ConfigureAwait(false);
if (roleExists)
return Errors.Authorization.RoleAlreadyExists;
@@ -61,7 +61,7 @@ internal sealed class RoleRepository : IRoleRepository
/// <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>
public async Task<ErrorOr<RoleEntity?>> GetByNameAsync(AuthorizationRole roleType, CancellationToken cancellationToken)
public async Task<ErrorOr<RoleEntity?>> GetByNameAsync(string roleType, CancellationToken cancellationToken)
{
return await _luminaDbContext.Roles
.Include(role => role.RolePermissions)
@@ -69,4 +69,61 @@ internal sealed class RoleRepository : IRoleRepository
.FirstOrDefaultAsync(role => role.RoleName == roleType, cancellationToken)
.ConfigureAwait(false);
}
/// <summary>
/// Gets a <see cref="RoleEntity"/> identified by <paramref name="id"/> from the storage medium.
/// </summary>
/// <param name="id">The id of the authorization 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"/> identified by <paramref name="id"/>, or an error.</returns>
public async Task<ErrorOr<RoleEntity?>> GetByIdAsync(Guid id, CancellationToken cancellationToken)
{
return await _luminaDbContext.Roles
.Include(role => role.RolePermissions)
.ThenInclude(rolePermission => rolePermission.Permission)
.FirstOrDefaultAsync(role => role.Id == id, cancellationToken)
.ConfigureAwait(false);
}
/// <summary>
/// Updates a role.
/// </summary>
/// <param name="data">The role to update.</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>
public async Task<ErrorOr<Updated>> UpdateAsync(RoleEntity data, CancellationToken cancellationToken)
{
// check if a role with the requested Id exists, and retrieve it
RoleEntity? foundRole = await _luminaDbContext.Roles
.Include(role => role.RolePermissions)
.ThenInclude(rolePermission => rolePermission.Permission)
.FirstOrDefaultAsync(role => role.Id == data.Id, cancellationToken).ConfigureAwait(false);
if (foundRole is null)
return Errors.Authorization.RoleNotFound;
// update scalar properties
_luminaDbContext.Entry(foundRole).CurrentValues.SetValues(data);
// update owned entities (their changes are not automatically tracked by EF)
foundRole.RolePermissions.Clear();
foreach (RolePermissionEntity rolePermission in data.RolePermissions)
foundRole.RolePermissions.Add(rolePermission);
return Result.Updated;
}
/// <summary>
/// Deletes a role identified by <paramref name="id"/> from the storage medium.
/// </summary>
/// <param name="id">The id of the role to be deleted.</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>
public async Task<ErrorOr<Deleted>> DeleteByIdAsync(Guid id, CancellationToken cancellationToken)
{
// check if a role with the requested Id exists, and retrieve it
RoleEntity? foundRole = await _luminaDbContext.Roles
.FirstOrDefaultAsync(role => role.Id == id, cancellationToken).ConfigureAwait(false);
if (foundRole is null)
return Errors.Authorization.RoleNotFound;
// then, remove it
_luminaDbContext.Remove(foundRole);
return Result.Deleted;
}
}
@@ -60,7 +60,7 @@ internal sealed class LibraryRepository : ILibraryRepository
/// <summary>
/// Updates a media library.
/// </summary>
/// <param name="data">Ther media library to update.</param>
/// <param name="data">The media library to update.</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>
public async Task<ErrorOr<Updated>> UpdateAsync(LibraryEntity data, CancellationToken cancellationToken)
@@ -48,9 +48,9 @@ public class DataSeedService : IDataSeedService
// create the default authorization permissions and add them to the repository.
PermissionEntity[] defaultPermissions =
[
new() { PermissionName = AuthorizationPermission.CanViewUsers, PermissionDescription = "Can view users", CreatedBy = adminId, CreatedOnUtc = _dateTimeProvider.UtcNow },
new() { PermissionName = AuthorizationPermission.CanDeleteUsers, PermissionDescription = "Can delete users", CreatedBy = adminId, CreatedOnUtc = _dateTimeProvider.UtcNow },
new() { PermissionName = AuthorizationPermission.CanRegisterUsers, PermissionDescription = "Can register users", CreatedBy = adminId, CreatedOnUtc = _dateTimeProvider.UtcNow }
new() { PermissionName = AuthorizationPermission.CanViewUsers, CreatedBy = adminId, CreatedOnUtc = _dateTimeProvider.UtcNow },
new() { PermissionName = AuthorizationPermission.CanDeleteUsers, CreatedBy = adminId, CreatedOnUtc = _dateTimeProvider.UtcNow },
new() { PermissionName = AuthorizationPermission.CanRegisterUsers, CreatedBy = adminId, CreatedOnUtc = _dateTimeProvider.UtcNow }
];
foreach (PermissionEntity permission in defaultPermissions)
{
@@ -75,7 +75,7 @@ public class DataSeedService : IDataSeedService
// create the default authorization roles and add them to the repository
RoleEntity[] defaultRoles =
[
new() { RoleName = AuthorizationRole.Admin, RoleDescription = "Administrator role", CreatedBy = userId, CreatedOnUtc = _dateTimeProvider.UtcNow }
new() { RoleName = "Admin", CreatedBy = userId, CreatedOnUtc = _dateTimeProvider.UtcNow }
];
foreach (RoleEntity role in defaultRoles)
{
@@ -100,7 +100,7 @@ public class DataSeedService : IDataSeedService
IPermissionRepository permissionRepository = _unitOfWork.GetRepository<IPermissionRepository>();
// get the admin role
ErrorOr<RoleEntity?> getAdminRoleResult = await roleRepository.GetByNameAsync(AuthorizationRole.Admin, cancellationToken).ConfigureAwait(false);
ErrorOr<RoleEntity?> getAdminRoleResult = await roleRepository.GetByNameAsync("Admin", cancellationToken).ConfigureAwait(false);
if (getAdminRoleResult.IsError)
return getAdminRoleResult.Errors;
@@ -146,7 +146,7 @@ public class DataSeedService : IDataSeedService
// get the admin role
ErrorOr<RoleEntity?> getAdminRoleResult = await roleRepository.GetByNameAsync(AuthorizationRole.Admin, cancellationToken).ConfigureAwait(false);
ErrorOr<RoleEntity?> getAdminRoleResult = await roleRepository.GetByNameAsync("Admin", cancellationToken).ConfigureAwait(false);
if (getAdminRoleResult.IsError)
return getAdminRoleResult.Errors;