优化数据过滤

This commit is contained in:
不做码农 2022-04-09 19:11:16 +08:00
parent ed483d8b56
commit 0833b70dd9
10 changed files with 53 additions and 53 deletions

View File

@ -33,28 +33,26 @@ namespace ZR.Admin.WebApi.Extensions
int dbType_bus = Convert.ToInt32(Configuration[OptionsSetting.ConnBusDbType]);
SugarIocServices.AddSqlSugar(new List<IocConfig>() {
new IocConfig() {
ConfigId = "0",
ConnectionString = connStr,
DbType = (IocDbType)dbType,
IsAutoCloseConnection = true
}, new IocConfig() {
ConfigId = "1",
ConnectionString = connStrBus,
DbType = (IocDbType)dbType_bus,
IsAutoCloseConnection = true
}
});
new IocConfig() {
ConfigId = "0",
ConnectionString = connStr,
DbType = (IocDbType)dbType,
IsAutoCloseConnection = true
}, new IocConfig() {
ConfigId = "1",
ConnectionString = connStrBus,
DbType = (IocDbType)dbType_bus,
IsAutoCloseConnection = true
}
});
SugarIocServices.ConfigurationSugar(db =>
{
#region db0
db.GetConnection(0).Aop.OnLogExecuting = (sql, pars) =>
{
var param = db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value));
FilterData(db.GetConnection(0));
logger.Info($"【sql语句】{sql}{param}");
var param = db.GetConnection(0).Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value));
FilterData(0);
logger.Info($"【sql语句】{sql}{param}\n");
};
db.GetConnection(0).Aop.OnError = (e) =>
@ -72,8 +70,8 @@ namespace ZR.Admin.WebApi.Extensions
//Db1
db.GetConnection(1).Aop.OnLogExecuting = (sql, pars) =>
{
var param = DbScoped.SugarScope.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value));
var param = db.GetConnection(1).Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value));
FilterData(1);
logger.Info($"【sql语句】{sql}, {param}");
};
//Db1错误日志
@ -88,7 +86,8 @@ namespace ZR.Admin.WebApi.Extensions
/// <summary>
/// 分页获取count 不会追加sql
/// </summary>
private static void FilterData(ISqlSugarClient sqlSugarClient)
/// <param name="configId">多库id</param>
private static void FilterData(int configId)
{
var u = App.User;
if (u == null) return;
@ -97,8 +96,8 @@ namespace ZR.Admin.WebApi.Extensions
if (user == null) return;
//管理员不过滤
if (user.RoleIds.Any(f => f.Equals("admin"))) return;
foreach (var role in user.Roles)
var db = DbScoped.SugarScope.GetConnection(configId);
foreach (var role in user.Roles.OrderBy(f => f.DataScope))
{
string dataScope = role.DataScope;
if (DATA_SCOPE_ALL.Equals(dataScope))//所有权限
@ -107,19 +106,14 @@ namespace ZR.Admin.WebApi.Extensions
}
else if (DATA_SCOPE_CUSTOM.Equals(dataScope))//自定数据权限
{
//有问题
//var roleDepts = db0.Queryable<SysRoleDept>()
//.Where(f => f.RoleId == role.RoleId).Select(f => f.DeptId).ToList();
//var filter1 = new TableFilterItem<SysDept>(it => roleDepts.Contains(it.DeptId));
//DbScoped.SugarScope.GetConnection(0).QueryFilter.Add(filter1);
//" OR {}.dept_id IN ( SELECT dept_id FROM sys_role_dept WHERE role_id = {} ) ", deptAlias, role.getRoleId()));
var filter1 = new TableFilterItem<SysUser>(it => SqlFunc.Subqueryable<SysRoleDept>().Where(f => f.DeptId == it.DeptId && f.RoleId == role.RoleId).Any());
db.QueryFilter.Add(filter1);
}
else if (DATA_SCOPE_DEPT.Equals(dataScope))//本部门数据
{
//有问题添加后的SQL 语句 是 AND deptId = @deptId
var exp = Expressionable.Create<SysDept>();
exp.Or(it => it.DeptId == user.DeptId);
var filter1 = new TableFilterItem<SysDept>(exp.ToExpression());
sqlSugarClient.QueryFilter.Add(filter1);
var filter1 = new TableFilterItem<SysDept>(it => it.DeptId == user.DeptId);
db.QueryFilter.Add(filter1);
}
else if (DATA_SCOPE_DEPT_AND_CHILD.Equals(dataScope))//本部门及以下数据
{
@ -128,7 +122,7 @@ namespace ZR.Admin.WebApi.Extensions
else if (DATA_SCOPE_SELF.Equals(dataScope))//仅本人数据
{
var filter1 = new TableFilterItem<SysUser>(it => it.UserId == user.UserId, true);
sqlSugarClient.QueryFilter.Add(filter1);
db.QueryFilter.Add(filter1);
}
}
}

View File

@ -131,7 +131,7 @@ namespace ZR.Admin.WebApi.Framework
{
var userData = jwtToken.FirstOrDefault(x => x.Type == ClaimTypes.UserData).Value;
var loginUser = JsonConvert.DeserializeObject<LoginUser>(userData);
var permissions = (List<string>)CacheHelper.GetCache(GlobalConstant.UserPermKEY + loginUser?.UserId);
var permissions = loginUser.Permissions;//TODO (List<string>)CacheHelper.GetCache(GlobalConstant.UserPermKEY + loginUser?.UserId);
if (loginUser?.UserName == "admin")
{
permissions = new List<string>() { GlobalConstant.AdminPerm };

View File

@ -57,7 +57,7 @@
<rules>
<!-- 除非调试需要,把 .NET Core 程序集的 Debug 输出都屏蔽 Trace -> Debug-> Info ->Warn-> Error-> Critical-->
<!--跳过所有级别的Microsoft组件的日志记录-->
<!--<logger name="Microsoft.*" writeTo="blackhole" final="true" />-->
<logger name="Microsoft.*" writeTo="blackhole" final="true" />
<!-- 除非调试需要,把系统的 Debug 输出都屏蔽 -->
<!--<logger name="System.*" writeTo="blackhole" final="true" />-->
<!-- Quartz -->

View File

@ -123,7 +123,7 @@ namespace ZR.Admin.WebApi
//2.再开启授权
app.UseAuthorization();
//开启session
app.UseSession();
//app.UseSession();
//开启缓存
app.UseResponseCaching();
//恢复/启动任务

View File

@ -25,7 +25,7 @@
<PackageReference Include="NLog.Web.AspNetCore" Version="4.14.0" />
<PackageReference Include="Snowflake.Core" Version="2.0.0" />
<PackageReference Include="SqlSugar.IOC" Version="1.8.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.3.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="7.0.2" />
<PackageReference Include="UAParser" Version="3.1.47" />
</ItemGroup>

View File

@ -12,6 +12,6 @@
<ItemGroup>
<PackageReference Include="JinianNet.JNTemplate" Version="2.2.5" />
<PackageReference Include="SqlSugarCoreNoDrive" Version="5.0.6" />
<PackageReference Include="SqlSugarCoreNoDrive" Version="5.0.6.6" />
</ItemGroup>
</Project>

View File

@ -7,7 +7,7 @@
<ItemGroup>
<PackageReference Include="EPPlus" Version="5.8.6" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="SqlSugarCoreNoDrive" Version="5.0.6" />
<PackageReference Include="SqlSugarCoreNoDrive" Version="5.0.6.6" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
</ItemGroup>

View File

@ -18,18 +18,24 @@ namespace ZR.Repository
public class BaseRepository<T> : SimpleClient<T> where T : class, new()
{
public ITenant itenant = null;//多租户事务
public BaseRepository(ISqlSugarClient client = null) : base(client)
public BaseRepository(ISqlSugarClient context = null) : base(context)
{
//通过特性拿到ConfigId
var configId = typeof(T).GetCustomAttribute<TenantAttribute>()?.configId;
if (configId != null)
//var configId = typeof(T).GetCustomAttribute<TenantAttribute>()?.configId;
//if (configId != null)
//{
// itenant = DbScoped.SugarScope;//设置租户接口
// Context = DbScoped.SugarScope.GetConnection(configId);
//}
//else
//{
// Context = context ?? DbScoped.SugarScope.GetConnection(1);//根据类传入的ConfigId自动选择
//}
Context = DbScoped.SugarScope.GetConnectionWithAttr<T>();
itenant = DbScoped.SugarScope;//设置租户接口
if (Context == null)
{
Context = DbScoped.SugarScope.GetConnection(configId);
itenant = DbScoped.SugarScope;//设置租户接口
}
else
{
Context = client ?? DbScoped.SugarScope.GetConnection(1);//根据类传入的ConfigId自动选择
Context = context ?? DbScoped.SugarScope.GetConnection(1);//根据类传入的ConfigId自动选择
}
}

View File

@ -14,7 +14,7 @@
<PackageReference Include="MySql.Data" Version="8.0.25" />
<PackageReference Include="NETCore.Encrypt" Version="2.1.0" />
<PackageReference Include="SqlSugar.IOC" Version="1.8.0" />
<PackageReference Include="SqlSugarCoreNoDrive" Version="5.0.6" />
<PackageReference Include="SqlSugarCoreNoDrive" Version="5.0.6.6" />
</ItemGroup>
<ItemGroup>

View File

@ -203,10 +203,10 @@ export default {
dictValue: '1',
dictLabel: '全部'
},
// {
// dictValue: "2",
// dictLabel: "",
// },
{
dictValue: "2",
dictLabel: "自定义",
},
{
dictValue: '3',
dictLabel: '本部门'