Browse Source

perf(saas): check the default connection string

pull/1114/head
colin 1 year ago
parent
commit
f2478945e6
  1. 30
      aspnet-core/modules/saas/LINGYUN.Abp.Saas.Application.Contracts/LINGYUN/Abp/Saas/Tenants/Dto/TenantConnectionStringSetInput.cs
  2. 31
      aspnet-core/modules/saas/LINGYUN.Abp.Saas.Application.Contracts/LINGYUN/Abp/Saas/Tenants/Dto/TenantCreateDto.cs
  3. 2
      aspnet-core/modules/saas/LINGYUN.Abp.Saas.Application.Contracts/LINGYUN/Abp/Saas/Tenants/ITenantAppService.cs
  4. 16
      aspnet-core/modules/saas/LINGYUN.Abp.Saas.Application/LINGYUN/Abp/Saas/Tenants/TenantAppService.cs
  5. 3
      aspnet-core/modules/saas/LINGYUN.Abp.Saas.Domain.Shared/LINGYUN/Abp/Saas/Localization/Resources/en.json
  6. 3
      aspnet-core/modules/saas/LINGYUN.Abp.Saas.Domain.Shared/LINGYUN/Abp/Saas/Localization/Resources/zh-Hans.json
  7. 4
      aspnet-core/modules/saas/LINGYUN.Abp.Saas.EntityFrameworkCore/LINGYUN/Abp/Saas/EntityFrameworkCore/EfCoreTenantRepository.cs
  8. 2
      aspnet-core/modules/saas/LINGYUN.Abp.Saas.HttpApi/LINGYUN/Abp/Saas/Tenants/TenantController.cs

30
aspnet-core/modules/saas/LINGYUN.Abp.Saas.Application.Contracts/LINGYUN/Abp/Saas/Tenants/Dto/TenantConnectionStringCreateOrUpdate.cs → aspnet-core/modules/saas/LINGYUN.Abp.Saas.Application.Contracts/LINGYUN/Abp/Saas/Tenants/Dto/TenantConnectionStringSetInput.cs

@ -1,15 +1,15 @@
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using Volo.Abp.Validation; using Volo.Abp.Validation;
namespace LINGYUN.Abp.Saas.Tenants; namespace LINGYUN.Abp.Saas.Tenants;
public class TenantConnectionStringCreateOrUpdate public class TenantConnectionStringSetInput
{ {
[Required] [Required]
[DynamicStringLength(typeof(TenantConnectionStringConsts), nameof(TenantConnectionStringConsts.MaxNameLength))] [DynamicStringLength(typeof(TenantConnectionStringConsts), nameof(TenantConnectionStringConsts.MaxNameLength))]
public string Name { get; set; } public string Name { get; set; }
[Required] [Required]
[DynamicStringLength(typeof(TenantConnectionStringConsts), nameof(TenantConnectionStringConsts.MaxValueLength))] [DynamicStringLength(typeof(TenantConnectionStringConsts), nameof(TenantConnectionStringConsts.MaxValueLength))]
public string Value { get; set; } public string Value { get; set; }
} }

31
aspnet-core/modules/saas/LINGYUN.Abp.Saas.Application.Contracts/LINGYUN/Abp/Saas/Tenants/Dto/TenantCreateDto.cs

@ -1,5 +1,11 @@
using System.Collections.Generic; using LINGYUN.Abp.Saas.Localization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Linq;
using Volo.Abp.Validation;
namespace LINGYUN.Abp.Saas.Tenants; namespace LINGYUN.Abp.Saas.Tenants;
@ -22,10 +28,31 @@ public class TenantCreateDto : TenantCreateOrUpdateBase
/// <summary> /// <summary>
/// 默认数据库连接字符串 /// 默认数据库连接字符串
/// </summary> /// </summary>
[DynamicStringLength(typeof(TenantConnectionStringConsts), nameof(TenantConnectionStringConsts.MaxValueLength))]
public string DefaultConnectionString { get; set; } public string DefaultConnectionString { get; set; }
/// <summary> /// <summary>
/// 其他数据库连接 /// 其他数据库连接
/// </summary> /// </summary>
public Dictionary<string, string> ConnectionStrings { get; set; } = new Dictionary<string, string>(); public List<TenantConnectionStringSetInput> ConnectionStrings { get; set; } = new List<TenantConnectionStringSetInput>();
public override IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
var validationResults = base.Validate(validationContext);
if (!UseSharedDatabase && DefaultConnectionString.IsNullOrWhiteSpace())
{
var saasResource = validationContext.GetRequiredService<IStringLocalizer<AbpSaasResource>>();
var errors = new ValidationResult[1]
{
new ValidationResult(
saasResource["IfUseCustomDataBaseDefaultConnectionStringIsRequiredMessage"],
new string[1]{ nameof(DefaultConnectionString) })
};
return validationResults.Union(errors);
}
return validationResults;
}
} }

2
aspnet-core/modules/saas/LINGYUN.Abp.Saas.Application.Contracts/LINGYUN/Abp/Saas/Tenants/ITenantAppService.cs

@ -26,7 +26,7 @@ public interface ITenantAppService :
Task<TenantConnectionStringDto> SetConnectionStringAsync( Task<TenantConnectionStringDto> SetConnectionStringAsync(
Guid id, Guid id,
TenantConnectionStringCreateOrUpdate input); TenantConnectionStringSetInput input);
Task DeleteConnectionStringAsync( Task DeleteConnectionStringAsync(
Guid id, Guid id,

16
aspnet-core/modules/saas/LINGYUN.Abp.Saas.Application/LINGYUN/Abp/Saas/Tenants/TenantAppService.cs

@ -83,18 +83,18 @@ public class TenantAppService : AbpSaasAppServiceBase, ITenantAppService
tenant.SetDisableTime(input.DisableTime); tenant.SetDisableTime(input.DisableTime);
input.MapExtraPropertiesTo(tenant); input.MapExtraPropertiesTo(tenant);
if (!input.UseSharedDatabase && !input.DefaultConnectionString.IsNullOrWhiteSpace()) if (!input.UseSharedDatabase)
{ {
await CheckConnectionString(input.DefaultConnectionString); await CheckConnectionString(input.DefaultConnectionString);
tenant.SetDefaultConnectionString(input.DefaultConnectionString); tenant.SetDefaultConnectionString(input.DefaultConnectionString);
}
if (input.ConnectionStrings.Any()) if (input.ConnectionStrings.Any())
{
foreach (var connectionString in input.ConnectionStrings)
{ {
await CheckConnectionString(connectionString.Value, connectionString.Key); foreach (var connectionString in input.ConnectionStrings)
tenant.SetConnectionString(connectionString.Key, connectionString.Value); {
await CheckConnectionString(connectionString.Value, connectionString.Name);
tenant.SetConnectionString(connectionString.Name, connectionString.Value);
}
} }
} }
@ -213,7 +213,7 @@ public class TenantAppService : AbpSaasAppServiceBase, ITenantAppService
} }
[Authorize(AbpSaasPermissions.Tenants.ManageConnectionStrings)] [Authorize(AbpSaasPermissions.Tenants.ManageConnectionStrings)]
public async virtual Task<TenantConnectionStringDto> SetConnectionStringAsync(Guid id, TenantConnectionStringCreateOrUpdate input) public async virtual Task<TenantConnectionStringDto> SetConnectionStringAsync(Guid id, TenantConnectionStringSetInput input)
{ {
await CheckConnectionString(input.Value, input.Name); await CheckConnectionString(input.Value, input.Name);

3
aspnet-core/modules/saas/LINGYUN.Abp.Saas.Domain.Shared/LINGYUN/Abp/Saas/Localization/Resources/en.json

@ -50,6 +50,7 @@
"Features:ExpiredRecoveryTimeDesc": "If the resource expiration is not handled for a long time, tenant resources will be reclaimed", "Features:ExpiredRecoveryTimeDesc": "If the resource expiration is not handled for a long time, tenant resources will be reclaimed",
"UnActived": "UnActived", "UnActived": "UnActived",
"RecycleStrategy:Reserve": "Reserve", "RecycleStrategy:Reserve": "Reserve",
"RecycleStrategy:Recycle": "Recycle" "RecycleStrategy:Recycle": "Recycle",
"IfUseCustomDataBaseDefaultConnectionStringIsRequiredMessage": "If you use a custom database, the default connection string must be required!"
} }
} }

3
aspnet-core/modules/saas/LINGYUN.Abp.Saas.Domain.Shared/LINGYUN/Abp/Saas/Localization/Resources/zh-Hans.json

@ -50,6 +50,7 @@
"Features:ExpiredRecoveryTimeDesc": "当资源超期多久没有处理, 将回收租户资源", "Features:ExpiredRecoveryTimeDesc": "当资源超期多久没有处理, 将回收租户资源",
"UnActived": "未启用", "UnActived": "未启用",
"RecycleStrategy:Reserve": "保留", "RecycleStrategy:Reserve": "保留",
"RecycleStrategy:Recycle": "回收" "RecycleStrategy:Recycle": "回收",
"IfUseCustomDataBaseDefaultConnectionStringIsRequiredMessage": "如果使用自定义数据库,默认连接字符串必须输入!"
} }
} }

4
aspnet-core/modules/saas/LINGYUN.Abp.Saas.EntityFrameworkCore/LINGYUN/Abp/Saas/EntityFrameworkCore/EfCoreTenantRepository.cs

@ -70,7 +70,7 @@ public class EfCoreTenantRepository : EfCoreRepository<ISaasDbContext, Tenant, G
var queryable = from tenant in tenantDbSet var queryable = from tenant in tenantDbSet
join edition in editionDbSet on tenant.EditionId equals edition.Id into eg join edition in editionDbSet on tenant.EditionId equals edition.Id into eg
from e in eg.DefaultIfEmpty() from e in eg.DefaultIfEmpty()
where tenant.Name.Equals(name) where tenant.Name.Equals(name) || tenant.NormalizedName.Equals(name)
orderby tenant.Id orderby tenant.Id
select new select new
{ {
@ -104,7 +104,7 @@ public class EfCoreTenantRepository : EfCoreRepository<ISaasDbContext, Tenant, G
var queryable = from tenant in tenantDbSet var queryable = from tenant in tenantDbSet
join edition in editionDbSet on tenant.EditionId equals edition.Id into eg join edition in editionDbSet on tenant.EditionId equals edition.Id into eg
from e in eg.DefaultIfEmpty() from e in eg.DefaultIfEmpty()
where tenant.Name.Equals(name) where tenant.Name.Equals(name) || tenant.NormalizedName.Equals(name)
orderby tenant.Id orderby tenant.Id
select new select new
{ {

2
aspnet-core/modules/saas/LINGYUN.Abp.Saas.HttpApi/LINGYUN/Abp/Saas/Tenants/TenantController.cs

@ -83,7 +83,7 @@ public class TenantController : AbpSaasControllerBase, ITenantAppService
[HttpPut] [HttpPut]
[Route("{id}/connection-string")] [Route("{id}/connection-string")]
[Authorize(AbpSaasPermissions.Tenants.ManageConnectionStrings)] [Authorize(AbpSaasPermissions.Tenants.ManageConnectionStrings)]
public virtual Task<TenantConnectionStringDto> SetConnectionStringAsync(Guid id, TenantConnectionStringCreateOrUpdate input) public virtual Task<TenantConnectionStringDto> SetConnectionStringAsync(Guid id, TenantConnectionStringSetInput input)
{ {
return TenantAppService.SetConnectionStringAsync(id, input); return TenantAppService.SetConnectionStringAsync(id, input);
} }

Loading…
Cancel
Save