I am trying to dynamically show or hide certain links in my mobile menu (using If blocks), and have a different home page for users of my app depending on their role. I currently have the following logic in the OnLoadComplete action of my Splash page (the CheckRoles action calls CheckTenantRole and CheckFormerTenantRole, where Tenant and Former Tenant are roles created in the Logic > Roles folder):
This seems to work sometimes, but I'm not sure whether the splash screen logic is always executed. As in, it seems that sometimes if the application is already loaded into memory(?) it just goes straight to the home screen.
I thought about adding this logic into the Login action, but users can use the app as the Anonymous role, so they would never hit this Login action.
Finally, I created a generic HomeScreen with a loading symbol, and in the OnRender event of that HomeScreen added logic similar to the above to redirect the user to the correct screen. HOWEVER, this for some reason kept overloading the personal environment for using too many resources and caused a 503 error (this was resolved by contacting support directly to restart the environment).
The Question
After all that pre-amble, my question is what is the best way to implement a role based menu and role based home screen in a mobile app?
Hi Russell,
I would say the best way is to:
Cheers,
José
Thanks José,
I have created a new HomeScreen called HomeScreenRedirector. In the OnInitialize event I created a javaScript node where i checked the following roles:
// check if the current user has the 'Tenant' role $parameters.IsTenant = $public.Security.checkIfCurrentUserHasRole($roles.Tenant); // check if the current user has the 'FormerTenant' role $parameters.IsFormerTenant = $public.Security.checkIfCurrentUserHasRole($roles.FormerTenant);
Using an If Statement after this I was able to redirect to a new screen depending on the value of the output parameter - wonderful!
Is there also a way to do this for the Registered role though? When I added the code below I received the error that follows:
// check if the current user has the 'Registered' role $parameters.IsRegistered = $public.Security.checkIfCurrentUserHasRole($roles.Registered);
Error: Invalid JavaScript : Unknown 'Registered' role used in 'JavaScript1'.
Hi Russel,
You can check if the GetUserId() is different from 0 (zero). If it is different then it has the Registered role.
Oh of course, José, I was so hung up on the javaScript I forgot about doing it a normal way. That's perfect, and seems to be working now, thank you so much for your help and speedy replies.
Regarding the role check issue,
TL;DR;
The $roles keyword, at this point, only allows to check for custom roles.
There is a lack of information regarding this restriction on the OutSystems documentation.
Both on the JavaScript API Role Check documentation and the JavaScript API Security Documentation only mentioned the $roles keyword can be used to check if the current user has a given role and does not inform that it's only valid for custom roles, so Anonymous and Registered Roles will return error if trying to check them with the JavaScript API.
Also on the Becoming a Front-End Developer in OutSystems Guided Path, on the OutSystems JavaScript $-keywords section, the Quiz asks this very same question, being the only reference that I was able to find where it informs that the $Roles keyword can only be used to check custom roles.
From what I've observed essentially on new developers, mainly people that don't come from a computer science or programming background, don't know / understand that doesn't make much sense to check for Anonymous and that a Registered Role can be checked using other options. So although there are workarounds, they are not mentioned / teached.
I already left feedback on the mentioned pages, hopefully the information will be added in a near future.
Also I can't prevent to mention that I hope this is not an exam question as the official documentation is misleading regarding this subject.