Skip to content

Commit

Permalink
feat: 添加租户字符串配置
Browse files Browse the repository at this point in the history
  • Loading branch information
WangJunZzz committed Apr 6, 2024
1 parent e23ea9b commit b4b1c9d
Show file tree
Hide file tree
Showing 38 changed files with 16,690 additions and 233 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace Lion.AbpPro.BasicManagement.Tenants.Dtos;

public class AddOrUpdateConnectionStringInput : IValidatableObject
{

/// <summary>
/// id
/// </summary>
public Guid Id { get; set; }

/// <summary>
/// 连接字符串名称
/// </summary>
public string Name { get; set; }

/// <summary>
/// 连接字符串地址
/// </summary>
public string Value { get; set; }

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
var localization = validationContext.GetRequiredService<IStringLocalizer<AbpProLocalizationResource>>();
if (Name.IsNullOrWhiteSpace())
{
yield return new ValidationResult(
localization[AbpProLocalizationErrorCodes.ErrorCode100003, nameof(Name)],
new[] { nameof(Name) }
);
}

if (Value.IsNullOrWhiteSpace())
{
yield return new ValidationResult(
localization[AbpProLocalizationErrorCodes.ErrorCode100003, nameof(Value)],
new[] { nameof(Value) }
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Lion.AbpPro.BasicManagement.Tenants.Dtos;

public class DeleteConnectionStringInput
{
/// <summary>
/// 连接字符串名称
/// </summary>
public string Name { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Lion.AbpPro.BasicManagement.Tenants.Dtos;

public class PageTenantConnectionStringInput
{
/// <summary>
/// 租户id
/// </summary>
public Guid Id { get; set; }

/// <summary>
/// 连接字符串名称
/// </summary>
public string Name { get; set; }

/// <summary>
/// 连接字符串地址
/// </summary>
public string Value { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Lion.AbpPro.BasicManagement.Tenants.Dtos;

public class PageTenantConnectionStringOutput
{
/// <summary>
/// 租户id
/// </summary>
public Guid TenantId { get; set; }

/// <summary>
/// 连接字符串名称
/// </summary>
public string Name { get; set; }

/// <summary>
/// 连接字符串地址
/// </summary>
public string Value { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,19 @@ public interface IVoloTenantAppService : IApplicationService

Task DeleteAsync(IdInput input);

Task<string> GetDefaultConnectionStringAsync(IdInput input);
/// <summary>
/// 分页获取租户连接字符串
/// </summary>
Task<PagedResultDto<PageTenantConnectionStringOutput>> PageConnectionStringsAsync(PageTenantConnectionStringInput input);

Task UpdateDefaultConnectionStringAsync(UpdateConnectionStringInput input);

Task DeleteDefaultConnectionStringAsync(IdInput input);
/// <summary>
/// 新增或者更新连接字符串
/// </summary>
Task AddOrUpdateConnectionStringAsync(AddOrUpdateConnectionStringInput input);

/// <summary>
/// 删除连接字符串
/// </summary>
Task DeleteConnectionStringAsync(DeleteConnectionStringInput input);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ public BasicManagementApplicationAutoMapperProfile()
CreateMap<IdentityRole, GetOrganizationUnitRoleOutput>();
CreateMap<IdentityRole, GetUnAddRoleOutput>();
CreateMap<IdentitySecurityLog, PagingIdentitySecurityLogOutput>();
CreateMap<TenantConnectionString, PageTenantConnectionStringOutput>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public override void ConfigureServices(ServiceConfigurationContext context)
options.Excludes.Add(SettingManagementPermissions.GroupName);
options.Excludes.Add(SettingManagementPermissions.Emailing);
options.Excludes.Add(TenantManagementPermissions.Tenants.ManageFeatures);
options.Excludes.Add(TenantManagementPermissions.Tenants.ManageConnectionStrings);
//options.Excludes.Add(TenantManagementPermissions.Tenants.ManageConnectionStrings);
});

context.Services.Configure<JwtOptions>(context.Services.GetConfiguration().GetSection("Jwt"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
using Volo.Abp.BackgroundJobs;
using Volo.Abp.FeatureManagement;
using Volo.Abp.SettingManagement;

namespace Lion.AbpPro.BasicManagement.Tenants
{
[Authorize(TenantManagementPermissions.Tenants.Default)]
public class VoloTenantAppService : BasicManagementAppService, IVoloTenantAppService
{
private readonly IAbpTenantAppService _abpTenantAppService;
private readonly ITenantAppService _tenantAppService;
private readonly ITenantRepository _tenantRepository;

public VoloTenantAppService(
IAbpTenantAppService abpTenantAppService,
ITenantAppService tenantAppService)
IAbpTenantAppService abpTenantAppService,
ITenantAppService tenantAppService,
ITenantRepository tenantRepository)
{
_abpTenantAppService = abpTenantAppService;
_tenantAppService = tenantAppService;
_tenantRepository = tenantRepository;
}

[AllowAnonymous]
public virtual async Task<FindTenantResultDto> FindTenantByNameAsync(FindTenantByNameInput input)
{
return await _abpTenantAppService.FindTenantByNameAsync(input.Name);
}


public virtual Task<PagedResultDto<TenantDto>> ListAsync(PagingTenantInput input)
{
var request = new GetTenantsInput
Expand All @@ -30,13 +37,13 @@ public virtual Task<PagedResultDto<TenantDto>> ListAsync(PagingTenantInput input
return _tenantAppService.GetListAsync(request);
}

[Authorize(policy:TenantManagementPermissions.Tenants.Create)]
[Authorize(policy: TenantManagementPermissions.Tenants.Create)]
public virtual Task<TenantDto> CreateAsync(TenantCreateDto input)
{
return _tenantAppService.CreateAsync(input);
}

[Authorize(policy:TenantManagementPermissions.Tenants.Update)]
[Authorize(policy: TenantManagementPermissions.Tenants.Update)]
public virtual Task<TenantDto> UpdateAsync(UpdateTenantInput input)
{
var request = new TenantUpdateDto()
Expand All @@ -46,29 +53,91 @@ public virtual Task<TenantDto> UpdateAsync(UpdateTenantInput input)
return _tenantAppService.UpdateAsync(input.Id, request);
}

[Authorize(policy:TenantManagementPermissions.Tenants.Delete)]
[Authorize(policy: TenantManagementPermissions.Tenants.Delete)]
public virtual Task DeleteAsync(IdInput input)
{
return _tenantAppService.DeleteAsync(input.Id);
}


[Authorize(TenantManagementPermissions.Tenants.ManageConnectionStrings)]
public virtual Task<string> GetDefaultConnectionStringAsync(IdInput input)
{
return _tenantAppService.GetDefaultConnectionStringAsync(input.Id);
public async Task<PagedResultDto<PageTenantConnectionStringOutput>> PageConnectionStringsAsync(PageTenantConnectionStringInput input)
{
var result = new PagedResultDto<PageTenantConnectionStringOutput>();
var tenant = await _tenantRepository.FindAsync(input.Id, true);

if (tenant == null)
{
throw new BusinessException(BasicManagementErrorCodes.TenantNotExist);
}

result.TotalCount = tenant.ConnectionStrings.Count;

var items = ObjectMapper.Map<List<TenantConnectionString>, List<PageTenantConnectionStringOutput>>(tenant.ConnectionStrings);
if (input.Name.IsNotNullOrWhiteSpace())
{
items = items.Where(e => e.Name.ToLower().Contains(input.Name.ToLower())).ToList();
}
if (input.Value.IsNotNullOrWhiteSpace())
{
items = items.Where(e => e.Value.ToLower().Contains(input.Value.ToLower())).ToList();
}
result.Items = items;

return result;
}

[Authorize(TenantManagementPermissions.Tenants.ManageConnectionStrings)]
public virtual Task UpdateDefaultConnectionStringAsync(UpdateConnectionStringInput input)
public async Task AddOrUpdateConnectionStringAsync(AddOrUpdateConnectionStringInput input)
{
return _tenantAppService.UpdateDefaultConnectionStringAsync(input.Id,
input.ConnectionString);

// abp 租户,feature,background,setting 模块不支持单独配置数据库
if (AbpTenantManagementDbProperties.ConnectionStringName.ToLower() == input.Name.ToLower() ||
AbpBackgroundJobsDbProperties.ConnectionStringName.ToLower()== input.Name.ToLower() ||
AbpFeatureManagementDbProperties.ConnectionStringName.ToLower() == input.Name.ToLower() ||
AbpSettingManagementDbProperties.ConnectionStringName.ToLower() == input.Name.ToLower())
{
throw new BusinessException(BasicManagementErrorCodes.NotSupportSetConnectionString);
}
var tenant = await _tenantRepository.FindAsync(input.Id, true);

if (tenant == null)
{
throw new BusinessException(BasicManagementErrorCodes.TenantNotExist);
}

var connectionString = tenant.ConnectionStrings.FirstOrDefault(e => e.Value == input.Name);
if (connectionString == null)
{
tenant.SetConnectionString(input.Name, input.Value);
}
else
{
if (connectionString.Value != input.Value)
{
tenant.SetConnectionString(input.Name, input.Value);
}
}
}

[Authorize(TenantManagementPermissions.Tenants.ManageConnectionStrings)]
public virtual Task DeleteDefaultConnectionStringAsync(IdInput input)
public async Task DeleteConnectionStringAsync(DeleteConnectionStringInput input)
{
return _tenantAppService.DeleteDefaultConnectionStringAsync(input.Id);
if (!CurrentTenant.Id.HasValue)
{
throw new BusinessException(BasicManagementErrorCodes.TenantNotExist);
}

var tenant = await _tenantRepository.FindAsync(CurrentTenant.Id.Value, true);

if (tenant == null)
{
throw new BusinessException(BasicManagementErrorCodes.TenantNotExist);
}
var connectionString = tenant.ConnectionStrings.FirstOrDefault(e => e.Value == input.Name);
if (connectionString != null)
{
tenant.RemoveConnectionString(input.Name);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ private async Task<LoginOutput> BuildResult(IdentityUser user)
/// 生成jwt token
/// </summary>
/// <returns></returns>
private string GenerateJwt(Guid userId, string userName, string name, string email,
string tenantId, List<string> roles)
private string GenerateJwt(Guid userId, string userName, string name, string email, string tenantId, List<string> roles)
{
var dateNow = Clock.Now;
var expirationTime = dateNow.AddHours(_jwtOptions.ExpirationTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ public static class BasicManagementErrorCodes
public const string UserLockedOut = BasicManagementConsts.NameSpace + ":100002";
public const string UserOrPasswordMismatch = BasicManagementConsts.NameSpace + ":100003";
public const string UserDisabled = BasicManagementConsts.NameSpace + ":100004";
public const string TenantNotExist = BasicManagementConsts.NameSpace + ":100005";
public const string NotSupportSetConnectionString = BasicManagementConsts.NameSpace + ":100006";
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"Lion.AbpPro.BasicManagement:100001": "OrganizationUnit Not Exist",
"Lion.AbpPro.BasicManagement:100002": "UserLockedOut",
"Lion.AbpPro.BasicManagement:100003": "UserOrPasswordMismatch",
"Lion.AbpPro.BasicManagement:100004": "UserDisabled"
"Lion.AbpPro.BasicManagement:100004": "UserDisabled",
"Lion.AbpPro.BasicManagement:100005": "Tenant Not Exist",
"Lion.AbpPro.BasicManagement:100006": "Not Support Set ConnectionString"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
"Lion.AbpPro.BasicManagement:100001": "组织机构不存在",
"Lion.AbpPro.BasicManagement:100002": "用户被锁定",
"Lion.AbpPro.BasicManagement:100003": "用户名或者密码错误",
"Lion.AbpPro.BasicManagement:100004": "用户已禁用"
"Lion.AbpPro.BasicManagement:100004": "用户已禁用",
"Lion.AbpPro.BasicManagement:100005": "租户不存在",
"Lion.AbpPro.BasicManagement:100006": "当前模块不支持设置数据库连接字符串"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,27 @@ public Task DeleteAsync(IdInput input)
{
return _voloTenantAppService.DeleteAsync(input);
}


[HttpPost("getConnectionString")]
[SwaggerOperation(summary: "获取租户连接字符串", Tags = new[] { "Tenants" })]
public Task<string> GetDefaultConnectionStringAsync(IdInput input)
[HttpPost("pageConnectionString")]
[SwaggerOperation(summary: "分页租户连接字符串", Tags = new[] { "Tenants" })]
public Task<PagedResultDto<PageTenantConnectionStringOutput>> PageConnectionStringsAsync(PageTenantConnectionStringInput input)
{
return _voloTenantAppService.GetDefaultConnectionStringAsync(input);
return _voloTenantAppService.PageConnectionStringsAsync(input);
}

[HttpPost("updateConnectionString")]
[SwaggerOperation(summary: "更新租户连接字符串", Tags = new[] { "Tenants" })]
public Task UpdateDefaultConnectionStringAsync(UpdateConnectionStringInput input)
[HttpPost("addOrUpdateConnectionString")]
[SwaggerOperation(summary: "新增或者更新租户所有连接字符串", Tags = new[] { "Tenants" })]
public Task AddOrUpdateConnectionStringAsync(AddOrUpdateConnectionStringInput input)
{
return _voloTenantAppService.UpdateDefaultConnectionStringAsync(input);
return _voloTenantAppService.AddOrUpdateConnectionStringAsync(input);
}

[HttpPost("deleteConnectionString")]
[SwaggerOperation(summary: "删除租户连接字符串", Tags = new[] { "Tenants" })]
public Task DeleteDefaultConnectionStringAsync(IdInput input)
public Task DeleteConnectionStringAsync(DeleteConnectionStringInput input)
{
return _voloTenantAppService.DeleteDefaultConnectionStringAsync(input);
return _voloTenantAppService.DeleteConnectionStringAsync(input);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private void SetProperties(string code, string displayText, string description,
SetTenantId(tenantId);
}

public void SetTenantId(Guid? tenantId)
private void SetTenantId(Guid? tenantId)
{
TenantId = tenantId;
}
Expand Down
Loading

0 comments on commit b4b1c9d

Please sign in to comment.