| | | 1 | | using Elsa.Common.Multitenancy; |
| | | 2 | | using Elsa.Tenants.Options; |
| | | 3 | | using JetBrains.Annotations; |
| | | 4 | | using Microsoft.Extensions.Configuration; |
| | | 5 | | using Microsoft.Extensions.Logging; |
| | | 6 | | using Microsoft.Extensions.Options; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Tenants.Providers; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Provides the implementation to retrieve tenant information from a configuration. |
| | | 12 | | /// </summary> |
| | | 13 | | [UsedImplicitly] |
| | | 14 | | public class ConfigurationTenantsProvider : ITenantsProvider |
| | | 15 | | { |
| | | 16 | | private readonly IConfiguration _configuration; |
| | | 17 | | private readonly ITenantService _tenantService; |
| | | 18 | | private readonly ILogger<ConfigurationTenantsProvider> _logger; |
| | 0 | 19 | | private ICollection<Tenant> _tenants = new List<Tenant>(); |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Initializes a new instance of the <see cref="ConfigurationTenantsProvider"/> class. |
| | | 23 | | /// </summary> |
| | 0 | 24 | | public ConfigurationTenantsProvider(IOptionsMonitor<TenantsOptions> options, IConfiguration configuration, ITenantSe |
| | | 25 | | { |
| | 0 | 26 | | _configuration = configuration; |
| | 0 | 27 | | _tenantService = tenantService; |
| | 0 | 28 | | _logger = logger; |
| | 0 | 29 | | UpdateTenants(options.CurrentValue); |
| | 0 | 30 | | options.OnChange(OnOptionsChanged); |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | private async void OnOptionsChanged(TenantsOptions options, string? name) |
| | | 34 | | { |
| | | 35 | | try |
| | | 36 | | { |
| | 0 | 37 | | UpdateTenants(options); |
| | 0 | 38 | | await _tenantService.RefreshAsync(); |
| | 0 | 39 | | } |
| | 0 | 40 | | catch (Exception e) |
| | | 41 | | { |
| | 0 | 42 | | _logger.LogError(e, "An error occurred while updating tenants."); |
| | 0 | 43 | | } |
| | 0 | 44 | | } |
| | | 45 | | |
| | | 46 | | private void UpdateTenants(TenantsOptions options) |
| | | 47 | | { |
| | 0 | 48 | | var tenants = options.Tenants.ToList(); |
| | | 49 | | |
| | | 50 | | // Rebind each Tenant's Configuration property manually using array indices |
| | 0 | 51 | | for (int i = 0; i < tenants.Count; i++) |
| | 0 | 52 | | tenants[i].Configuration = _configuration.GetSection($"Multitenancy:Tenants:{i}:Configuration"); |
| | | 53 | | |
| | 0 | 54 | | _tenants = tenants; |
| | 0 | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <inheritdoc /> |
| | | 58 | | public Task<IEnumerable<Tenant>> ListAsync(CancellationToken cancellationToken = default) |
| | | 59 | | { |
| | 0 | 60 | | return Task.FromResult<IEnumerable<Tenant>>(_tenants); |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <inheritdoc /> |
| | | 64 | | public Task<Tenant?> FindAsync(TenantFilter filter, CancellationToken cancellationToken = default) |
| | | 65 | | { |
| | 0 | 66 | | var tenantsQueryable = _tenants.AsQueryable(); |
| | 0 | 67 | | var tenant = filter.Apply(tenantsQueryable).FirstOrDefault(); |
| | 0 | 68 | | return Task.FromResult(tenant); |
| | | 69 | | } |
| | | 70 | | } |