Perspective issue after making the necessary changes in start up script

Hello,
I am currently trying to enable a button when administrator users role are logged in from perspective and disable a button when only regular users are logged in.

I have attached the snapshot of the code!

Can you please help me out with this issue?
2023-07-25

You haven't explained what your issue is.

I'll tell you one thing, though: system.user.getRoles returns a List of roles, so comparing it directly to "Admin" won't work.

Apologies,

I have created two roles with one being "Administrator" and the other being "User"
And there's a spelling error - it is Administrator and not Admin

The problem lies when the button is not enabled or disabled for the respective conditions when I try to login with Admin or User credentials

Where does this script live? Is it a custom method on the root container?

The script is written in the root container on startup

I think the issue is what Kevin had mentioned. You are trying to compare a list to a string. You could check if 'Administrator' is in the list of roles if "Admin" in role:. A simpler approach may be to use an expression binding directly on the enabled property of the button. indexOf('Administrator',{session.props.auth.user.roles})>-1 might work correctly in the expression binding.

The expression binding should be: indexOf({session.props.auth.user.roles}, 'Administrator')>-1. I had switched the order of the parameters.

3 Likes

In expressions, hasRole(role, username, usersource) will also work (the second and third argument are required for Perspective):

You'll need to get username and user source from the session props, here's a more specific example. I just included the other one because of the nice screenshot:

1 Like

To make it clear, you should never use the startup script to disable graphic components directly (not that you can). You should always use a binding on the component, or, in rare circumstances, a script from another graphical component in the same view.

Consider the situation where you need to add other conditions to the enable button as well. If you use a startup script, you're screwed (so too are you using another component script without thinking, hence why I said that would be rare)

4 Likes

Thank you caleb
It is working now