| | | 1 | | using Elsa.Common.Multitenancy; |
| | | 2 | | using Elsa.Tenants.Options; |
| | | 3 | | using Microsoft.Extensions.Logging; |
| | | 4 | | using Microsoft.Extensions.Options; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Tenants; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Resolves the tenant using a pipeline of resolvers. |
| | | 10 | | /// </summary> |
| | 298 | 11 | | public class DefaultTenantResolverPipelineInvoker( |
| | 298 | 12 | | IOptions<MultitenancyOptions> options, |
| | 298 | 13 | | ITenantsProvider tenantsProvider, |
| | 298 | 14 | | IServiceProvider serviceProvider, |
| | 298 | 15 | | ILogger<DefaultTenantResolverPipelineInvoker> logger) : ITenantResolverPipelineInvoker |
| | | 16 | | { |
| | | 17 | | public async Task<Tenant?> InvokePipelineAsync(CancellationToken cancellationToken = default) |
| | | 18 | | { |
| | 298 | 19 | | var resolutionPipeline = options.Value.TenantResolverPipelineBuilder.Build(serviceProvider); |
| | 298 | 20 | | var tenants = await tenantsProvider.ListAsync(cancellationToken); |
| | 1481 | 21 | | var tenantsDictionary = tenants.ToDictionary(x => x.Id.NormalizeTenantId()); |
| | 298 | 22 | | var context = new TenantResolverContext(tenantsDictionary, cancellationToken); |
| | | 23 | | |
| | 896 | 24 | | foreach (var resolver in resolutionPipeline) |
| | | 25 | | { |
| | 298 | 26 | | var result = await resolver.ResolveAsync(context); |
| | | 27 | | |
| | 298 | 28 | | if (result.IsResolved) |
| | | 29 | | { |
| | 296 | 30 | | var resolvedTenantId = result.ResolveTenantId(); |
| | | 31 | | |
| | 296 | 32 | | if (tenantsDictionary.TryGetValue(resolvedTenantId, out var tenant)) |
| | 295 | 33 | | return tenant; |
| | | 34 | | |
| | 1 | 35 | | logger.LogWarning("Tenant with ID {TenantId} was resolved but could not be found in the tenant store.", |
| | 1 | 36 | | return null; |
| | | 37 | | } |
| | | 38 | | } |
| | | 39 | | |
| | 2 | 40 | | return null; |
| | 298 | 41 | | } |
| | | 42 | | } |