Assigning a role to a user on runtime

Assigning a role to a user on runtime

Hi all,

I wanted to know if its possible to create a user and assign a role to said user, all during runtime?

Hi Pedro,

Check the How2UserRegistration application in the forge.
It's an example on how to do exaclty what you want.

Not sure if I like the GrantUserManagerRole/RevokeUserManagerRole in the example though.
You don't need that if you are creating a user when your current user already has permissions to do it. (The example is for self registration and the User_Create action from users makes a permission check)

The only thing the User_Create is to check for existing username and call the entity CreateUser action.
It's probably better to do that directly instead of giving the UserManagerRole temporarily to a new user just to be able to call User_Create.

João Rosado
In short, yes :)

Man this what I'm loving about Outsystems, there's a module for everything! :)
For those finding this topic and not wanting to check the forge application, here are the steps:

1) Create a user by referencing the User system table and call the CreateUser entity action (filling in the right attributes etc. Use the EncryptPassword function from the Users eSpace to encrypt the password);
2) Grant roles to that user by calling the appropriate GrantXxxRole actions (where Xxx is the role name). You can find these below the Roles themselves. (Unfortunately, there's no generic action that lets you grant a role based purely on its name.)