[ADD] Add cancellation tokens to async functions

This commit is contained in:
max 2025-01-08 16:22:59 +01:00
parent 28fcd74acf
commit fd733b7238
4 changed files with 36 additions and 27 deletions

View File

@ -16,6 +16,7 @@ public partial class AuthorityManager
IServiceProvider services, IServiceProvider services,
ICryptographer cryptographer, ICryptographer cryptographer,
IUserRepository userRepository, IUserRepository userRepository,
IRoleRepository roleRepository,
IPasswordHasher passwordHasher) IPasswordHasher passwordHasher)
{ {
_logger = LogService.RegisterLogger<AuthorityManager>(); _logger = LogService.RegisterLogger<AuthorityManager>();
@ -23,6 +24,7 @@ public partial class AuthorityManager
Services = services; Services = services;
Cryptographer = cryptographer; Cryptographer = cryptographer;
UserRepository = userRepository; UserRepository = userRepository;
RoleRepository = roleRepository;
PasswordHasher = passwordHasher; PasswordHasher = passwordHasher;
} }
@ -33,6 +35,7 @@ public partial class AuthorityManager
public ICryptographer Cryptographer { get; } public ICryptographer Cryptographer { get; }
public IUserRepository UserRepository { get; } public IUserRepository UserRepository { get; }
public IRoleRepository RoleRepository { get; }
public IPasswordHasher PasswordHasher { get; } public IPasswordHasher PasswordHasher { get; }

View File

@ -1,6 +1,26 @@
using DotBased.AspNet.Authority.Models.Authority;
namespace DotBased.AspNet.Authority.Managers; namespace DotBased.AspNet.Authority.Managers;
public partial class AuthorityManager public partial class AuthorityManager
{ {
public async Task<Result<AuthorityRole>> CreateRoleAsync(AuthorityRole role, CancellationToken? cancellationToken = null)
{
return Result<AuthorityRole>.Failed("Not implemented!");
}
public async Task<Result> DeleteRoleAsync(AuthorityRole role, CancellationToken? cancellationToken = null)
{
return Result.Failed("Not implemented!");
}
public async Task<Result<AuthorityRole>> UpdateRoleAsync(AuthorityRole role, CancellationToken? cancellationToken = null)
{
return Result<AuthorityRole>.Failed("Not implemented!");
}
public async Task AddUserToRole(AuthorityUser user, AuthorityRole role, CancellationToken? cancellationToken = null)
{
}
} }

View File

@ -34,13 +34,13 @@ public partial class AuthorityManager
return errors.Count > 0 ? ValidationResult.Failed(errors) : ValidationResult.Ok(); return errors.Count > 0 ? ValidationResult.Failed(errors) : ValidationResult.Ok();
} }
public async Task<ListResult<AuthorityUser>> SearchUsersAsync(string query, int maxResults = 20, int offset = 0) public async Task<ListResult<AuthorityUser>> SearchUsersAsync(string query, int maxResults = 20, int offset = 0, CancellationToken? cancellationToken = null)
{ {
var searchResult = await UserRepository.GetAuthorityUsersAsync(query, maxResults, offset); var searchResult = await UserRepository.GetAuthorityUsersAsync(query, maxResults, offset, cancellationToken);
return searchResult.Item1 == null ? ListResult<AuthorityUser>.Failed("No results!") : ListResult<AuthorityUser>.Ok(searchResult.Item1, searchResult.Item2); return searchResult.Item1 == null ? ListResult<AuthorityUser>.Failed("No results!") : ListResult<AuthorityUser>.Ok(searchResult.Item1, searchResult.Item2);
} }
public async Task<AuthorityResult<AuthorityUser>> UpdatePasswordAsync(AuthorityUser user, string password) public async Task<AuthorityResult<AuthorityUser>> UpdatePasswordAsync(AuthorityUser user, string password, CancellationToken? cancellationToken = null)
{ {
var passwordValidation = await ValidatePasswordAsync(user, password); var passwordValidation = await ValidatePasswordAsync(user, password);
if (!passwordValidation.Success) if (!passwordValidation.Success)
@ -53,11 +53,11 @@ public partial class AuthorityManager
user.PasswordHash = await PasswordHasher.HashPasswordAsync(password); user.PasswordHash = await PasswordHasher.HashPasswordAsync(password);
user.SecurityVersion = GenerateVersion(); user.SecurityVersion = GenerateVersion();
var updateResult = await UserRepository.UpdateUserAsync(user); var updateResult = await UserRepository.UpdateUserAsync(user, cancellationToken);
return updateResult == null ? AuthorityResult<AuthorityUser>.Error("Failed to save updates!") : AuthorityResult<AuthorityUser>.Ok(updateResult); return updateResult == null ? AuthorityResult<AuthorityUser>.Error("Failed to save updates!") : AuthorityResult<AuthorityUser>.Ok(updateResult);
} }
public async Task<AuthorityResult<AuthorityUser>> CreateUserAsync(AuthorityUser userModel, string password) public async Task<AuthorityResult<AuthorityUser>> CreateUserAsync(AuthorityUser userModel, string password, CancellationToken? cancellationToken = null)
{ {
var userValidation = await ValidateUserAsync(userModel); var userValidation = await ValidateUserAsync(userModel);
var passwordValidation = await ValidatePasswordAsync(userModel, password); var passwordValidation = await ValidatePasswordAsync(userModel, password);
@ -74,22 +74,22 @@ public partial class AuthorityManager
var hashedPassword = await PasswordHasher.HashPasswordAsync(password); var hashedPassword = await PasswordHasher.HashPasswordAsync(password);
userModel.PasswordHash = hashedPassword; userModel.PasswordHash = hashedPassword;
var userCreationResult = await UserRepository.CreateUserAsync(userModel); var userCreationResult = await UserRepository.CreateUserAsync(userModel, cancellationToken);
return userCreationResult != null return userCreationResult != null
? AuthorityResult<AuthorityUser>.Ok(userCreationResult) ? AuthorityResult<AuthorityUser>.Ok(userCreationResult)
: AuthorityResult<AuthorityUser>.Error("Failed to create user in repository!"); : AuthorityResult<AuthorityUser>.Error("Failed to create user in repository!");
} }
public async Task<Result<AuthorityUser>> UpdateUserAsync(AuthorityUser model) public async Task<Result<AuthorityUser>> UpdateUserAsync(AuthorityUser model, CancellationToken? cancellationToken = null)
{ {
var updateResult = await UserRepository.UpdateUserAsync(model); var updateResult = await UserRepository.UpdateUserAsync(model, cancellationToken);
return updateResult != null ? Result<AuthorityUser>.Ok(updateResult) : Result<AuthorityUser>.Failed("Failed to update user in repository!"); return updateResult != null ? Result<AuthorityUser>.Ok(updateResult) : Result<AuthorityUser>.Failed("Failed to update user in repository!");
} }
public async Task<bool> DeleteUserAsync(AuthorityUser model) public async Task<bool> DeleteUserAsync(AuthorityUser model, CancellationToken? cancellationToken = null)
{ {
var deleteResult = await UserRepository.DeleteUserAsync(model); var deleteResult = await UserRepository.DeleteUserAsync(model, cancellationToken);
return deleteResult; return deleteResult;
} }

View File

@ -1,22 +1,8 @@
namespace DotBased.AspNet.Authority.Models.Authority; namespace DotBased.AspNet.Authority.Models.Authority;
public class AuthorityRole : AuthorityRole<Guid> public abstract class AuthorityRole
{ {
public AuthorityRole(string name) : this() public Guid Id { get; set; }
{
Name = name;
}
public AuthorityRole()
{
Id = Guid.NewGuid();
CreatedDate = DateTime.Now;
}
}
public abstract class AuthorityRole<TKey> where TKey : IEquatable<TKey>
{
public TKey Id { get; set; }
public string? Name { get; set; } public string? Name { get; set; }