update DbExtension.cs

This commit is contained in:
不做码农 2022-01-22 16:57:38 +08:00
parent 4acf941b90
commit f5f5000854

View File

@ -13,6 +13,17 @@ namespace ZR.Admin.WebApi.Extensions
public static class DbExtension public static class DbExtension
{ {
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
//全部数据权限
public static string DATA_SCOPE_ALL = "1";
//自定数据权限
public static string DATA_SCOPE_CUSTOM = "2";
//部门数据权限
public static string DATA_SCOPE_DEPT = "3";
//部门及以下数据权限
public static string DATA_SCOPE_DEPT_AND_CHILD = "4";
//仅本人数据权限
public static string DATA_SCOPE_SELF = "5";
public static void AddDb(IConfiguration Configuration) public static void AddDb(IConfiguration Configuration)
{ {
string connStr = Configuration.GetConnectionString(OptionsSetting.ConnAdmin); string connStr = Configuration.GetConnectionString(OptionsSetting.ConnAdmin);
@ -35,22 +46,22 @@ namespace ZR.Admin.WebApi.Extensions
} }
}); });
//每次Sql执行前事件 //每次Sql执行前事件
var db0 = DbScoped.SugarScope.GetConnection(0);
db0.Aop.OnLogExecuting = (sql, pars) => DbScoped.SugarScope.GetConnection(0).Aop.OnLogExecuting = (sql, pars) =>
{ {
var param = DbScoped.SugarScope.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)); var param = DbScoped.SugarScope.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value));
FilterData(db0); FilterData(DbScoped.SugarScope.GetConnection(0));
logger.Info($"{sql}{param}"); logger.Info($"{sql}{param}");
}; };
//出错打印日志 //出错打印日志
db0.Aop.OnError = (e) => DbScoped.SugarScope.GetConnection(0).Aop.OnError = (e) =>
{ {
logger.Error(e, $"执行SQL出错{e.Message}"); logger.Error(e, $"执行SQL出错{e.Message}");
}; };
//SQL执行完 //SQL执行完
db0.Aop.OnLogExecuted = (sql, pars) => DbScoped.SugarScope.GetConnection(0).Aop.OnLogExecuted = (sql, pars) =>
{ {
//执行完了可以输出SQL执行时间 (OnLogExecutedDelegate) //执行完了可以输出SQL执行时间 (OnLogExecutedDelegate)
}; };
@ -66,53 +77,50 @@ namespace ZR.Admin.WebApi.Extensions
{ {
logger.Error($"执行Sql语句失败{e.Sql},原因:{e.Message}"); logger.Error($"执行Sql语句失败{e.Sql},原因:{e.Message}");
}; };
} }
private static void FilterData(SqlSugarProvider db0) private static void FilterData(SqlSugarProvider db0)
{ {
var u = App.User; var u = App.User;
if (u != null && u.Identity.IsAuthenticated) if (u == null) return;
{
//获取当前用户的信息 //获取当前用户的信息
var user = JwtUtil.GetLoginUser(App.HttpContext); var user = JwtUtil.GetLoginUser(App.HttpContext);
if (user != null) if (user == null) return;
{ //管理员不过滤
//非管理员过滤数据权限 if (user.RoleIds.Any(f => f.Equals("admin"))) return;
if (!user.RoleIds.Any(f => f.Equals("admin")))
{
//TODO 实现范围过滤
foreach (var role in user.Roles) foreach (var role in user.Roles)
{ {
string dataScope = role.DataScope; string dataScope = role.DataScope;
if ("1".Equals(dataScope)) if (DATA_SCOPE_ALL.Equals(dataScope))//所有权限
{ {
break; break;
} }
else if ("2".Equals(dataScope)) else if (DATA_SCOPE_CUSTOM.Equals(dataScope))//自定数据权限
{ {
//var roleDepts = db0.Queryable<SysRoleDept>() //var roleDepts = db0.Queryable<SysRoleDept>()
//.Where(f => f.RoleId == role.RoleId).Select(f => f.DeptId).ToList(); //.Where(f => f.RoleId == role.RoleId).Select(f => f.DeptId).ToList();
//var filter1 = new TableFilterItem<SysDept>(it => roleDepts.Contains(it.DeptId)); //var filter1 = new TableFilterItem<SysDept>(it => roleDepts.Contains(it.DeptId));
} }
else if ("3".Equals(dataScope)) else if (DATA_SCOPE_DEPT.Equals(dataScope))//本部门数据
{ {
var filter1 = new TableFilterItem<SysDept>(it => it.DeptId == user.DeptId); //有问题添加后的SQL 语句 是 AND deptId = @deptId
var exp = Expressionable.Create<SysDept>();
exp.Or(it => it.DeptId == user.DeptId);
var filter1 = new TableFilterItem<SysDept>(exp.ToExpression());
DbScoped.SugarScope.GetConnection(0).QueryFilter.Add(filter1);
Console.WriteLine("本部门数据过滤");
} }
else if ("4".Equals(dataScope)) else if (DATA_SCOPE_DEPT_AND_CHILD.Equals(dataScope))//本部门及以下数据
{ {
//SQl OR {}.dept_id IN ( SELECT dept_id FROM sys_dept WHERE dept_id = {} or find_in_set( {} , ancestors ) )
} }
else if ("5".Equals(dataScope)) else if (DATA_SCOPE_SELF.Equals(dataScope))//仅本人数据
{ {
var filter1 = new TableFilterItem<SysUser>(it => it.UserId == user.UserId); var filter1 = new TableFilterItem<SysUser>(it => it.UserId == user.UserId);
DbScoped.SugarScope.GetConnection(0).QueryFilter.Add(filter1);
} }
} }
} }
} }
} }
//TODO 在此实现数据过滤
//DbScoped.SugarScope.GetConnection(0).QueryFilter.Add(new TableFilterItem<SysUser>(it => it.DeptId == 333)); //为Order表置全局条件
}
}
}