Skip to content

Commit

Permalink
update data privilege, if model is basepoco, privilege filter will sh…
Browse files Browse the repository at this point in the history
…ow items that this persion created
  • Loading branch information
liuliang-wt committed Sep 7, 2021
1 parent 407075e commit 5868152
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
2 changes: 1 addition & 1 deletion demo/WalkingTec.Mvvm.Demo/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public List<IDataPrivilege> DataPrivilegeSettings()
//Add data privilege to specific type
//指定哪些模型需要数据权限
//pris.Add(new DataPrivilegeInfo<City>("城市权限", m => m.Name));
//pris.Add(new DataPrivilegeInfo<School>("学校权限", m => m.SchoolName));
pris.Add(new DataPrivilegeInfo<School>("学校权限", m => m.SchoolName));
//pris.Add(new DataPrivilegeInfo<Major>("专业权限", m => m.MajorName));
return pris;
}
Expand Down
40 changes: 24 additions & 16 deletions src/WalkingTec.Mvvm.Core/Extensions/DCExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,29 +277,37 @@ public static List<ComboSelectListItem> GetSelectListItems<T>(this IQueryable<T>
private static IQueryable<T> AppendSelfDPWhere<T>(IQueryable<T> query, WTMContext wtmcontext, List<SimpleDataPri> 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<Func<T, bool>>(Expression.NotEqual(Expression.Constant(1), Expression.Constant(1)), pe));
query = query.Where(Expression.Lambda<Func<T, bool>>(selfexp, pe));
}
else
{
//在dps中找到和baseQuery源数据表名一样的关联id
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<Func<T, bool>>(Expression.NotEqual(Expression.Constant(1), Expression.Constant(1)), pe));
query = query.Where(Expression.Lambda<Func<T, bool>>(selfexp, pe));
}
else
{
if (!ids.Contains(null))
{
query = query.Where(ids.GetContainIdExpression<T>());
query = query.Where(Expression.Lambda<Func<T, bool>>(Expression.OrElse(selfexp, ids.GetContainIdExpression<T>())));
}
}
}
Expand Down Expand Up @@ -358,14 +366,21 @@ public static IQueryable<T> DPWhere<T>(this IQueryable<T> baseQuery, WTMContext
public static IQueryable<T> DPWhere<T>(this IQueryable<T> baseQuery,WTMContext wtmcontext,List<string> tableName, params Expression<Func<T, object>>[] 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)
Expand Down Expand Up @@ -399,7 +414,7 @@ public static IQueryable<T> DPWhere<T>(this IQueryable<T> baseQuery,WTMContext w
//如果dps为空,则拼接一个返回假的表达式,这样就查询不出任何数据
if (dps == null)
{
exp = falseExp;
exp = selfexp;
}
else
{
Expand Down Expand Up @@ -435,15 +450,7 @@ public static IQueryable<T> DPWhere<T>(this IQueryable<T> 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
Expand Down Expand Up @@ -477,6 +484,7 @@ public static IQueryable<T> DPWhere<T>(this IQueryable<T> baseQuery,WTMContext w
{
exp = ids.GetContainIdExpression(typeof(T), pe, peid).Body;
}
exp = Expression.OrElse(selfexp, exp);
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/WalkingTec.Mvvm.Core/Models/TopBasePoco.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}


Expand Down

0 comments on commit 5868152

Please sign in to comment.