diff --git a/src/Lumina.Application/Common/DataAccess/Entities/Authorization/PermissionEntity.cs b/src/Lumina.Application/Common/DataAccess/Entities/Authorization/PermissionEntity.cs
new file mode 100644
index 00000000..ea2bd897
--- /dev/null
+++ b/src/Lumina.Application/Common/DataAccess/Entities/Authorization/PermissionEntity.cs
@@ -0,0 +1,55 @@
+#region ========================================================================= USING =====================================================================================
+using Lumina.Application.Common.DataAccess.Entities.Common;
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+#endregion
+
+namespace Lumina.Application.Common.DataAccess.Entities.Authorization;
+
+///
+/// Represents a permission that can be assigned to roles or users within the authorization system.
+///
+[DebuggerDisplay("PermissionName: {PermissionName}")]
+public class PermissionEntity : IStorageEntity, IAuditableEntity
+{
+ ///
+ /// Gets the Id of the permission.
+ ///
+ public Guid Id { get; init; }
+
+ ///
+ /// Gets the name of the permission.
+ ///
+ public required string PermissionName { get; init; }
+
+ ///
+ /// Gets the collection of role permission associations that include this permission.
+ ///
+ public required ICollection RolePermissions { get; init; } = [];
+
+ ///
+ /// Gets the collection of user permission associations that include this permission.
+ ///
+ public required ICollection UserPermissions { get; init; } = [];
+
+ ///
+ /// Gets or sets the time and date when the entity was added.
+ ///
+ public required DateTime CreatedOnUtc { get; set; }
+
+ ///
+ /// Gets or sets the Id of the user that created the entity.
+ ///
+ public required Guid CreatedBy { get; set; }
+
+ ///
+ /// Gets or sets the optional time and date when the entity was updated.
+ ///
+ public DateTime? UpdatedOnUtc { get; set; }
+
+ ///
+ /// Gets or sets the optional Id of the user that updated the entity.
+ ///
+ public Guid? UpdatedBy { get; set; }
+}
diff --git a/src/Lumina.Application/Common/DataAccess/Entities/Authorization/RoleEntity.cs b/src/Lumina.Application/Common/DataAccess/Entities/Authorization/RoleEntity.cs
new file mode 100644
index 00000000..203f5802
--- /dev/null
+++ b/src/Lumina.Application/Common/DataAccess/Entities/Authorization/RoleEntity.cs
@@ -0,0 +1,55 @@
+#region ========================================================================= USING =====================================================================================
+using Lumina.Application.Common.DataAccess.Entities.Common;
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+#endregion
+
+namespace Lumina.Application.Common.DataAccess.Entities.Authorization;
+
+///
+/// Repository entity for a permission in the authorization system.
+///
+[DebuggerDisplay("RoleName: {RoleName}")]
+public class RoleEntity : IStorageEntity
+{
+ ///
+ /// Gets the Id of the role.
+ ///
+ public Guid Id { get; init; }
+
+ ///
+ /// Gets the name of the role.
+ ///
+ public required string RoleName { get; init; }
+
+ ///
+ /// Gets the collection of user roles associations that include this role.
+ ///
+ public required ICollection UserRoles { get; init; } = [];
+
+ ///
+ /// Gets the collection of role permission associations that include this role.
+ ///
+ public required ICollection RolePermissions { get; init; } = [];
+
+ ///
+ /// Gets or sets the time and date when the entity was added.
+ ///
+ public required DateTime CreatedOnUtc { get; set; }
+
+ ///
+ /// Gets or sets the Id of the user that created the entity.
+ ///
+ public required Guid CreatedBy { get; set; }
+
+ ///
+ /// Gets or sets the optional time and date when the entity was updated.
+ ///
+ public DateTime? UpdatedOnUtc { get; set; }
+
+ ///
+ /// Gets or sets the optional Id of the user that updated the entity.
+ ///
+ public Guid? UpdatedBy { get; set; }
+}
diff --git a/src/Lumina.Application/Common/DataAccess/Entities/Authorization/RolePermissionEntity.cs b/src/Lumina.Application/Common/DataAccess/Entities/Authorization/RolePermissionEntity.cs
new file mode 100644
index 00000000..b0187d33
--- /dev/null
+++ b/src/Lumina.Application/Common/DataAccess/Entities/Authorization/RolePermissionEntity.cs
@@ -0,0 +1,59 @@
+#region ========================================================================= USING =====================================================================================
+using Lumina.Application.Common.DataAccess.Entities.Common;
+using System;
+using System.Diagnostics;
+#endregion
+
+namespace Lumina.Application.Common.DataAccess.Entities.Authorization;
+
+///
+/// Represents the association between a role and a permission within the authorization system.
+///
+[DebuggerDisplay("Role: {Role.RoleName}, Permission: {Permission.PermissionName}")]
+public class RolePermissionEntity : IStorageEntity, IAuditableEntity
+{
+ ///
+ /// Gets the Id of the role permission.
+ ///
+ public Guid Id { get; init; }
+
+ ///
+ /// Gets the Id of the role of the role permission.
+ ///
+ public required Guid RoleId { get; init; }
+
+ ///
+ /// Gets the role of the role permission.
+ ///
+ public required RoleEntity Role { get; init; }
+
+ ///
+ /// Gets the Id of the permission of the role permissions.
+ ///
+ public required Guid PermissionId { get; init; }
+
+ ///
+ /// Gets the permission of the role permission.
+ ///
+ public required PermissionEntity Permission { get; init; }
+
+ ///
+ /// Gets or sets the time and date when the entity was added.
+ ///
+ public required DateTime CreatedOnUtc { get; set; }
+
+ ///
+ /// Gets or sets the Id of the user that created the entity.
+ ///
+ public required Guid CreatedBy { get; set; }
+
+ ///
+ /// Gets or sets the optional time and date when the entity was updated.
+ ///
+ public DateTime? UpdatedOnUtc { get; set; }
+
+ ///
+ /// Gets or sets the optional Id of the user that updated the entity.
+ ///
+ public Guid? UpdatedBy { get; set; }
+}
diff --git a/src/Lumina.Application/Common/DataAccess/Entities/Authorization/UserPermissionEntity.cs b/src/Lumina.Application/Common/DataAccess/Entities/Authorization/UserPermissionEntity.cs
new file mode 100644
index 00000000..9c13373d
--- /dev/null
+++ b/src/Lumina.Application/Common/DataAccess/Entities/Authorization/UserPermissionEntity.cs
@@ -0,0 +1,60 @@
+#region ========================================================================= USING =====================================================================================
+using Lumina.Application.Common.DataAccess.Entities.Common;
+using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
+using System;
+using System.Diagnostics;
+#endregion
+
+namespace Lumina.Application.Common.DataAccess.Entities.Authorization;
+
+///
+/// Represents the association between a user and a permission within the authorization system.
+///
+[DebuggerDisplay("User: {User.Username}, Permission: {Permission.PermissionName}")]
+public class UserPermissionEntity : IStorageEntity, IAuditableEntity
+{
+ ///
+ /// Gets the Id of the user permission.
+ ///
+ public Guid Id { get; init; }
+
+ ///
+ /// Gets the Id of the user of the user permission.
+ ///
+ public required Guid UserId { get; init; }
+
+ ///
+ /// Gets the user of the user permission.
+ ///
+ public required UserEntity User { get; init; }
+
+ ///
+ /// Gets the Id of the permission of the user permission.
+ ///
+ public required Guid PermissionId { get; init; }
+
+ ///
+ /// Gets the permission of the user permission.
+ ///
+ public required PermissionEntity Permission { get; init; }
+
+ ///
+ /// Gets or sets the time and date when the entity was added.
+ ///
+ public required DateTime CreatedOnUtc { get; set; }
+
+ ///
+ /// Gets or sets the Id of the user that created the entity.
+ ///
+ public required Guid CreatedBy { get; set; }
+
+ ///
+ /// Gets or sets the optional time and date when the entity was updated.
+ ///
+ public DateTime? UpdatedOnUtc { get; set; }
+
+ ///
+ /// Gets or sets the optional Id of the user that updated the entity.
+ ///
+ public Guid? UpdatedBy { get; set; }
+}
diff --git a/src/Lumina.Application/Common/DataAccess/Entities/Authorization/UserRoleEntity.cs b/src/Lumina.Application/Common/DataAccess/Entities/Authorization/UserRoleEntity.cs
new file mode 100644
index 00000000..3d8a1f3c
--- /dev/null
+++ b/src/Lumina.Application/Common/DataAccess/Entities/Authorization/UserRoleEntity.cs
@@ -0,0 +1,60 @@
+#region ========================================================================= USING =====================================================================================
+using Lumina.Application.Common.DataAccess.Entities.Common;
+using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
+using System;
+using System.Diagnostics;
+#endregion
+
+namespace Lumina.Application.Common.DataAccess.Entities.Authorization;
+
+///
+/// Represents the association between a user and a role within the authorization system.
+///
+[DebuggerDisplay("User: {User.Username}, Role: {Role.RoleName}")]
+public class UserRoleEntity : IStorageEntity, IAuditableEntity
+{
+ ///
+ /// Gets the Id of the user role.
+ ///
+ public Guid Id { get; init; }
+
+ ///
+ /// Gets the Id of the user of the user role.
+ ///
+ public required Guid UserId { get; init; }
+
+ ///
+ /// Gets the user of the user role.
+ ///
+ public required UserEntity User { get; init; }
+
+ ///
+ /// Gets the Id of the role of the user role.
+ ///
+ public required Guid RoleId { get; init; }
+
+ ///
+ /// Gets the role of the user role.
+ ///
+ public required RoleEntity Role { get; init; }
+
+ ///
+ /// Gets or sets the time and date when the entity was added.
+ ///
+ public required DateTime CreatedOnUtc { get; set; }
+
+ ///
+ /// Gets or sets the Id of the user that created the entity.
+ ///
+ public required Guid CreatedBy { get; set; }
+
+ ///
+ /// Gets or sets the optional time and date when the entity was updated.
+ ///
+ public DateTime? UpdatedOnUtc { get; set; }
+
+ ///
+ /// Gets or sets the optional Id of the user that updated the entity.
+ ///
+ public Guid? UpdatedBy { get; set; }
+}
diff --git a/src/Lumina.Application/Common/DataAccess/Entities/Common/IAuditableEntity.cs b/src/Lumina.Application/Common/DataAccess/Entities/Common/IAuditableEntity.cs
new file mode 100644
index 00000000..60ba9b18
--- /dev/null
+++ b/src/Lumina.Application/Common/DataAccess/Entities/Common/IAuditableEntity.cs
@@ -0,0 +1,31 @@
+#region ========================================================================= USING =====================================================================================
+using System;
+#endregion
+
+namespace Lumina.Application.Common.DataAccess.Entities.Common;
+
+///
+/// Interface for the properties required for auditing changes to an entity.
+///
+public interface IAuditableEntity
+{
+ ///
+ /// Gets or sets the time and date when the entity was added.
+ ///
+ DateTime CreatedOnUtc { get; set; }
+
+ ///
+ /// Gets or sets the Id of the user that created the entity.
+ ///
+ Guid CreatedBy { get; set; }
+
+ ///
+ /// Gets or sets the optional time and date when the entity was updated.
+ ///
+ DateTime? UpdatedOnUtc { get; set; }
+
+ ///
+ /// Gets or sets the optional Id of the user that updated the entity.
+ ///
+ Guid? UpdatedBy { get; set; }
+}
diff --git a/src/Lumina.Application/Common/DataAccess/Entities/Common/IStorageEntity.cs b/src/Lumina.Application/Common/DataAccess/Entities/Common/IStorageEntity.cs
index f3bb814e..d598f72d 100644
--- a/src/Lumina.Application/Common/DataAccess/Entities/Common/IStorageEntity.cs
+++ b/src/Lumina.Application/Common/DataAccess/Entities/Common/IStorageEntity.cs
@@ -13,14 +13,4 @@ public interface IStorageEntity
/// Gets or sets the identifier of the entity.
///
Guid Id { get; init; }
-
- ///
- /// Gets or sets the time and date when the entity was added.
- ///
- DateTime Created { get; set; }
-
- ///
- /// Gets or sets the optional time and date when the entity was updated.
- ///
- DateTime? Updated { get; set; }
}
diff --git a/src/Lumina.Application/Common/DataAccess/Entities/UsersManagement/UserEntity.cs b/src/Lumina.Application/Common/DataAccess/Entities/UsersManagement/UserEntity.cs
index fba2d476..6c282a68 100644
--- a/src/Lumina.Application/Common/DataAccess/Entities/UsersManagement/UserEntity.cs
+++ b/src/Lumina.Application/Common/DataAccess/Entities/UsersManagement/UserEntity.cs
@@ -1,4 +1,5 @@
#region ========================================================================= USING =====================================================================================
+using Lumina.Application.Common.DataAccess.Entities.Authorization;
using Lumina.Application.Common.DataAccess.Entities.Common;
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
using System;
@@ -12,7 +13,7 @@ namespace Lumina.Application.Common.DataAccess.Entities.UsersManagement;
/// Repository entity for a user.
///
[DebuggerDisplay("Username: {Username}")]
-public class UserEntity : IStorageEntity
+public class UserEntity : IStorageEntity, IAuditableEntity
{
///
/// Gets the Id of the user.
@@ -39,16 +40,6 @@ public class UserEntity : IStorageEntity
///
public string? TotpSecret { get; set; }
- ///
- /// Gets or sets the optional token used for account verification or password reset.
- ///
- public string? VerificationToken { get; set; } // TODO: to be deleted, not used
-
- ///
- /// Gets or sets the optional date and time when the verification token was created.
- ///
- public DateTime? VerificationTokenCreated { get; set; } // TODO: to be deleted, not used
-
///
/// Gets or sets the date and time when the temporary password used in password recovery was created.
///
@@ -59,13 +50,33 @@ public class UserEntity : IStorageEntity
///
public required ICollection Libraries { get; init; } = [];
+ ///
+ /// Gets the collection of user roles associations that include this user.
+ ///
+ public required ICollection UserRoles { get; init; } = [];
+
+ ///
+ /// Gets the collection of user permission associations that include this user.
+ ///
+ public required ICollection UserPermissions { get; init; } = [];
+
///
/// Gets the time and date when the entity was added.
///
- public required DateTime Created { get; set; }
+ public required DateTime CreatedOnUtc { get; set; }
+
+ ///
+ /// Gets or sets the Id of the user that created the entity.
+ ///
+ public required Guid CreatedBy { get; set; }
///
/// Gets the optional time and date when the entity was updated.
///
- public DateTime? Updated { get; set; }
+ public DateTime? UpdatedOnUtc { get; set; }
+
+ ///
+ /// Gets or sets the optional Id of the user that updated the entity.
+ ///
+ public Guid? UpdatedBy { get; set; }
}
diff --git a/src/Lumina.Application/Common/DataAccess/Repositories/Users/IUserRepository.cs b/src/Lumina.Application/Common/DataAccess/Repositories/Users/IUserRepository.cs
index f45bf2fd..da4745d6 100644
--- a/src/Lumina.Application/Common/DataAccess/Repositories/Users/IUserRepository.cs
+++ b/src/Lumina.Application/Common/DataAccess/Repositories/Users/IUserRepository.cs
@@ -3,6 +3,7 @@ using ErrorOr;
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
using Lumina.Application.Common.DataAccess.Repositories.Common.Actions;
using Lumina.Application.Common.DataAccess.Repositories.Common.Base;
+using System;
using System.Threading;
using System.Threading.Tasks;
#endregion
@@ -13,6 +14,7 @@ namespace Lumina.Application.Common.DataAccess.Repositories.Users;
/// Interface for the repository for users.
///
public interface IUserRepository : IRepository,
+ IGetByIdRepositoryAction,
IGetAllRepositoryAction,
IInsertRepositoryAction,
IUpdateRepositoryAction
diff --git a/src/Lumina.Application/Common/Infrastructure/Authentication/ICurrentUserService.cs b/src/Lumina.Application/Common/Infrastructure/Authentication/ICurrentUserService.cs
new file mode 100644
index 00000000..73973ba2
--- /dev/null
+++ b/src/Lumina.Application/Common/Infrastructure/Authentication/ICurrentUserService.cs
@@ -0,0 +1,16 @@
+#region ========================================================================= USING =====================================================================================
+using System;
+#endregion
+
+namespace Lumina.Application.Common.Infrastructure.Authentication;
+
+///
+/// Defines a contract for accessing the current user's information.
+///
+public interface ICurrentUserService
+{
+ ///
+ /// Gets the unique identifier of the currently authenticated user. Returns if no user is authenticated.
+ ///
+ Guid? UserId { get; }
+}
diff --git a/src/Lumina.Application/Common/Infrastructure/Authorization/IAuthorizationService.cs b/src/Lumina.Application/Common/Infrastructure/Authorization/IAuthorizationService.cs
new file mode 100644
index 00000000..0c418f0f
--- /dev/null
+++ b/src/Lumina.Application/Common/Infrastructure/Authorization/IAuthorizationService.cs
@@ -0,0 +1,41 @@
+#region ========================================================================= USING =====================================================================================
+using Lumina.Application.Common.Infrastructure.Authorization.Policies.Common.Base;
+using System;
+using System.Threading;
+using System.Threading.Tasks;
+#endregion
+
+namespace Lumina.Application.Common.Infrastructure.Authorization;
+
+///
+/// Interface for the service for managing authorization.
+///
+public interface IAuthorizationService
+{
+ ///
+ /// Determines whether the specified user has a specific permission.
+ ///
+ /// The Id of the user for whom to check the permission.
+ /// The name of the permission to check.
+ /// Cancellation token that can be used to stop the execution.
+ /// if the user has the specified permission, otherwise.
+ Task HasPermissionAsync(Guid userId, string permission, CancellationToken cancellationToken);
+
+ ///
+ /// Determines whether the specified user belongs to a specific role.
+ ///
+ /// The Id of the user for whom to check the role.
+ /// The name of the role to check.
+ /// Cancellation token that can be used to stop the execution.
+ /// if the user is in the specified role, otherwise.
+ Task IsInRoleAsync(Guid userId, string role, CancellationToken cancellationToken);
+
+ ///
+ /// Evaluates whether the specified user meets the conditions defined in the specified authorization policy.
+ ///
+ /// The type of authorization policy to evaluate.
+ /// The Id of the user for whom to evaluate the policy.
+ /// Cancellation token that can be used to stop the execution.
+ /// if the user satisfies the policy, otherwise.
+ Task EvaluatePolicyAsync(Guid userId, CancellationToken cancellationToken) where TAuthorizationPolicy : IAuthorizationPolicy;
+}
diff --git a/src/Lumina.Application/Common/Infrastructure/Authorization/Policies/Common/Base/IAuthorizationPolicy.cs b/src/Lumina.Application/Common/Infrastructure/Authorization/Policies/Common/Base/IAuthorizationPolicy.cs
new file mode 100644
index 00000000..29d3228c
--- /dev/null
+++ b/src/Lumina.Application/Common/Infrastructure/Authorization/Policies/Common/Base/IAuthorizationPolicy.cs
@@ -0,0 +1,21 @@
+#region ========================================================================= USING =====================================================================================
+using System;
+using System.Threading;
+using System.Threading.Tasks;
+#endregion
+
+namespace Lumina.Application.Common.Infrastructure.Authorization.Policies.Common.Base;
+
+///
+/// Defines a contract for policy evaluation in the application.
+///
+public interface IAuthorizationPolicy
+{
+ ///
+ /// Evaluates a policy for a user identified by .
+ ///
+ /// The Id of the user for which to evaluate the policy.
+ /// Cancellation token that can be used to stop the execution.
+ /// if the policy evaluation succeeds, otherwise.
+ Task EvaluateAsync(Guid userId, CancellationToken cancellationToken);
+}
diff --git a/src/Lumina.Application/Common/Infrastructure/Authorization/Policies/Over18/IOver18Policy.cs b/src/Lumina.Application/Common/Infrastructure/Authorization/Policies/Over18/IOver18Policy.cs
new file mode 100644
index 00000000..be1d7aaf
--- /dev/null
+++ b/src/Lumina.Application/Common/Infrastructure/Authorization/Policies/Over18/IOver18Policy.cs
@@ -0,0 +1,12 @@
+#region ========================================================================= USING =====================================================================================
+using Lumina.Application.Common.Infrastructure.Authorization.Policies.Common.Base;
+#endregion
+
+namespace Lumina.Application.Common.Infrastructure.Authorization.Policies.Over18;
+
+///
+/// Interface for the Over18 authorization policy.
+///
+public interface IOver18Policy : IAuthorizationPolicy
+{
+}
diff --git a/src/Lumina.Application/Common/Mapping/UsersManagement/Users/UserEntityMapping.cs b/src/Lumina.Application/Common/Mapping/UsersManagement/Users/UserEntityMapping.cs
index 1eccd2c6..7a08efc6 100644
--- a/src/Lumina.Application/Common/Mapping/UsersManagement/Users/UserEntityMapping.cs
+++ b/src/Lumina.Application/Common/Mapping/UsersManagement/Users/UserEntityMapping.cs
@@ -20,8 +20,8 @@ public static class UserEntityMapping
return new UserResponse(
repositoryEntity.Id,
repositoryEntity.Username,
- repositoryEntity.Created,
- repositoryEntity.Updated
+ repositoryEntity.CreatedOnUtc,
+ repositoryEntity.UpdatedOnUtc
);
}
}
diff --git a/src/Lumina.Application/Core/Maintenance/ApplicationSetup/Commands/SetupApplication/SetupApplicationCommandHandler.cs b/src/Lumina.Application/Core/Maintenance/ApplicationSetup/Commands/SetupApplication/SetupApplicationCommandHandler.cs
index 5f2ce8b7..e3e1a73e 100644
--- a/src/Lumina.Application/Core/Maintenance/ApplicationSetup/Commands/SetupApplication/SetupApplicationCommandHandler.cs
+++ b/src/Lumina.Application/Core/Maintenance/ApplicationSetup/Commands/SetupApplication/SetupApplicationCommandHandler.cs
@@ -6,6 +6,7 @@ using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.Errors;
using Lumina.Application.Common.Infrastructure.Authentication;
using Lumina.Application.Common.Infrastructure.Security;
+using Lumina.Application.Common.Infrastructure.Time;
using Lumina.Contracts.Responses.Authentication;
using Mediator;
using System;
@@ -27,6 +28,7 @@ public class SetupApplicationCommandHandler : IRequestHandler
/// Initializes a new instance of the class.
@@ -36,18 +38,21 @@ public class SetupApplicationCommandHandler : IRequestHandlerInjected service for cryptographic functionality.
/// Injected service for generating and validating TOTP tokens.
/// Injected service for generating QR codes.
+ /// Injected service for time related concerns.
public SetupApplicationCommandHandler(
IUnitOfWork unitOfWork,
IHashService hashService,
ICryptographyService cryptographyService,
ITotpTokenGenerator totpTokenGenerator,
- IQRCodeGenerator qRCodeGenerator)
+ IQRCodeGenerator qRCodeGenerator,
+ IDateTimeProvider dateTimeProvider)
{
_unitOfWork = unitOfWork;
_hashService = hashService;
_cryptographyService = cryptographyService;
_totpTokenGenerator = totpTokenGenerator;
_qRCodeGenerator = qRCodeGenerator;
+ _dateTimeProvider = dateTimeProvider;
}
///
@@ -69,13 +74,17 @@ public class SetupApplicationCommandHandler : IRequestHandler
/// Initializes a new instance of the class.
@@ -34,18 +36,21 @@ public class RegisterUserCommandHandler : IRequestHandlerInjected service for cryptographic functionality.
/// Injected service for generating and validating TOTP tokens.
/// Injected service for generating QR codes.
+ /// Injected service for time related concerns.
public RegisterUserCommandHandler(
IUnitOfWork unitOfWork,
IHashService hashService,
ICryptographyService cryptographyService,
ITotpTokenGenerator totpTokenGenerator,
- IQRCodeGenerator qRCodeGenerator)
+ IQRCodeGenerator qRCodeGenerator,
+ IDateTimeProvider dateTimeProvider)
{
_unitOfWork = unitOfWork;
_hashService = hashService;
_cryptographyService = cryptographyService;
_totpTokenGenerator = totpTokenGenerator;
_qRCodeGenerator = qRCodeGenerator;
+ _dateTimeProvider = dateTimeProvider;
}
///
@@ -66,13 +71,17 @@ public class RegisterUserCommandHandler : IRequestHandler
/// Initializes a new instance of the class.
@@ -34,18 +36,21 @@ public class LoginUserQueryHandler : IRequestHandlerInjected service for generating JWT tokens.
/// Injected service for generating and validating TOTP tokens.
/// Injected service for cryptographic functionality.
+ /// Injected service for time related concerns.
public LoginUserQueryHandler(
IUnitOfWork unitOfWork,
IHashService hashService,
IJwtTokenGenerator jwtTokenGenerator,
ITotpTokenGenerator totpTokenGenerator,
- ICryptographyService cryptographyService)
+ ICryptographyService cryptographyService,
+ IDateTimeProvider dateTimeProvider)
{
_unitOfWork = unitOfWork;
_hashService = hashService;
_jwtTokenGenerator = jwtTokenGenerator;
_totpTokenGenerator = totpTokenGenerator;
_cryptographyService = cryptographyService;
+ _dateTimeProvider = dateTimeProvider;
}
///
@@ -72,7 +77,7 @@ public class LoginUserQueryHandler : IRequestHandler
+/// Configures the entity mapping for the entity.
+///
+public class PermissionConfiguration : IEntityTypeConfiguration
+{
+ ///
+ /// Configures the entity.
+ ///
+ /// The builder to be used to configure the entity.
+ public void Configure(EntityTypeBuilder builder)
+ {
+ builder.ToTable("Permissions");
+ builder.HasKey(permission => permission.Id);
+ builder.Property(permission => permission.Id)
+ .ValueGeneratedOnAdd()
+ .HasColumnOrder(0);
+
+ builder.Property(permission => permission.PermissionName)
+ .IsRequired()
+ .HasMaxLength(100)
+ .HasColumnOrder(1);
+
+ // audit
+ builder.Property(permission => permission.CreatedOnUtc)
+ .IsRequired()
+ .HasColumnOrder(2);
+
+ builder.Property(permission => permission.CreatedBy)
+ .IsRequired()
+ .HasColumnOrder(3);
+
+ builder.Property(permission => permission.UpdatedOnUtc)
+ .HasDefaultValue(null)
+ .HasColumnOrder(4);
+
+ builder.Property(permission => permission.UpdatedBy)
+ .HasDefaultValue(null)
+ .HasColumnOrder(5);
+ }
+}
diff --git a/src/Lumina.DataAccess/Common/Configuration/RoleConfiguration.cs b/src/Lumina.DataAccess/Common/Configuration/RoleConfiguration.cs
new file mode 100644
index 00000000..9811c82e
--- /dev/null
+++ b/src/Lumina.DataAccess/Common/Configuration/RoleConfiguration.cs
@@ -0,0 +1,48 @@
+#region ========================================================================= USING =====================================================================================
+using Lumina.Application.Common.DataAccess.Entities.Authorization;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Metadata.Builders;
+#endregion
+
+namespace Lumina.DataAccess.Common.Configuration;
+
+///
+/// Configures the entity mapping for the entity.
+///
+public class RoleConfiguration : IEntityTypeConfiguration
+{
+ ///
+ /// Configures the entity.
+ ///
+ /// The builder to be used to configure the entity.
+ public void Configure(EntityTypeBuilder builder)
+ {
+ builder.ToTable("Roles");
+ builder.HasKey(role => role.Id);
+ builder.Property(role => role.Id)
+ .ValueGeneratedOnAdd()
+ .HasColumnOrder(0);
+
+ builder.Property(role => role.RoleName)
+ .IsRequired()
+ .HasMaxLength(100)
+ .HasColumnOrder(1);
+
+ // audit
+ builder.Property(role => role.CreatedOnUtc)
+ .IsRequired()
+ .HasColumnOrder(2);
+
+ builder.Property(role => role.CreatedBy)
+ .IsRequired()
+ .HasColumnOrder(3);
+
+ builder.Property(role => role.UpdatedOnUtc)
+ .HasDefaultValue(null)
+ .HasColumnOrder(4);
+
+ builder.Property(role => role.UpdatedBy)
+ .HasDefaultValue(null)
+ .HasColumnOrder(5);
+ }
+}
diff --git a/src/Lumina.DataAccess/Common/Configuration/RolePermissionConfiguration.cs b/src/Lumina.DataAccess/Common/Configuration/RolePermissionConfiguration.cs
new file mode 100644
index 00000000..5cf96e00
--- /dev/null
+++ b/src/Lumina.DataAccess/Common/Configuration/RolePermissionConfiguration.cs
@@ -0,0 +1,60 @@
+#region ========================================================================= USING =====================================================================================
+using Lumina.Application.Common.DataAccess.Entities.Authorization;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Metadata.Builders;
+#endregion
+
+namespace Lumina.DataAccess.Common.Configuration;
+
+///
+/// Configures the entity mapping for the entity.
+///
+public class RolePermissionConfiguration : IEntityTypeConfiguration
+{
+ ///
+ /// Configures the entity.
+ ///
+ /// The builder to be used to configure the entity.
+ public void Configure(EntityTypeBuilder builder)
+ {
+ builder.ToTable("RolePermissions");
+
+ // composite primary key
+ builder.HasKey(rolePermission => new { rolePermission.RoleId, rolePermission.PermissionId });
+
+ // foreign key: Role
+ builder.HasOne(rolePermission => rolePermission.Role)
+ .WithMany(user => user.RolePermissions)
+ .HasForeignKey(rolePermission => rolePermission.RoleId)
+ .IsRequired()
+ .OnDelete(DeleteBehavior.Cascade); // deleting a Role deletes associated RolePermission
+
+ // foreign key: Permission
+ builder.HasOne(rolePermission => rolePermission.Permission)
+ .WithMany(role => role.RolePermissions)
+ .HasForeignKey(rolePermission => rolePermission.PermissionId)
+ .IsRequired()
+ .OnDelete(DeleteBehavior.Cascade); // deleting a Permission deletes associated RolePermission
+
+ // audit
+ builder.Property(rolePermission => rolePermission.CreatedOnUtc)
+ .IsRequired()
+ .HasColumnOrder(1);
+
+ builder.Property(rolePermission => rolePermission.CreatedBy)
+ .IsRequired()
+ .HasColumnOrder(2);
+
+ builder.Property(rolePermission => rolePermission.UpdatedOnUtc)
+ .HasDefaultValue(null)
+ .HasColumnOrder(3);
+
+ builder.Property(rolePermission => rolePermission.UpdatedBy)
+ .HasDefaultValue(null)
+ .HasColumnOrder(4);
+
+ // indexes
+ builder.HasIndex(userRole => new { userRole.RoleId, userRole.PermissionId })
+ .IsUnique();
+ }
+}
diff --git a/src/Lumina.DataAccess/Common/Configuration/UserConfiguration.cs b/src/Lumina.DataAccess/Common/Configuration/UserConfiguration.cs
index 3cc33a73..038dd94a 100644
--- a/src/Lumina.DataAccess/Common/Configuration/UserConfiguration.cs
+++ b/src/Lumina.DataAccess/Common/Configuration/UserConfiguration.cs
@@ -33,24 +33,53 @@ public class UserConfiguration : IEntityTypeConfiguration
.HasColumnOrder(2);
builder.Property(user => user.TempPassword)
+ .HasDefaultValue(null)
.HasColumnOrder(3);
builder.Property(user => user.TotpSecret)
+ .HasDefaultValue(null)
.HasColumnOrder(4);
- builder.Property(user => user.VerificationToken)
+ builder.Property(user => user.TempPasswordCreated)
+ .HasDefaultValue(null)
.HasColumnOrder(5);
- builder.Property(user => user.VerificationTokenCreated)
- .HasColumnOrder(6);
-
- builder.Property(user => user.TempPasswordCreated)
- .HasColumnOrder(7);
-
+ // relationships
builder.HasMany(user => user.Libraries)
.WithOne(library => library.User)
.HasForeignKey(library => library.UserId)
.IsRequired() // a library must have a user
.OnDelete(DeleteBehavior.Cascade);
+
+ builder.HasMany(user => user.UserRoles)
+ .WithOne(userRole => userRole.User)
+ .HasForeignKey(userRole => userRole.UserId)
+ .OnDelete(DeleteBehavior.Cascade);
+
+ builder.HasMany(user => user.UserPermissions)
+ .WithOne(userPermission => userPermission.User)
+ .HasForeignKey(userPermission => userPermission.UserId)
+ .OnDelete(DeleteBehavior.Cascade);
+
+ // audit
+ builder.Property(user => user.CreatedOnUtc)
+ .IsRequired()
+ .HasColumnOrder(6);
+
+ builder.Property(user => user.CreatedBy)
+ .IsRequired()
+ .HasColumnOrder(7);
+
+ builder.Property(user => user.UpdatedOnUtc)
+ .HasDefaultValue(null)
+ .HasColumnOrder(8);
+
+ builder.Property(user => user.UpdatedBy)
+ .HasDefaultValue(null)
+ .HasColumnOrder(9);
+
+ // indexes
+ builder.HasIndex(user => user.Username)
+ .IsUnique(); // queried frequently
}
}
diff --git a/src/Lumina.DataAccess/Common/Configuration/UserPermissionConfiguration.cs b/src/Lumina.DataAccess/Common/Configuration/UserPermissionConfiguration.cs
new file mode 100644
index 00000000..32fc2894
--- /dev/null
+++ b/src/Lumina.DataAccess/Common/Configuration/UserPermissionConfiguration.cs
@@ -0,0 +1,60 @@
+#region ========================================================================= USING =====================================================================================
+using Lumina.Application.Common.DataAccess.Entities.Authorization;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Metadata.Builders;
+#endregion
+
+namespace Lumina.DataAccess.Common.Configuration;
+
+///
+/// Configures the entity mapping for the entity.
+///
+public class UserPermissionConfiguration : IEntityTypeConfiguration
+{
+ ///
+ /// Configures the entity.
+ ///
+ /// The builder to be used to configure the entity.
+ public void Configure(EntityTypeBuilder builder)
+ {
+ builder.ToTable("UserPermissions");
+
+ // composite primary key
+ builder.HasKey(userPermission => new { userPermission.UserId, userPermission.PermissionId });
+
+ // foreign key: User
+ builder.HasOne(userPermission => userPermission.User)
+ .WithMany(user => user.UserPermissions)
+ .HasForeignKey(userPermission => userPermission.UserId)
+ .IsRequired()
+ .OnDelete(DeleteBehavior.Cascade); // deleting a User deletes associated UserPermissions
+
+ // foreign key: Permission
+ builder.HasOne(userPermission => userPermission.Permission)
+ .WithMany(role => role.UserPermissions)
+ .HasForeignKey(userPermission => userPermission.PermissionId)
+ .IsRequired()
+ .OnDelete(DeleteBehavior.Cascade); // deleting a Permission deletes associated UserPermissions
+
+ // audit
+ builder.Property(userPermission => userPermission.CreatedOnUtc)
+ .IsRequired()
+ .HasColumnOrder(1);
+
+ builder.Property(userPermission => userPermission.CreatedBy)
+ .IsRequired()
+ .HasColumnOrder(2);
+
+ builder.Property(userPermission => userPermission.UpdatedOnUtc)
+ .HasDefaultValue(null)
+ .HasColumnOrder(3);
+
+ builder.Property(userPermission => userPermission.UpdatedBy)
+ .HasDefaultValue(null)
+ .HasColumnOrder(4);
+
+ // indexes
+ builder.HasIndex(userRole => new { userRole.UserId, userRole.PermissionId })
+ .IsUnique();
+ }
+}
diff --git a/src/Lumina.DataAccess/Common/Configuration/UserRoleConfiguration.cs b/src/Lumina.DataAccess/Common/Configuration/UserRoleConfiguration.cs
new file mode 100644
index 00000000..30c04630
--- /dev/null
+++ b/src/Lumina.DataAccess/Common/Configuration/UserRoleConfiguration.cs
@@ -0,0 +1,60 @@
+#region ========================================================================= USING =====================================================================================
+using Lumina.Application.Common.DataAccess.Entities.Authorization;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Metadata.Builders;
+#endregion
+
+namespace Lumina.DataAccess.Common.Configuration;
+
+///
+/// Configures the entity mapping for the entity.
+///
+public class UserRoleConfiguration : IEntityTypeConfiguration
+{
+ ///
+ /// Configures the entity.
+ ///
+ /// The builder to be used to configure the entity.
+ public void Configure(EntityTypeBuilder builder)
+ {
+ builder.ToTable("UserRoles");
+
+ // composite primary key
+ builder.HasKey(userRole => new { userRole.UserId, userRole.RoleId });
+
+ // foreign key: User
+ builder.HasOne(userRole => userRole.User)
+ .WithMany(user => user.UserRoles)
+ .HasForeignKey(userRole => userRole.UserId)
+ .IsRequired()
+ .OnDelete(DeleteBehavior.Cascade); // deleting a User deletes associated UserRoles
+
+ // foreign key: Role
+ builder.HasOne(userRole => userRole.Role)
+ .WithMany(role => role.UserRoles)
+ .HasForeignKey(userRole => userRole.RoleId)
+ .IsRequired()
+ .OnDelete(DeleteBehavior.Cascade); // deleting a Role deletes associated UserRoles
+
+ // audit
+ builder.Property(userRole => userRole.CreatedOnUtc)
+ .IsRequired()
+ .HasColumnOrder(1);
+
+ builder.Property(userRole => userRole.CreatedBy)
+ .IsRequired()
+ .HasColumnOrder(2);
+
+ builder.Property(userRole => userRole.UpdatedOnUtc)
+ .HasDefaultValue(null)
+ .HasColumnOrder(3);
+
+ builder.Property(userRole => userRole.UpdatedBy)
+ .HasDefaultValue(null)
+ .HasColumnOrder(4);
+
+ // indexes
+ builder.HasIndex(userRole => new { userRole.UserId, userRole.RoleId })
+ .IsUnique();
+ }
+}
diff --git a/src/Lumina.DataAccess/Common/DependencyInjection/DataAccessLayerServices.cs b/src/Lumina.DataAccess/Common/DependencyInjection/DataAccessLayerServices.cs
index 53050eeb..17d8082a 100644
--- a/src/Lumina.DataAccess/Common/DependencyInjection/DataAccessLayerServices.cs
+++ b/src/Lumina.DataAccess/Common/DependencyInjection/DataAccessLayerServices.cs
@@ -1,6 +1,7 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Repositories.Common.Base;
using Lumina.Application.Common.DataAccess.UoW;
+using Lumina.DataAccess.Common.Interceptors;
using Lumina.DataAccess.Core.Repositories.Common.Factory;
using Lumina.DataAccess.Core.UoW;
using Microsoft.EntityFrameworkCore;
@@ -31,11 +32,13 @@ public static class DataAccessLayerServices
string? basePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (!Directory.Exists(basePath))
throw new DirectoryNotFoundException($"The base path '{basePath}' does not exist.");
- services.AddDbContext(options =>
+ services.AddDbContext((serviceProvider, options) =>
{
options.UseSqlite($"Data Source={Path.Combine(basePath, "Lumina.db")}");
+ options.AddInterceptors(serviceProvider.GetRequiredService());
});
services.AddScoped();
+ services.AddScoped();
Type[]? dataAccessLayerTypes = Assembly.GetExecutingAssembly().GetTypes();
Type? genericRepositoryType = typeof(IRepository<>);
@@ -45,16 +48,16 @@ public static class DataAccessLayerServices
services.AddScoped(iRepositoryFactoryType!, repositoryFactoryType!);
// get all classes implementing IRepository (all repository classes) and register them as their corresponding repository interface
- IEnumerable repositoryTypes = dataAccessLayerTypes.Where(t => !t.IsInterface &&
- t.GetInterfaces()
- .Any(i => i.IsGenericType &&
- i.GetGenericTypeDefinition() == genericRepositoryType));
+ IEnumerable repositoryTypes = dataAccessLayerTypes.Where(type => !type.IsInterface &&
+ type.GetInterfaces()
+ .Any(type => type.IsGenericType &&
+ type.GetGenericTypeDefinition() == genericRepositoryType));
foreach (Type type in repositoryTypes)
{
services.AddScoped(type.GetInterfaces() // TODO: change to scoped when DbContext is implemented
- .Where(i => !i.IsGenericType &&
- i.GetInterfaces()
- .Any(j => j.GetGenericTypeDefinition() == genericRepositoryType))
+ .Where(type => !type.IsGenericType &&
+ type.GetInterfaces()
+ .Any(type => type.GetGenericTypeDefinition() == genericRepositoryType))
.First(), type);
}
}
diff --git a/src/Lumina.DataAccess/Common/Interceptors/UpdateAuditableEntitiesInterceptor.cs b/src/Lumina.DataAccess/Common/Interceptors/UpdateAuditableEntitiesInterceptor.cs
new file mode 100644
index 00000000..be086ab6
--- /dev/null
+++ b/src/Lumina.DataAccess/Common/Interceptors/UpdateAuditableEntitiesInterceptor.cs
@@ -0,0 +1,111 @@
+#region ========================================================================= USING =====================================================================================
+using Lumina.Application.Common.DataAccess.Entities.Common;
+using Lumina.Application.Common.Infrastructure.Authentication;
+using Lumina.Application.Common.Infrastructure.Time;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.ChangeTracking;
+using Microsoft.EntityFrameworkCore.Diagnostics;
+using System;
+using System.Threading;
+using System.Threading.Tasks;
+#endregion
+
+namespace Lumina.DataAccess.Common.Interceptors;
+
+///
+/// Interceptor for updating audit fields automatically for entities implementing .
+///
+public sealed class UpdateAuditableEntitiesInterceptor : SaveChangesInterceptor
+{
+ private readonly ICurrentUserService _currentUserService;
+ private readonly IDateTimeProvider _dateTimeProvider;
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// Injected service to retrieve the current user information.
+ /// Injected service for time related functionality.
+ public UpdateAuditableEntitiesInterceptor(ICurrentUserService currentUserService, IDateTimeProvider dateTimeProvider)
+ {
+ _currentUserService = currentUserService;
+ _dateTimeProvider = dateTimeProvider;
+ }
+
+ ///
+ /// Intercepts the synchronous SaveChanges operation to update audit fields.
+ ///
+ /// The event data containing the .
+ /// The result of the SaveChanges operation.
+ /// The interception result.
+ public override InterceptionResult SavingChanges(DbContextEventData eventData, InterceptionResult result)
+ {
+ UpdateAuditableEntities(eventData.Context);
+ return base.SavingChanges(eventData, result);
+ }
+
+ ///
+ /// Intercepts the asynchronous SaveChangesAsync operation to update audit fields.
+ ///
+ /// The event data containing the .
+ /// The result of the SaveChanges operation.
+ /// Cancellation token that can be used to stop the execution.
+ /// A containing the interception result.
+ public override ValueTask> SavingChangesAsync(DbContextEventData eventData, InterceptionResult result, CancellationToken cancellationToken = default)
+ {
+ UpdateAuditableEntities(eventData.Context);
+ return base.SavingChangesAsync(eventData, result, cancellationToken);
+ }
+
+ ///
+ /// Updates the audit fields for all changed entities that implement IAuditableEntity.
+ ///
+ /// The containing the entities to update.
+ private void UpdateAuditableEntities(DbContext? context)
+ {
+ if (context is null)
+ return;
+
+ Guid? userId = _currentUserService.UserId;
+ DateTime utcNow = _dateTimeProvider.UtcNow;
+
+ // retrieve all entities implementing IAuditableEntity that are being tracked and have either Added or Modified state
+ foreach (EntityEntry entry in context.ChangeTracker.Entries())
+ {
+ if (entry.State == EntityState.Added)
+ SetCreationProperties(entry.Entity, userId, utcNow);
+ else if(entry.State == EntityState.Modified)
+ SetModificationProperties(entry, userId, utcNow);
+ }
+ }
+
+ ///
+ /// Sets the creation audit properties for a newly added entity.
+ ///
+ /// The entity being created.
+ /// The Id of the user creating the entity.
+ /// The current UTC timestamp.
+ private static void SetCreationProperties(IAuditableEntity entity, Guid? userId, DateTime utcNow)
+ {
+ entity.CreatedOnUtc = utcNow;
+ entity.CreatedBy = userId ?? default;
+ entity.UpdatedOnUtc = null;
+ entity.UpdatedBy = null;
+ }
+
+ ///
+ /// Sets the modification audit properties for an updated entity while protecting creation properties.
+ ///
+ /// The entity entry being modified.
+ /// The Id of the user modifying the entity.
+ /// The current UTC timestamp.
+ private static void SetModificationProperties(EntityEntry entry, Guid? userId, DateTime utcNow)
+ {
+ // prevent modification of creation properties
+ entry.Property(auditableEntity => auditableEntity.CreatedOnUtc).IsModified = false;
+ entry.Property(auditableEntity => auditableEntity.CreatedBy).IsModified = false;
+
+ // set modification properties
+ entry.Entity.UpdatedOnUtc = utcNow;
+ entry.Entity.UpdatedBy = userId;
+ }
+}
diff --git a/src/Lumina.DataAccess/Common/Migrations/20241122152616_AddRolesAndPermissions.Designer.cs b/src/Lumina.DataAccess/Common/Migrations/20241122152616_AddRolesAndPermissions.Designer.cs
new file mode 100644
index 00000000..02cd7b75
--- /dev/null
+++ b/src/Lumina.DataAccess/Common/Migrations/20241122152616_AddRolesAndPermissions.Designer.cs
@@ -0,0 +1,706 @@
+//
+using System;
+using Lumina.DataAccess.Core.UoW;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Infrastructure;
+using Microsoft.EntityFrameworkCore.Migrations;
+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
+
+#nullable disable
+
+namespace Lumina.DataAccess.Common.Migrations
+{
+ [DbContext(typeof(LuminaDbContext))]
+ [Migration("20241122152616_AddRolesAndPermissions")]
+ partial class AddRolesAndPermissions
+ {
+ ///
+ protected override void BuildTargetModel(ModelBuilder modelBuilder)
+ {
+#pragma warning disable 612, 618
+ modelBuilder.HasAnnotation("ProductVersion", "9.0.0");
+
+ modelBuilder.Entity("BookGenres", b =>
+ {
+ b.Property("BookId")
+ .HasColumnType("TEXT");
+
+ b.Property("GenreId")
+ .HasColumnType("TEXT");
+
+ b.HasKey("BookId", "GenreId");
+
+ b.HasIndex("GenreId");
+
+ b.ToTable("BookGenres", (string)null);
+ });
+
+ modelBuilder.Entity("BookTags", b =>
+ {
+ b.Property("BookId")
+ .HasColumnType("TEXT");
+
+ b.Property("TagId")
+ .HasColumnType("TEXT");
+
+ b.HasKey("BookId", "TagId");
+
+ b.HasIndex("TagId");
+
+ b.ToTable("BookTags", (string)null);
+ });
+
+ modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.Authorization.PermissionEntity", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("TEXT")
+ .HasColumnOrder(0);
+
+ b.Property("CreatedBy")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(3);
+
+ b.Property("CreatedOnUtc")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(2);
+
+ b.Property("PermissionName")
+ .IsRequired()
+ .HasMaxLength(100)
+ .HasColumnType("TEXT")
+ .HasColumnOrder(1);
+
+ b.Property("UpdatedBy")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(5);
+
+ b.Property("UpdatedOnUtc")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(4);
+
+ b.HasKey("Id");
+
+ b.ToTable("Permissions", (string)null);
+ });
+
+ modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.Authorization.RoleEntity", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("TEXT")
+ .HasColumnOrder(0);
+
+ b.Property("CreatedBy")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(3);
+
+ b.Property("CreatedOnUtc")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(2);
+
+ b.Property("RoleName")
+ .IsRequired()
+ .HasMaxLength(100)
+ .HasColumnType("TEXT")
+ .HasColumnOrder(1);
+
+ b.Property("UpdatedBy")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(5);
+
+ b.Property("UpdatedOnUtc")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(4);
+
+ b.HasKey("Id");
+
+ b.ToTable("Roles", (string)null);
+ });
+
+ modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.Authorization.RolePermissionEntity", b =>
+ {
+ b.Property("RoleId")
+ .HasColumnType("TEXT");
+
+ b.Property("PermissionId")
+ .HasColumnType("TEXT");
+
+ b.Property("CreatedBy")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(2);
+
+ b.Property("CreatedOnUtc")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(1);
+
+ b.Property("Id")
+ .HasColumnType("TEXT");
+
+ b.Property("UpdatedBy")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(4);
+
+ b.Property("UpdatedOnUtc")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(3);
+
+ b.HasKey("RoleId", "PermissionId");
+
+ b.HasIndex("PermissionId");
+
+ b.HasIndex("RoleId", "PermissionId")
+ .IsUnique();
+
+ b.ToTable("RolePermissions", (string)null);
+ });
+
+ modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.Authorization.UserPermissionEntity", b =>
+ {
+ b.Property("UserId")
+ .HasColumnType("TEXT");
+
+ b.Property("PermissionId")
+ .HasColumnType("TEXT");
+
+ b.Property("CreatedBy")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(2);
+
+ b.Property("CreatedOnUtc")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(1);
+
+ b.Property("Id")
+ .HasColumnType("TEXT");
+
+ b.Property("UpdatedBy")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(4);
+
+ b.Property("UpdatedOnUtc")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(3);
+
+ b.HasKey("UserId", "PermissionId");
+
+ b.HasIndex("PermissionId");
+
+ b.HasIndex("UserId", "PermissionId")
+ .IsUnique();
+
+ b.ToTable("UserPermissions", (string)null);
+ });
+
+ modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.Authorization.UserRoleEntity", b =>
+ {
+ b.Property("UserId")
+ .HasColumnType("TEXT");
+
+ b.Property("RoleId")
+ .HasColumnType("TEXT");
+
+ b.Property("CreatedBy")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(2);
+
+ b.Property("CreatedOnUtc")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(1);
+
+ b.Property("Id")
+ .HasColumnType("TEXT");
+
+ b.Property("UpdatedBy")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(4);
+
+ b.Property("UpdatedOnUtc")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(3);
+
+ b.HasKey("UserId", "RoleId");
+
+ b.HasIndex("RoleId");
+
+ b.HasIndex("UserId", "RoleId")
+ .IsUnique();
+
+ b.ToTable("UserRoles", (string)null);
+ });
+
+ modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.Common.GenreEntity", b =>
+ {
+ b.Property("Name")
+ .HasMaxLength(50)
+ .HasColumnType("TEXT");
+
+ b.HasKey("Name");
+
+ b.ToTable("Genres", (string)null);
+ });
+
+ modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.Common.TagEntity", b =>
+ {
+ b.Property("Name")
+ .HasMaxLength(50)
+ .HasColumnType("TEXT");
+
+ b.HasKey("Name");
+
+ b.ToTable("Tags", (string)null);
+ });
+
+ modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management.LibraryEntity", b =>
+ {
+ b.Property("Id")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(0);
+
+ b.Property("Created")
+ .HasColumnType("TEXT");
+
+ b.Property("LibraryType")
+ .IsRequired()
+ .HasColumnType("TEXT")
+ .HasColumnOrder(2);
+
+ b.Property("Title")
+ .IsRequired()
+ .HasMaxLength(255)
+ .HasColumnType("TEXT")
+ .HasColumnOrder(1);
+
+ b.Property("Updated")
+ .HasColumnType("TEXT");
+
+ b.Property("UserId")
+ .HasColumnType("TEXT");
+
+ b.HasKey("Id");
+
+ b.HasIndex("UserId");
+
+ b.ToTable("Libraries", (string)null);
+ });
+
+ modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary.BookEntity", b =>
+ {
+ b.Property("Id")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(0);
+
+ b.Property("ASIN")
+ .HasMaxLength(10)
+ .HasColumnType("TEXT")
+ .HasColumnOrder(21);
+
+ b.Property("AppleBooksId")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(29);
+
+ b.Property("BarnesAndNobleId")
+ .HasMaxLength(10)
+ .HasColumnType("TEXT")
+ .HasColumnOrder(28);
+
+ b.Property("Created")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(30);
+
+ b.Property("Description")
+ .HasMaxLength(2000)
+ .HasColumnType("TEXT")
+ .HasColumnOrder(3);
+
+ b.Property("Edition")
+ .HasMaxLength(50)
+ .HasColumnType("TEXT")
+ .HasColumnOrder(19);
+
+ b.Property("Format")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(18);
+
+ b.Property("GoodreadsId")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(22);
+
+ b.Property("GoogleBooksId")
+ .HasMaxLength(12)
+ .HasColumnType("TEXT")
+ .HasColumnOrder(27);
+
+ b.Property("LCCN")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(23);
+
+ b.Property("LanguageCode")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(10);
+
+ b.Property("LanguageName")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(11);
+
+ b.Property("LanguageNativeName")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(12);
+
+ b.Property("LibraryThingId")
+ .HasMaxLength(50)
+ .HasColumnType("TEXT")
+ .HasColumnOrder(26);
+
+ b.Property("OCLCNumber")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(24);
+
+ b.Property("OpenLibraryId")
+ .HasMaxLength(50)
+ .HasColumnType("TEXT")
+ .HasColumnOrder(25);
+
+ b.Property("OriginalLanguageCode")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(13);
+
+ b.Property("OriginalLanguageName")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(14);
+
+ b.Property("OriginalLanguageNativeName")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(15);
+
+ b.Property("OriginalReleaseDate")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(4);
+
+ b.Property("OriginalReleaseYear")
+ .HasColumnType("INTEGER")
+ .HasColumnOrder(5);
+
+ b.Property("OriginalTitle")
+ .HasMaxLength(255)
+ .HasColumnType("TEXT")
+ .HasColumnOrder(2);
+
+ b.Property("PageCount")
+ .HasColumnType("INTEGER")
+ .HasColumnOrder(17);
+
+ b.Property("Publisher")
+ .HasMaxLength(100)
+ .HasColumnType("TEXT")
+ .HasColumnOrder(16);
+
+ b.Property("ReReleaseDate")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(6);
+
+ b.Property("ReReleaseYear")
+ .HasColumnType("INTEGER")
+ .HasColumnOrder(7);
+
+ b.Property("ReleaseCountry")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(8);
+
+ b.Property("ReleaseVersion")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(9);
+
+ b.Property("Title")
+ .IsRequired()
+ .HasMaxLength(255)
+ .HasColumnType("TEXT")
+ .HasColumnOrder(1);
+
+ b.Property("Updated")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(31);
+
+ b.Property("VolumeNumber")
+ .HasColumnType("INTEGER")
+ .HasColumnOrder(20);
+
+ b.HasKey("Id");
+
+ b.ToTable("Books", (string)null);
+ });
+
+ modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.UsersManagement.UserEntity", b =>
+ {
+ b.Property("Id")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(0);
+
+ b.Property("CreatedBy")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(7);
+
+ b.Property("CreatedOnUtc")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(6);
+
+ b.Property("Password")
+ .IsRequired()
+ .HasColumnType("TEXT")
+ .HasColumnOrder(2);
+
+ b.Property("TempPassword")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(3);
+
+ b.Property("TempPasswordCreated")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(5);
+
+ b.Property("TotpSecret")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(4);
+
+ b.Property("UpdatedBy")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(9);
+
+ b.Property("UpdatedOnUtc")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(8);
+
+ b.Property("Username")
+ .IsRequired()
+ .HasMaxLength(255)
+ .HasColumnType("TEXT")
+ .HasColumnOrder(1);
+
+ b.HasKey("Id");
+
+ b.HasIndex("Username")
+ .IsUnique();
+
+ b.ToTable("Users", (string)null);
+ });
+
+ modelBuilder.Entity("BookGenres", b =>
+ {
+ b.HasOne("Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary.BookEntity", null)
+ .WithMany()
+ .HasForeignKey("BookId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("Lumina.Application.Common.DataAccess.Entities.Common.GenreEntity", null)
+ .WithMany()
+ .HasForeignKey("GenreId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("BookTags", b =>
+ {
+ b.HasOne("Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary.BookEntity", null)
+ .WithMany()
+ .HasForeignKey("BookId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("Lumina.Application.Common.DataAccess.Entities.Common.TagEntity", null)
+ .WithMany()
+ .HasForeignKey("TagId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.Authorization.RolePermissionEntity", b =>
+ {
+ b.HasOne("Lumina.Application.Common.DataAccess.Entities.Authorization.PermissionEntity", "Permission")
+ .WithMany("RolePermissions")
+ .HasForeignKey("PermissionId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("Lumina.Application.Common.DataAccess.Entities.Authorization.RoleEntity", "Role")
+ .WithMany("RolePermissions")
+ .HasForeignKey("RoleId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.Navigation("Permission");
+
+ b.Navigation("Role");
+ });
+
+ modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.Authorization.UserPermissionEntity", b =>
+ {
+ b.HasOne("Lumina.Application.Common.DataAccess.Entities.Authorization.PermissionEntity", "Permission")
+ .WithMany("UserPermissions")
+ .HasForeignKey("PermissionId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("Lumina.Application.Common.DataAccess.Entities.UsersManagement.UserEntity", "User")
+ .WithMany("UserPermissions")
+ .HasForeignKey("UserId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.Navigation("Permission");
+
+ b.Navigation("User");
+ });
+
+ modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.Authorization.UserRoleEntity", b =>
+ {
+ b.HasOne("Lumina.Application.Common.DataAccess.Entities.Authorization.RoleEntity", "Role")
+ .WithMany("UserRoles")
+ .HasForeignKey("RoleId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("Lumina.Application.Common.DataAccess.Entities.UsersManagement.UserEntity", "User")
+ .WithMany("UserRoles")
+ .HasForeignKey("UserId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.Navigation("Role");
+
+ b.Navigation("User");
+ });
+
+ modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management.LibraryEntity", b =>
+ {
+ b.HasOne("Lumina.Application.Common.DataAccess.Entities.UsersManagement.UserEntity", "User")
+ .WithMany("Libraries")
+ .HasForeignKey("UserId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.OwnsMany("Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management.LibraryContentLocationEntity", "ContentLocations", b1 =>
+ {
+ b1.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("TEXT");
+
+ b1.Property("LibraryId")
+ .HasColumnType("TEXT");
+
+ b1.Property("Path")
+ .IsRequired()
+ .HasMaxLength(260)
+ .HasColumnType("TEXT")
+ .HasColumnName("Path");
+
+ b1.HasKey("Id");
+
+ b1.HasIndex("LibraryId");
+
+ b1.ToTable("LibraryContentLocations", (string)null);
+
+ b1.WithOwner()
+ .HasForeignKey("LibraryId");
+ });
+
+ b.Navigation("ContentLocations");
+
+ b.Navigation("User");
+ });
+
+ modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary.BookEntity", b =>
+ {
+ b.OwnsMany("Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary.BookRatingEntity", "Ratings", b1 =>
+ {
+ b1.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("TEXT");
+
+ b1.Property("BookId")
+ .HasColumnType("TEXT");
+
+ b1.Property("MaxValue")
+ .HasColumnType("decimal(3,2)");
+
+ b1.Property("Source")
+ .IsRequired()
+ .HasMaxLength(50)
+ .HasColumnType("TEXT");
+
+ b1.Property("Value")
+ .HasColumnType("decimal(3,2)");
+
+ b1.Property("VoteCount")
+ .HasColumnType("INTEGER");
+
+ b1.HasKey("Id");
+
+ b1.HasIndex("BookId");
+
+ b1.ToTable("BookRatings", (string)null);
+
+ b1.WithOwner()
+ .HasForeignKey("BookId");
+ });
+
+ b.OwnsMany("Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary.IsbnEntity", "ISBNs", b1 =>
+ {
+ b1.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("TEXT");
+
+ b1.Property("BookId")
+ .HasColumnType("TEXT");
+
+ b1.Property("Format")
+ .IsRequired()
+ .HasMaxLength(6)
+ .HasColumnType("TEXT");
+
+ b1.Property("Value")
+ .IsRequired()
+ .HasMaxLength(13)
+ .HasColumnType("TEXT")
+ .HasColumnName("ISBN");
+
+ b1.HasKey("Id");
+
+ b1.HasIndex("BookId");
+
+ b1.ToTable("BookISBNs", (string)null);
+
+ b1.WithOwner()
+ .HasForeignKey("BookId");
+ });
+
+ b.Navigation("ISBNs");
+
+ b.Navigation("Ratings");
+ });
+
+ modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.Authorization.PermissionEntity", b =>
+ {
+ b.Navigation("RolePermissions");
+
+ b.Navigation("UserPermissions");
+ });
+
+ modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.Authorization.RoleEntity", b =>
+ {
+ b.Navigation("RolePermissions");
+
+ b.Navigation("UserRoles");
+ });
+
+ modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.UsersManagement.UserEntity", b =>
+ {
+ b.Navigation("Libraries");
+
+ b.Navigation("UserPermissions");
+
+ b.Navigation("UserRoles");
+ });
+#pragma warning restore 612, 618
+ }
+ }
+}
diff --git a/src/Lumina.DataAccess/Common/Migrations/20241122152616_AddRolesAndPermissions.cs b/src/Lumina.DataAccess/Common/Migrations/20241122152616_AddRolesAndPermissions.cs
new file mode 100644
index 00000000..7d402296
--- /dev/null
+++ b/src/Lumina.DataAccess/Common/Migrations/20241122152616_AddRolesAndPermissions.cs
@@ -0,0 +1,331 @@
+using System;
+using Microsoft.EntityFrameworkCore.Migrations;
+
+#nullable disable
+
+namespace Lumina.DataAccess.Common.Migrations;
+
+///
+public partial class AddRolesAndPermissions : Migration
+{
+ ///
+ protected override void Up(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.DropColumn(
+ name: "Updated",
+ table: "Users");
+
+ migrationBuilder.RenameColumn(
+ name: "VerificationTokenCreated",
+ table: "Users",
+ newName: "UpdatedOnUtc");
+
+ migrationBuilder.RenameColumn(
+ name: "VerificationToken",
+ table: "Users",
+ newName: "UpdatedBy");
+
+ migrationBuilder.RenameColumn(
+ name: "Created",
+ table: "Users",
+ newName: "CreatedOnUtc");
+
+ migrationBuilder.AlterColumn(
+ name: "TempPasswordCreated",
+ table: "Users",
+ type: "TEXT",
+ nullable: true,
+ oldClrType: typeof(DateTime),
+ oldType: "TEXT",
+ oldNullable: true)
+ .Annotation("Relational:ColumnOrder", 5)
+ .OldAnnotation("Relational:ColumnOrder", 7);
+
+ migrationBuilder.AlterColumn(
+ name: "UpdatedOnUtc",
+ table: "Users",
+ type: "TEXT",
+ nullable: true,
+ oldClrType: typeof(DateTime),
+ oldType: "TEXT",
+ oldNullable: true)
+ .Annotation("Relational:ColumnOrder", 8)
+ .OldAnnotation("Relational:ColumnOrder", 6);
+
+ migrationBuilder.AlterColumn(
+ name: "UpdatedBy",
+ table: "Users",
+ type: "TEXT",
+ nullable: true,
+ oldClrType: typeof(string),
+ oldType: "TEXT",
+ oldNullable: true)
+ .Annotation("Relational:ColumnOrder", 9)
+ .OldAnnotation("Relational:ColumnOrder", 5);
+
+ migrationBuilder.AlterColumn(
+ name: "CreatedOnUtc",
+ table: "Users",
+ type: "TEXT",
+ nullable: false,
+ oldClrType: typeof(DateTime),
+ oldType: "TEXT")
+ .Annotation("Relational:ColumnOrder", 6);
+
+ migrationBuilder.AddColumn(
+ name: "CreatedBy",
+ table: "Users",
+ type: "TEXT",
+ nullable: false,
+ defaultValue: new Guid("00000000-0000-0000-0000-000000000000"))
+ .Annotation("Relational:ColumnOrder", 7);
+
+ migrationBuilder.CreateTable(
+ name: "Permissions",
+ columns: table => new
+ {
+ Id = table.Column(type: "TEXT", nullable: false),
+ PermissionName = table.Column(type: "TEXT", maxLength: 100, nullable: false),
+ CreatedOnUtc = table.Column(type: "TEXT", nullable: false),
+ CreatedBy = table.Column(type: "TEXT", nullable: false),
+ UpdatedOnUtc = table.Column(type: "TEXT", nullable: true),
+ UpdatedBy = table.Column(type: "TEXT", nullable: true)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_Permissions", x => x.Id);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "Roles",
+ columns: table => new
+ {
+ Id = table.Column(type: "TEXT", nullable: false),
+ RoleName = table.Column(type: "TEXT", maxLength: 100, nullable: false),
+ CreatedOnUtc = table.Column(type: "TEXT", nullable: false),
+ CreatedBy = table.Column(type: "TEXT", nullable: false),
+ UpdatedOnUtc = table.Column(type: "TEXT", nullable: true),
+ UpdatedBy = table.Column(type: "TEXT", nullable: true)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_Roles", x => x.Id);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "UserPermissions",
+ columns: table => new
+ {
+ CreatedOnUtc = table.Column(type: "TEXT", nullable: false),
+ CreatedBy = table.Column(type: "TEXT", nullable: false),
+ UpdatedOnUtc = table.Column(type: "TEXT", nullable: true),
+ UpdatedBy = table.Column(type: "TEXT", nullable: true),
+ UserId = table.Column(type: "TEXT", nullable: false),
+ PermissionId = table.Column(type: "TEXT", nullable: false),
+ Id = table.Column(type: "TEXT", nullable: false)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_UserPermissions", x => new { x.UserId, x.PermissionId });
+ table.ForeignKey(
+ name: "FK_UserPermissions_Permissions_PermissionId",
+ column: x => x.PermissionId,
+ principalTable: "Permissions",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ table.ForeignKey(
+ name: "FK_UserPermissions_Users_UserId",
+ column: x => x.UserId,
+ principalTable: "Users",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "RolePermissions",
+ columns: table => new
+ {
+ CreatedOnUtc = table.Column(type: "TEXT", nullable: false),
+ CreatedBy = table.Column(type: "TEXT", nullable: false),
+ UpdatedOnUtc = table.Column(type: "TEXT", nullable: true),
+ UpdatedBy = table.Column(type: "TEXT", nullable: true),
+ RoleId = table.Column(type: "TEXT", nullable: false),
+ PermissionId = table.Column(type: "TEXT", nullable: false),
+ Id = table.Column(type: "TEXT", nullable: false)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_RolePermissions", x => new { x.RoleId, x.PermissionId });
+ table.ForeignKey(
+ name: "FK_RolePermissions_Permissions_PermissionId",
+ column: x => x.PermissionId,
+ principalTable: "Permissions",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ table.ForeignKey(
+ name: "FK_RolePermissions_Roles_RoleId",
+ column: x => x.RoleId,
+ principalTable: "Roles",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "UserRoles",
+ columns: table => new
+ {
+ CreatedOnUtc = table.Column(type: "TEXT", nullable: false),
+ CreatedBy = table.Column(type: "TEXT", nullable: false),
+ UpdatedOnUtc = table.Column(type: "TEXT", nullable: true),
+ UpdatedBy = table.Column(type: "TEXT", nullable: true),
+ UserId = table.Column(type: "TEXT", nullable: false),
+ RoleId = table.Column(type: "TEXT", nullable: false),
+ Id = table.Column(type: "TEXT", nullable: false)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_UserRoles", x => new { x.UserId, x.RoleId });
+ table.ForeignKey(
+ name: "FK_UserRoles_Roles_RoleId",
+ column: x => x.RoleId,
+ principalTable: "Roles",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ table.ForeignKey(
+ name: "FK_UserRoles_Users_UserId",
+ column: x => x.UserId,
+ principalTable: "Users",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ });
+
+ migrationBuilder.CreateIndex(
+ name: "IX_Users_Username",
+ table: "Users",
+ column: "Username",
+ unique: true);
+
+ migrationBuilder.CreateIndex(
+ name: "IX_RolePermissions_PermissionId",
+ table: "RolePermissions",
+ column: "PermissionId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_RolePermissions_RoleId_PermissionId",
+ table: "RolePermissions",
+ columns: new[] { "RoleId", "PermissionId" },
+ unique: true);
+
+ migrationBuilder.CreateIndex(
+ name: "IX_UserPermissions_PermissionId",
+ table: "UserPermissions",
+ column: "PermissionId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_UserPermissions_UserId_PermissionId",
+ table: "UserPermissions",
+ columns: new[] { "UserId", "PermissionId" },
+ unique: true);
+
+ migrationBuilder.CreateIndex(
+ name: "IX_UserRoles_RoleId",
+ table: "UserRoles",
+ column: "RoleId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_UserRoles_UserId_RoleId",
+ table: "UserRoles",
+ columns: new[] { "UserId", "RoleId" },
+ unique: true);
+ }
+
+ ///
+ protected override void Down(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.DropTable(
+ name: "RolePermissions");
+
+ migrationBuilder.DropTable(
+ name: "UserPermissions");
+
+ migrationBuilder.DropTable(
+ name: "UserRoles");
+
+ migrationBuilder.DropTable(
+ name: "Permissions");
+
+ migrationBuilder.DropTable(
+ name: "Roles");
+
+ migrationBuilder.DropIndex(
+ name: "IX_Users_Username",
+ table: "Users");
+
+ migrationBuilder.DropColumn(
+ name: "CreatedBy",
+ table: "Users");
+
+ migrationBuilder.RenameColumn(
+ name: "UpdatedOnUtc",
+ table: "Users",
+ newName: "VerificationTokenCreated");
+
+ migrationBuilder.RenameColumn(
+ name: "UpdatedBy",
+ table: "Users",
+ newName: "VerificationToken");
+
+ migrationBuilder.RenameColumn(
+ name: "CreatedOnUtc",
+ table: "Users",
+ newName: "Created");
+
+ migrationBuilder.AlterColumn(
+ name: "TempPasswordCreated",
+ table: "Users",
+ type: "TEXT",
+ nullable: true,
+ oldClrType: typeof(DateTime),
+ oldType: "TEXT",
+ oldNullable: true)
+ .Annotation("Relational:ColumnOrder", 7)
+ .OldAnnotation("Relational:ColumnOrder", 5);
+
+ migrationBuilder.AlterColumn(
+ name: "VerificationTokenCreated",
+ table: "Users",
+ type: "TEXT",
+ nullable: true,
+ oldClrType: typeof(DateTime),
+ oldType: "TEXT",
+ oldNullable: true)
+ .Annotation("Relational:ColumnOrder", 6)
+ .OldAnnotation("Relational:ColumnOrder", 8);
+
+ migrationBuilder.AlterColumn(
+ name: "VerificationToken",
+ table: "Users",
+ type: "TEXT",
+ nullable: true,
+ oldClrType: typeof(Guid),
+ oldType: "TEXT",
+ oldNullable: true)
+ .Annotation("Relational:ColumnOrder", 5)
+ .OldAnnotation("Relational:ColumnOrder", 9);
+
+ migrationBuilder.AlterColumn(
+ name: "Created",
+ table: "Users",
+ type: "TEXT",
+ nullable: false,
+ oldClrType: typeof(DateTime),
+ oldType: "TEXT")
+ .OldAnnotation("Relational:ColumnOrder", 6);
+
+ migrationBuilder.AddColumn(
+ name: "Updated",
+ table: "Users",
+ type: "TEXT",
+ nullable: true);
+ }
+}
diff --git a/src/Lumina.DataAccess/Common/Migrations/LuminaDbContextModelSnapshot.cs b/src/Lumina.DataAccess/Common/Migrations/LuminaDbContextModelSnapshot.cs
index 5c6ec318..e0c5dfe3 100644
--- a/src/Lumina.DataAccess/Common/Migrations/LuminaDbContextModelSnapshot.cs
+++ b/src/Lumina.DataAccess/Common/Migrations/LuminaDbContextModelSnapshot.cs
@@ -15,7 +15,7 @@ namespace Lumina.DataAccess.Common.Migrations
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
- modelBuilder.HasAnnotation("ProductVersion", "8.0.10");
+ modelBuilder.HasAnnotation("ProductVersion", "9.0.0");
modelBuilder.Entity("BookGenres", b =>
{
@@ -47,6 +47,185 @@ namespace Lumina.DataAccess.Common.Migrations
b.ToTable("BookTags", (string)null);
});
+ modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.Authorization.PermissionEntity", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("TEXT")
+ .HasColumnOrder(0);
+
+ b.Property("CreatedBy")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(3);
+
+ b.Property("CreatedOnUtc")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(2);
+
+ b.Property("PermissionName")
+ .IsRequired()
+ .HasMaxLength(100)
+ .HasColumnType("TEXT")
+ .HasColumnOrder(1);
+
+ b.Property("UpdatedBy")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(5);
+
+ b.Property("UpdatedOnUtc")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(4);
+
+ b.HasKey("Id");
+
+ b.ToTable("Permissions", (string)null);
+ });
+
+ modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.Authorization.RoleEntity", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("TEXT")
+ .HasColumnOrder(0);
+
+ b.Property("CreatedBy")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(3);
+
+ b.Property("CreatedOnUtc")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(2);
+
+ b.Property("RoleName")
+ .IsRequired()
+ .HasMaxLength(100)
+ .HasColumnType("TEXT")
+ .HasColumnOrder(1);
+
+ b.Property("UpdatedBy")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(5);
+
+ b.Property("UpdatedOnUtc")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(4);
+
+ b.HasKey("Id");
+
+ b.ToTable("Roles", (string)null);
+ });
+
+ modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.Authorization.RolePermissionEntity", b =>
+ {
+ b.Property("RoleId")
+ .HasColumnType("TEXT");
+
+ b.Property("PermissionId")
+ .HasColumnType("TEXT");
+
+ b.Property("CreatedBy")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(2);
+
+ b.Property("CreatedOnUtc")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(1);
+
+ b.Property("Id")
+ .HasColumnType("TEXT");
+
+ b.Property("UpdatedBy")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(4);
+
+ b.Property("UpdatedOnUtc")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(3);
+
+ b.HasKey("RoleId", "PermissionId");
+
+ b.HasIndex("PermissionId");
+
+ b.HasIndex("RoleId", "PermissionId")
+ .IsUnique();
+
+ b.ToTable("RolePermissions", (string)null);
+ });
+
+ modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.Authorization.UserPermissionEntity", b =>
+ {
+ b.Property("UserId")
+ .HasColumnType("TEXT");
+
+ b.Property("PermissionId")
+ .HasColumnType("TEXT");
+
+ b.Property("CreatedBy")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(2);
+
+ b.Property("CreatedOnUtc")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(1);
+
+ b.Property("Id")
+ .HasColumnType("TEXT");
+
+ b.Property("UpdatedBy")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(4);
+
+ b.Property("UpdatedOnUtc")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(3);
+
+ b.HasKey("UserId", "PermissionId");
+
+ b.HasIndex("PermissionId");
+
+ b.HasIndex("UserId", "PermissionId")
+ .IsUnique();
+
+ b.ToTable("UserPermissions", (string)null);
+ });
+
+ modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.Authorization.UserRoleEntity", b =>
+ {
+ b.Property("UserId")
+ .HasColumnType("TEXT");
+
+ b.Property("RoleId")
+ .HasColumnType("TEXT");
+
+ b.Property("CreatedBy")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(2);
+
+ b.Property("CreatedOnUtc")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(1);
+
+ b.Property("Id")
+ .HasColumnType("TEXT");
+
+ b.Property("UpdatedBy")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(4);
+
+ b.Property("UpdatedOnUtc")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(3);
+
+ b.HasKey("UserId", "RoleId");
+
+ b.HasIndex("RoleId");
+
+ b.HasIndex("UserId", "RoleId")
+ .IsUnique();
+
+ b.ToTable("UserRoles", (string)null);
+ });
+
modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.Common.GenreEntity", b =>
{
b.Property("Name")
@@ -254,8 +433,13 @@ namespace Lumina.DataAccess.Common.Migrations
.HasColumnType("TEXT")
.HasColumnOrder(0);
- b.Property("Created")
- .HasColumnType("TEXT");
+ b.Property("CreatedBy")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(7);
+
+ b.Property("CreatedOnUtc")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(6);
b.Property("Password")
.IsRequired()
@@ -268,14 +452,19 @@ namespace Lumina.DataAccess.Common.Migrations
b.Property("TempPasswordCreated")
.HasColumnType("TEXT")
- .HasColumnOrder(7);
+ .HasColumnOrder(5);
b.Property("TotpSecret")
.HasColumnType("TEXT")
.HasColumnOrder(4);
- b.Property("Updated")
- .HasColumnType("TEXT");
+ b.Property("UpdatedBy")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(9);
+
+ b.Property("UpdatedOnUtc")
+ .HasColumnType("TEXT")
+ .HasColumnOrder(8);
b.Property("Username")
.IsRequired()
@@ -283,16 +472,11 @@ namespace Lumina.DataAccess.Common.Migrations
.HasColumnType("TEXT")
.HasColumnOrder(1);
- b.Property("VerificationToken")
- .HasColumnType("TEXT")
- .HasColumnOrder(5);
-
- b.Property("VerificationTokenCreated")
- .HasColumnType("TEXT")
- .HasColumnOrder(6);
-
b.HasKey("Id");
+ b.HasIndex("Username")
+ .IsUnique();
+
b.ToTable("Users", (string)null);
});
@@ -326,6 +510,63 @@ namespace Lumina.DataAccess.Common.Migrations
.IsRequired();
});
+ modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.Authorization.RolePermissionEntity", b =>
+ {
+ b.HasOne("Lumina.Application.Common.DataAccess.Entities.Authorization.PermissionEntity", "Permission")
+ .WithMany("RolePermissions")
+ .HasForeignKey("PermissionId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("Lumina.Application.Common.DataAccess.Entities.Authorization.RoleEntity", "Role")
+ .WithMany("RolePermissions")
+ .HasForeignKey("RoleId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.Navigation("Permission");
+
+ b.Navigation("Role");
+ });
+
+ modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.Authorization.UserPermissionEntity", b =>
+ {
+ b.HasOne("Lumina.Application.Common.DataAccess.Entities.Authorization.PermissionEntity", "Permission")
+ .WithMany("UserPermissions")
+ .HasForeignKey("PermissionId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("Lumina.Application.Common.DataAccess.Entities.UsersManagement.UserEntity", "User")
+ .WithMany("UserPermissions")
+ .HasForeignKey("UserId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.Navigation("Permission");
+
+ b.Navigation("User");
+ });
+
+ modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.Authorization.UserRoleEntity", b =>
+ {
+ b.HasOne("Lumina.Application.Common.DataAccess.Entities.Authorization.RoleEntity", "Role")
+ .WithMany("UserRoles")
+ .HasForeignKey("RoleId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("Lumina.Application.Common.DataAccess.Entities.UsersManagement.UserEntity", "User")
+ .WithMany("UserRoles")
+ .HasForeignKey("UserId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.Navigation("Role");
+
+ b.Navigation("User");
+ });
+
modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management.LibraryEntity", b =>
{
b.HasOne("Lumina.Application.Common.DataAccess.Entities.UsersManagement.UserEntity", "User")
@@ -375,8 +616,7 @@ namespace Lumina.DataAccess.Common.Migrations
b1.Property("BookId")
.HasColumnType("TEXT");
- b1.Property("MaxValue")
- .IsRequired()
+ b1.Property("MaxValue")
.HasColumnType("decimal(3,2)");
b1.Property("Source")
@@ -384,8 +624,7 @@ namespace Lumina.DataAccess.Common.Migrations
.HasMaxLength(50)
.HasColumnType("TEXT");
- b1.Property("Value")
- .IsRequired()
+ b1.Property("Value")
.HasColumnType("decimal(3,2)");
b1.Property("VoteCount")
@@ -436,9 +675,27 @@ namespace Lumina.DataAccess.Common.Migrations
b.Navigation("Ratings");
});
+ modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.Authorization.PermissionEntity", b =>
+ {
+ b.Navigation("RolePermissions");
+
+ b.Navigation("UserPermissions");
+ });
+
+ modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.Authorization.RoleEntity", b =>
+ {
+ b.Navigation("RolePermissions");
+
+ b.Navigation("UserRoles");
+ });
+
modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.UsersManagement.UserEntity", b =>
{
b.Navigation("Libraries");
+
+ b.Navigation("UserPermissions");
+
+ b.Navigation("UserRoles");
});
#pragma warning restore 612, 618
}
diff --git a/src/Lumina.DataAccess/Core/Repositories/Common/Factory/IRepositoryFactory.cs b/src/Lumina.DataAccess/Core/Repositories/Common/Factory/IRepositoryFactory.cs
index e2c4608b..92336b33 100644
--- a/src/Lumina.DataAccess/Core/Repositories/Common/Factory/IRepositoryFactory.cs
+++ b/src/Lumina.DataAccess/Core/Repositories/Common/Factory/IRepositoryFactory.cs
@@ -1,4 +1,4 @@
-namespace Lumina.DataAccess.Core.Repositories.Common.Factory;
+namespace Lumina.DataAccess.Core.Repositories.Common.Factory;
///
/// Interface for the repositories factory.
@@ -6,9 +6,10 @@
public interface IRepositoryFactory
{
///
- /// Creates a new repository of type .
+ /// Creates a new repository of type .
///
- /// The type of repository to create.
- /// A repository of type .
- TResult CreateRepository() where TResult : notnull;
-}
\ No newline at end of file
+ /// The type of repository to create.
+ /// A repository of type .
+ /// Thrown when the type of the requested repository has not been registered.
+ TRepository CreateRepository() where TRepository : notnull;
+}
diff --git a/src/Lumina.DataAccess/Core/Repositories/Common/Factory/RepositoryFactory.cs b/src/Lumina.DataAccess/Core/Repositories/Common/Factory/RepositoryFactory.cs
index f18e6c09..52172887 100644
--- a/src/Lumina.DataAccess/Core/Repositories/Common/Factory/RepositoryFactory.cs
+++ b/src/Lumina.DataAccess/Core/Repositories/Common/Factory/RepositoryFactory.cs
@@ -1,4 +1,4 @@
-#region ========================================================================= USING =====================================================================================
+#region ========================================================================= USING =====================================================================================
using Microsoft.Extensions.DependencyInjection;
using System;
#endregion
@@ -22,13 +22,13 @@ public class RepositoryFactory : IRepositoryFactory
}
///
- /// Creates a new repository of type .
+ /// Creates a new repository of type .
///
- /// The type of repository to create.
- /// A repository of type .
+ /// The type of repository to create.
+ /// A repository of type .
/// Thrown when the type of the requested repository has not been registered.
- public TResult CreateRepository() where TResult : notnull
+ public TRepository CreateRepository() where TRepository : notnull
{
- return _serviceProvider.GetRequiredService() ?? throw new ArgumentException();
+ return _serviceProvider.GetRequiredService() ?? throw new ArgumentException();
}
-}
\ No newline at end of file
+}
diff --git a/src/Lumina.DataAccess/Core/Repositories/Users/UserRepository.cs b/src/Lumina.DataAccess/Core/Repositories/Users/UserRepository.cs
index 68b02bc1..684f9a0a 100644
--- a/src/Lumina.DataAccess/Core/Repositories/Users/UserRepository.cs
+++ b/src/Lumina.DataAccess/Core/Repositories/Users/UserRepository.cs
@@ -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
/// An