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

OnTenantNotResolved #628

Open
DanieleSky opened this issue Jan 17, 2023 · 9 comments
Open

OnTenantNotResolved #628

DanieleSky opened this issue Jan 17, 2023 · 9 comments

Comments

@DanieleSky
Copy link

Hi,
I'm using your library in a ASP.NET Core project.
This is the configuration:

services
  .AddMultiTenant<T>(config =>
  {
      config.Events.OnTenantNotResolved = context =>
      {
          throw new BadHttpRequestException($"Tenant {context.Identifier} not found");
      };
  })
  .WithDistributedCacheStore()
  .WithHeaderStrategy("x-tenant-id");

I've two questions:

  1. How I can return a 404 response when a tenant is not resolved instead to throw an exception? I think that OnTenantNotResolved should be a func in Task<T> so I could return an IActionResult.
    I tried with this solution Exceptions logged using OnTenantNotResolved #554 but don't work because the line Task.CompleteTask don't interrupt the execution of the code and I receive this exception Unable to resolve service for type 'Finbuckle.MultiTenant.ITenantInfo' while attempting to activate when i use the dependency injection in my controller
    public MyController(IHttpClientFactory httpClientFactory, ITenantInfo tenantInfo)

  2. Why when the header "x-tenant-id" is missing the event OnTenantNotResolved don't raise? Seems the strategy is ignored. Is right?

Thanks!

@AndrewTriesToCode
Copy link
Sponsor Contributor

Hi, for (1) I plan to build in support for this, but for now the easiest method is to place a simple middleware right after the UseMultiTenant<T> middleware that checks for null tenant and returns a 404 error. Something like:

app.Use(async (context, next) =>
{
if(context.GetMultiTenantContext?.TenantInfo is null)
{
// build and return a 404 response...
}

await next.Invoke();

});

For (2) I'm not sure, I'll need to test that. I would want it to raise in that situation.

thatgoofydev added a commit to thatgoofydev/Finbuckle.MultiTenant that referenced this issue Aug 29, 2023
thatgoofydev added a commit to thatgoofydev/Finbuckle.MultiTenant that referenced this issue Aug 29, 2023
github-actions bot pushed a commit that referenced this issue Dec 21, 2023
## [6.13.0](v6.12.0...v6.13.0) (2023-12-21)

### Features

* .net8.0 LTS release support ([#770](#770)) ([d7f08f9](d7f08f9))

### Bug Fixes

* OnTenantNotResolved not called correctly ([#729](#729)) ([a26081c](a26081c)), closes [#628](#628)
@AndrewTriesToCode
Copy link
Sponsor Contributor

🎉 This issue has been resolved in version 6.13.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

@natelaff
Copy link
Contributor

natelaff commented Jan 15, 2024

This change broke my code and I was the original contributor of the OnTenantNotResolved haha. Now it seems to be firing way too early?

@AndrewTriesToCode Actually what seems to be happening is its no longer respecting IgnoredIdentifiers before that OnTenantNotResolved event fires. Which is odd because I don't see anything in the commit. But between 6.12 and 6.13 that broke.

@AndrewTriesToCode
Copy link
Sponsor Contributor

Hi @natelaff glad to hear from you.

The change in implementation here was intended to fire the not found event if no tenant identifier was found -- prior it was only firing this event if an identifier was found but no tenant resolved from store. I'll take a closer look when I can but feel free to submit any fixes as you see fit. Maybe a distinction between identifier found but no tenant resolved and no identifier found would make sense but I still think the primary use case in general is tenant not found -- either way. Of course it should also respect explicitly ignore identifiers.

@natelaff
Copy link
Contributor

@AndrewTriesToCode i haven't had time to wrap my head around the change. I looked at it, but it looks like that event system changed even a little bit since I submitted it. But to me, the purpose of the TenantNotFound event was it identified there was a tenant (unless explicitly ignored by identifiers) but couldn't find it in the store.

Seems to me routing or whatever other pattern you're using should take care of the tenant identifier null, and not this event. Kind of seems like someone shoe horned this in and broke its original intent.

@AndrewTriesToCode
Copy link
Sponsor Contributor

AndrewTriesToCode commented Jan 25, 2024

I’m leaning toward a TenantIdentifierNotFound event to complement the existing one and revert that one back to how it was. What do you think? Maybe event rename the current one to TenantInfoNotFound to be explicit.

@natelaff
Copy link
Contributor

natelaff commented Jan 25, 2024

Not being in the group of people that experienced this issue, it's hard to say. Granted, that event was added by me for a very specific purpose so it always just worked for me haha -- until now.

From my understanding of the issue that influenced this change, it's almost like they want TenantIdentifierNotFound event? Not even TenantInfo, because info would be resolved past that point? Seems like they want this to trigger earlier than that?

EDIT: Ah, yes we're thinking the exact same thing!

@goforebroke
Copy link

goforebroke commented Feb 19, 2024

@AndrewTriesToCode

Hi Andrew,

After reading through this, the current implementation of "OnTenantNotResolved" checks if there is an identifier found based on your strategy(s). You are considering reverting "OnTenantNotResolved" to its original implementation, which fires if a tenant is not found in the tenant store(s). You are considering creating another event, possibly "TenantIdentifierNotFound", that that implements the current logic found in "OnTenantNotResolved"? If so, I think this would be a nice addition to the Finbuckle.

@AndrewTriesToCode
Copy link
Sponsor Contributor

Thanks guys. I'll work up a PR soon. Working on getting the new options functionality out first, then this.

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

No branches or pull requests

4 participants