mirror of
https://github.com/hmaxnl/DotBased.git
synced 2025-01-18 10:04:20 +01:00
[REFACTOR] Split manager class, refactored/cleaned classes
This commit is contained in:
parent
90cd0a2828
commit
28fcd74acf
|
@ -26,9 +26,6 @@ public static class AuthorityProviderExtensions
|
|||
services.TryAddScoped<IPhoneNumberVerifier, PhoneNumberVerifier>();
|
||||
services.TryAddScoped<IUserVerifier, UserVerifier>();*/
|
||||
services.TryAddScoped<AuthorityManager>();
|
||||
services.TryAddScoped<AuthorityUserManager>();
|
||||
services.TryAddScoped<AuthorityGroupManager>();
|
||||
services.TryAddScoped<AuthorityRoleManager>();
|
||||
return new AuthorityBuilder(services);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
namespace DotBased.AspNet.Authority.Managers;
|
||||
|
||||
public class AuthorityGroupManager
|
||||
public partial class AuthorityManager
|
||||
{
|
||||
|
||||
}
|
|
@ -2,22 +2,28 @@ using System.Reflection;
|
|||
using DotBased.AspNet.Authority.Attributes;
|
||||
using DotBased.AspNet.Authority.Crypto;
|
||||
using DotBased.AspNet.Authority.Models.Options;
|
||||
using DotBased.AspNet.Authority.Repositories;
|
||||
using DotBased.AspNet.Authority.Validators;
|
||||
using DotBased.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace DotBased.AspNet.Authority.Managers;
|
||||
|
||||
public class AuthorityManager
|
||||
public partial class AuthorityManager
|
||||
{
|
||||
public AuthorityManager(
|
||||
IOptions<AuthorityOptions> options,
|
||||
IServiceProvider services,
|
||||
ICryptographer cryptographer)
|
||||
ICryptographer cryptographer,
|
||||
IUserRepository userRepository,
|
||||
IPasswordHasher passwordHasher)
|
||||
{
|
||||
_logger = LogService.RegisterLogger<AuthorityManager>();
|
||||
Options = options.Value ?? new AuthorityOptions();
|
||||
Options = options.Value;
|
||||
Services = services;
|
||||
Cryptographer = cryptographer;
|
||||
UserRepository = userRepository;
|
||||
PasswordHasher = passwordHasher;
|
||||
}
|
||||
|
||||
private readonly ILogger _logger;
|
||||
|
@ -25,6 +31,13 @@ public class AuthorityManager
|
|||
public IServiceProvider Services { get; }
|
||||
public AuthorityOptions Options { 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();
|
||||
|
@ -38,12 +51,7 @@ public class AuthorityManager
|
|||
/// <typeparam name="TModel">The class with the properties to protect.</typeparam>
|
||||
public async Task HandlePropertyProtection<TModel>(TModel data, bool protection)
|
||||
{
|
||||
var props = GetProtectedPropertiesValues<TModel>(data);
|
||||
if (Cryptographer == null)
|
||||
{
|
||||
_logger.Warning("No cryptographer specified! Cannot encrypt/decrypt properties.");
|
||||
return;
|
||||
}
|
||||
var props = GetProtectedPropertiesValues(data);
|
||||
if (props.Count == 0)
|
||||
{
|
||||
return;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
namespace DotBased.AspNet.Authority.Managers;
|
||||
|
||||
public class AuthorityRoleManager
|
||||
public partial class AuthorityManager
|
||||
{
|
||||
|
||||
}
|
|
@ -1,41 +1,11 @@
|
|||
using DotBased.AspNet.Authority.Crypto;
|
||||
using DotBased.AspNet.Authority.Models;
|
||||
using DotBased.AspNet.Authority.Models.Authority;
|
||||
using DotBased.AspNet.Authority.Models.Validation;
|
||||
using DotBased.AspNet.Authority.Repositories;
|
||||
using DotBased.AspNet.Authority.Validators;
|
||||
using DotBased.Logging;
|
||||
|
||||
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)
|
||||
{
|
||||
List<ValidationError> errors = [];
|
||||
|
@ -81,7 +51,7 @@ public class AuthorityUserManager
|
|||
}
|
||||
|
||||
user.PasswordHash = await PasswordHasher.HashPasswordAsync(password);
|
||||
user.SecurityVersion = AuthorityManager.GenerateVersion();
|
||||
user.SecurityVersion = GenerateVersion();
|
||||
|
||||
var updateResult = await UserRepository.UpdateUserAsync(user);
|
||||
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);
|
||||
}
|
||||
|
||||
userModel.Version = AuthorityManager.GenerateVersion();
|
||||
userModel.SecurityVersion = AuthorityManager.GenerateVersion();
|
||||
userModel.Version = GenerateVersion();
|
||||
userModel.SecurityVersion = GenerateVersion();
|
||||
var hashedPassword = await PasswordHasher.HashPasswordAsync(password);
|
||||
userModel.PasswordHash = hashedPassword;
|
||||
|
||||
|
|
|
@ -6,5 +6,5 @@ namespace DotBased.AspNet.Authority.Validators;
|
|||
|
||||
public interface IPasswordValidator
|
||||
{
|
||||
public Task<ValidationResult> ValidatePasswordAsync(AuthorityUserManager userManager, AuthorityUser user, string password);
|
||||
public Task<ValidationResult> ValidatePasswordAsync(AuthorityManager manager, AuthorityUser user, string password);
|
||||
}
|
|
@ -6,5 +6,5 @@ namespace DotBased.AspNet.Authority.Validators;
|
|||
|
||||
public interface IUserValidator
|
||||
{
|
||||
public Task<ValidationResult> ValidateUserAsync(AuthorityUserManager manager, AuthorityUser user);
|
||||
public Task<ValidationResult> ValidateUserAsync(AuthorityManager manager, AuthorityUser user);
|
||||
}
|
|
@ -8,7 +8,7 @@ public class PasswordEqualsValidator : IPasswordValidator
|
|||
{
|
||||
private const string ValidatorId = "Authority.Validator.Password.Equals";
|
||||
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 = [];
|
||||
var hashedPassword = await userManager.PasswordHasher.HashPasswordAsync(password);
|
||||
|
|
|
@ -8,19 +8,18 @@ namespace DotBased.AspNet.Authority.Validators;
|
|||
/// <summary>
|
||||
/// Validates the password against the options that is configured.
|
||||
/// </summary>
|
||||
/// <typeparam name="TUser">The user model used.</typeparam>
|
||||
public class PasswordOptionsValidator : IPasswordValidator
|
||||
{
|
||||
private const string ValidatorId = "Authority.Validator.Password.Options";
|
||||
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)
|
||||
{
|
||||
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>();
|
||||
|
||||
if (password.IsNullOrEmpty() || password.Length < passwordOptions.RequiredLength)
|
||||
|
|
|
@ -10,11 +10,11 @@ public class UserValidator : IUserValidator
|
|||
private const string ValidatorId = "Authority.Validator.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 = [];
|
||||
|
||||
var userOptions = manager.AuthorityManager.Options.User;
|
||||
var userOptions = manager.Options.User;
|
||||
|
||||
if (userOptions.RequireUniqueEmail)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue
Block a user