From 12efc92ac4605a8c68783f217d64c1f33bf08dd1 Mon Sep 17 00:00:00 2001 From: max Date: Sat, 4 Jan 2025 00:52:04 +0100 Subject: [PATCH] [CHANGE] Implementation updates --- .../Managers/AuthorityUserManager.cs | 48 +++++++++++++++++-- .../Repositories/IUserRepository.cs | 21 ++++---- 2 files changed, 55 insertions(+), 14 deletions(-) diff --git a/DotBased.AspNet.Authority/Managers/AuthorityUserManager.cs b/DotBased.AspNet.Authority/Managers/AuthorityUserManager.cs index b5a9003..f72af18 100644 --- a/DotBased.AspNet.Authority/Managers/AuthorityUserManager.cs +++ b/DotBased.AspNet.Authority/Managers/AuthorityUserManager.cs @@ -64,6 +64,34 @@ public class AuthorityUserManager where TUser : class return errors.Count > 0 ? ValidationResult.Failed(errors) : ValidationResult.Ok(); } + public async Task> SearchUsersAsync(string query, int maxResults = 20, int offset = 0) + { + var searchResult = await UserRepository.GetUsersAsync(query, maxResults, offset); + return searchResult.Item1 == null ? ListResult.Failed("No results!") : ListResult.Ok(searchResult.Item1, searchResult.Item2); + } + + public async Task> UpdatePasswordAsync(TUser user, string password) + { + if (user is not AuthorityUserBase userBase) + { + return AuthorityResult.Error($"Given user is not of base type {nameof(AuthorityUserBase)}!"); + } + + var passwordValidation = await ValidatePasswordAsync(user, password); + if (!passwordValidation.Success) + { + List errors = []; + errors.AddRange(passwordValidation.Errors); + return AuthorityResult.Failed(errors, ResultFailReason.Validation); + } + + userBase.PasswordHash = await PasswordHasher.HashPasswordAsync(password); + userBase.SecurityVersion = AuthorityManager.GenerateVersion(); + + var updateResult = await UserRepository.UpdateUserAsync(user); + return updateResult == null ? AuthorityResult.Error("Failed to save updates!") : AuthorityResult.Ok(updateResult); + } + public async Task> CreateUserAsync(TUser userModel, string password) { if (userModel is not AuthorityUserBase userBase) @@ -81,10 +109,8 @@ public class AuthorityUserManager where TUser : class return AuthorityResult.Failed(errors, ResultFailReason.Validation); } - var version = AuthorityManager.GenerateVersion(); - userBase.Version = version; - var securityVersion = AuthorityManager.GenerateVersion(); - userBase.SecurityVersion = securityVersion; + userBase.Version = AuthorityManager.GenerateVersion(); + userBase.SecurityVersion = AuthorityManager.GenerateVersion(); var hashedPassword = await PasswordHasher.HashPasswordAsync(password); userBase.PasswordHash = hashedPassword; @@ -94,4 +120,18 @@ public class AuthorityUserManager where TUser : class ? AuthorityResult.Ok(userCreationResult) : AuthorityResult.Error("Failed to create user in repository!"); } + + public async Task> UpdateUserAsync(TUser model) + { + var updateResult = await UserRepository.UpdateUserAsync(model); + return updateResult != null ? Result.Ok(updateResult) : Result.Failed("Failed to update user in repository!"); + } + + public async Task DeleteUserAsync(TUser model) + { + var deleteResult = await UserRepository.DeleteUserAsync(model); + return deleteResult; + } + + } \ No newline at end of file diff --git a/DotBased.AspNet.Authority/Repositories/IUserRepository.cs b/DotBased.AspNet.Authority/Repositories/IUserRepository.cs index 8385b26..46e833a 100644 --- a/DotBased.AspNet.Authority/Repositories/IUserRepository.cs +++ b/DotBased.AspNet.Authority/Repositories/IUserRepository.cs @@ -2,14 +2,15 @@ namespace DotBased.AspNet.Authority.Repositories; public interface IUserRepository where TUser : class { - public Task GetUserByIdAsync(string id); - public Task GetUserIdAsync(TUser user); - public Task GetUserByEmailAsync(string email); - public Task SetVersionAsync(TUser user, long version); - public Task GetVersionAsync(TUser user); - public Task SetSecurityVersionAsync(TUser user, long version); - public Task GetSecurityVersionAsync(TUser user); - public Task CreateUserAsync(TUser user); - public Task UpdateUserAsync(TUser user); - public Task DeleteUserAsync(TUser user); + public Task GetUserByIdAsync(string id, CancellationToken? cancellationToken = null); + public Task GetUserIdAsync(TUser user, CancellationToken? cancellationToken = null); + public Task?, int>> GetUsersAsync(string query, int maxResults = 20, int offset = 0, CancellationToken? cancellationToken = null); + public Task GetUserByEmailAsync(string email, CancellationToken? cancellationToken = null); + public Task SetVersionAsync(TUser user, long version, CancellationToken? cancellationToken = null); + public Task GetVersionAsync(TUser user, CancellationToken? cancellationToken = null); + public Task SetSecurityVersionAsync(TUser user, long version, CancellationToken? cancellationToken = null); + public Task GetSecurityVersionAsync(TUser user, CancellationToken? cancellationToken = null); + public Task CreateUserAsync(TUser user, CancellationToken? cancellationToken = null); + public Task UpdateUserAsync(TUser user, CancellationToken? cancellationToken = null); + public Task DeleteUserAsync(TUser user, CancellationToken? cancellationToken = null); } \ No newline at end of file