Преглед изворни кода

😁增加'仅本人数据'全局过滤器

zuohuaijun пре 3 година
родитељ
комит
ad6a4ff2dd

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

@@ -21,9 +21,9 @@
     <PackageReference Include="DotNetCore.Compile.Environment" Version="3.2.0" />
     <PackageReference Include="DotNetCore.Natasha.CSharp" Version="5.1.0" />
     <PackageReference Include="FluentEmail.Smtp" Version="3.0.2" />
-    <PackageReference Include="Furion.Extras.Authentication.JwtBearer" Version="4.8.8.3" />
-    <PackageReference Include="Furion.Extras.ObjectMapper.Mapster" Version="4.8.8.3" />
-    <PackageReference Include="Furion.Pure" Version="4.8.8.3" />
+    <PackageReference Include="Furion.Extras.Authentication.JwtBearer" Version="4.8.8.4" />
+    <PackageReference Include="Furion.Extras.ObjectMapper.Mapster" Version="4.8.8.4" />
+    <PackageReference Include="Furion.Pure" Version="4.8.8.4" />
     <PackageReference Include="IPTools.China" Version="1.6.0" />
     <PackageReference Include="Lazy.Captcha.Core" Version="2.0.3" />
     <PackageReference Include="Magicodes.IE.Excel" Version="2.7.4.4" />

+ 2 - 2
Admin.NET/Admin.NET.Core/Const/CacheConst.cs

@@ -26,9 +26,9 @@ public class CacheConst
     public const string KeyOrgIdList = "org:";
 
     /// <summary>
-    /// 最大角色数据范围缓存
+    /// 角色最大数据范围缓存
     /// </summary>
-    public const string KeyMaxDataScopeType = "maxDataScopeType:";
+    public const string KeyMaxDataScope = "maxDataScope:";
 
     /// <summary>
     /// 验证码缓存

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

@@ -236,10 +236,10 @@ public class SysOrgService : IDynamicApiController, ITransient
     /// <returns></returns>
     private async Task<List<long>> GetUserOrgIdList(List<SysRole> roleList)
     {
-        // 按最大范围策略设定(如果同时拥有ALL和SELF的权限,则结果ALL)
+        // 按最大范围策略设定(若同时拥有ALL和SELF权限,则结果ALL)
         int strongerDataScopeType = (int)DataScopeEnum.Self;
 
-        // 数据范围拥有的角色集合
+        // 角色集合拥有的数据范围
         var customDataScopeRoleIdList = new List<long>();
         if (roleList != null && roleList.Count > 0)
         {
@@ -258,7 +258,7 @@ public class SysOrgService : IDynamicApiController, ITransient
         var orgIdList2 = await GetOrgIdListByDataScope(strongerDataScopeType);
 
         // 缓存当前用户最大角色数据范围
-        _sysCacheService.Set(CacheConst.KeyMaxDataScopeType + _userManager.UserId, strongerDataScopeType);
+        _sysCacheService.Set(CacheConst.KeyMaxDataScope + _userManager.UserId, strongerDataScopeType);
 
         // 并集机构集合
         return orgIdList1.Union(orgIdList2).ToList();

+ 55 - 5
Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarFilter.cs

@@ -8,15 +8,18 @@ public static class SqlSugarFilter
     private static readonly ICache _cache = Cache.Default;
 
     /// <summary>
-    /// 配置用户机构范围过滤器
+    /// 配置用户机构集合过滤器
     /// </summary>
     public static void SetOrgEntityFilter(SqlSugarScopeProvider db)
     {
+        // 若仅本人数据,则直接返回
+        if (SetDataScopeFilter(db) == (int)DataScopeEnum.Self) return;
+
         var userId = App.User?.FindFirst(ClaimConst.UserId)?.Value;
         if (string.IsNullOrWhiteSpace(userId)) return;
 
-        // 配置用户机构范围缓存
-        var cacheKey = $"db:{db.CurrentConnectionConfig.ConfigId}:UserId:{userId}";
+        // 配置用户机构集合缓存
+        var cacheKey = $"db:{db.CurrentConnectionConfig.ConfigId}:orgList:{userId}";
         var orgFilter = _cache.Get<ConcurrentDictionary<Type, LambdaExpression>>(cacheKey);
         if (orgFilter == null)
         {
@@ -51,13 +54,60 @@ public static class SqlSugarFilter
         }
     }
 
+    /// <summary>
+    /// 配置用户仅本人数据过滤器
+    /// </summary>
+    public static int SetDataScopeFilter(SqlSugarScopeProvider db)
+    {
+        var maxDataScope = (int)DataScopeEnum.All;
+
+        var userId = App.User?.FindFirst(ClaimConst.UserId)?.Value;
+        if (string.IsNullOrWhiteSpace(userId)) return maxDataScope;
+
+        // 获取用户最大数据范围---仅本人数据
+        maxDataScope = App.GetService<SysCacheService>().Get<int>(CacheConst.KeyMaxDataScope + userId);
+        if (maxDataScope != (int)DataScopeEnum.Self) return maxDataScope;
+
+        // 配置用户数据范围缓存
+        var cacheKey = $"db:{db.CurrentConnectionConfig.ConfigId}:dataScope:{userId}";
+        var dataScopeFilter = _cache.Get<ConcurrentDictionary<Type, LambdaExpression>>(cacheKey);
+        if (dataScopeFilter == null)
+        {
+            // 获取业务实体数据表
+            var entityTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass
+                && u.BaseType == typeof(EntityBaseData));
+            if (!entityTypes.Any()) return maxDataScope;
+
+            dataScopeFilter = new ConcurrentDictionary<Type, LambdaExpression>();
+            foreach (var entityType in entityTypes)
+            {
+                // 排除非当前数据库实体
+                var tAtt = entityType.GetCustomAttribute<TenantAttribute>();
+                if ((tAtt != null && db.CurrentConnectionConfig.ConfigId.ToString() != tAtt.configId.ToString()))
+                    continue;
+
+                var lambda = DynamicExpressionParser.ParseLambda(new[] {
+                    Expression.Parameter(entityType, "u") }, typeof(bool), $"u.{nameof(EntityBaseData.CreateUserId)}=@0", userId);
+                db.QueryFilter.AddTableFilter(entityType, lambda);
+                dataScopeFilter.TryAdd(entityType, lambda);
+            }
+            _cache.Add(cacheKey, dataScopeFilter);
+        }
+        else
+        {
+            foreach (var filter in dataScopeFilter)
+                db.QueryFilter.AddTableFilter(filter.Key, filter.Value);
+        }
+        return maxDataScope;
+    }
+
     /// <summary>
     /// 配置自定义过滤器
     /// </summary>
     public static void SetCustomEntityFilter(SqlSugarScopeProvider db)
     {
-        // 配置用户机构范围缓存
-        var cacheKey = $"db:{db.CurrentConnectionConfig.ConfigId}:Custom";
+        // 配置自定义缓存
+        var cacheKey = $"db:{db.CurrentConnectionConfig.ConfigId}:custom";
         var tableFilterItemList = _cache.Get<List<TableFilterItem<object>>>(cacheKey);
         if (tableFilterItemList == null)
         {