mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-08-24 16:18:45 +08:00
增加SqlSugar启动
This commit is contained in:
parent
0699413f39
commit
a6c79bea7c
@ -17,6 +17,7 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using SqlSugar;
|
||||||
|
|
||||||
namespace Infrastructure
|
namespace Infrastructure
|
||||||
{
|
{
|
||||||
@ -198,6 +199,23 @@ namespace Infrastructure
|
|||||||
return query;
|
return query;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 转换FilterGroup为Lambda表达式
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="query"></param>
|
||||||
|
/// <param name="parametername"></param>
|
||||||
|
/// <param name="filterGroup"></param>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static ISugarQueryable<T> GenerateFilter<T>(this ISugarQueryable<T> query, string parametername,
|
||||||
|
FilterGroup filterGroup)
|
||||||
|
{
|
||||||
|
var param = CreateLambdaParam<T>(parametername);
|
||||||
|
Expression result = ConvertGroup<T>(filterGroup, param);
|
||||||
|
query = query.Where(param.GenerateTypeLambda<T>(result));
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 转换filtergroup为表达式
|
/// 转换filtergroup为表达式
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
|
||||||
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
|
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
|
||||||
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.2" />
|
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.2" />
|
||||||
|
<PackageReference Include="SqlSugarCore" Version="5.1.4.102" />
|
||||||
<PackageReference Include="StackExchange.Redis" Version="2.2.4" />
|
<PackageReference Include="StackExchange.Redis" Version="2.2.4" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
99
OpenAuth.App/Base/SqlSugarBaseApp.cs
Normal file
99
OpenAuth.App/Base/SqlSugarBaseApp.cs
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using Infrastructure;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using OpenAuth.App.Interface;
|
||||||
|
using OpenAuth.Repository.Core;
|
||||||
|
using OpenAuth.Repository.Domain;
|
||||||
|
using OpenAuth.Repository.Interface;
|
||||||
|
using SqlSugar;
|
||||||
|
|
||||||
|
namespace OpenAuth.App
|
||||||
|
{
|
||||||
|
public abstract class SqlSugarBaseApp<T>
|
||||||
|
{
|
||||||
|
protected ISqlSugarClient SugarClient;
|
||||||
|
|
||||||
|
protected IAuth _auth;
|
||||||
|
|
||||||
|
public SqlSugarBaseApp(ISqlSugarClient client, IAuth auth)
|
||||||
|
{
|
||||||
|
SugarClient = client;
|
||||||
|
_auth = auth;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取当前登录用户的数据访问权限
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="parametername">linq表达式参数的名称,如u=>u.name中的"u"</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
protected ISugarQueryable<T> GetDataPrivilege(string parametername)
|
||||||
|
{
|
||||||
|
var loginUser = _auth.GetCurrentUser();
|
||||||
|
if (loginUser.User.Account == Define.SYSTEM_USERNAME) return SugarClient.Queryable<T>(); //超级管理员特权
|
||||||
|
|
||||||
|
var moduleName = typeof(T).Name;
|
||||||
|
var rule = SugarClient.Queryable<DataPrivilegeRule>().First(u => u.SourceCode == moduleName);
|
||||||
|
if (rule == null) return SugarClient.Queryable<T>(); //没有设置数据规则,那么视为该资源允许被任何主体查看
|
||||||
|
if (rule.PrivilegeRules.Contains(Define.DATAPRIVILEGE_LOGINUSER) ||
|
||||||
|
rule.PrivilegeRules.Contains(Define.DATAPRIVILEGE_LOGINROLE)||
|
||||||
|
rule.PrivilegeRules.Contains(Define.DATAPRIVILEGE_LOGINORG))
|
||||||
|
{
|
||||||
|
|
||||||
|
//即把{loginUser} =='xxxxxxx'换为 loginUser.User.Id =='xxxxxxx',从而把当前登录的用户名与当时设计规则时选定的用户id对比
|
||||||
|
rule.PrivilegeRules = rule.PrivilegeRules.Replace(Define.DATAPRIVILEGE_LOGINUSER, loginUser.User.Id);
|
||||||
|
|
||||||
|
var roles = loginUser.Roles.Select(u => u.Id).ToList();
|
||||||
|
roles.Sort(); //按字母排序,这样可以进行like操作
|
||||||
|
rule.PrivilegeRules = rule.PrivilegeRules.Replace(Define.DATAPRIVILEGE_LOGINROLE,
|
||||||
|
string.Join(',',roles));
|
||||||
|
|
||||||
|
var orgs = loginUser.Orgs.Select(u => u.Id).ToList();
|
||||||
|
orgs.Sort();
|
||||||
|
rule.PrivilegeRules = rule.PrivilegeRules.Replace(Define.DATAPRIVILEGE_LOGINORG,
|
||||||
|
string.Join(',',orgs));
|
||||||
|
}
|
||||||
|
return SugarClient.Queryable<T>().GenerateFilter(parametername,
|
||||||
|
JsonHelper.Instance.Deserialize<FilterGroup>(rule.PrivilegeRules));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 计算实体更新的层级信息
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="U">U必须是一个继承TreeEntity的结构</typeparam>
|
||||||
|
/// <param name="entity"></param>
|
||||||
|
public void CaculateCascade<U>(U entity) where U : TreeEntity
|
||||||
|
{
|
||||||
|
if (entity.ParentId == "") entity.ParentId = null;
|
||||||
|
string cascadeId;
|
||||||
|
int currentCascadeId = 1; //当前结点的级联节点最后一位
|
||||||
|
var sameLevels = SugarClient.Queryable<U>().Where(o => o.ParentId == entity.ParentId && o.Id != entity.Id);
|
||||||
|
foreach (var obj in sameLevels.ToList())
|
||||||
|
{
|
||||||
|
int objCascadeId = int.Parse(obj.CascadeId.TrimEnd('.').Split('.').Last());
|
||||||
|
if (currentCascadeId <= objCascadeId) currentCascadeId = objCascadeId + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(entity.ParentId))
|
||||||
|
{
|
||||||
|
var parentOrg = SugarClient.Queryable<U>().First(o => o.Id == entity.ParentId);
|
||||||
|
if (parentOrg != null)
|
||||||
|
{
|
||||||
|
cascadeId = parentOrg.CascadeId + currentCascadeId + ".";
|
||||||
|
entity.ParentName = parentOrg.Name;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new Exception("未能找到该组织的父节点信息");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cascadeId = ".0." + currentCascadeId + ".";
|
||||||
|
entity.ParentName = "根节点";
|
||||||
|
}
|
||||||
|
|
||||||
|
entity.CascadeId = cascadeId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -15,6 +15,7 @@ using Microsoft.Extensions.Options;
|
|||||||
using Moq;
|
using Moq;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
using OpenAuth.Repository.Interface;
|
using OpenAuth.Repository.Interface;
|
||||||
|
using SqlSugar;
|
||||||
|
|
||||||
namespace OpenAuth.Repository.Test
|
namespace OpenAuth.Repository.Test
|
||||||
{
|
{
|
||||||
@ -66,6 +67,58 @@ namespace OpenAuth.Repository.Test
|
|||||||
|
|
||||||
serviceCollection.AddDbContext<OpenAuthDBContext>();
|
serviceCollection.AddDbContext<OpenAuthDBContext>();
|
||||||
|
|
||||||
|
var dbtypes = config.GetSection("AppSetting:DbTypes").GetChildren()
|
||||||
|
.ToDictionary(x => x.Key, x => x.Value);
|
||||||
|
|
||||||
|
var connectionString = config.GetSection("ConnectionStrings")["OpenAuthDBContext"];
|
||||||
|
Console.WriteLine($"单元测试数据库信息:{dbtypes[httpContextAccessorMock.Object.GetTenantId()]}/{connectionString}");
|
||||||
|
|
||||||
|
serviceCollection.AddScoped<ISqlSugarClient>(s =>
|
||||||
|
{
|
||||||
|
|
||||||
|
SqlSugarClient sqlSugar;
|
||||||
|
if(dbtypes.ContainsValue(Define.DBTYPE_SQLSERVER))
|
||||||
|
{
|
||||||
|
sqlSugar = new SqlSugarClient (new ConnectionConfig()
|
||||||
|
{
|
||||||
|
DbType = SqlSugar.DbType.SqlServer,
|
||||||
|
ConnectionString = connectionString,
|
||||||
|
IsAutoCloseConnection = true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else if(dbtypes.ContainsValue(Define.DBTYPE_MYSQL)) //mysql
|
||||||
|
{
|
||||||
|
sqlSugar = new SqlSugarClient (new ConnectionConfig()
|
||||||
|
{
|
||||||
|
DbType = SqlSugar.DbType.MySql,
|
||||||
|
ConnectionString = connectionString,
|
||||||
|
IsAutoCloseConnection = true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else if(dbtypes.ContainsValue(Define.DBTYPE_PostgreSQL)) //PostgreSQL
|
||||||
|
{
|
||||||
|
sqlSugar = new SqlSugarClient (new ConnectionConfig()
|
||||||
|
{
|
||||||
|
DbType = SqlSugar.DbType.PostgreSQL,
|
||||||
|
ConnectionString = connectionString,
|
||||||
|
IsAutoCloseConnection = true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sqlSugar = new SqlSugarClient (new ConnectionConfig()
|
||||||
|
{
|
||||||
|
DbType = SqlSugar.DbType.Oracle,
|
||||||
|
ConnectionString = connectionString,
|
||||||
|
IsAutoCloseConnection = true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return sqlSugar;
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var builder = new ContainerBuilder();
|
var builder = new ContainerBuilder();
|
||||||
|
|
||||||
//注册repository层
|
//注册repository层
|
||||||
@ -76,12 +129,6 @@ namespace OpenAuth.Repository.Test
|
|||||||
var _container = builder.Build();
|
var _container = builder.Build();
|
||||||
_autofacServiceProvider = new AutofacServiceProvider(_container);
|
_autofacServiceProvider = new AutofacServiceProvider(_container);
|
||||||
|
|
||||||
var dbtypes = config.GetSection("AppSetting:DbTypes").GetChildren()
|
|
||||||
.ToDictionary(x => x.Key, x => x.Value);
|
|
||||||
|
|
||||||
Console.WriteLine($"单元测试数据库信息:{dbtypes[httpContextAccessorMock.Object.GetTenantId()]}/{config.GetSection("ConnectionStrings")["OpenAuthDBContext"]}");
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
93
OpenAuth.Repository/Test/TestSugarDynamicLinq.cs
Normal file
93
OpenAuth.Repository/Test/TestSugarDynamicLinq.cs
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using Infrastructure;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace OpenAuth.Repository.Test
|
||||||
|
{
|
||||||
|
class TestSugarDynamicLinq : TestBase
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
public void GenerateFilter()
|
||||||
|
{
|
||||||
|
var dbcontext = _autofacServiceProvider.GetService<OpenAuthDBContext>();
|
||||||
|
var json = @"
|
||||||
|
{
|
||||||
|
""Operation"": ""and"",
|
||||||
|
""Filters"": [{
|
||||||
|
""Key"": ""Name"",
|
||||||
|
""Contrast"": ""=="",
|
||||||
|
""Value"": ""admin""
|
||||||
|
|
||||||
|
},
|
||||||
|
{
|
||||||
|
""Key"": ""Name"",
|
||||||
|
""Contrast"": ""=="",
|
||||||
|
""Value"": ""admin""
|
||||||
|
}
|
||||||
|
],
|
||||||
|
""Children"": [{
|
||||||
|
""Operation"": ""or"",
|
||||||
|
""Filters"": [{
|
||||||
|
""Key"": ""Name"",
|
||||||
|
""Contrast"": ""=="",
|
||||||
|
""Value"": ""admin""
|
||||||
|
},
|
||||||
|
{
|
||||||
|
""Key"": ""Name"",
|
||||||
|
""Contrast"": ""=="",
|
||||||
|
""Value"": ""admin""
|
||||||
|
}
|
||||||
|
],
|
||||||
|
""Children"": [
|
||||||
|
|
||||||
|
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
]
|
||||||
|
}
|
||||||
|
";
|
||||||
|
|
||||||
|
var query = dbcontext.Users.GenerateFilter("c",json);
|
||||||
|
Console.WriteLine(query.Expression.ToString());
|
||||||
|
|
||||||
|
Console.WriteLine(JsonHelper.Instance.Serialize(query.ToList()));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestDynamic()
|
||||||
|
{
|
||||||
|
FilterGroup sub = new FilterGroup
|
||||||
|
{
|
||||||
|
Operation = "or"
|
||||||
|
};
|
||||||
|
sub.Filters = new[]
|
||||||
|
{
|
||||||
|
new Filter {Key = "Name", Value = "name", Contrast = "=="},
|
||||||
|
new Filter {Key = "Sex", Value = "10", Contrast = "=="}
|
||||||
|
};
|
||||||
|
|
||||||
|
FilterGroup filterGroup = new FilterGroup
|
||||||
|
{
|
||||||
|
Operation = "and"
|
||||||
|
};
|
||||||
|
filterGroup.Filters = new[]
|
||||||
|
{
|
||||||
|
new Filter {Key = "Account", Value = "name", Contrast = "=="},
|
||||||
|
new Filter {Key = "Password", Value = "10", Contrast = "=="}
|
||||||
|
};
|
||||||
|
|
||||||
|
filterGroup.Children = new[]
|
||||||
|
{
|
||||||
|
sub
|
||||||
|
};
|
||||||
|
|
||||||
|
var dbcontext = _autofacServiceProvider.GetService<OpenAuthDBContext>();
|
||||||
|
|
||||||
|
var query = dbcontext.Users.GenerateFilter("c",JsonHelper.Instance.Serialize(filterGroup));
|
||||||
|
Console.WriteLine(query.Expression.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -22,6 +22,7 @@ using OpenAuth.App;
|
|||||||
using OpenAuth.App.HostedService;
|
using OpenAuth.App.HostedService;
|
||||||
using OpenAuth.Repository;
|
using OpenAuth.Repository;
|
||||||
using OpenAuth.WebApi.Model;
|
using OpenAuth.WebApi.Model;
|
||||||
|
using SqlSugar;
|
||||||
using Swashbuckle.AspNetCore.SwaggerUI;
|
using Swashbuckle.AspNetCore.SwaggerUI;
|
||||||
|
|
||||||
namespace OpenAuth.WebApi
|
namespace OpenAuth.WebApi
|
||||||
@ -173,6 +174,50 @@ namespace OpenAuth.WebApi
|
|||||||
|
|
||||||
services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(Configuration["DataProtection"]));
|
services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(Configuration["DataProtection"]));
|
||||||
|
|
||||||
|
services.AddScoped<ISqlSugarClient>(s =>
|
||||||
|
{
|
||||||
|
|
||||||
|
SqlSugarClient sqlSugar;
|
||||||
|
if(dbtypes.ContainsValue(Define.DBTYPE_SQLSERVER))
|
||||||
|
{
|
||||||
|
sqlSugar = new SqlSugarClient (new ConnectionConfig()
|
||||||
|
{
|
||||||
|
DbType = SqlSugar.DbType.SqlServer,
|
||||||
|
ConnectionString = connectionString,
|
||||||
|
IsAutoCloseConnection = true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else if(dbtypes.ContainsValue(Define.DBTYPE_MYSQL)) //mysql
|
||||||
|
{
|
||||||
|
sqlSugar = new SqlSugarClient (new ConnectionConfig()
|
||||||
|
{
|
||||||
|
DbType = SqlSugar.DbType.MySql,
|
||||||
|
ConnectionString = connectionString,
|
||||||
|
IsAutoCloseConnection = true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else if(dbtypes.ContainsValue(Define.DBTYPE_PostgreSQL)) //PostgreSQL
|
||||||
|
{
|
||||||
|
sqlSugar = new SqlSugarClient (new ConnectionConfig()
|
||||||
|
{
|
||||||
|
DbType = SqlSugar.DbType.PostgreSQL,
|
||||||
|
ConnectionString = connectionString,
|
||||||
|
IsAutoCloseConnection = true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sqlSugar = new SqlSugarClient (new ConnectionConfig()
|
||||||
|
{
|
||||||
|
DbType = SqlSugar.DbType.Oracle,
|
||||||
|
ConnectionString = connectionString,
|
||||||
|
IsAutoCloseConnection = true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return sqlSugar;
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
//设置定时启动的任务
|
//设置定时启动的任务
|
||||||
services.AddHostedService<QuartzService>();
|
services.AddHostedService<QuartzService>();
|
||||||
@ -232,7 +277,7 @@ namespace OpenAuth.WebApi
|
|||||||
app.UseSwaggerUI(c =>
|
app.UseSwaggerUI(c =>
|
||||||
{
|
{
|
||||||
c.IndexStream = () =>
|
c.IndexStream = () =>
|
||||||
GetType().GetTypeInfo().Assembly.GetManifestResourceStream("OpenAuth.WebApi.index.html");
|
IntrospectionExtensions.GetTypeInfo(GetType()).Assembly.GetManifestResourceStream("OpenAuth.WebApi.index.html");
|
||||||
|
|
||||||
foreach (var controller in GetControllers())
|
foreach (var controller in GetControllers())
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user