abp icon indicating copy to clipboard operation
abp copied to clipboard

Is it possible to change the tenant after login?

Open mperk opened this issue 5 years ago • 13 comments

Used separate identityserver4 and angular. In one application have multiple tenants. There is two or more users. All of them have same username, mail address, other infos, but have different tenantId. Is it possible to change the tenant after login? if it is ok how can i do it, can you give me some hints please

mperk avatar Jan 18 '21 14:01 mperk

https://docs.abp.io/en/abp/latest/Multi-Tenancy#change-the-current-tenant Does this work for you?

ilkayilknur avatar Jan 18 '21 14:01 ilkayilknur

Of course it depends on whether you want to switch tenant completely:

  • if yes: ABP has separate AbpUsers accounts per tenant, so you need to make sure the users exist for the tenant (also null/Host tenant). You can link users with AbpLinkUsers to be able to really switch between tenants faster.
  • if not: @ilkayilknur his solution should get you what you need in most cases, but it's of course temporary and I'd be quite careful with it

davidzwa avatar Jan 18 '21 14:01 davidzwa

Thanks @ilkayilknur , @davidzwa I tried the CurrentTenant.Change after I check user tenants. But the angular side and current auth cookie/token did not change

mperk avatar Jan 18 '21 15:01 mperk

Yes this makes sense; depending on the authentication method you use, the JWT/access token contents and thus the claims inside it will not change without refreshing the login. This is exactly what I meant: the CurrentTenant.Change method is only temporary and basically allows you to change the tenancy scope of the API, but not the client. That requires you to change tenant explicitly, so the TenantId in the Authorization headers is adapted (ApiInterceptor service does this). https://github.com/abpframework/abp/blob/dev/npm/ng-packs/packages/core/src/lib/interceptors/api.interceptor.ts

So how it works in Angular:

  1. oAuthState is set after login
  2. API request causes call to ApiInterceptor (HttpInterceptor) appends TenantId as __tenant Http-header
  3. API receives this and recognizes TenantId (it configures the ICurrentTenant and DataFilter<IMultiTenant> for database/store queries

For Angular, if you went for the basic template, the easiest way is to logout and login to make sure the TenantId in the oAuthState is changed (in the browser/storage). This is not the best user experience. So, we have to build something which can logout/login without the user noticing this switch. This is definitely possible, but requires work.

I'm also looking into making this easier for my users as well, as I'm looking at how google does this (instant switch between logins). It's not easy to do this. Does the previous look like what you are trying to achieve?

davidzwa avatar Jan 18 '21 15:01 davidzwa

I'm collecting useful information here:

  • Switching users requires token change (by authors) https://github.com/abpframework/abp/issues/5413
  • Host/tenant access https://github.com/abpframework/abp/issues/1796
  • Impersonating users https://github.com/abpframework/abp/issues/1796
  • Complete tenancy/admin disabling https://github.com/abpframework/abp/issues/2202
  • Switch between tenants on login page https://github.com/abpframework/abp/issues/4694

Let me know what your requirements are, maybe disabling tenancy for certain API controllers/services could help, or maybe the linked user accounts can or maybe you need complete switch between tenant... my deadline for this feature is not soon, so it's dependent on your answer. 👍🏼

davidzwa avatar Jan 18 '21 15:01 davidzwa

I used separate identityserver4 and angular version. In one application have multiple tenants. I want to If a user have multiple tenants, the user can change the tenant with a selectbox on header.

mperk avatar Jan 18 '21 16:01 mperk

You should accept then that there is no such thing as 1 user, multiple tenants. ABP requires 1 user per tenant, so you need clones per tenant. Work on that first.

davidzwa avatar Jan 18 '21 17:01 davidzwa

Actually, i said wrongly, sorry. There is two or more user. All of them have same username, mail address, but have different tenantId. I can check the users with username.

mperk avatar Jan 18 '21 17:01 mperk

Please close the issue, as I've provided enough information on what Abp allows you to do.

davidzwa avatar Jan 19 '21 10:01 davidzwa

sorry but my question is still there. I know already your all suggestions. Maybe someone help to me in future

mperk avatar Jan 19 '21 10:01 mperk

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Mar 20 '21 11:03 stale[bot]

Hi, we have custom implementation for switching a tenant after a user logged in. It includes handling a custom grant type - we named it switch_tenant - by custom server-side validator ( SwitchToTenantGrantValidator : IExtensionGrantValidator), where it is sent by our custom version of authService of Angular app as a grant-type HttpParam. Everything works as expected. But we wonder if there is more advanced mechanism of doing this by ABP means in version 4.3.0?

alexandis avatar May 11 '21 08:05 alexandis

I've been wanting to be able to allow one user to access multiple accounts without logging back in for a long time. something like this https://github.com/abpframework/abp/issues/2611

steves-bits avatar Mar 16 '22 14:03 steves-bits