267
Views
7
Comments
Solved
Visible menu based on user role login
Question
Application Type
Reactive

Hi, I'm doing check role function when user login to my reactive web app with condition below:

- User login by ID and back end will get the ID to check in Emp table, if 'Department' of this ID is 'HR' they will see the sub menu, if the 'Department' is 'User', they won't see this menu

Here is my DB

I add the 'CheckRole' action to check role when user login 

But I don't know how to get ID and check in DB, please help me on this

Here is my oml.

MultiSearch_Ex.oml
2023-03-16 16-29-51
Paulo Rosário
Solution

Hello Bella, 

So let's take follow a new improved approach. 

I modified your oml and added an action that will grant the HRRole to a user that has the "HR" value in the Emp table. This happens when the user logs in. 

Once the user has been granted the role we can use the platform's Role actions to check if the user can see the menu.

This pattern can be used for any extra role you need. One important aspect is that this only looks at the value in the emp table, so if this value changes ( from HR to PM for example ) the PM will still have the HR role. To avoid this you can add some logic to the logout flow to remove the role once the user logs out. Ideally, you would use a back office to change roles. 

I will attach the modified oml so you can take a better look.

Hope this helps!

Paulo Rosário

MultiSearch_Ex_Modified.oml
UserImage.jpg
Bella Nguyen

Thank you for all your help, its really work :) 

2023-03-16 16-29-51
Paulo Rosário
2023-03-16 16-29-51
Paulo Rosário

Hello Bella, 

You can use the GetUserId() function to get the ID of the logged user.

This can be used in aggregates as well.

By looking at your oml I would sugest you use the Role functunality from OUtsystems. You can assign roles maualy to a user in your users backoffice ( <yourenviromentURL>/Users/ ) , by doing this you can just use the CheckRole function in your menu and avoid extra logic.


Hope this helps! 

Paulo Rosário

2024-09-12 02-43-38
Deepsagar Dubey

Hi @Bella Nguyen 

Once user logged in then you can use GetUserId() function it'll return you the logged in User id,

and in your employee table create a relationship with the Users table means add a new column UserId so basically all your employee will be the User, then fetch record from Employee table where Employee.UserId = GetUserId() 

after that you can get department and implement your check.

I hope it'll help you,

Thanks
Deep

UserImage.jpg
Bella Nguyen

Thank you for both suggestions, I've tried all of theme but still not have expected result. Could you please have a quick check my oml for it?

MultiSearch_Ex.oml
2024-09-12 02-43-38
Deepsagar Dubey

Hi,

I checked your code first of all you need to understand that, all your login happening from Users table, and you need to assign Roles to the particular user.

But you are try to obtain role from Employee table, that's why you need to create a relationship in Employee and user then platform can identify role.

Also here you are trying to check it by IsHR local variable and you assigned it to CheckRole client action but this action didn't have any usage or call.


you need to change your process.

Thanks
Deep

2023-03-16 16-29-51
Paulo Rosário
Solution

Hello Bella, 

So let's take follow a new improved approach. 

I modified your oml and added an action that will grant the HRRole to a user that has the "HR" value in the Emp table. This happens when the user logs in. 

Once the user has been granted the role we can use the platform's Role actions to check if the user can see the menu.

This pattern can be used for any extra role you need. One important aspect is that this only looks at the value in the emp table, so if this value changes ( from HR to PM for example ) the PM will still have the HR role. To avoid this you can add some logic to the logout flow to remove the role once the user logs out. Ideally, you would use a back office to change roles. 

I will attach the modified oml so you can take a better look.

Hope this helps!

Paulo Rosário

MultiSearch_Ex_Modified.oml
UserImage.jpg
Bella Nguyen

Thank you for all your help, its really work :) 

2023-03-16 16-29-51
Paulo Rosário
Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.