数据权限优化

dev
cjy 2 months ago
parent 77eedc4aa8
commit 0c7d5b0c90

@ -1454,101 +1454,84 @@ public class CommonService : ICommonService
var rule = tenantDb.Queryable<CodeDataRule>()
.First(u => u.UserId == long.Parse(user.UserId) && u.PermissionEntity.ToLower() == moduleName && u.Status == StatusEnum.Enable);
if (rule == null) {
return tenantDb.Queryable<T>();
//var contidions = new List<IConditionalModel>();
//contidions.Add(new ConditionalModel { FieldName = "CreateBy", ConditionalType = ConditionalType.Equal, FieldValue = user.UserId });
//return tenantDb.Queryable<T>().Where(contidions); //没有设置数据规则,那么视为该资源允许被创建人查看
////没有设置数据规则,那么视为该资源允许被任何主体查看
}
#region 可视权限
if ((long)rule.VisibleTemplateId == 0)
{
var contidions = new List<IConditionalModel>();
contidions.Add(new ConditionalModel { FieldName = "CreateBy", ConditionalType = ConditionalType.Equal, FieldValue = user.UserId });
return tenantDb.Queryable<T>().Where(contidions); //没有设置数据规则,那么视为该资源允许被创建人查看
}
var ruleInfo = tenantDb.Queryable<CodeDataRuleTemplate>().First(x => x.Id == (long)rule.VisibleTemplateId);
if (ruleInfo.RuleScope == "all")
{
//var contidions = new List<IConditionalModel>();
//contidions.Add(new ConditionalModel { FieldName = "Deleted", ConditionalType = ConditionalType.Equal, FieldValue = "0" });
//return tenantDb.Queryable<T>().ClearFilter().Where(contidions);
return tenantDb.Queryable<T>().ClearFilter<IOrgId>(); //单条语句清空,只影响当前语句
}
if (ruleInfo.RuleScope == "none")
if (rule.IsNull())
{
SqlSugar.Check.ExceptionEasy("NO Operation", "没有数据查看权限!");
return tenantDb.Queryable<T>(); //默认查询
}
if (ruleInfo.RuleScope == "self_org")
{
return tenantDb.Queryable<T>();
//ruleInfo.DataRules = ruleInfo.DataRules.Replace(DataRuleConst.LoginOrg, userInfo.DefaultOrgId.ToString());
}
if (ruleInfo.RuleScope == "self")
else
{
//ruleInfo.DataRules = ruleInfo.DataRules.Replace(DataRuleConst.LoginUser, user.UserId);
var contidions = new List<IConditionalModel>();
contidions.Add(new ConditionalModel { FieldName = "CreateBy", ConditionalType = ConditionalType.Equal, FieldValue = user.UserId });
return tenantDb.Queryable<T>().Where(contidions); //没有设置数据规则,那么视为该资源允许被创建人查看
}
if (ruleInfo.RuleScope == "self_dept")
{
if (String.IsNullOrEmpty(userInfo.DeptName))
#region 可视权限
if ((long)rule.VisibleTemplateId == 0)
{
SqlSugar.Check.ExceptionEasy("NO Operation", "当前员工没有默认归属部门!");
contidions.Add(new ConditionalModel { FieldName = "CreateBy", ConditionalType = ConditionalType.Equal, FieldValue = user.UserId });
return tenantDb.Queryable<T>().Where(contidions); //查询自身
}
var deptUsers = db.Queryable<SysUser>().Where(x => x.DeptId == userInfo.DeptId).Select(x => x.Id).ToList();
var contidions = new List<IConditionalModel>();
contidions.Add(new ConditionalModel { FieldName = "CreateBy", ConditionalType = ConditionalType.In, FieldValue = string.Join(",", deptUsers) });
return tenantDb.Queryable<T>().Where(contidions);
//ruleInfo.DataRules = ruleInfo.DataRules.Replace(DataRuleConst.LoginDept, string.Join(",", deptUsers));
}
if (ruleInfo.RuleScope == "select_org")
{
//var orgUsers = db.Queryable<SysUser>().Where(x => x.DefaultOrgId == userInfo.DefaultOrgId).Select(x => x.Id).ToList();
//ruleInfo.DataRules = ruleInfo.DataRules.Replace(DataRuleConst.LoginDept, string.Join(",", orgUsers));
var conditions1 = ruleInfo.DataRules.ConvertSqlSugarExpression();
var conditionalModels1 =
db.ConfigQuery.Context.Utilities.JsonToConditionalModels(
JsonConvert.SerializeObject(conditions1));
return tenantDb.Queryable<T>().ClearFilter<IOrgId>().Where(conditionalModels1); //单条语句清空,只影响当前语句
}
if (ruleInfo.RuleScope == "select_user")
{
var conditions2 = ruleInfo.DataRules.ConvertSqlSugarExpression();
var conditionalModels2 =
db.ConfigQuery.Context.Utilities.JsonToConditionalModels(
JsonConvert.SerializeObject(conditions2));
return tenantDb.Queryable<T>().ClearFilter<IOrgId>().Where(conditionalModels2);//单条语句清空,只影响当前语句
}
if (ruleInfo.DataRules.IsNull())
{
var contidions = new List<IConditionalModel>();
contidions.Add(new ConditionalModel { FieldName = "CreateBy", ConditionalType = ConditionalType.Equal, FieldValue = user.UserId });
return tenantDb.Queryable<T>().Where(contidions); //没有设置数据规则,那么视为该资源允许被创建人查看
}
#endregion
var ruleInfo = tenantDb.Queryable<CodeDataRuleTemplate>().First(x => x.Id == (long)rule.VisibleTemplateId);
if (ruleInfo.RuleScope == "all")
{
return tenantDb.Queryable<T>().ClearFilter<IOrgId>(); //查询全部
}
else if (ruleInfo.RuleScope == "none")
{
SqlSugar.Check.ExceptionEasy("NO Operation", "没有数据查看权限!");//返回提示
}
else if (ruleInfo.RuleScope == "self_org")
{
return tenantDb.Queryable<T>();//默认查询
}
else if (ruleInfo.RuleScope == "self")
{
contidions.Add(new ConditionalModel { FieldName = "CreateBy", ConditionalType = ConditionalType.Equal, FieldValue = user.UserId });
return tenantDb.Queryable<T>().Where(contidions); //查询创建人自身信息
}
else if (ruleInfo.RuleScope == "self_dept")
{
if (String.IsNullOrEmpty(userInfo.DeptName))
{
SqlSugar.Check.ExceptionEasy("NO Operation", "当前员工没有默认归属部门!");
}
var deptUsers = db.Queryable<SysUser>().Where(x => x.DeptId == userInfo.DeptId).Select(x => x.Id).ToList();
contidions.Add(new ConditionalModel { FieldName = "CreateBy", ConditionalType = ConditionalType.In, FieldValue = string.Join(",", deptUsers) });
return tenantDb.Queryable<T>().Where(contidions); //查询本部门
}
else if (ruleInfo.RuleScope == "select_org")
{
var conditions1 = ruleInfo.DataRules.ConvertSqlSugarExpression();
var conditionalModels1 =
db.ConfigQuery.Context.Utilities.JsonToConditionalModels(
JsonConvert.SerializeObject(conditions1));
return tenantDb.Queryable<T>().ClearFilter<IOrgId>().Where(conditionalModels1); //查询指定机构
}
else if (ruleInfo.RuleScope == "select_user")
{
var conditions2 = ruleInfo.DataRules.ConvertSqlSugarExpression();
var conditionalModels2 =
db.ConfigQuery.Context.Utilities.JsonToConditionalModels(
JsonConvert.SerializeObject(conditions2));
return tenantDb.Queryable<T>().ClearFilter<IOrgId>().Where(conditionalModels2);//查询指定人员
}
else if (ruleInfo.RuleScope == "customize")
{
var conditions = ruleInfo.DataRules.ConvertSqlSugarExpression();
var conditionalModels =
db.ConfigQuery.Context.Utilities.JsonToConditionalModels(
JsonConvert.SerializeObject(conditions));
return tenantDb.Queryable<T>().Where(conditionalModels);//自定义查询
}
else if (ruleInfo.DataRules.IsNull())
{
contidions.Add(new ConditionalModel { FieldName = "CreateBy", ConditionalType = ConditionalType.Equal, FieldValue = user.UserId });
return tenantDb.Queryable<T>().Where(contidions); //没有设置数据规则,那么视为该资源允许被创建人查看
}
if (ruleInfo.RuleScope == "customize")
{
return tenantDb.Queryable<T>();
//var conditions = ruleInfo.DataRules.ConvertSqlSugarExpression();
//var conditionalModels =
// db.ConfigQuery.Context.Utilities.JsonToConditionalModels(
// JsonConvert.SerializeObject(conditions));
//return tenantDb.Queryable<T>().Where(conditionalModels);
#endregion
}
return tenantDb.Queryable<T>();
}
#region 获取数据库表及字段属性

Loading…
Cancel
Save