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

[Question] MultiTenancy without stores? #805

Open
Thijs153 opened this issue Apr 8, 2024 · 5 comments · May be fixed by #807
Open

[Question] MultiTenancy without stores? #805

Thijs153 opened this issue Apr 8, 2024 · 5 comments · May be fixed by #807
Labels

Comments

@Thijs153
Copy link

Thijs153 commented Apr 8, 2024

Hey @AndrewTriesToCode, I’m relatively new to this library and I’m enjoying it so far, but I have a question: Is it somehow possible to implement MultiTenancy without specifying/implementing any stores?

I’m using Keycloak to specify my tenants (realms). I’ve already implemented the authentication part (custom) and I’m not trying to use ‘WithPerTenantAuthentication’ here. The tenant name/identifier can be retrieved from the token, so that’ll be my strategy.

As for the TenantInfo, I just need one with the id/identifier set to this claim value, without any additional settings. I don’t really like having to specify all the different tenant names in the config. Hence the question.

So again, is it somehow possible to have a sort of ‘tenant resolver’ that returns a Tenant with the id/identifier set to a value in a token, without having to specify/configure a store?

I hope this makes sense, thanks!

@AndrewTriesToCode
Copy link
Sponsor Contributor

Hi, I see what you mean. I recommend a very basic store that does nothing but implement IMultiTenantStore<TTenantInfo>.TryGetByIdentifier to return a new tenant info object with the identifier passed into it. It doesn't actually look anything up, just makes the tenant info instance and returns it. Then you would register it using WithStore<T>.

@Thijs153
Copy link
Author

Thijs153 commented Apr 9, 2024

Thanks for the quick reply. Your suggestion works for me 👍

For anyone coming across this issue, I've implemented the store like this:

public sealed class KeycloakTenantStore : IMultiTenantStore<TenantInfo>
{
    public async Task<TenantInfo?> TryGetByIdentifierAsync(string identifier)
        => await Task.FromResult(new TenantInfo { Id = identifier, Identifier = identifier, });

    public async Task<TenantInfo?> TryGetAsync(string id)
        => await Task.FromResult(new TenantInfo { Id = id, Identifier = id, });
    
    public Task<bool> TryAddAsync(TenantInfo tenantInfo)
        => throw new NotImplementedException();
    
    public Task<bool> TryUpdateAsync(TenantInfo tenantInfo)
        => throw new NotImplementedException();

    public Task<bool> TryRemoveAsync(string identifier)
        => throw new NotImplementedException();
    
    public Task<IEnumerable<TenantInfo>> GetAllAsync()
        => throw new NotImplementedException();
}

The issue can be closed in my opinion.

@AndrewTriesToCode
Copy link
Sponsor Contributor

Nice. It might make sense to add this to the library for testing and certain use cases. Would you be up for submitting it as a PR?

@Thijs153
Copy link
Author

@AndrewTriesToCode, sure. How would you like me to call this new 'store'?
And is it just creating a new store, creating an extension method for it, and that's is? Or do I need to do more things?

@AndrewTriesToCode
Copy link
Sponsor Contributor

Hi, adding the store to the Finbuckle.MultiTenant stores folder, adding an extension method to MultiTenantBuilder and adding a short section to the stores Docs is what I’d recommend. I think calling it the Echo store makes sense— just gives back what you put in.

Thijs153 pushed a commit to Thijs153/Finbuckle.MultiTenant that referenced this issue Apr 11, 2024
@Thijs153 Thijs153 linked a pull request Apr 11, 2024 that will close this issue
Thijs153 pushed a commit to Thijs153/Finbuckle.MultiTenant that referenced this issue Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

2 participants