You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.7 KiB
C#

using Microsoft.Extensions.Localization;
using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy;
using Volo.Abp.MultiTenancy.Localization;
namespace NPin.Framework.TenantManagement.Domain;
[Dependency(ReplaceServices = true)]
public class NPinTenantConfigurationProvider : ITenantConfigurationProvider, ITransientDependency
{
protected virtual ITenantResolver TenantResolver { get; }
protected virtual ITenantStore TenantStore { get; }
protected virtual ITenantResolveResultAccessor TenantResolveResultAccessor { get; }
protected virtual IStringLocalizer<AbpMultiTenancyResource> StringLocalizer { get; }
public NPinTenantConfigurationProvider(
ITenantResolver tenantResolver,
ITenantStore tenantStore,
ITenantResolveResultAccessor tenantResolveResultAccessor,
IStringLocalizer<AbpMultiTenancyResource> stringLocalizer)
{
TenantResolver = tenantResolver;
TenantStore = tenantStore;
TenantResolveResultAccessor = tenantResolveResultAccessor;
StringLocalizer = stringLocalizer;
}
public async Task<TenantConfiguration?> GetAsync(bool saveResolveResult = false)
{
var resolveResult = await TenantResolver.ResolveTenantIdOrNameAsync();
if (saveResolveResult)
{
TenantResolveResultAccessor.Result = resolveResult;
}
TenantConfiguration? tenant = null;
if (resolveResult.TenantIdOrName != null)
{
tenant = await FindTenantAsync(resolveResult.TenantIdOrName);
if (tenant == null)
{
throw new BusinessException(
code: "Volo.AbpIo.MultiTenancy:010001",
message: StringLocalizer["TenantNotFoundMessage"],
details: StringLocalizer["TenantNotFoundDetails", resolveResult.TenantIdOrName]
);
}
if (!tenant.IsActive)
{
throw new BusinessException(
code: "Volo.AbpIo.MultiTenancy:010002",
message: StringLocalizer["TenantNotActiveMessage"],
details: StringLocalizer["TenantNotActiveDetails", resolveResult.TenantIdOrName]
);
}
}
return tenant;
}
protected virtual async Task<TenantConfiguration?> FindTenantAsync(string tenantIdOrName)
{
if (Guid.TryParse(tenantIdOrName, out var parsedTenantId))
{
return await TenantStore.FindAsync(parsedTenantId);
}
else
{
return await TenantStore.FindAsync(tenantIdOrName);
}
}
}