From f906f026e7b4f9684dfb17931c243270ae3f05ed Mon Sep 17 00:00:00 2001 From: duykasama Date: Thu, 21 Mar 2024 01:19:52 +0700 Subject: [PATCH] [Hotfix][Duy] Allow admin to create staff --- .../Exceptions/RoleNotFoundException.cs | 19 ++++++++++++++++ Domus.Service/Implementations/UserService.cs | 22 +++++++++++++++++-- .../Requests/Users/CreateUserRequest.cs | 2 ++ 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 Domus.Service/Exceptions/RoleNotFoundException.cs diff --git a/Domus.Service/Exceptions/RoleNotFoundException.cs b/Domus.Service/Exceptions/RoleNotFoundException.cs new file mode 100644 index 0000000..444de6b --- /dev/null +++ b/Domus.Service/Exceptions/RoleNotFoundException.cs @@ -0,0 +1,19 @@ +using Domus.Common.Exceptions; + +namespace Domus.Service.Exceptions; + +public class RoleNotFoundException : ArgumentException, INotFoundException +{ + private readonly string? _customMessage; + public override string Message => _customMessage ?? Message; + + public RoleNotFoundException(string customMessage) + { + _customMessage = customMessage; + } + + public RoleNotFoundException() + { + _customMessage = "Role not found"; + } +} diff --git a/Domus.Service/Implementations/UserService.cs b/Domus.Service/Implementations/UserService.cs index 101b35d..659c617 100644 --- a/Domus.Service/Implementations/UserService.cs +++ b/Domus.Service/Implementations/UserService.cs @@ -53,12 +53,30 @@ public async Task CreateUser(CreateUserRequest request) throw new UserAlreadyExistsException("The username is already in use"); if (!Regex.IsMatch(request.Password, PasswordConstants.PasswordPattern)) throw new PasswordTooWeakException(PasswordConstants.PasswordPatternErrorMessage); + if (!string.IsNullOrEmpty(request.Role) && await _roleManager.RoleExistsAsync(request.Role)) + throw new RoleNotFoundException(); var user = _mapper.Map(request); var result = await _userManager.CreateAsync(user, request.Password); - await EnsureRoleExistsAsync(UserRoleConstants.CLIENT); - await _userManager.AddToRoleAsync(user, UserRoleConstants.CLIENT); + if (string.IsNullOrEmpty(request.Role)) + { + await EnsureRoleExistsAsync(UserRoleConstants.CLIENT); + await _userManager.AddToRoleAsync(user, UserRoleConstants.CLIENT); + } + else if (request.Role == UserRoleConstants.STAFF) + { + await EnsureRoleExistsAsync(UserRoleConstants.CLIENT); + await EnsureRoleExistsAsync(UserRoleConstants.STAFF); + await _userManager.AddToRoleAsync(user, UserRoleConstants.CLIENT); + await _userManager.AddToRoleAsync(user, UserRoleConstants.STAFF); + } + else + { + await EnsureRoleExistsAsync(request.Role); + await _userManager.AddToRoleAsync(user, request.Role); + } + if (result.Succeeded) { var returnedUser = await _userRepository.GetAsync(u => u.Email == request.Email); diff --git a/Domus.Service/Models/Requests/Users/CreateUserRequest.cs b/Domus.Service/Models/Requests/Users/CreateUserRequest.cs index def7824..8d9c038 100644 --- a/Domus.Service/Models/Requests/Users/CreateUserRequest.cs +++ b/Domus.Service/Models/Requests/Users/CreateUserRequest.cs @@ -27,4 +27,6 @@ public class CreateUserRequest [Required] [MatchesPattern(PasswordConstants.PasswordPattern, PasswordConstants.PasswordPatternErrorMessage)] public string Password { get; set; } = null!; + + public string? Role { get; set; } }