浏览代码

Merge branch 'master' of http://git.fwt.com/Hotline/hotline

TANG JIANG 2 年之前
父节点
当前提交
e72efaff9f

+ 24 - 2
src/Hotline.Api/Controllers/UserController.cs

@@ -130,16 +130,23 @@ public class UserController : BaseController
     {
         var account = await _accountRepository.GetAsync(dto.Id, HttpContext.RequestAborted);
         if (account is null)
-            throw UserFriendlyException.SameMessage("账号不存在");
+            throw UserFriendlyException.SameMessage("账号不存在");
         if (_accountDomainService.IsLockedOut(account))
-            throw UserFriendlyException.SameMessage("该账号已被锁定");
+            throw UserFriendlyException.SameMessage("账号已被锁定");
+        if(account.IsDeleted)
+            throw UserFriendlyException.SameMessage("账号不存在");
 
         var user = await _userRepository.GetAsync(dto.Id, HttpContext.RequestAborted);
         if (user is null)
             throw UserFriendlyException.SameMessage("无效用户编号");
+        if (user.IsDeleted)
+            throw UserFriendlyException.SameMessage("账号不存在");
 
         _mapper.Map(dto, user);
         await _userRepository.UpdateAsync(user, HttpContext.RequestAborted);
+
+        //set roles
+        await _accountRepository.SetAccountRolesAsync(account.Id, dto.RoleIds, HttpContext.RequestAborted);
     }
 
     /// <summary>
@@ -166,6 +173,9 @@ public class UserController : BaseController
 
             //initial pwd
             await _accountDomainService.InitialPasswordAsync(account, HttpContext.RequestAborted);
+
+            //set roles
+            await _accountRepository.SetAccountRolesAsync(account.Id, dto.RoleIds, HttpContext.RequestAborted);
             return account.Id;
         }
         else
@@ -173,6 +183,9 @@ public class UserController : BaseController
             if (_accountDomainService.IsLockedOut(account))
                 throw UserFriendlyException.SameMessage("该账号已被锁定,请联系管理员");
 
+            //set roles
+            await _accountRepository.SetAccountRolesAsync(account.Id, dto.RoleIds, HttpContext.RequestAborted);
+
             var user = await _userRepository.GetAsync(account.Id, HttpContext.RequestAborted);
             if (user is null)
             {
@@ -286,6 +299,11 @@ public class UserController : BaseController
         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("账号不存在");
+
         var result = await _accountDomainService.ResetPasswordAsync(account, dto.CurrentPassword, dto.NewPassword,
              HttpContext.RequestAborted);
         if (!result.Succeeded)
@@ -304,6 +322,10 @@ public class UserController : BaseController
         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);
     }
 

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

@@ -29,14 +29,13 @@ 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, d => d.Roles)
                 .Map(d => d.UserName, x => x.Account.UserName)
                 .Map(d => d.OrgName, x => x.Organization.OrgName)
-                .Map(d => d.Roles, x => string.Join(',', x.Roles.Select(d => d.DisplayName)))
                 .Map(d => d.State, x => x.IsDeleted ? "已删除" : "正常")
                 .IgnoreNullValues(true);
 
             config.NewConfig<Role, RoleDto>()
+                .IgnoreIf((s, d) => s.Accounts == null, d => d.AccountIds)
                 .Map(d => d.AccountIds, x => x.Accounts.Select(d => d.Id))
                 .Map(d => d.State, x => x.IsDeleted ? "已删除" : "正常");
 

+ 15 - 3
src/Hotline.Share/Dtos/Users/UserDto.cs

@@ -1,4 +1,5 @@
-using Hotline.Share.Enums.Order;
+using Hotline.Share.Dtos.Roles;
+using Hotline.Share.Enums.Order;
 using XF.Utility.EnumExtensions;
 
 namespace Hotline.Share.Dtos.Users;
@@ -14,13 +15,20 @@ public record UserDto : AddUserDto
 
     public string OrgName { get; set; }
 
-    public string Roles { get; set; }
-    
+    public string RoleNames => string.Join(',', Roles.Select(d => d.DisplayName));
+
     public string State { get; set; }
+
+    public bool IsDeleted { get; set; }
+
+    public IReadOnlyList<RoleDto> Roles { get; set; }
 }
 
 public record AddUserDto
 {
+    /// <summary>
+    /// 账号
+    /// </summary>
     public string UserName { get; set; }
 
     /// <summary>
@@ -56,6 +64,8 @@ public record AddUserDto
     public string? DefaultTelNo { get; set; }
 
     public string Email { get; set; }
+
+    public ICollection<string> RoleIds { get; set; }
 }
 
 public record UpdateUserDto
@@ -95,4 +105,6 @@ public record UpdateUserDto
     public string? DefaultTelNo { get; set; }
 
     public string Email { get; set; }
+
+    public ICollection<string> RoleIds { get; set; }
 }