feat: 完成db创建优化
This commit is contained in:
@@ -20,10 +20,15 @@ namespace Yi.Framework.Rbac.Domain.Shared.Consts
|
||||
public const string No_Permission = "登录禁用!该用户分配无任何权限,无意义登录!";
|
||||
public const string No_Role = "登录禁用!该用户分配无任何角色,无意义登录!";
|
||||
|
||||
//子租户管理员
|
||||
public const string Admin = "cc";
|
||||
public const string AdminRolesCode = "admin";
|
||||
public const string AdminPermissionCode = "*:*:*";
|
||||
|
||||
//租户管理员
|
||||
public const string TenantAdmin = "ccadmin";
|
||||
public const string TenantAdminPermissionCode = "*";
|
||||
|
||||
public const string DefaultRoleCode = "default";
|
||||
public const string CommonRoleName = "common";
|
||||
}
|
||||
|
||||
@@ -110,7 +110,7 @@ namespace Yi.Framework.TenantManagement.Application
|
||||
[HttpPut("tenant/init/{id}")]
|
||||
public async Task InitAsync([FromRoute]Guid id)
|
||||
{
|
||||
using (CurrentTenant.Change(id,"test"))
|
||||
using (CurrentTenant.Change(id))
|
||||
{
|
||||
await CodeFirst(this.LazyServiceProvider);
|
||||
await _dataSeeder.SeedAsync(id);
|
||||
|
||||
@@ -1,9 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Volo.Abp.MultiTenancy;
|
||||
using Volo.Abp.MultiTenancy;
|
||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||
|
||||
namespace Yi.Framework.TenantManagement.Domain
|
||||
@@ -12,7 +7,12 @@ namespace Yi.Framework.TenantManagement.Domain
|
||||
{
|
||||
public static IDisposable ChangeMaster(this ICurrentTenant currentTenant)
|
||||
{
|
||||
return currentTenant.Change(null, DbConnOptions.MasterTenantDbDefaultName);
|
||||
return currentTenant.Change(null, DbConnOptions.MasterTenantName);
|
||||
}
|
||||
|
||||
public static IDisposable ChangeDefalut(this ICurrentTenant currentTenant)
|
||||
{
|
||||
return currentTenant.Change(null, DbConnOptions.DefaultTenantName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Volo.Abp.Data;
|
||||
using Volo.Abp.Domain;
|
||||
using Volo.Abp.Modularity;
|
||||
using Volo.Abp.MultiTenancy;
|
||||
@@ -15,7 +16,9 @@ namespace Yi.Framework.TenantManagement.Domain
|
||||
public override void ConfigureServices(ServiceConfigurationContext context)
|
||||
{
|
||||
var services = context.Services;
|
||||
services.Replace(new ServiceDescriptor(typeof(ITenantStore),typeof(SqlSugarTenantStore), ServiceLifetime.Transient));
|
||||
services.Replace(new ServiceDescriptor(typeof(ITenantStore), typeof(SqlSugarTenantStore), ServiceLifetime.Transient));
|
||||
|
||||
services.Replace(new ServiceDescriptor(typeof(IConnectionStringResolver), typeof(YiMultiTenantConnectionStringResolver), ServiceLifetime.Transient));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Volo.Abp.Data;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
|
||||
namespace Volo.Abp.MultiTenancy;
|
||||
|
||||
[Dependency(ReplaceServices = true)]
|
||||
public class YiMultiTenantConnectionStringResolver : DefaultConnectionStringResolver
|
||||
{
|
||||
private readonly ICurrentTenant _currentTenant;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
public YiMultiTenantConnectionStringResolver(
|
||||
IOptionsMonitor<AbpDbConnectionOptions> options,
|
||||
ICurrentTenant currentTenant,
|
||||
IServiceProvider serviceProvider)
|
||||
: base(options)
|
||||
{
|
||||
_currentTenant = currentTenant;
|
||||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
public override async Task<string> ResolveAsync(string? connectionStringName = null)
|
||||
{
|
||||
if (_currentTenant.Id == null)
|
||||
{
|
||||
//No current tenant, fallback to default logic
|
||||
return await base.ResolveAsync(connectionStringName);
|
||||
}
|
||||
|
||||
var tenant = await FindTenantConfigurationAsync(_currentTenant.Id.Value);
|
||||
|
||||
if (tenant == null || tenant.ConnectionStrings.IsNullOrEmpty())
|
||||
{
|
||||
//Tenant has not defined any connection string, fallback to default logic
|
||||
return await base.ResolveAsync(connectionStringName);
|
||||
}
|
||||
|
||||
var tenantDefaultConnectionString = tenant.ConnectionStrings?.Default;
|
||||
|
||||
//Requesting default connection string...
|
||||
//if (connectionStringName == null ||
|
||||
// connectionStringName == ConnectionStrings.DefaultConnectionStringName)
|
||||
//{
|
||||
// //Return tenant's default or global default
|
||||
// return !tenantDefaultConnectionString.IsNullOrWhiteSpace()
|
||||
// ? tenantDefaultConnectionString!
|
||||
// : Options.ConnectionStrings.Default!;
|
||||
//}
|
||||
|
||||
//Requesting specific connection string...
|
||||
var connString = tenant.ConnectionStrings?.FirstOrDefault().Value;
|
||||
if (!connString.IsNullOrWhiteSpace())
|
||||
{
|
||||
//Found for the tenant
|
||||
return connString!;
|
||||
}
|
||||
|
||||
//Fallback to the mapped database for the specific connection string
|
||||
var database = Options.Databases.GetMappedDatabaseOrNull(connectionStringName);
|
||||
if (database != null && database.IsUsedByTenants)
|
||||
{
|
||||
connString = tenant.ConnectionStrings?.GetOrDefault(database.DatabaseName);
|
||||
if (!connString.IsNullOrWhiteSpace())
|
||||
{
|
||||
//Found for the tenant
|
||||
return connString!;
|
||||
}
|
||||
}
|
||||
|
||||
//Fallback to tenant's default connection string if available
|
||||
if (!tenantDefaultConnectionString.IsNullOrWhiteSpace())
|
||||
{
|
||||
return tenantDefaultConnectionString!;
|
||||
}
|
||||
|
||||
return await base.ResolveAsync(connectionStringName);
|
||||
}
|
||||
|
||||
[Obsolete("Use ResolveAsync method.")]
|
||||
public override string Resolve(string? connectionStringName = null)
|
||||
{
|
||||
if (_currentTenant.Id == null)
|
||||
{
|
||||
//No current tenant, fallback to default logic
|
||||
return base.Resolve(connectionStringName);
|
||||
}
|
||||
|
||||
var tenant = FindTenantConfiguration(_currentTenant.Id.Value);
|
||||
|
||||
if (tenant == null || tenant.ConnectionStrings.IsNullOrEmpty())
|
||||
{
|
||||
//Tenant has not defined any connection string, fallback to default logic
|
||||
return base.Resolve(connectionStringName);
|
||||
}
|
||||
|
||||
var tenantDefaultConnectionString = tenant.ConnectionStrings?.Default;
|
||||
|
||||
//Requesting default connection string...
|
||||
if (connectionStringName == null ||
|
||||
connectionStringName == ConnectionStrings.DefaultConnectionStringName)
|
||||
{
|
||||
//Return tenant's default or global default
|
||||
return !tenantDefaultConnectionString.IsNullOrWhiteSpace()
|
||||
? tenantDefaultConnectionString!
|
||||
: Options.ConnectionStrings.Default!;
|
||||
}
|
||||
|
||||
//Requesting specific connection string...
|
||||
var connString = tenant.ConnectionStrings?.GetOrDefault(connectionStringName);
|
||||
if (!connString.IsNullOrWhiteSpace())
|
||||
{
|
||||
//Found for the tenant
|
||||
return connString!;
|
||||
}
|
||||
|
||||
//Fallback to tenant's default connection string if available
|
||||
if (!tenantDefaultConnectionString.IsNullOrWhiteSpace())
|
||||
{
|
||||
return tenantDefaultConnectionString!;
|
||||
}
|
||||
|
||||
//Try to find the specific connection string for given name
|
||||
var connStringInOptions = Options.ConnectionStrings.GetOrDefault(connectionStringName);
|
||||
if (!connStringInOptions.IsNullOrWhiteSpace())
|
||||
{
|
||||
return connStringInOptions!;
|
||||
}
|
||||
|
||||
//Fallback to the global default connection string
|
||||
var defaultConnectionString = Options.ConnectionStrings.Default;
|
||||
if (!defaultConnectionString.IsNullOrWhiteSpace())
|
||||
{
|
||||
return defaultConnectionString!;
|
||||
}
|
||||
|
||||
throw new AbpException("No connection string defined!");
|
||||
}
|
||||
|
||||
protected virtual async Task<TenantConfiguration?> FindTenantConfigurationAsync(Guid tenantId)
|
||||
{
|
||||
using (var serviceScope = _serviceProvider.CreateScope())
|
||||
{
|
||||
var tenantStore = serviceScope
|
||||
.ServiceProvider
|
||||
.GetRequiredService<ITenantStore>();
|
||||
|
||||
return await tenantStore.FindAsync(tenantId);
|
||||
}
|
||||
}
|
||||
|
||||
[Obsolete("Use FindTenantConfigurationAsync method.")]
|
||||
protected virtual TenantConfiguration? FindTenantConfiguration(Guid tenantId)
|
||||
{
|
||||
using (var serviceScope = _serviceProvider.CreateScope())
|
||||
{
|
||||
var tenantStore = serviceScope
|
||||
.ServiceProvider
|
||||
.GetRequiredService<ITenantStore>();
|
||||
|
||||
return tenantStore.Find(tenantId);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user