Grant Role in Multi-Tenant Application not working at registration

Grant Role in Multi-Tenant Application not working at registration

  

I have a multi-tenant application that Users will register for themselves.  This means that the Create User action is being called from an anonymous session.  When I create the User and several other classes used with that user I simply set the Tenant_Id based on the business logic.

However, when I run a GrantRole for the application's role, it then does not show up that that user has that role.

I can see that this is creating a User_Role properly, but it seems to be creating it under a different Tenant, not the Tenant that the user exists under.

GrantRole does not take in a TenantId as input so how should I handle this?

I could create the User_Role manually, but is there a better way?

Thanks,

Jordan.

Hi Jordan,

Did you use the TenantSwitch System Action?

Aren't you able to Login rigth after the user creation and granting the role after?

Kilian:

I'd been having some problems with Tenant switch, it seemed to break the Action completely when called inside of an action. Wondering if it has something to do with "When switching tenant, the session is cleared (equivalent to an implicit logout, before changing the tenant)" that is described in the action. It wasn't behaving correctly. Also not sure if it works for Anonymous users at all.

Tiago:

If I just grant the role to everyone who logs in then there really isn't a point in having the role anyway, it would essentially just be using Registered.

Hi Jordan,


can you tell us what is the error you are getting while using tenant switch?


Also check settings in user entity.



Thanks,

Rajat Agrawal

Hi Jordan,

The Grant*Role actions will grant the specified User that Role, in the currently selected Tenant. That means you will need to either call TenantSwitch or manually create a new User_Role entity record (that associates a User with a Role on a Tenant).

I'd advise against direct manipulation of System entities if you can use the builtin functions/actions, but you do have that option. What was the incorrect behaviour you were experiencing with TenantSwitch?

Solution

Multitenant Management component may be used a reference on how to do this by populating the User_Role entity.

Solution

I was trying to recreate the behavior I was having with TenantSwitch without having to break my application again but I could not seem to replicate it. For now I am going to set the role manually, and just replicate the logic from Multitenant Management