hasRole() not working in component script

Hi,

I am trying to use the following script snippet in a visionWindowOpened script:

if hasRole(“Supervisor”):
system.gui.messageBox(“True”,“Alert”)
else:
system.gui.messageBox(“False”,“Alert”)

It does not seem to do anything. If I replace the ‘hasRole(“Supervisor”)’ with ‘True’ or ‘False’, the script executes properly. It appears that hasRole(“Supervisor”) is not evaluating. Thoughts?

hasRole() is an expression function and you’re trying to use it in scripting - a very common mistake.

Try using system.user.getUser() to get the User, then getting the roles from the User object and seeing if “Supervisor” is one of them.

support.inductiveautomation.com … etuser.htm

Thanks. I actually found this to be easier:

if “Supervisor” in system.security.getRoles():
doSomething
else:
doSomethingElse

It seems to work, do you see any issues with this approach?

1 Like

[quote=“qversa”]Thanks. I actually found this to be easier:

if “Supervisor” in system.security.getRoles():
doSomething
else:
doSomethingElse

It seems to work, do you see any issues with this approach?[/quote]

That seems like a better way to do it :thumb_right: