[REFACTOR] Split manager class, refactored/cleaned classes

This commit is contained in:
max 2025-01-08 16:06:25 +01:00
parent 90cd0a2828
commit 28fcd74acf
10 changed files with 30 additions and 56 deletions

View File

@ -26,9 +26,6 @@ public static class AuthorityProviderExtensions
services.TryAddScoped<IPhoneNumberVerifier, PhoneNumberVerifier>(); services.TryAddScoped<IPhoneNumberVerifier, PhoneNumberVerifier>();
services.TryAddScoped<IUserVerifier, UserVerifier>();*/ services.TryAddScoped<IUserVerifier, UserVerifier>();*/
services.TryAddScoped<AuthorityManager>(); services.TryAddScoped<AuthorityManager>();
services.TryAddScoped<AuthorityUserManager>();
services.TryAddScoped<AuthorityGroupManager>();
services.TryAddScoped<AuthorityRoleManager>();
return new AuthorityBuilder(services); return new AuthorityBuilder(services);
} }

View File

@ -1,6 +1,6 @@
namespace DotBased.AspNet.Authority.Managers; namespace DotBased.AspNet.Authority.Managers;
public class AuthorityGroupManager public partial class AuthorityManager
{ {
} }

View File

@ -2,22 +2,28 @@ using System.Reflection;
using DotBased.AspNet.Authority.Attributes; using DotBased.AspNet.Authority.Attributes;
using DotBased.AspNet.Authority.Crypto; using DotBased.AspNet.Authority.Crypto;
using DotBased.AspNet.Authority.Models.Options; using DotBased.AspNet.Authority.Models.Options;
using DotBased.AspNet.Authority.Repositories;
using DotBased.AspNet.Authority.Validators;
using DotBased.Logging; using DotBased.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
namespace DotBased.AspNet.Authority.Managers; namespace DotBased.AspNet.Authority.Managers;
public class AuthorityManager public partial class AuthorityManager
{ {
public AuthorityManager( public AuthorityManager(
IOptions<AuthorityOptions> options, IOptions<AuthorityOptions> options,
IServiceProvider services, IServiceProvider services,
ICryptographer cryptographer) ICryptographer cryptographer,
IUserRepository userRepository,
IPasswordHasher passwordHasher)
{ {
_logger = LogService.RegisterLogger<AuthorityManager>(); _logger = LogService.RegisterLogger<AuthorityManager>();
Options = options.Value ?? new AuthorityOptions(); Options = options.Value;
Services = services; Services = services;
Cryptographer = cryptographer; Cryptographer = cryptographer;
UserRepository = userRepository;
PasswordHasher = passwordHasher;
} }
private readonly ILogger _logger; private readonly ILogger _logger;
@ -26,6 +32,13 @@ public class AuthorityManager
public AuthorityOptions Options { get; } public AuthorityOptions Options { get; }
public ICryptographer Cryptographer { get; } public ICryptographer Cryptographer { get; }
public IUserRepository UserRepository { get; }
public IPasswordHasher PasswordHasher { get; }
public IEnumerable<IPasswordValidator> PasswordValidators { get; } = [];
public IEnumerable<IUserValidator> UserValidators { get; } = [];
public long GenerateVersion() => DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); public long GenerateVersion() => DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
@ -38,12 +51,7 @@ public class AuthorityManager
/// <typeparam name="TModel">The class with the properties to protect.</typeparam> /// <typeparam name="TModel">The class with the properties to protect.</typeparam>
public async Task HandlePropertyProtection<TModel>(TModel data, bool protection) public async Task HandlePropertyProtection<TModel>(TModel data, bool protection)
{ {
var props = GetProtectedPropertiesValues<TModel>(data); var props = GetProtectedPropertiesValues(data);
if (Cryptographer == null)
{
_logger.Warning("No cryptographer specified! Cannot encrypt/decrypt properties.");
return;
}
if (props.Count == 0) if (props.Count == 0)
{ {
return; return;

View File

@ -1,6 +1,6 @@
namespace DotBased.AspNet.Authority.Managers; namespace DotBased.AspNet.Authority.Managers;
public class AuthorityRoleManager public partial class AuthorityManager
{ {
} }

View File

@ -1,41 +1,11 @@
using DotBased.AspNet.Authority.Crypto;
using DotBased.AspNet.Authority.Models; using DotBased.AspNet.Authority.Models;
using DotBased.AspNet.Authority.Models.Authority; using DotBased.AspNet.Authority.Models.Authority;
using DotBased.AspNet.Authority.Models.Validation; using DotBased.AspNet.Authority.Models.Validation;
using DotBased.AspNet.Authority.Repositories;
using DotBased.AspNet.Authority.Validators;
using DotBased.Logging;
namespace DotBased.AspNet.Authority.Managers; namespace DotBased.AspNet.Authority.Managers;
public class AuthorityUserManager public partial class AuthorityManager
{ {
public AuthorityUserManager(
AuthorityManager manager,
IUserRepository userRepository,
IPasswordHasher passwordHasher,
IEnumerable<IPasswordValidator>? passwordValidators,
IEnumerable<IUserValidator>? userValidators)
{
_logger = LogService.RegisterLogger<AuthorityUserManager>();
AuthorityManager = manager;
UserRepository = userRepository;
PasswordHasher = passwordHasher;
if (passwordValidators != null)
PasswordValidators = passwordValidators;
if (userValidators != null)
UserValidators = userValidators;
}
private readonly ILogger _logger;
public AuthorityManager AuthorityManager { get; }
public IUserRepository UserRepository { get; }
public IPasswordHasher PasswordHasher { get; }
public IEnumerable<IPasswordValidator> PasswordValidators { get; } = [];
public IEnumerable<IUserValidator> UserValidators { get; } = [];
public async Task<ValidationResult> ValidatePasswordAsync(AuthorityUser user, string password) public async Task<ValidationResult> ValidatePasswordAsync(AuthorityUser user, string password)
{ {
List<ValidationError> errors = []; List<ValidationError> errors = [];
@ -81,7 +51,7 @@ public class AuthorityUserManager
} }
user.PasswordHash = await PasswordHasher.HashPasswordAsync(password); user.PasswordHash = await PasswordHasher.HashPasswordAsync(password);
user.SecurityVersion = AuthorityManager.GenerateVersion(); user.SecurityVersion = GenerateVersion();
var updateResult = await UserRepository.UpdateUserAsync(user); var updateResult = await UserRepository.UpdateUserAsync(user);
return updateResult == null ? AuthorityResult<AuthorityUser>.Error("Failed to save updates!") : AuthorityResult<AuthorityUser>.Ok(updateResult); return updateResult == null ? AuthorityResult<AuthorityUser>.Error("Failed to save updates!") : AuthorityResult<AuthorityUser>.Ok(updateResult);
@ -99,8 +69,8 @@ public class AuthorityUserManager
return AuthorityResult<AuthorityUser>.Failed(errors, ResultFailReason.Validation); return AuthorityResult<AuthorityUser>.Failed(errors, ResultFailReason.Validation);
} }
userModel.Version = AuthorityManager.GenerateVersion(); userModel.Version = GenerateVersion();
userModel.SecurityVersion = AuthorityManager.GenerateVersion(); userModel.SecurityVersion = GenerateVersion();
var hashedPassword = await PasswordHasher.HashPasswordAsync(password); var hashedPassword = await PasswordHasher.HashPasswordAsync(password);
userModel.PasswordHash = hashedPassword; userModel.PasswordHash = hashedPassword;

View File

@ -6,5 +6,5 @@ namespace DotBased.AspNet.Authority.Validators;
public interface IPasswordValidator public interface IPasswordValidator
{ {
public Task<ValidationResult> ValidatePasswordAsync(AuthorityUserManager userManager, AuthorityUser user, string password); public Task<ValidationResult> ValidatePasswordAsync(AuthorityManager manager, AuthorityUser user, string password);
} }

View File

@ -6,5 +6,5 @@ namespace DotBased.AspNet.Authority.Validators;
public interface IUserValidator public interface IUserValidator
{ {
public Task<ValidationResult> ValidateUserAsync(AuthorityUserManager manager, AuthorityUser user); public Task<ValidationResult> ValidateUserAsync(AuthorityManager manager, AuthorityUser user);
} }

View File

@ -8,7 +8,7 @@ public class PasswordEqualsValidator : IPasswordValidator
{ {
private const string ValidatorId = "Authority.Validator.Password.Equals"; private const string ValidatorId = "Authority.Validator.Password.Equals";
private const string ValidationBase = "Authority.Validation.Password"; private const string ValidationBase = "Authority.Validation.Password";
public async Task<ValidationResult> ValidatePasswordAsync(AuthorityUserManager userManager, AuthorityUser user, string password) public async Task<ValidationResult> ValidatePasswordAsync(AuthorityManager userManager, AuthorityUser user, string password)
{ {
List<ValidationError> errors = []; List<ValidationError> errors = [];
var hashedPassword = await userManager.PasswordHasher.HashPasswordAsync(password); var hashedPassword = await userManager.PasswordHasher.HashPasswordAsync(password);

View File

@ -8,19 +8,18 @@ namespace DotBased.AspNet.Authority.Validators;
/// <summary> /// <summary>
/// Validates the password against the options that is configured. /// Validates the password against the options that is configured.
/// </summary> /// </summary>
/// <typeparam name="TUser">The user model used.</typeparam>
public class PasswordOptionsValidator : IPasswordValidator public class PasswordOptionsValidator : IPasswordValidator
{ {
private const string ValidatorId = "Authority.Validator.Password.Options"; private const string ValidatorId = "Authority.Validator.Password.Options";
private const string ValidationBase = "Authority.Validation.Password"; private const string ValidationBase = "Authority.Validation.Password";
public async Task<ValidationResult> ValidatePasswordAsync(AuthorityUserManager userManager, AuthorityUser user, string password) public async Task<ValidationResult> ValidatePasswordAsync(AuthorityManager userManager, AuthorityUser user, string password)
{ {
if (userManager == null) if (userManager == null)
{ {
throw new ArgumentNullException(nameof(userManager), "User manager is not provided!"); throw new ArgumentNullException(nameof(userManager), "User manager is not provided!");
} }
var passwordOptions = userManager.AuthorityManager.Options.Password; var passwordOptions = userManager.Options.Password;
var errors = new List<ValidationError>(); var errors = new List<ValidationError>();
if (password.IsNullOrEmpty() || password.Length < passwordOptions.RequiredLength) if (password.IsNullOrEmpty() || password.Length < passwordOptions.RequiredLength)

View File

@ -10,11 +10,11 @@ public class UserValidator : IUserValidator
private const string ValidatorId = "Authority.Validator.User"; private const string ValidatorId = "Authority.Validator.User";
private const string ValidationBase = "Authority.Validation.User"; private const string ValidationBase = "Authority.Validation.User";
public async Task<ValidationResult> ValidateUserAsync(AuthorityUserManager manager, AuthorityUser user) public async Task<ValidationResult> ValidateUserAsync(AuthorityManager manager, AuthorityUser user)
{ {
List<ValidationError> errors = []; List<ValidationError> errors = [];
var userOptions = manager.AuthorityManager.Options.User; var userOptions = manager.Options.User;
if (userOptions.RequireUniqueEmail) if (userOptions.RequireUniqueEmail)
{ {