Implements Authorization system

This commit is contained in:
BinaryAlley committed 2024-11-22 18:02:42 +02:00
1 parent 5bfb2ca212
commit 2c9c210e43
68 files changed
+2734 -336

No files matched your search

@@ -1,4 +1,4 @@
namespace Lumina.DataAccess.Core.Repositories.Common.Factory;
namespace Lumina.DataAccess.Core.Repositories.Common.Factory;
/// <summary>
/// Interface for the repositories factory.
@@ -6,9 +6,10 @@
public interface IRepositoryFactory
{
/// <summary>
/// Creates a new repository of type <typeparamref name="TResult"/>.
/// Creates a new repository of type <typeparamref name="TRepository"/>.
/// </summary>
/// <typeparam name="TResult">The type of repository to create.</typeparam>
/// <returns>A repository of type <typeparamref name="TResult"/>.</returns>
TResult CreateRepository<TResult>() where TResult : notnull;
}
/// <typeparam name="TRepository">The type of repository to create.</typeparam>
/// <returns>A repository of type <typeparamref name="TRepository"/>.</returns>
/// <exception cref="ArgumentException">Thrown when the type of the requested repository has not been registered.</exception>
TRepository CreateRepository<TRepository>() where TRepository : notnull;
}
@@ -1,4 +1,4 @@
#region ========================================================================= USING =====================================================================================
#region ========================================================================= USING =====================================================================================
using Microsoft.Extensions.DependencyInjection;
using System;
#endregion
@@ -22,13 +22,13 @@ public class RepositoryFactory : IRepositoryFactory
}
/// <summary>
/// Creates a new repository of type <typeparamref name="TResult"/>.
/// Creates a new repository of type <typeparamref name="TRepository"/>.
/// </summary>
/// <typeparam name="TResult">The type of repository to create.</typeparam>
/// <returns>A repository of type <typeparamref name="TResult"/>.</returns>
/// <typeparam name="TRepository">The type of repository to create.</typeparam>
/// <returns>A repository of type <typeparamref name="TRepository"/>.</returns>
/// <exception cref="ArgumentException">Thrown when the type of the requested repository has not been registered.</exception>
public TResult CreateRepository<TResult>() where TResult : notnull
public TRepository CreateRepository<TRepository>() where TRepository : notnull
{
return _serviceProvider.GetRequiredService<TResult>() ?? throw new ArgumentException();
return _serviceProvider.GetRequiredService<TRepository>() ?? throw new ArgumentException();
}
}
}
@@ -1,13 +1,14 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Repositories.Users;
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
using Lumina.Application.Common.DataAccess.Repositories.Users;
using Lumina.DataAccess.Core.UoW;
using Lumina.Domain.Common.Errors;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
#endregion
namespace Lumina.DataAccess.Core.Repositories.Users;
@@ -62,7 +63,17 @@ internal sealed class UserRepository : IUserRepository
/// <returns>An <see cref="ErrorOr{TValue}"/> containing either a <see cref="UserEntity"/> if found, or an error.</returns>
public async Task<ErrorOr<UserEntity?>> GetByUsernameAsync(string username, CancellationToken cancellationToken)
{
return await _luminaDbContext.Users.FirstOrDefaultAsync(user => user.Username == username, cancellationToken).ConfigureAwait(false);
return await _luminaDbContext.Users
.Include(user => user.Libraries)
.ThenInclude(library => library.ContentLocations)
.Include(user => user.UserPermissions)
.ThenInclude(userPermission => userPermission.Permission)
.Include(user => user.UserRoles)
.ThenInclude(userRole => userRole.Role)
.ThenInclude(role => role.RolePermissions)
.ThenInclude(rolePermission => rolePermission.Permission)
.FirstOrDefaultAsync(user => user.Username == username, cancellationToken)
.ConfigureAwait(false);
}
/// <summary>
@@ -73,10 +84,41 @@ internal sealed class UserRepository : IUserRepository
/// <returns>An <see cref="ErrorOr{TValue}"/> representing either a successfull operation, or an error.</returns>
public async Task<ErrorOr<Updated>> UpdateAsync(UserEntity data, CancellationToken cancellationToken)
{
UserEntity? foundUser = await _luminaDbContext.Users.FirstOrDefaultAsync(user => user.Username == data.Username, cancellationToken).ConfigureAwait(false);
UserEntity? foundUser = await _luminaDbContext.Users
.Include(user => user.Libraries)
.ThenInclude(library => library.ContentLocations)
.Include(user => user.UserPermissions)
.ThenInclude(userPermission => userPermission.Permission)
.Include(user => user.UserRoles)
.ThenInclude(userRole => userRole.Role)
.ThenInclude(role => role.RolePermissions)
.ThenInclude(rolePermission => rolePermission.Permission)
.FirstOrDefaultAsync(user => user.Username == data.Username, cancellationToken)
.ConfigureAwait(false);
if (foundUser is null)
return Errors.Users.UserDoesNotExist;
_luminaDbContext.Entry(foundUser).CurrentValues.SetValues(data);
return Result.Updated;
}
/// <summary>
/// Gets a <see cref="UserEntity"/> identified by <paramref name="id"/> from the storage medium.
/// </summary>
/// <param name="id">The id of the user 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="UserEntity"/> identified by <paramref name="id"/>, or an error.</returns>
public async Task<ErrorOr<UserEntity?>> GetByIdAsync(Guid id, CancellationToken cancellationToken)
{
return await _luminaDbContext.Users
.Include(user => user.Libraries)
.ThenInclude(library => library.ContentLocations)
.Include(user => user.UserPermissions)
.ThenInclude(userPermission => userPermission.Permission)
.Include(user => user.UserRoles)
.ThenInclude(userRole => userRole.Role)
.ThenInclude(role => role.RolePermissions)
.ThenInclude(rolePermission => rolePermission.Permission)
.FirstOrDefaultAsync(user => user.Id == id, cancellationToken)
.ConfigureAwait(false);
}
}
@@ -1,16 +1,11 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Common;
using Lumina.Application.Common.DataAccess.Entities.Authorization;
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
using Lumina.Domain.Common.Events;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.DataAccess.Core.UoW;
@@ -25,6 +20,11 @@ public class LuminaDbContext : DbContext
public virtual DbSet<BookEntity> Books { get; set; } = null!;
public virtual DbSet<UserEntity> Users { get; set; } = null!;
public virtual DbSet<LibraryEntity> Libraries { get; set; } = null!;
public virtual DbSet<RoleEntity> Roles { get; set; } = null!;
public virtual DbSet<UserRoleEntity> UserRoles { get; set; } = null!;
public virtual DbSet<PermissionEntity> Permissions { get; set; } = null!;
public virtual DbSet<UserPermissionEntity> UserPermissions { get; set; } = null!;
public virtual DbSet<RolePermissionEntity> RolePermissions { get; set; } = null!;
/// <summary>
/// Initializes a new instance of the <see cref="LuminaDbContext"/> class.
@@ -46,51 +46,4 @@ public class LuminaDbContext : DbContext
base.OnModelCreating(modelBuilder);
}
/// <summary>
/// Overrides the SaveChanges method of the DbContext class to automatically set the Created and Updated properties of entities that implement the <see cref="IStorageEntity"/>.
/// </summary>
/// <returns>The number of objects written to the underlying database.</returns>
public override int SaveChanges()
{
HandleTimestamps();
return base.SaveChanges();
}
/// <summary>
/// Overrides the SaveChangesAsync method of the DbContext class to automatically set the Created and Updated properties of entities that implement the <see cref="IStorageEntity"/>.
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns>A <see cref="Task"/> containing the number of objects written to the underlying database.</returns>
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
{
HandleTimestamps();
return base.SaveChangesAsync(cancellationToken);
}
/// <summary>
/// Updates timestamp properties for tracked entities implementing <see cref="IStorageEntity"/> based on their state in the change tracker.
/// </summary>
/// <remarks>
/// <list type="bullet">
/// <item><description><c>Created</c>: Set for added entities.</description></item>
/// <item><description><c>Updated</c>: Set for modified entities.</description></item>
/// </list>
/// </remarks>
private void HandleTimestamps()
{
// get all the entity entries that are either added or modified
IEnumerable<EntityEntry> entries = ChangeTracker.Entries()
.Where(entity => entity.Entity is IStorageEntity &&
(entity.State is EntityState.Added or EntityState.Modified));
foreach (EntityEntry? entityEntry in entries)
{
// if the entity is in Added state, set the Created property to the current date and time
if (entityEntry.State == EntityState.Added)
((IStorageEntity)entityEntry.Entity).Created = DateTime.UtcNow;
// otherwise, if the entity is in Modified state, set the Updated property to the current date and time
else if (entityEntry.State == EntityState.Modified)
((IStorageEntity)entityEntry.Entity).Updated = DateTime.UtcNow;
}
}
}