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 StringLocalizer { get; } public NPinTenantConfigurationProvider( ITenantResolver tenantResolver, ITenantStore tenantStore, ITenantResolveResultAccessor tenantResolveResultAccessor, IStringLocalizer stringLocalizer) { TenantResolver = tenantResolver; TenantStore = tenantStore; TenantResolveResultAccessor = tenantResolveResultAccessor; StringLocalizer = stringLocalizer; } public async Task 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 FindTenantAsync(string tenantIdOrName) { if (Guid.TryParse(tenantIdOrName, out var parsedTenantId)) { return await TenantStore.FindAsync(parsedTenantId); } else { return await TenantStore.FindAsync(tenantIdOrName); } } }