[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

@@ -1,6 +1,6 @@
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.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;

View File

@@ -1,6 +1,6 @@
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.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;