How to get logged in user role?

Hi, 


In reactive web I am using System's user, User_Role and Role entities. However I am able to get logged in user's id by using GetUserId() function but what is the best way to fetch logged in user's role ?

I created a custom role but I can not use CheckSuperAdminRole function in an expression or anywhere to check loggedin user's role. I want to hide few buttons based on roles.

Sohan wrote:

Hi, 


In reactive web I am using System's user, User_Role and Role entities. However I am able to get logged in user's id by using GetUserId() function but what is the best way to fetch logged in user's role ?

I created a custom role but I can not use CheckSuperAdminRole function in an expression or anywhere to check loggedin user's role. I want to hide few buttons based on roles.

Hello Sohan did you see this page on the forum?

https://www.outsystems.com/forums/discussion/29777/cannot-use-check-roles-function/


Hello Sohan,

You can use the Roles methods only on the server side. For client side role checking OutSystems provides Javascript API. Check the documentation here 

You need to use the method checkIfCurrentUserHasRole

Hope this helps !!!


Hi Bruno and Amal,

Thanks for the reply.  I checked in dependencies and found User_CheckRole under UserLocalManagement library. What is  its use?  Can it help to identify logged in user role?

Hi,

 Please check the screenshot and you will get understood how to activate a button for a particular role. In the Visible property you have to put the check role function. if you need it for two roles then you have to put 2 check role functions with a or separator.

I hope this will resolve your issues.

Thank you,

Sudip

Solution

Hi Sohan,

Sudip's solution is correct for treditional web app but not for reactive. 

If you want to use it in your visible property of any screen widget then you can create an action using "Fetch Data from Other Sources" which is available on screen when you right click.

In this action your service side role actions will be available which you can call and assign their result to your output variable. This output variable will be now available in all places of your screen like expressions, screen actions etc.

Don't forget to check the role server side upon your button click event before performing the actual operation.

Solution

Nikhil Gaur wrote:

Hi Sohan,

Sudip's solution is correct for treditional web app but not for reactive. 

If you want to use it in your visible property of any screen widget then you can create an action using "Fetch Data from Other Sources" which is available on screen when you right click.

In this action your service side role actions will be available which you can call and assign their result to your output variable. This output variable will be now available in all places of your screen like expressions, screen actions etc.

Don't forget to check the role server side upon your button click event before performing the actual operation.

Thanks Nikhil


It worked for me.


Hi Shoan,

The system will work in Reactive also. You have to implement some small logic. You have to take the Role and role to the user table from the system. and needs to fetch the Role name and check it in the visible property. It will work fine.

Please check the screen,

Thank you,

Sudip

Sudip Dey wrote:

Hi Shoan,

The system will work in Reactive also. You have to implement some small logic. You have to take the Role and role to the user table from the system. and needs to fetch the Role name and check it in the visible property. It will work fine.

Please check the screen,

Thank you,

Sudip

Hi Sudip,

Yes you are right. This is also an alternative. But you are using hardcoded role names which is not a good practice.