| | | 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> |
| | 0 | 11 | | public class DefaultTenantResolverPipelineInvoker( |
| | 0 | 12 | | IOptions<MultitenancyOptions> options, |
| | 0 | 13 | | ITenantsProvider tenantsProvider, |
| | 0 | 14 | | IServiceProvider serviceProvider, |
| | 0 | 15 | | ILogger<DefaultTenantResolverPipelineInvoker> logger) : ITenantResolverPipelineInvoker |
| | | 16 | | { |
| | | 17 | | public async Task<Tenant?> InvokePipelineAsync(CancellationToken cancellationToken = default) |
| | | 18 | | { |
| | 0 | 19 | | var resolutionPipeline = options.Value.TenantResolverPipelineBuilder.Build(serviceProvider); |
| | 0 | 20 | | var tenantsDictionary = (await tenantsProvider.ListAsync(cancellationToken)).ToDictionary(x => x.Id); |
| | 0 | 21 | | var context = new TenantResolverContext(tenantsDictionary, cancellationToken); |
| | | 22 | | |
| | 0 | 23 | | foreach (var resolver in resolutionPipeline) |
| | | 24 | | { |
| | 0 | 25 | | var result = await resolver.ResolveAsync(context); |
| | | 26 | | |
| | 0 | 27 | | if (result.IsResolved) |
| | | 28 | | { |
| | 0 | 29 | | var resolvedTenantId = result.ResolveTenantId(); |
| | | 30 | | |
| | 0 | 31 | | if (tenantsDictionary.TryGetValue(resolvedTenantId, out var tenant)) |
| | 0 | 32 | | return tenant; |
| | | 33 | | |
| | 0 | 34 | | logger.LogWarning("Tenant with ID {TenantId} was resolved but could not be found in the tenant store.", |
| | 0 | 35 | | return null; |
| | | 36 | | } |
| | | 37 | | } |
| | | 38 | | |
| | 0 | 39 | | return null; |
| | 0 | 40 | | } |
| | | 41 | | } |