using DotBased.AspNet.Authority.Models; using DotBased.AspNet.Authority.Models.Authority; using DotBased.AspNet.Authority.Models.Validation; namespace DotBased.AspNet.Authority.Managers; public partial class AuthorityManager { public async Task ValidatePasswordAsync(AuthorityUser user, string password) { List 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(); } public async Task ValidateUserAsync(AuthorityUser user) { List errors = []; foreach (var userValidator in UserValidators) { var validationResult = await userValidator.ValidateUserAsync(this, user); if (!validationResult.Success) { errors.AddRange(validationResult.Errors); } } return errors.Count > 0 ? ValidationResult.Failed(errors) : ValidationResult.Ok(); } public async Task> SearchUsersAsync(string query, int maxResults = 20, int offset = 0, CancellationToken? cancellationToken = null) { var searchResult = await UserRepository.GetAuthorityUsersAsync(query, maxResults, offset, cancellationToken); return searchResult.Item1 == null ? ListResult.Failed("No results!") : ListResult.Ok(searchResult.Item1, searchResult.Item2); } public async Task> UpdatePasswordAsync(AuthorityUser user, string password, CancellationToken? cancellationToken = null) { var passwordValidation = await ValidatePasswordAsync(user, password); if (!passwordValidation.Success) { List errors = []; errors.AddRange(passwordValidation.Errors); return AuthorityResult.Failed(errors, ResultFailReason.Validation); } user.PasswordHash = await PasswordHasher.HashPasswordAsync(password); user.SecurityVersion = GenerateVersion(); var updateResult = await UserRepository.UpdateUserAsync(user, cancellationToken); return updateResult == null ? AuthorityResult.Error("Failed to save updates!") : AuthorityResult.Ok(updateResult); } public async Task> CreateUserAsync(AuthorityUser userModel, string password, CancellationToken? cancellationToken = null) { var userValidation = await ValidateUserAsync(userModel); var passwordValidation = await ValidatePasswordAsync(userModel, password); if (!userValidation.Success || !passwordValidation.Success) { List errors = []; errors.AddRange(userValidation.Errors); errors.AddRange(passwordValidation.Errors); return AuthorityResult.Failed(errors, ResultFailReason.Validation); } userModel.Version = GenerateVersion(); userModel.SecurityVersion = GenerateVersion(); var hashedPassword = await PasswordHasher.HashPasswordAsync(password); userModel.PasswordHash = hashedPassword; var userCreationResult = await UserRepository.CreateUserAsync(userModel, cancellationToken); return userCreationResult != null ? AuthorityResult.Ok(userCreationResult) : AuthorityResult.Error("Failed to create user in repository!"); } public async Task> UpdateUserAsync(AuthorityUser model, CancellationToken? cancellationToken = null) { var updateResult = await UserRepository.UpdateUserAsync(model, cancellationToken); return updateResult != null ? Result.Ok(updateResult) : Result.Failed("Failed to update user in repository!"); } public async Task DeleteUserAsync(AuthorityUser model, CancellationToken? cancellationToken = null) { var deleteResult = await UserRepository.DeleteUserAsync(model, cancellationToken); return deleteResult; } }