mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-08-23 22:11:35 +08:00
调整为多租户模式
This commit is contained in:
parent
1228c6e875
commit
1ed50271bf
@ -1,4 +1,4 @@
|
||||
namespace OpenAuth.App
|
||||
namespace Infrastructure
|
||||
{
|
||||
/// <summary>
|
||||
/// 配置项
|
@ -1,32 +1,31 @@
|
||||
using System;
|
||||
|
||||
namespace OpenAuth.App
|
||||
{
|
||||
public static class Define
|
||||
{
|
||||
public static string USERROLE = "UserRole"; //用户角色关联KEY
|
||||
public const string ROLERESOURCE= "RoleResource"; //角色资源关联KEY
|
||||
public const string USERORG = "UserOrg"; //用户机构关联KEY
|
||||
public const string ROLEELEMENT = "RoleElement"; //角色菜单关联KEY
|
||||
public const string ROLEMODULE = "RoleModule"; //角色模块关联KEY
|
||||
public const string ROLEDATAPROPERTY = "RoleDataProperty"; //角色数据字段权限
|
||||
|
||||
public const string DBTYPE_SQLSERVER = "SqlServer"; //sql server
|
||||
public const string DBTYPE_MYSQL = "MySql"; //sql server
|
||||
|
||||
|
||||
public const int INVALID_TOKEN = 50014; //token无效
|
||||
|
||||
public const string TOKEN_NAME = "X-Token";
|
||||
|
||||
|
||||
public const string SYSTEM_USERNAME = "System";
|
||||
public const string SYSTEM_USERPWD = "123456";
|
||||
|
||||
public const string DATAPRIVILEGE_LOGINUSER = "{loginUser}"; //数据权限配置中,当前登录用户的key
|
||||
public const string DATAPRIVILEGE_LOGINROLE = "{loginRole}"; //数据权限配置中,当前登录用户角色的key
|
||||
public const string DATAPRIVILEGE_LOGINORG = "{loginOrg}"; //数据权限配置中,当前登录用户部门的key
|
||||
|
||||
public const string JOBMAPKEY = "OpenJob";
|
||||
}
|
||||
}
|
||||
namespace Infrastructure
|
||||
{
|
||||
public static class Define
|
||||
{
|
||||
public static string USERROLE = "UserRole"; //用户角色关联KEY
|
||||
public const string ROLERESOURCE= "RoleResource"; //角色资源关联KEY
|
||||
public const string USERORG = "UserOrg"; //用户机构关联KEY
|
||||
public const string ROLEELEMENT = "RoleElement"; //角色菜单关联KEY
|
||||
public const string ROLEMODULE = "RoleModule"; //角色模块关联KEY
|
||||
public const string ROLEDATAPROPERTY = "RoleDataProperty"; //角色数据字段权限
|
||||
|
||||
public const string DBTYPE_SQLSERVER = "SqlServer"; //sql server
|
||||
public const string DBTYPE_MYSQL = "MySql"; //sql server
|
||||
|
||||
|
||||
public const int INVALID_TOKEN = 50014; //token无效
|
||||
|
||||
public const string TOKEN_NAME = "X-Token";
|
||||
public const string TENANT_ID = "tenantId";
|
||||
|
||||
|
||||
public const string SYSTEM_USERNAME = "System";
|
||||
public const string SYSTEM_USERPWD = "123456";
|
||||
|
||||
public const string DATAPRIVILEGE_LOGINUSER = "{loginUser}"; //数据权限配置中,当前登录用户的key
|
||||
public const string DATAPRIVILEGE_LOGINROLE = "{loginRole}"; //数据权限配置中,当前登录用户角色的key
|
||||
public const string DATAPRIVILEGE_LOGINORG = "{loginOrg}"; //数据权限配置中,当前登录用户部门的key
|
||||
|
||||
public const string JOBMAPKEY = "OpenJob";
|
||||
}
|
||||
}
|
@ -11,7 +11,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="9.0.0" />
|
||||
<PackageReference Include="EnyimMemcachedCore" Version="2.1.5" />
|
||||
<PackageReference Include="log4net" Version="2.0.8" />
|
||||
<PackageReference Include="log4net" Version="2.0.12" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="3.1.2" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
|
@ -14,6 +14,7 @@
|
||||
//</summary>
|
||||
// ***********************************************************************
|
||||
|
||||
using Infrastructure;
|
||||
using OpenAuth.Repository;
|
||||
using OpenAuth.Repository.Domain;
|
||||
using OpenAuth.Repository.Interface;
|
||||
|
@ -1,4 +1,5 @@
|
||||
using System.Threading.Tasks;
|
||||
using Infrastructure;
|
||||
using Quartz;
|
||||
|
||||
namespace OpenAuth.App.Jobs
|
||||
|
@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Infrastructure;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using OpenAuth.App.Interface;
|
||||
using OpenAuth.App.Request;
|
||||
|
@ -5,6 +5,7 @@ using OpenAuth.App.Response;
|
||||
using OpenAuth.Repository.Domain;
|
||||
using OpenAuth.Repository.Interface;
|
||||
using System.Linq;
|
||||
using Infrastructure;
|
||||
using OpenAuth.App.Request;
|
||||
using OpenAuth.Repository;
|
||||
|
||||
|
@ -2,6 +2,7 @@ using Infrastructure.Cache;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using OpenAuth.App.Interface;
|
||||
using System;
|
||||
using Infrastructure;
|
||||
using Microsoft.Extensions.Options;
|
||||
using OpenAuth.Repository.Domain;
|
||||
|
||||
|
@ -3,6 +3,7 @@
|
||||
* 处理登录逻辑,验证客户段提交的账号密码,保存登录信息
|
||||
*/
|
||||
using System;
|
||||
using Infrastructure;
|
||||
using Infrastructure.Cache;
|
||||
using OpenAuth.Repository;
|
||||
using OpenAuth.Repository.Domain;
|
||||
|
@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Infrastructure;
|
||||
using Infrastructure.Cache;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
@ -1,7 +1,10 @@
|
||||
using Autofac.Extensions.DependencyInjection;
|
||||
using Infrastructure;
|
||||
using Infrastructure.Cache;
|
||||
using Infrastructure.Extensions.AutofacManager;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Moq;
|
||||
@ -23,12 +26,24 @@ namespace OpenAuth.App.Test
|
||||
serviceCollection.AddOptions();
|
||||
serviceCollection.AddLogging();
|
||||
|
||||
//模拟配置文件
|
||||
var optionMock = new Mock<IOptions<AppSetting>>();
|
||||
optionMock.Setup(x => x.Value).Returns(new AppSetting { DbType = Define.DBTYPE_MYSQL});
|
||||
serviceCollection.AddScoped(x => optionMock.Object);
|
||||
|
||||
// 测试my sql
|
||||
serviceCollection.AddDbContext<OpenAuthDBContext>(options =>
|
||||
//模拟多租户id
|
||||
var configMock = new Mock<IConfiguration>();
|
||||
configMock.Setup(x => x.GetSection("ConnectionStrings")[Define.TENANT_ID]).Returns("");
|
||||
serviceCollection.AddScoped(x => configMock.Object);
|
||||
|
||||
var httpContextAccessorMock = new Mock<IHttpContextAccessor>();
|
||||
httpContextAccessorMock.Setup(x => x.HttpContext.Request.Query[Define.TOKEN_NAME]).Returns("tokentest");
|
||||
httpContextAccessorMock.Setup(x => x.HttpContext.Request.Query[Define.TENANT_ID]).Returns("OpenAuthDBContext");
|
||||
|
||||
serviceCollection.AddScoped(x => httpContextAccessorMock.Object);
|
||||
|
||||
// 测试my sql
|
||||
serviceCollection.AddDbContext<OpenAuthDBContext>(options =>
|
||||
options.UseMySql("server=127.0.0.1;user id=root;database=openauthdb;password=000000"));
|
||||
|
||||
// serviceCollection.AddDbContext<OpenAuthDBContext>(options =>
|
||||
|
@ -1,6 +1,7 @@
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Reflection;
|
||||
using Infrastructure;
|
||||
using Infrastructure.Cache;
|
||||
using Infrastructure.Provider;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
@ -23,10 +23,6 @@ namespace OpenAuth.App.Test
|
||||
cachemock.Setup(x => x.Get<UserAuthSession>("tokentest")).Returns(new UserAuthSession { Account = Define.SYSTEM_USERNAME });
|
||||
services.AddScoped(x => cachemock.Object);
|
||||
|
||||
var httpContextAccessorMock = new Mock<IHttpContextAccessor>();
|
||||
httpContextAccessorMock.Setup(x => x.HttpContext.Request.Query[Define.TOKEN_NAME]).Returns("tokentest");
|
||||
|
||||
services.AddScoped(x => httpContextAccessorMock.Object);
|
||||
|
||||
var logMock = new Mock<ILogger<FileApp>>();
|
||||
services.AddScoped(x => logMock.Object);
|
||||
|
@ -1,4 +1,5 @@
|
||||
using System.Net.Http;
|
||||
using Infrastructure;
|
||||
using Infrastructure.Cache;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using NUnit.Framework;
|
||||
|
@ -1,4 +1,5 @@
|
||||
using Infrastructure.Cache;
|
||||
using Infrastructure;
|
||||
using Infrastructure.Cache;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using NUnit.Framework;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Castle.Core.Internal;
|
||||
using Infrastructure;
|
||||
using Infrastructure.Extensions;
|
||||
using OpenAuth.App.Interface;
|
||||
using OpenAuth.App.Request;
|
||||
|
@ -6,6 +6,7 @@ using IdentityServer4.Extensions;
|
||||
using IdentityServer4.Models;
|
||||
using IdentityServer4.Services;
|
||||
using IdentityServer4.Test;
|
||||
using Infrastructure;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using OpenAuth.App;
|
||||
using OpenAuth.Repository.Domain;
|
||||
|
@ -11,6 +11,7 @@ using IdentityServer4.Extensions;
|
||||
using IdentityServer4.Models;
|
||||
using IdentityServer4.Services;
|
||||
using IdentityServer4.Stores;
|
||||
using Infrastructure;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
@ -6,6 +6,7 @@ using System.Collections.Generic;
|
||||
using System.Security.Claims;
|
||||
using IdentityModel;
|
||||
using IdentityServer4.Test;
|
||||
using Infrastructure;
|
||||
using OpenAuth.App;
|
||||
|
||||
namespace OpenAuth.IdentityServer.Quickstart
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
|
||||
using Autofac;
|
||||
using Infrastructure;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
@ -13,6 +13,7 @@
|
||||
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.2" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.3" />
|
||||
<PackageReference Include="Moq" Version="4.13.1" />
|
||||
<PackageReference Include="MySql.Data" Version="8.0.13" />
|
||||
|
||||
<PackageReference Include="NUnit" Version="3.12.0" />
|
||||
|
@ -1,7 +1,9 @@
|
||||
using System;
|
||||
using Infrastructure;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Console;
|
||||
using Microsoft.Extensions.Options;
|
||||
using OpenAuth.Repository.Domain;
|
||||
using OpenAuth.Repository.QueryObj;
|
||||
|
||||
@ -12,21 +14,64 @@ namespace OpenAuth.Repository
|
||||
{
|
||||
|
||||
private ILoggerFactory _LoggerFactory;
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
optionsBuilder.EnableSensitiveDataLogging (true); //允许打印参数
|
||||
optionsBuilder.UseLoggerFactory (_LoggerFactory);
|
||||
private IHttpContextAccessor _httpContextAccessor;
|
||||
private IConfiguration _configuration;
|
||||
private IOptions<AppSetting> _appConfiguration;
|
||||
|
||||
base.OnConfiguring (optionsBuilder);
|
||||
}
|
||||
|
||||
public OpenAuthDBContext(DbContextOptions<OpenAuthDBContext> options, ILoggerFactory loggerFactory)
|
||||
public OpenAuthDBContext(DbContextOptions<OpenAuthDBContext> options, ILoggerFactory loggerFactory,
|
||||
IHttpContextAccessor httpContextAccessor, IConfiguration configuration, IOptions<AppSetting> appConfiguration)
|
||||
: base(options)
|
||||
{
|
||||
_LoggerFactory = loggerFactory;
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
_configuration = configuration;
|
||||
_appConfiguration = appConfiguration;
|
||||
}
|
||||
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
optionsBuilder.EnableSensitiveDataLogging(true); //允许打印参数
|
||||
optionsBuilder.UseLoggerFactory(_LoggerFactory);
|
||||
InitTenant(optionsBuilder);
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
|
||||
//初始化多租户信息,根据租户id调整数据库
|
||||
private void InitTenant(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
if (_httpContextAccessor == null || _httpContextAccessor.HttpContext == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//读取多租户ID
|
||||
string tenantId = _httpContextAccessor.HttpContext.Request.Query[Define.TENANT_ID];
|
||||
if (string.IsNullOrEmpty(tenantId))
|
||||
{
|
||||
tenantId = _httpContextAccessor.HttpContext.Request.Headers[Define.TENANT_ID];
|
||||
}
|
||||
|
||||
//如果没有租户id,或租户用的是默认的OpenAuthDBContext,则不做任何调整
|
||||
if (string.IsNullOrEmpty(tenantId) || tenantId == "OpenAuthDBContext")
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
string connect = _configuration.GetConnectionString(tenantId);
|
||||
if (string.IsNullOrEmpty(connect)) return;
|
||||
|
||||
var dbType =_appConfiguration.Value.DbType;
|
||||
if (dbType == Define.DBTYPE_SQLSERVER)
|
||||
{
|
||||
optionsBuilder.UseSqlServer(connect);
|
||||
}
|
||||
else //mysql
|
||||
{
|
||||
optionsBuilder.UseMySql(connect);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.Entity<DataPrivilegeRule>()
|
||||
|
@ -1,8 +1,13 @@
|
||||
using System.Reflection;
|
||||
using Autofac;
|
||||
using Autofac.Extensions.DependencyInjection;
|
||||
using Infrastructure;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using OpenAuth.Repository.Interface;
|
||||
|
||||
@ -26,6 +31,22 @@ namespace OpenAuth.Repository.Test
|
||||
serviceCollection.AddScoped(typeof(IRepository<,>), typeof(BaseRepository<,>));
|
||||
serviceCollection.AddScoped(typeof(IUnitWork<>), typeof(UnitWork<>));
|
||||
|
||||
//模拟配置文件
|
||||
var optionMock = new Mock<IOptions<AppSetting>>();
|
||||
optionMock.Setup(x => x.Value).Returns(new AppSetting { DbType = Define.DBTYPE_MYSQL });
|
||||
serviceCollection.AddScoped(x => optionMock.Object);
|
||||
|
||||
//模拟多租户id
|
||||
var configMock = new Mock<IConfiguration>();
|
||||
configMock.Setup(x => x.GetSection("ConnectionStrings")[Define.TENANT_ID]).Returns("");
|
||||
serviceCollection.AddScoped(x => configMock.Object);
|
||||
|
||||
var httpContextAccessorMock = new Mock<IHttpContextAccessor>();
|
||||
httpContextAccessorMock.Setup(x => x.HttpContext.Request.Query[Define.TOKEN_NAME]).Returns("tokentest");
|
||||
httpContextAccessorMock.Setup(x => x.HttpContext.Request.Query[Define.TENANT_ID]).Returns("OpenAuthDBContext");
|
||||
|
||||
serviceCollection.AddScoped(x => httpContextAccessorMock.Object);
|
||||
|
||||
serviceCollection.AddDbContext<OpenAuthDBContext>(options =>
|
||||
options.UseSqlServer("Data Source=.;Initial Catalog=OpenAuthDB;User=sa;Password=000000;Integrated Security=True"));
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Infrastructure;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.OpenApi.Models;
|
||||
|
@ -7,6 +7,7 @@
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Infrastructure;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.OpenApi.Models;
|
||||
|
@ -24,7 +24,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="log4net" Version="2.0.8" />
|
||||
<PackageReference Include="log4net" Version="2.0.12" />
|
||||
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="3.0.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.2" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="3.1.2" />
|
||||
|
@ -34,11 +34,6 @@ namespace OpenAuth.WebApi.Test
|
||||
cachemock.Setup(x => x.Get<UserAuthSession>("tokentest")).Returns(new UserAuthSession{Account = "admin"});
|
||||
services.AddScoped(x => cachemock.Object);
|
||||
|
||||
var httpContextAccessorMock = new Mock<IHttpContextAccessor>();
|
||||
httpContextAccessorMock.Setup(x => x.HttpContext.Request.Query[Define.TOKEN_NAME]).Returns("tokentest");
|
||||
|
||||
services.AddScoped(x => httpContextAccessorMock.Object);
|
||||
|
||||
services.AddMvc().AddControllersAsServices();
|
||||
services.AddScoped<CheckController>();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user