Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BasePathStrategy is not respecting the strategy chain order when setted with WithBasePathStrategy builder method #877

Closed
DiogoLuisBarros opened this issue Sep 12, 2024 · 3 comments · Fixed by #884

Comments

@DiogoLuisBarros
Copy link

DiogoLuisBarros commented Sep 12, 2024

Steps to reproduce

  1. Add one non-HttpContext dependant strategy
  2. Register the BasePathStrategy with the WithBasePathStrategy builder method
  3. Run your code in a way that a tenant resolution is invoked
  4. You get a MultiTenantException with the following message: "BasePathStrategy expects HttpContext."

My code:

var multitenantBuilder = services
    .AddMultiTenant<TenantExpandedInfo>()
    .WithStrategy<ForcedTenantStrategy>(ServiceLifetime.Singleton)
    .WithBasePathStrategy()
    .WithEFCoreStore<TenantDbContext, TenantExpandedInfo>()
    .WithPerTenantAuthentication();

Calling WithBasePathStrategy will not respect the chain of strategies because there are some checks regarding HttpContext that are being done, even if the previous strategy is resolved (in this case it is my custom ForcedTenantStrategy).

Please, check the following code:
https://github.com/Finbuckle/Finbuckle.MultiTenant/blob/main/src/Finbuckle.MultiTenant.AspNetCore/Extensions/MultiTenantBuilderExtensions.cs#L224C35-L224C106

Expected behaviour

These checks that are made because of the BasePathStrategy register process (during the OnTenantResolved delegate registration), but should should only be made if the strategy type is BasePathStrategy

@DiogoLuisBarros DiogoLuisBarros changed the title BasePathStrategy is not respecting chain order when setted with WithBasePathStrategy builder method BasePathStrategy is not respecting the strategy chain order when setted with WithBasePathStrategy builder method Sep 12, 2024
@DiogoLuisBarros
Copy link
Author

Proposed fix:

public static MultiTenantBuilder<TTenantInfo> WithBasePathStrategy<TTenantInfo>(
    this MultiTenantBuilder<TTenantInfo> builder, Action<BasePathStrategyOptions> configureOptions)
    where TTenantInfo : class, ITenantInfo, new()
{
    builder.Services.Configure(configureOptions);
    builder.Services.Configure<MultiTenantOptions>(options =>
    {
        var origOnTenantResolved = options.Events.OnTenantResolved;
        options.Events.OnTenantResolved = tenantResolvedContext =>
        {
            if (tenantResolvedContext.StrategyType == typeof(BasePathStrategy))
            {
                var httpContext = tenantResolvedContext.Context as HttpContext ??
                              throw new MultiTenantException("BasePathStrategy expects HttpContext.");

                if (httpContext.RequestServices.GetRequiredService<IOptions<BasePathStrategyOptions>>().Value
                        .RebaseAspNetCorePathBase)
                {
                    httpContext.Request.Path.StartsWithSegments($"/{tenantResolvedContext.TenantInfo?.Identifier}",
                        out var matched, out var
                            newPath);
                    httpContext.Request.PathBase = httpContext.Request.PathBase.Add(matched);
                    httpContext.Request.Path = newPath;
                }
            }

            return origOnTenantResolved(tenantResolvedContext);
        };
    });

    return builder.WithStrategy<BasePathStrategy>(ServiceLifetime.Singleton);
}

It worked for me and seems perfectly fine!

@AndrewTriesToCode
Copy link
Contributor

Thanks for flagging. I will be removing fixing this in the upcoming release.

@AndrewTriesToCode
Copy link
Contributor

🎉 This issue has been resolved in version 8.0.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

2 participants