xf 2 år sedan
förälder
incheckning
a5d20216d9

+ 15 - 19
src/Hotline.Api/Controllers/UserController.cs

@@ -129,12 +129,7 @@ public class UserController : BaseController
     public async Task Update([FromBody] UpdateUserDto dto)
     {
         var account = await _accountRepository.GetAsync(dto.Id, HttpContext.RequestAborted);
-        if (account is null)
-            throw UserFriendlyException.SameMessage("账号不存在");
-        if (_accountDomainService.IsLockedOut(account))
-            throw UserFriendlyException.SameMessage("账号已被锁定");
-        if(account.IsDeleted)
-            throw UserFriendlyException.SameMessage("账号不存在");
+        CheckAccountStatus(account);
 
         var user = await _userRepository.GetAsync(dto.Id, HttpContext.RequestAborted);
         if (user is null)
@@ -297,12 +292,7 @@ public class UserController : BaseController
     public async Task ChangePassword([FromBody] ChangePasswordDto dto)
     {
         var account = await _accountRepository.GetAsync(_sessionContext.RequiredUserId, HttpContext.RequestAborted);
-        if (account == null)
-            throw UserFriendlyException.SameMessage("无效账号编号");
-        if (_accountDomainService.IsLockedOut(account))
-            throw UserFriendlyException.SameMessage("账号已被锁定");
-        if (account.IsDeleted)
-            throw UserFriendlyException.SameMessage("账号不存在");
+        CheckAccountStatus(account);
 
         var result = await _accountDomainService.ResetPasswordAsync(account, dto.CurrentPassword, dto.NewPassword,
              HttpContext.RequestAborted);
@@ -320,13 +310,8 @@ public class UserController : BaseController
     public async Task InitialPassword(string userId)
     {
         var account = await _accountRepository.GetAsync(userId, HttpContext.RequestAborted);
-        if (account == null)
-            throw UserFriendlyException.SameMessage("无效账号编号");
-        if (_accountDomainService.IsLockedOut(account))
-            throw UserFriendlyException.SameMessage("账号已被锁定");
-        if (account.IsDeleted)
-            throw UserFriendlyException.SameMessage("账号不存在");
-        await _accountDomainService.InitialPasswordAsync(account, HttpContext.RequestAborted);
+        CheckAccountStatus(account);
+         await _accountDomainService.InitialPasswordAsync(account, HttpContext.RequestAborted);
     }
 
     /// <summary>
@@ -370,4 +355,15 @@ public class UserController : BaseController
             GenderOptions = EnumExts.GetDescriptions<EGender>()
         };
     }
+
+
+    private void CheckAccountStatus(Account? account)
+    {
+        if (account == null)
+            throw UserFriendlyException.SameMessage("无效账号编号");
+        if (_accountDomainService.IsLockedOut(account))
+            throw UserFriendlyException.SameMessage("账号已被锁定");
+        if (account.IsDeleted)
+            throw UserFriendlyException.SameMessage("账号不存在");
+    }
 }

+ 2 - 0
src/Hotline.Application/Mappers/MapperConfigs.cs

@@ -29,9 +29,11 @@ namespace Hotline.Application.Mappers
             config.NewConfig<User, UserDto>()
                 .IgnoreIf((s, d) => s.Account == null, d => d.UserName)
                 .IgnoreIf((s, d) => s.Organization == null, d => d.OrgName)
+                .IgnoreIf((s, d) => s.Roles == null || !s.Roles.Any(), d => d.RoleNames)
                 .Map(d => d.UserName, x => x.Account.UserName)
                 .Map(d => d.OrgName, x => x.Organization.OrgName)
                 .Map(d => d.State, x => x.IsDeleted ? "已删除" : "正常")
+                .Map(d => d.RoleNames, x => string.Join(',', x.Roles.Select(s => s.DisplayName)))
                 .IgnoreNullValues(true);
 
             config.NewConfig<Role, RoleDto>()

+ 2 - 2
src/Hotline.Share/Dtos/Users/UserDto.cs

@@ -15,13 +15,13 @@ public record UserDto : AddUserDto
 
     public string OrgName { get; set; }
 
-    public string RoleNames => string.Join(',', Roles.Select(d => d.DisplayName));
+    public string RoleNames { get; set; }
 
     public string State { get; set; }
 
     public bool IsDeleted { get; set; }
 
-    public IReadOnlyList<RoleDto> Roles { get; set; } = new List<RoleDto>();
+    public IReadOnlyList<RoleDto> Roles { get; set; }
 }
 
 public record AddUserDto