2024-12-26 20:01:57 +01:00
|
|
|
using DotBased.AspNet.Authority.Crypto;
|
|
|
|
using DotBased.AspNet.Authority.Models.Validation;
|
|
|
|
using DotBased.AspNet.Authority.Repositories;
|
2024-12-25 22:50:04 +01:00
|
|
|
using DotBased.AspNet.Authority.Validators;
|
|
|
|
using DotBased.Logging;
|
|
|
|
|
|
|
|
namespace DotBased.AspNet.Authority.Services;
|
|
|
|
|
2024-12-26 20:01:57 +01:00
|
|
|
public class AuthorityUserManager<TUser> where TUser : class
|
2024-12-25 22:50:04 +01:00
|
|
|
{
|
|
|
|
public AuthorityUserManager(
|
|
|
|
AuthorityManager manager,
|
2024-12-26 20:01:57 +01:00
|
|
|
IUserRepository<TUser> userRepository,
|
|
|
|
IPasswordHasher passwordHasher,
|
2024-12-25 22:50:04 +01:00
|
|
|
IEnumerable<IPasswordValidator<TUser>>? passwordValidators,
|
|
|
|
IEnumerable<IUserValidator<TUser>>? userValidators)
|
|
|
|
{
|
|
|
|
_logger = LogService.RegisterLogger<AuthorityUserManager<TUser>>();
|
|
|
|
AuthorityManager = manager;
|
2024-12-26 20:01:57 +01:00
|
|
|
UserRepository = userRepository;
|
|
|
|
PasswordHasher = passwordHasher;
|
2024-12-25 22:50:04 +01:00
|
|
|
if (passwordValidators != null)
|
|
|
|
PasswordValidators = passwordValidators;
|
|
|
|
if (userValidators != null)
|
|
|
|
UserValidators = userValidators;
|
|
|
|
}
|
|
|
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
public AuthorityManager AuthorityManager { get; }
|
2024-12-26 20:01:57 +01:00
|
|
|
public IUserRepository<TUser> UserRepository { get; }
|
|
|
|
|
|
|
|
public IPasswordHasher PasswordHasher { get; }
|
2024-12-25 22:50:04 +01:00
|
|
|
|
|
|
|
public IEnumerable<IPasswordValidator<TUser>> PasswordValidators { get; } = [];
|
|
|
|
public IEnumerable<IUserValidator<TUser>> UserValidators { get; } = [];
|
2024-12-26 20:01:57 +01:00
|
|
|
|
|
|
|
public async Task<ValidationResult> ValidatePasswordAsync(TUser user, string password)
|
|
|
|
{
|
|
|
|
List<ValidationError> errors = [];
|
|
|
|
foreach (var validator in PasswordValidators)
|
|
|
|
{
|
|
|
|
var validatorResult = await validator.ValidatePasswordAsync(this, user, password);
|
|
|
|
if (!validatorResult.Success)
|
|
|
|
{
|
|
|
|
errors.AddRange(validatorResult.Errors);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors.Count > 0 ? ValidationResult.Failed(errors) : ValidationResult.Ok();
|
|
|
|
}
|
2024-12-25 22:50:04 +01:00
|
|
|
}
|