diff --git a/demo/WalkingTec.Mvvm.Demo/Startup.cs b/demo/WalkingTec.Mvvm.Demo/Startup.cs index b95955f83..92c73283a 100644 --- a/demo/WalkingTec.Mvvm.Demo/Startup.cs +++ b/demo/WalkingTec.Mvvm.Demo/Startup.cs @@ -114,7 +114,7 @@ public List DataPrivilegeSettings() //Add data privilege to specific type //指定哪些模型需要数据权限 //pris.Add(new DataPrivilegeInfo("城市权限", m => m.Name)); - //pris.Add(new DataPrivilegeInfo("学校权限", m => m.SchoolName)); + pris.Add(new DataPrivilegeInfo("学校权限", m => m.SchoolName)); //pris.Add(new DataPrivilegeInfo("专业权限", m => m.MajorName)); return pris; } diff --git a/src/WalkingTec.Mvvm.Core/Extensions/DCExtension.cs b/src/WalkingTec.Mvvm.Core/Extensions/DCExtension.cs index c907668e9..cb2e1eb9c 100644 --- a/src/WalkingTec.Mvvm.Core/Extensions/DCExtension.cs +++ b/src/WalkingTec.Mvvm.Core/Extensions/DCExtension.cs @@ -277,15 +277,23 @@ public static List GetSelectListItems(this IQueryable private static IQueryable AppendSelfDPWhere(IQueryable query, WTMContext wtmcontext, List dps) where T : TopBasePoco { var dpsSetting = wtmcontext?.DataPrivilegeSettings; - ParameterExpression pe = Expression.Parameter(typeof(T)); - Expression peid = Expression.Property(pe, typeof(T).GetSingleProperty("ID")); + Type modelTye = typeof(T); + bool isBasePoco = typeof(IBasePoco).IsAssignableFrom(modelTye); + ParameterExpression pe = Expression.Parameter(modelTye); + Expression peid = Expression.Property(pe, modelTye.GetSingleProperty("ID")); //循环数据权限,加入到where条件中,达到自动过滤的效果 + + Expression selfexp = Expression.NotEqual(Expression.Constant(1), Expression.Constant(1)); + if(isBasePoco == true) + { + selfexp = Expression.Equal(Expression.Property(pe, "CreateBy"), Expression.Constant(wtmcontext.LoginUserInfo?.ITCode)); + } if (dpsSetting?.Where(x => x.ModelName == query.ElementType.Name).SingleOrDefault() != null) { //如果dps参数是空,则生成 1!=1 这种错误的表达式,这样就查不到任何数据了 if (dps == null) { - query = query.Where(Expression.Lambda>(Expression.NotEqual(Expression.Constant(1), Expression.Constant(1)), pe)); + query = query.Where(Expression.Lambda>(selfexp, pe)); } else { @@ -293,13 +301,13 @@ private static IQueryable AppendSelfDPWhere(IQueryable query, WTMContex var ids = dps.Where(x => x.TableName == query.ElementType.Name).Select(x => x.RelateId).ToList(); if (ids == null || ids.Count() == 0) { - query = query.Where(Expression.Lambda>(Expression.NotEqual(Expression.Constant(1), Expression.Constant(1)), pe)); + query = query.Where(Expression.Lambda>(selfexp, pe)); } else { if (!ids.Contains(null)) { - query = query.Where(ids.GetContainIdExpression()); + query = query.Where(Expression.Lambda>(Expression.OrElse(selfexp, ids.GetContainIdExpression()))); } } } @@ -358,14 +366,21 @@ public static IQueryable DPWhere(this IQueryable baseQuery, WTMContext public static IQueryable DPWhere(this IQueryable baseQuery,WTMContext wtmcontext,List tableName, params Expression>[] IdFields) where T:TopBasePoco { var dps = wtmcontext?.LoginUserInfo?.DataPrivileges; + Type modelTye = typeof(T); + bool isBasePoco = typeof(IBasePoco).IsAssignableFrom(modelTye); // var dpsSetting = BaseVM.AllDPS; - ParameterExpression pe = Expression.Parameter(typeof(T)); + ParameterExpression pe = Expression.Parameter(modelTye); Expression left1 = Expression.Constant(1); Expression right1 = Expression.Constant(1); Expression trueExp = Expression.Equal(left1, right1); Expression falseExp = Expression.NotEqual(left1, right1); Expression finalExp = null; + Expression selfexp = falseExp; + if (isBasePoco == true) + { + selfexp = Expression.Equal(Expression.Property(pe, "CreateBy"), Expression.Constant(wtmcontext.LoginUserInfo?.ITCode)); + } int tindex = 0; //循环所有关联外键 foreach (var IdField in IdFields) @@ -399,7 +414,7 @@ public static IQueryable DPWhere(this IQueryable baseQuery,WTMContext w //如果dps为空,则拼接一个返回假的表达式,这样就查询不出任何数据 if (dps == null) { - exp = falseExp; + exp = selfexp; } else { @@ -435,15 +450,7 @@ public static IQueryable DPWhere(this IQueryable baseQuery,WTMContext w //如果没有关联的id,则拼接一个返回假的where,是语句查询不到任何数据 if (ids == null || ids.Count() == 0) { - exp = falseExp; - //if (peid.Type == typeof(Guid)) - //{ - // exp = Expression.Equal(peid, Expression.Constant(Guid.NewGuid())); - //} - //else - //{ - // exp = Expression.Equal(peid, Expression.Constant(null)); - //} + exp = selfexp; } //如果有关联 Id else @@ -477,6 +484,7 @@ public static IQueryable DPWhere(this IQueryable baseQuery,WTMContext w { exp = ids.GetContainIdExpression(typeof(T), pe, peid).Body; } + exp = Expression.OrElse(selfexp, exp); } } } diff --git a/src/WalkingTec.Mvvm.Core/Models/TopBasePoco.cs b/src/WalkingTec.Mvvm.Core/Models/TopBasePoco.cs index 5138e7b22..7a73aac35 100644 --- a/src/WalkingTec.Mvvm.Core/Models/TopBasePoco.cs +++ b/src/WalkingTec.Mvvm.Core/Models/TopBasePoco.cs @@ -95,6 +95,21 @@ public void SetID(object id) idpro.SetValue(this, id.ConvertValue(idpro.PropertyType)); } + + private bool? _isBasePoco = null; + [NotMapped] + [JsonIgnore] + public bool IsBasePoco + { + get + { + if(_isBasePoco == null) + { + _isBasePoco = typeof(IBasePoco).IsAssignableFrom(this.GetType()); + } + return _isBasePoco.Value; + } + } }