< Summary

Information
Class: Elsa.Tenants.AspNetCore.RoutePrefixTenantResolver
Assembly: Elsa.Tenants.AspNetCore
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Tenants.AspNetCore/Resolvers/RoutePrefixTenantResolver.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 32
Coverable lines: 32
Total lines: 69
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 12
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
ResolveAsync()0%7280%
FindTenantByPrefix()0%2040%
GetTenantRoutePrefixLookup()100%210%
GetPath(...)100%210%
GetMatchingRouteValues(...)100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Tenants.AspNetCore/Resolvers/RoutePrefixTenantResolver.cs

#LineLine coverage
 1using Elsa.Common.Multitenancy;
 2using Elsa.Extensions;
 3using Elsa.Http;
 4using Microsoft.AspNetCore.Http;
 5using Microsoft.Extensions.DependencyInjection;
 6
 7namespace 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>
 012public 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    {
 019        var httpContext = httpContextAccessor.HttpContext;
 20
 021        if (httpContext == null)
 022            return Unresolved();
 23
 024        var path = GetPath(httpContext);
 025        var routeData = GetMatchingRouteValues(path);
 26
 027        if (routeData == null)
 028            return Unresolved();
 29
 030        var routeValues = routeData.RouteValues;
 031        var tenantPrefix = routeValues["tenantPrefix"] as string;
 32
 033        if (string.IsNullOrWhiteSpace(tenantPrefix))
 034            return Unresolved();
 35
 036        var tenant = await FindTenantByPrefix(tenantPrefix);
 037        var tenantId = tenant?.Id;
 038        return AutoResolve(tenantId);
 039    }
 40
 41    private async Task<Tenant?> FindTenantByPrefix(string tenantPrefix)
 42    {
 043        _tenantRoutePrefixLookup ??= await GetTenantRoutePrefixLookup();
 044        return _tenantRoutePrefixLookup.TryGetValue(tenantPrefix, out var tenant) ? tenant : null;
 045    }
 46
 47    private async Task<Dictionary<string,Tenant>> GetTenantRoutePrefixLookup()
 48    {
 049        var tenantsProvider = serviceProvider.GetRequiredService<ITenantsProvider>();
 050        var tenants = await tenantsProvider.ListAsync();
 051        return tenants.ToDictionary(x => x.GetRoutePrefix()!);
 052    }
 53
 054    private string GetPath(HttpContext httpContext) => httpContext.Request.Path.Value!.NormalizeRoute();
 55
 56    private HttpRouteData? GetMatchingRouteValues(string path)
 57    {
 058        var routeMatcher = serviceProvider.GetRequiredService<IRouteMatcher>();
 059        var routeTable = serviceProvider.GetRequiredService<IRouteTable>();
 60
 061        var matchingRouteQuery =
 062            from routeData in routeTable
 063            let routeValues = routeMatcher.Match(routeData.Route, path)
 064            where routeValues != null
 065            select new HttpRouteData(routeData.Route, routeData.DataTokens, routeValues);
 66
 067        return matchingRouteQuery.FirstOrDefault();
 68    }
 69}