|
|
|
@ -1,5 +1,6 @@ |
|
|
|
using LINGYUN.Abp.Saas.Features; |
|
|
|
using Microsoft.AspNetCore.Authorization; |
|
|
|
using Microsoft.Extensions.Logging; |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Linq; |
|
|
|
@ -8,7 +9,6 @@ using Volo.Abp; |
|
|
|
using Volo.Abp.Application.Dtos; |
|
|
|
using Volo.Abp.Data; |
|
|
|
using Volo.Abp.EventBus.Distributed; |
|
|
|
using Volo.Abp.Features; |
|
|
|
using Volo.Abp.MultiTenancy; |
|
|
|
using Volo.Abp.ObjectExtending; |
|
|
|
|
|
|
|
@ -20,15 +20,18 @@ public class TenantAppService : AbpSaasAppServiceBase, ITenantAppService |
|
|
|
protected IDistributedEventBus EventBus { get; } |
|
|
|
protected ITenantRepository TenantRepository { get; } |
|
|
|
protected ITenantManager TenantManager { get; } |
|
|
|
protected IConnectionStringChecker ConnectionStringChecker { get; } |
|
|
|
|
|
|
|
public TenantAppService( |
|
|
|
ITenantRepository tenantRepository, |
|
|
|
ITenantManager tenantManager, |
|
|
|
IDistributedEventBus eventBus) |
|
|
|
IDistributedEventBus eventBus, |
|
|
|
IConnectionStringChecker connectionStringChecker) |
|
|
|
{ |
|
|
|
EventBus = eventBus; |
|
|
|
TenantRepository = tenantRepository; |
|
|
|
TenantManager = tenantManager; |
|
|
|
ConnectionStringChecker = connectionStringChecker; |
|
|
|
} |
|
|
|
|
|
|
|
public async virtual Task<TenantDto> GetAsync(Guid id) |
|
|
|
@ -80,6 +83,7 @@ public class TenantAppService : AbpSaasAppServiceBase, ITenantAppService |
|
|
|
|
|
|
|
if (!input.UseSharedDatabase && !input.DefaultConnectionString.IsNullOrWhiteSpace()) |
|
|
|
{ |
|
|
|
await CheckConnectionString(input.DefaultConnectionString); |
|
|
|
tenant.SetDefaultConnectionString(input.DefaultConnectionString); |
|
|
|
} |
|
|
|
|
|
|
|
@ -87,6 +91,7 @@ public class TenantAppService : AbpSaasAppServiceBase, ITenantAppService |
|
|
|
{ |
|
|
|
foreach (var connectionString in input.ConnectionStrings) |
|
|
|
{ |
|
|
|
await CheckConnectionString(connectionString.Value, connectionString.Key); |
|
|
|
tenant.SetConnectionString(connectionString.Key, connectionString.Value); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -119,6 +124,7 @@ public class TenantAppService : AbpSaasAppServiceBase, ITenantAppService |
|
|
|
await EventBus.PublishAsync(eto); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
await CurrentUnitOfWork.SaveChangesAsync(); |
|
|
|
|
|
|
|
return ObjectMapper.Map<Tenant, TenantDto>(tenant); |
|
|
|
@ -157,12 +163,17 @@ public class TenantAppService : AbpSaasAppServiceBase, ITenantAppService |
|
|
|
} |
|
|
|
|
|
|
|
// 租户删除时查询会失效, 在删除前确认
|
|
|
|
var strategy = await FeatureChecker.GetAsync(SaasFeatureNames.Tenant.RecycleStrategy, RecycleStrategy.Recycle); |
|
|
|
var recycleStrategy = RecycleStrategy.Recycle; |
|
|
|
var strategySet = await FeatureChecker.GetOrNullAsync(SaasFeatureNames.Tenant.RecycleStrategy); |
|
|
|
if (!strategySet.IsNullOrWhiteSpace() && Enum.TryParse<RecycleStrategy>(strategySet, out var strategy)) |
|
|
|
{ |
|
|
|
recycleStrategy = strategy; |
|
|
|
} |
|
|
|
var eto = new TenantDeletedEto |
|
|
|
{ |
|
|
|
Id = tenant.Id, |
|
|
|
Name = tenant.Name, |
|
|
|
Strategy = strategy, |
|
|
|
Strategy = recycleStrategy, |
|
|
|
EntityVersion = tenant.EntityVersion, |
|
|
|
DefaultConnectionString = tenant.FindDefaultConnectionString(), |
|
|
|
}; |
|
|
|
@ -259,4 +270,34 @@ public class TenantAppService : AbpSaasAppServiceBase, ITenantAppService |
|
|
|
|
|
|
|
await CurrentUnitOfWork.SaveChangesAsync(); |
|
|
|
} |
|
|
|
|
|
|
|
protected async virtual Task CheckConnectionString(string connectionString, string name = null) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
var checkResult = await ConnectionStringChecker.CheckAsync(connectionString); |
|
|
|
// 检查连接是否可用
|
|
|
|
if (!checkResult.Connected) |
|
|
|
{ |
|
|
|
throw name.IsNullOrWhiteSpace() |
|
|
|
? new BusinessException(AbpSaasErrorCodes.InvalidDefaultConnectionString) |
|
|
|
: new BusinessException(AbpSaasErrorCodes.InvalidConnectionString) |
|
|
|
.WithData("Name", name); |
|
|
|
} |
|
|
|
// 默认连接字符串改变不能影响到现有数据库
|
|
|
|
if (checkResult.DatabaseExists && name.IsNullOrWhiteSpace()) |
|
|
|
{ |
|
|
|
throw new BusinessException(AbpSaasErrorCodes.DefaultConnectionStringDatabaseExists); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (Exception e) |
|
|
|
{ |
|
|
|
Logger.LogWarning("An error occurred while checking the validity of the connection string"); |
|
|
|
Logger.LogWarning(e.Message); |
|
|
|
throw name.IsNullOrWhiteSpace() |
|
|
|
? new BusinessException(AbpSaasErrorCodes.InvalidDefaultConnectionString) |
|
|
|
: new BusinessException(AbpSaasErrorCodes.InvalidConnectionString) |
|
|
|
.WithData("Name", name); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|