30
Views
21
Comments
Solved
How to filter an agregate by the last user that logged in?
Application Type
Traditional Web

Hi! So I am building an app and, based on one attribute of the User, I want to change the display of the menu (Set some things invisible). So, I set on display this:

IdentifierToInteger(GetUsers.List.Current.User.Attribute1) = 1

It is always showing me one user, no matter which user I login with. So,should I use a filter, which one?

Rank: #4257
Solution

Hello.

Well actually this IS a problem. As I have already seen your oml file from your previous question I can tell you this:
Usually you would filter an User aggregate for the currently logged in user - as everybody here said - with the built in function GetUserId(). But this is only working with the User entity from the System.
In your application, you built your own User and Role entities. Your User-id is of type Long Integer and not a foreign key to the System User. Which is why your aggregate will still return all the users that are in the database and the container visibility is still not behaving as desired.
If you use the built in User and Roles entities, the suggestions from everyone here will work. If you want to use your own entities, I don´ t know how to retrieve the current user in a secure way.
 

Rank: #2442
Solution

Also, based on your question. You should use roles to check if a user is allowed to see something like @Rahul Sahu recommended. Don't put this in the display property of the container though.. This will just add a display: none in the inspector and can easily be tampered with to be visible. Use an if widget instead. Also I have the feeling that you don't understand the platform too well. Did you follow the training courses? I would recommend to check them out.

You can find the guided paths on the following link:

https://www.outsystems.com/training/paths/


mvp_badge
MVP
Rank: #27
Solution

Yes that's the problem just replace the GetUser (entity action) with the builtin GetUserId() action. 

mvp_badge
MVP
Rank: #27

What do you query in the GetUsers.List.Current.User.Attribute1 query/aggregate? is it filtered by Current userId with the GetUserId() builtin action?


Rank: #13553

I did not understand you but my aggregate only has the user info, nothing more. No filters, no nothing

mvp_badge
MVP
Rank: #27

That explains it; you have to add the filter User.Id = GetUserId(); this will filter the aggregate on current user.

Rank: #13553

But When I try to use GetUserId() i need to put something between the (), what do i put?

Menu2png.PNG

Champion
Rank: #377

Hi Gonçalo,

Matthias was refering to the Built-in action GetUserId(). This is a function that returns the Id of the current logged- in user.
Your screenshot shows GetUser(), which is an entity action to return a record of the User entity.

Regards,
Lennart

Rank: #287

Hi Gonçalo 

Matthias is right, You have to use the GetUserId action to get login user id. And set it to filter.

I don't know what the Attribute1 you mean, But show/hide the menu item mostly depends on the login user role.

If so,  insdead of using aggregate, You should use CheckRoleAction for each role.

Kind Regard

mvp_badge
MVP
Rank: #27

Hi @Gonçalo NogueiraCould you show us the aggragate youre using, then we can point where to put the filter on GetUserId().

Rank: #13553

Here you go

Menu2png.PNG

mvp_badge
MVP
Rank: #27
Solution

Yes that's the problem just replace the GetUser (entity action) with the builtin GetUserId() action. 

Rank: #13553
Menu3.PNG

Rank: #2442

Hi Goncalo, 

Not really sure what you mean with "what do I put inside that", the getUserId() doesn't need anything between the brackets. It will just return the userid of the currently logged in user.

Below is a screenshot of how it should look.

 

Rank: #13553

Okay, my msitake
Anyways. I have done that and it still does not work


I have 2 accounts, one with Attribute1 = 1 and another with Attribute1 = 2
No matter with wich one I login. It does not show the menu

Menu4.PNG

Rank: #13553
Menu5.PNG

mvp_badge
MVP
Rank: #27

It's in the wrong order User.Id = GetUserId(); but that is not the problem i think

What does the worning say?

Rank: #13553

Warning says that I am comparing two diferent types of data, that I should use a data conversion function or recheck the expression
But I dont think thats the problem because no matter whcih account I login, I tdoes not show me nothing

Rank: #4257
Solution

Hello.

Well actually this IS a problem. As I have already seen your oml file from your previous question I can tell you this:
Usually you would filter an User aggregate for the currently logged in user - as everybody here said - with the built in function GetUserId(). But this is only working with the User entity from the System.
In your application, you built your own User and Role entities. Your User-id is of type Long Integer and not a foreign key to the System User. Which is why your aggregate will still return all the users that are in the database and the container visibility is still not behaving as desired.
If you use the built in User and Roles entities, the suggestions from everyone here will work. If you want to use your own entities, I don´ t know how to retrieve the current user in a secure way.
 

mvp_badge
MVP
Rank: #27

That explains a lot; didn't see that coming.... Please use standards @Gonçalo Nogueira, this is really not the way to go. Take the advice of @Nick Vandebriel  and follow the guided path online training.

Rank: #2442

Hi Goncalo try putting getuserid() instead of getuser()

Rank: #88

HI, 

i think you should try roles for it. 

create a role and based on a role you can hide and show menu.


Regards
RS

mvp_badge
MVP
Rank: #27

Agree, but this query isn't working because the wrong action is used.

Rank: #2442
Solution

Also, based on your question. You should use roles to check if a user is allowed to see something like @Rahul Sahu recommended. Don't put this in the display property of the container though.. This will just add a display: none in the inspector and can easily be tampered with to be visible. Use an if widget instead. Also I have the feeling that you don't understand the platform too well. Did you follow the training courses? I would recommend to check them out.

You can find the guided paths on the following link:

https://www.outsystems.com/training/paths/