Selaa lähdekoodia

😁删除角色时,若有用户则禁止删除

zuohuaijun 2 vuotta sitten
vanhempi
commit
3bb9e052da

+ 1 - 1
Admin.NET/Admin.NET.Application/Configuration/Logging.json

@@ -31,7 +31,7 @@
       "BahLogLevel": "Information", // Oops.Oh 和 Oops.Bah 业务日志输出级别
       "WithReturnValue": true, // 是否包含返回值,默认true
       "ReturnValueThreshold": 500, // 返回值字符串阈值,默认0全量输出
-      "JsonBehavior": "OnlyJson", // 是否输出Json,默认None(OnlyJson、All)
+      "JsonBehavior": "None", // 是否输出Json,默认None(OnlyJson、All)
       "JsonIndented": false, // 是否格式化Json
       "UseUtcTimestamp": false // 时间格式UTC、LOCAL
     }

+ 1 - 1
Admin.NET/Admin.NET.Core/Admin.NET.Core.csproj

@@ -33,7 +33,7 @@
     <PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.NewtonsoftJson" Version="6.0.25" />
     <PackageReference Include="Microsoft.AspNetCore.SignalR.StackExchangeRedis" Version="6.0.25" />
     <PackageReference Include="NEST" Version="7.17.5" />
-    <PackageReference Include="NewLife.Redis" Version="5.5.2023.1102" />
+    <PackageReference Include="NewLife.Redis" Version="5.5.2023.1201" />
     <PackageReference Include="OnceMi.AspNetCore.OSS" Version="1.1.9" />
     <PackageReference Include="SKIT.FlurlHttpClient.Wechat.Api" Version="2.34.0" />
     <PackageReference Include="SKIT.FlurlHttpClient.Wechat.TenpayV3" Version="2.20.1" />

+ 6 - 0
Admin.NET/Admin.NET.Core/Enum/ErrorCodeEnum.cs

@@ -178,6 +178,12 @@ public enum ErrorCodeEnum
     [ErrorCodeItemMetadata("已将其他地方登录账号下线")]
     D1024,
 
+    /// <summary>
+    /// 此角色下面存在账号禁止删除
+    /// </summary>
+    [ErrorCodeItemMetadata("此角色下面存在账号禁止删除")]
+    D1025,
+
     /// <summary>
     /// 父机构不存在
     /// </summary>

+ 1 - 1
Admin.NET/Admin.NET.Core/Service/Org/SysOrgService.cs

@@ -59,7 +59,6 @@ public class SysOrgService : IDynamicApiController, ITransient
                 .ToListAsync();
         }
 
-        var sysOrg = await _sysOrgRep.GetSingleAsync(u => u.Id == input.Id);
         var orgTree = new List<SysOrg>();
         if (_userManager.SuperAdmin)
         {
@@ -72,6 +71,7 @@ public class SysOrgService : IDynamicApiController, ITransient
             HandlerOrgTree(orgTree, userOrgIdList);
         }
 
+        var sysOrg = await _sysOrgRep.GetSingleAsync(u => u.Id == input.Id);
         if (sysOrg != null)
         {
             sysOrg.Children = orgTree;

+ 2 - 2
Admin.NET/Admin.NET.Core/Service/Pos/SysPosService.cs

@@ -89,13 +89,13 @@ public class SysPosService : IDynamicApiController, ITransient
         if (!_userManager.SuperAdmin && sysPos.CreateUserId != _userManager.UserId)
             throw Oops.Oh(ErrorCodeEnum.D6002);
 
-        // 该职位下是否有用户
+        // 若职位有用户则禁止删除
         var hasPosEmp = await _sysPosRep.ChangeRepository<SqlSugarRepository<SysUser>>()
             .IsAnyAsync(u => u.PosId == input.Id);
         if (hasPosEmp)
             throw Oops.Oh(ErrorCodeEnum.D6001);
 
-        // 该附属职位下是否有用户
+        // 若附属职位有用户则禁止删除
         var hasExtPosEmp = await _sysUserExtOrgService.HasUserPos(input.Id);
         if (hasExtPosEmp)
             throw Oops.Oh(ErrorCodeEnum.D6001);

+ 6 - 0
Admin.NET/Admin.NET.Core/Service/Role/SysRoleService.cs

@@ -133,10 +133,16 @@ public class SysRoleService : IDynamicApiController, ITransient
     [DisplayName("删除角色")]
     public async Task DeleteRole(DeleteRoleInput input)
     {
+        // 禁止删除系统管理员角色
         var sysRole = await _sysRoleRep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
         if (sysRole.Code == CommonConst.SysAdminRole)
             throw Oops.Oh(ErrorCodeEnum.D1019);
 
+        // 若角色有用户则禁止删除
+        var userIds = await _sysUserRoleService.GetUserIdList(input.Id);
+        if (userIds != null && userIds.Count > 0)
+            throw Oops.Oh(ErrorCodeEnum.D1025);
+
         await _sysRoleRep.DeleteAsync(sysRole);
 
         // 级联删除角色机构数据

+ 0 - 1
Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarRepository.cs

@@ -52,7 +52,6 @@ public class SqlSugarRepository<T> : SimpleClient<T> where T : class, new()
         // 根据租户Id切换库连接, 为空则返回默认库连接
         var sqlSugarScopeProvider = App.GetRequiredService<SysTenantService>().GetTenantDbConnectionScope(long.Parse(tenantId));
         if (sqlSugarScopeProvider == null) return;
-
         base.Context = sqlSugarScopeProvider;
     }
 }