[CHANGE] Implementation updates

This commit is contained in:
max 2025-01-04 00:52:04 +01:00
parent efc8259930
commit 12efc92ac4
2 changed files with 55 additions and 14 deletions

View File

@ -64,6 +64,34 @@ public class AuthorityUserManager<TUser> where TUser : class
return errors.Count > 0 ? ValidationResult.Failed(errors) : ValidationResult.Ok(); return errors.Count > 0 ? ValidationResult.Failed(errors) : ValidationResult.Ok();
} }
public async Task<ListResult<TUser>> SearchUsersAsync(string query, int maxResults = 20, int offset = 0)
{
var searchResult = await UserRepository.GetUsersAsync(query, maxResults, offset);
return searchResult.Item1 == null ? ListResult<TUser>.Failed("No results!") : ListResult<TUser>.Ok(searchResult.Item1, searchResult.Item2);
}
public async Task<AuthorityResult<TUser>> UpdatePasswordAsync(TUser user, string password)
{
if (user is not AuthorityUserBase userBase)
{
return AuthorityResult<TUser>.Error($"Given user is not of base type {nameof(AuthorityUserBase)}!");
}
var passwordValidation = await ValidatePasswordAsync(user, password);
if (!passwordValidation.Success)
{
List<ValidationError> errors = [];
errors.AddRange(passwordValidation.Errors);
return AuthorityResult<TUser>.Failed(errors, ResultFailReason.Validation);
}
userBase.PasswordHash = await PasswordHasher.HashPasswordAsync(password);
userBase.SecurityVersion = AuthorityManager.GenerateVersion();
var updateResult = await UserRepository.UpdateUserAsync(user);
return updateResult == null ? AuthorityResult<TUser>.Error("Failed to save updates!") : AuthorityResult<TUser>.Ok(updateResult);
}
public async Task<AuthorityResult<TUser>> CreateUserAsync(TUser userModel, string password) public async Task<AuthorityResult<TUser>> CreateUserAsync(TUser userModel, string password)
{ {
if (userModel is not AuthorityUserBase userBase) if (userModel is not AuthorityUserBase userBase)
@ -81,10 +109,8 @@ public class AuthorityUserManager<TUser> where TUser : class
return AuthorityResult<TUser>.Failed(errors, ResultFailReason.Validation); return AuthorityResult<TUser>.Failed(errors, ResultFailReason.Validation);
} }
var version = AuthorityManager.GenerateVersion(); userBase.Version = AuthorityManager.GenerateVersion();
userBase.Version = version; userBase.SecurityVersion = AuthorityManager.GenerateVersion();
var securityVersion = AuthorityManager.GenerateVersion();
userBase.SecurityVersion = securityVersion;
var hashedPassword = await PasswordHasher.HashPasswordAsync(password); var hashedPassword = await PasswordHasher.HashPasswordAsync(password);
userBase.PasswordHash = hashedPassword; userBase.PasswordHash = hashedPassword;
@ -94,4 +120,18 @@ public class AuthorityUserManager<TUser> where TUser : class
? AuthorityResult<TUser>.Ok(userCreationResult) ? AuthorityResult<TUser>.Ok(userCreationResult)
: AuthorityResult<TUser>.Error("Failed to create user in repository!"); : AuthorityResult<TUser>.Error("Failed to create user in repository!");
} }
public async Task<Result<TUser>> UpdateUserAsync(TUser model)
{
var updateResult = await UserRepository.UpdateUserAsync(model);
return updateResult != null ? Result<TUser>.Ok(updateResult) : Result<TUser>.Failed("Failed to update user in repository!");
}
public async Task<bool> DeleteUserAsync(TUser model)
{
var deleteResult = await UserRepository.DeleteUserAsync(model);
return deleteResult;
}
} }

View File

@ -2,14 +2,15 @@ namespace DotBased.AspNet.Authority.Repositories;
public interface IUserRepository<TUser> where TUser : class public interface IUserRepository<TUser> where TUser : class
{ {
public Task<TUser?> GetUserByIdAsync(string id); public Task<TUser?> GetUserByIdAsync(string id, CancellationToken? cancellationToken = null);
public Task<string> GetUserIdAsync(TUser user); public Task<string> GetUserIdAsync(TUser user, CancellationToken? cancellationToken = null);
public Task<TUser?> GetUserByEmailAsync(string email); public Task<Tuple<List<TUser>?, int>> GetUsersAsync(string query, int maxResults = 20, int offset = 0, CancellationToken? cancellationToken = null);
public Task SetVersionAsync(TUser user, long version); public Task<TUser?> GetUserByEmailAsync(string email, CancellationToken? cancellationToken = null);
public Task<long> GetVersionAsync(TUser user); public Task SetVersionAsync(TUser user, long version, CancellationToken? cancellationToken = null);
public Task SetSecurityVersionAsync(TUser user, long version); public Task<long> GetVersionAsync(TUser user, CancellationToken? cancellationToken = null);
public Task<long> GetSecurityVersionAsync(TUser user); public Task SetSecurityVersionAsync(TUser user, long version, CancellationToken? cancellationToken = null);
public Task<TUser?> CreateUserAsync(TUser user); public Task<long> GetSecurityVersionAsync(TUser user, CancellationToken? cancellationToken = null);
public Task<TUser?> UpdateUserAsync(TUser user); public Task<TUser?> CreateUserAsync(TUser user, CancellationToken? cancellationToken = null);
public Task<bool> DeleteUserAsync(TUser user); public Task<TUser?> UpdateUserAsync(TUser user, CancellationToken? cancellationToken = null);
public Task<bool> DeleteUserAsync(TUser user, CancellationToken? cancellationToken = null);
} }