mirror of https://github.com/abpframework/abp.git
6 changed files with 64 additions and 60 deletions
@ -0,0 +1,26 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.TenantManagement; |
||||
|
|
||||
|
public class AbpTenantValidator : ITenantValidator, ITransientDependency |
||||
|
{ |
||||
|
protected ITenantRepository TenantRepository { get; } |
||||
|
|
||||
|
public AbpTenantValidator(ITenantRepository tenantRepository) |
||||
|
{ |
||||
|
TenantRepository = tenantRepository; |
||||
|
} |
||||
|
|
||||
|
public virtual async Task ValidateAsync(Tenant tenant) |
||||
|
{ |
||||
|
Check.NotNullOrWhiteSpace(tenant.Name, nameof(tenant.Name)); |
||||
|
Check.NotNullOrWhiteSpace(tenant.NormalizedName, nameof(tenant.NormalizedName)); |
||||
|
|
||||
|
var owner = await TenantRepository.FindByNameAsync(tenant.NormalizedName); |
||||
|
if (owner != null && owner.Id != tenant.Id) |
||||
|
{ |
||||
|
throw new BusinessException("Volo.Abp.TenantManagement:DuplicateTenantName").WithData("Name", tenant.NormalizedName); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,28 +0,0 @@ |
|||||
using System; |
|
||||
using System.Threading.Tasks; |
|
||||
using Volo.Abp.DependencyInjection; |
|
||||
using Volo.Abp.Domain.Services; |
|
||||
|
|
||||
namespace Volo.Abp.TenantManagement; |
|
||||
|
|
||||
public class DefaultTenantNameValidator : ITenantNameValidator, ITransientDependency |
|
||||
{ |
|
||||
protected ITenantRepository TenantRepository { get; } |
|
||||
|
|
||||
public DefaultTenantNameValidator(ITenantRepository tenantRepository) |
|
||||
{ |
|
||||
TenantRepository = tenantRepository; |
|
||||
} |
|
||||
|
|
||||
public virtual async Task ValidateAsync(string normalizedTenantName, Guid? expectedTenantId = null) |
|
||||
{ |
|
||||
Check.NotNullOrWhiteSpace(normalizedTenantName, nameof(normalizedTenantName)); |
|
||||
|
|
||||
var existingTenant = await TenantRepository.FindByNameAsync(normalizedTenantName); |
|
||||
if (existingTenant != null && existingTenant.Id != expectedTenantId) |
|
||||
{ |
|
||||
throw new BusinessException("Volo.Abp.TenantManagement:DuplicateTenantName") |
|
||||
.WithData("Name", normalizedTenantName); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,9 +0,0 @@ |
|||||
using System; |
|
||||
using System.Threading.Tasks; |
|
||||
|
|
||||
namespace Volo.Abp.TenantManagement; |
|
||||
|
|
||||
public interface ITenantNameValidator |
|
||||
{ |
|
||||
Task ValidateAsync(string normalizedTenantName, Guid? expectedTenantId = null); |
|
||||
} |
|
||||
@ -0,0 +1,8 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Volo.Abp.TenantManagement; |
||||
|
|
||||
|
public interface ITenantValidator |
||||
|
{ |
||||
|
Task ValidateAsync(Tenant tenant); |
||||
|
} |
||||
@ -1,35 +1,47 @@ |
|||||
using System; |
using System; |
||||
using System.Threading.Tasks; |
using System.Threading.Tasks; |
||||
using Volo.Abp.Guids; |
using Shouldly; |
||||
using Xunit; |
using Xunit; |
||||
|
|
||||
namespace Volo.Abp.TenantManagement; |
namespace Volo.Abp.TenantManagement; |
||||
|
|
||||
public class TenantNameValidator_Tests : AbpTenantManagementDomainTestBase |
public class TenantValidator_Tests : AbpTenantManagementDomainTestBase |
||||
{ |
{ |
||||
private readonly ITenantNameValidator _tenantNameValidator; |
private readonly TenantManager _tenantManager; |
||||
private readonly ITenantRepository _tenantRepository; |
private readonly ITenantRepository _tenantRepository; |
||||
public TenantNameValidator_Tests() |
|
||||
|
public TenantValidator_Tests() |
||||
{ |
{ |
||||
|
_tenantManager = GetRequiredService<TenantManager>(); |
||||
_tenantRepository = GetRequiredService<ITenantRepository>(); |
_tenantRepository = GetRequiredService<ITenantRepository>(); |
||||
_tenantNameValidator = GetRequiredService<ITenantNameValidator>(); |
|
||||
} |
} |
||||
|
|
||||
[Fact] |
[Fact] |
||||
public async Task Should_Throw_If_Name_Is_Null() |
public async Task Should_Throw_If_Name_Is_Null() |
||||
{ |
{ |
||||
await Assert.ThrowsAsync<ArgumentException>(() => _tenantNameValidator.ValidateAsync(null)); |
await Assert.ThrowsAsync<ArgumentException>(() => _tenantManager.CreateAsync("")); |
||||
} |
} |
||||
|
|
||||
[Fact] |
[Fact] |
||||
public async Task Should_Throw_If_Duplicate_Name() |
public async Task Should_Throw_If_Duplicate_Name() |
||||
{ |
{ |
||||
await Assert.ThrowsAsync<BusinessException>(() => _tenantNameValidator.ValidateAsync("VOLOSOFT")); |
await Assert.ThrowsAsync<BusinessException>(() => _tenantManager.CreateAsync("VOLOSOFT")); |
||||
|
|
||||
|
var tenant = await _tenantRepository.FindByNameAsync("ABP"); |
||||
|
await Assert.ThrowsAsync<BusinessException>(() => _tenantManager.ChangeNameAsync(tenant, "VOLOSOFT")); |
||||
} |
} |
||||
|
|
||||
[Fact] |
[Fact] |
||||
public async Task Should_Not_Throw_For_Unique_Name() |
public async Task Should_Not_Throw_For_Unique_Name() |
||||
{ |
{ |
||||
await _tenantNameValidator.ValidateAsync("UNIQUE-TENANT-NAME"); |
var tenant = await _tenantManager.CreateAsync("VOLOSOFT2"); |
||||
|
await _tenantRepository.InsertAsync(tenant); |
||||
|
|
||||
|
tenant = await _tenantRepository.FindByNameAsync("ABP"); |
||||
|
await _tenantManager.ChangeNameAsync(tenant, "VOLOSOFT3"); |
||||
|
await _tenantRepository.UpdateAsync(tenant); |
||||
|
|
||||
|
tenant = await _tenantRepository.FindByNameAsync("VOLOSOFT3"); |
||||
|
tenant.ShouldNotBeNull(); |
||||
} |
} |
||||
} |
} |
||||
|
|||||
Loading…
Reference in new issue