Переглянути джерело

1.缓存最大角色数据范围,用于在配置自定义实体过滤器中进行过滤仅个人数据时用。
2.修复实体机构过滤器中orgIds.Count==0,生成的sql语句条件中会出现1=2的bug

kenny 3 роки тому
батько
коміт
dd5a8f87bc

+ 5 - 0
Admin.NET/Admin.NET.Core/Const/CacheConst.cs

@@ -25,6 +25,11 @@ public class CacheConst
     /// </summary>
     public const string KeyOrgIdList = "org:";
 
+    /// <summary>
+    /// 最大角色数据范围缓存
+    /// </summary>
+    public const string KeyStrongerDataScope = "strongerdatascope:";
+
     /// <summary>
     /// 验证码缓存
     /// </summary>

+ 4 - 0
Admin.NET/Admin.NET.Core/Service/Cache/ISysCacheService.cs

@@ -29,4 +29,8 @@ public interface ISysCacheService
     Task<List<string>> GetPermission(long userId);
 
     Task SetPermission(long userId, List<string> permissions);
+
+    Task<int?> GetStrongerDataScopeType(long userId);
+
+    Task SetStrongerDataScopeType(long userId, int dataScope);
 }

+ 28 - 0
Admin.NET/Admin.NET.Core/Service/Cache/SysCacheService.cs

@@ -241,4 +241,32 @@ public class SysCacheService : ISysCacheService, IDynamicApiController, ISinglet
 
         await AddCacheKey(cacheKey);
     }
+
+    /// <summary>
+    /// 获取最大角色数据范围
+    /// </summary>
+    /// <param name="userId"></param>
+    /// <returns></returns>
+    [NonAction]
+    public async Task<int?> GetStrongerDataScopeType(long userId)
+    {
+        var cacheKey = CacheConst.KeyStrongerDataScope + userId;
+        var res = await _cache.GetStringAsync(cacheKey);
+        return string.IsNullOrWhiteSpace(res) ? null : int.Parse(res);
+    }
+
+    /// <summary>
+    /// 缓存最大角色数据范围
+    /// </summary>
+    /// <param name="userId"></param>
+    /// <param name="dataScope"></param>
+    /// <returns></returns>
+    [NonAction]
+    public async Task SetStrongerDataScopeType(long userId, int dataScope)
+    {
+        var cacheKey = CacheConst.KeyStrongerDataScope + userId;
+        await _cache.SetStringAsync(cacheKey, dataScope.ToString());
+
+        await AddCacheKey(cacheKey);
+    }
 }

+ 4 - 0
Admin.NET/Admin.NET.Core/Service/Org/SysOrgService.cs

@@ -245,6 +245,10 @@ public class SysOrgService : IDynamicApiController, ITransient
         var orgIdList1 = await _sysRoleOrgService.GetRoleOrgIdList(customDataScopeRoleIdList);
         // 根据数据范围获取机构集合
         var orgIdList2 = await GetOrgIdListByDataScope(strongerDataScopeType);
+
+        var userId = _userManager.UserId;
+        //缓存当前用户最大范围策略设定权限值
+        await _sysCacheService.SetStrongerDataScopeType(userId, strongerDataScopeType); // 存缓存
         // 并集机构集合
         return orgIdList1.Concat(orgIdList2).Distinct().ToList();
     }

+ 2 - 0
Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs

@@ -229,6 +229,8 @@ public static class SqlSugarSetup
         // 获取用户机构Id集合
         var orgIds = await App.GetService<SysCacheService>().GetOrgIdList(long.Parse(userId));
         if (orgIds == null) return;
+        //如果不判断orgIds.Count==0,生成的sql语句条件中会出现1=2的bug
+        if (orgIds.Count == 0) return;
 
         foreach (var dataEntityType in dataEntityTypes)
         {