| | | 1 | | using Elsa.Common.Multitenancy; |
| | | 2 | | using Elsa.Extensions; |
| | | 3 | | using Elsa.Http; |
| | | 4 | | using Microsoft.AspNetCore.Http; |
| | | 5 | | using Microsoft.Extensions.DependencyInjection; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Tenants.AspNetCore; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Resolves the tenant based on the route prefix in the request URL. The tenant ID is expected to be part of the route. |
| | | 11 | | /// </summary> |
| | 0 | 12 | | public class RoutePrefixTenantResolver(IHttpContextAccessor httpContextAccessor, IServiceProvider serviceProvider) : Ten |
| | | 13 | | { |
| | | 14 | | private Dictionary<string, Tenant>? _tenantRoutePrefixLookup; |
| | | 15 | | |
| | | 16 | | /// <inheritdoc /> |
| | | 17 | | protected override async Task<TenantResolverResult> ResolveAsync(TenantResolverContext context) |
| | | 18 | | { |
| | 0 | 19 | | var httpContext = httpContextAccessor.HttpContext; |
| | | 20 | | |
| | 0 | 21 | | if (httpContext == null) |
| | 0 | 22 | | return Unresolved(); |
| | | 23 | | |
| | 0 | 24 | | var path = GetPath(httpContext); |
| | 0 | 25 | | var routeData = GetMatchingRouteValues(path); |
| | | 26 | | |
| | 0 | 27 | | if (routeData == null) |
| | 0 | 28 | | return Unresolved(); |
| | | 29 | | |
| | 0 | 30 | | var routeValues = routeData.RouteValues; |
| | 0 | 31 | | var tenantPrefix = routeValues["tenantPrefix"] as string; |
| | | 32 | | |
| | 0 | 33 | | if (string.IsNullOrWhiteSpace(tenantPrefix)) |
| | 0 | 34 | | return Unresolved(); |
| | | 35 | | |
| | 0 | 36 | | var tenant = await FindTenantByPrefix(tenantPrefix); |
| | 0 | 37 | | var tenantId = tenant?.Id; |
| | 0 | 38 | | return AutoResolve(tenantId); |
| | 0 | 39 | | } |
| | | 40 | | |
| | | 41 | | private async Task<Tenant?> FindTenantByPrefix(string tenantPrefix) |
| | | 42 | | { |
| | 0 | 43 | | _tenantRoutePrefixLookup ??= await GetTenantRoutePrefixLookup(); |
| | 0 | 44 | | return _tenantRoutePrefixLookup.TryGetValue(tenantPrefix, out var tenant) ? tenant : null; |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | private async Task<Dictionary<string,Tenant>> GetTenantRoutePrefixLookup() |
| | | 48 | | { |
| | 0 | 49 | | var tenantsProvider = serviceProvider.GetRequiredService<ITenantsProvider>(); |
| | 0 | 50 | | var tenants = await tenantsProvider.ListAsync(); |
| | 0 | 51 | | return tenants.ToDictionary(x => x.GetRoutePrefix()!); |
| | 0 | 52 | | } |
| | | 53 | | |
| | 0 | 54 | | private string GetPath(HttpContext httpContext) => httpContext.Request.Path.Value!.NormalizeRoute(); |
| | | 55 | | |
| | | 56 | | private HttpRouteData? GetMatchingRouteValues(string path) |
| | | 57 | | { |
| | 0 | 58 | | var routeMatcher = serviceProvider.GetRequiredService<IRouteMatcher>(); |
| | 0 | 59 | | var routeTable = serviceProvider.GetRequiredService<IRouteTable>(); |
| | | 60 | | |
| | 0 | 61 | | var matchingRouteQuery = |
| | 0 | 62 | | from routeData in routeTable |
| | 0 | 63 | | let routeValues = routeMatcher.Match(routeData.Route, path) |
| | 0 | 64 | | where routeValues != null |
| | 0 | 65 | | select new HttpRouteData(routeData.Route, routeData.DataTokens, routeValues); |
| | | 66 | | |
| | 0 | 67 | | return matchingRouteQuery.FirstOrDefault(); |
| | | 68 | | } |
| | | 69 | | } |