# hasRole() not working in component script

**URL:** <https://forum.inductiveautomation.com/t/hasrole-not-working-in-component-script/9549>\
**Category:** Ignition\
**Created:** [January 15, 2015, 6:01pm UTC](https://forum.inductiveautomation.com/t/hasrole-not-working-in-component-script/9549 "2015-01-15T18:01:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![qversa](https://avatars.discourse-cdn.com/v4/letter/q/35a633/32.png) [@qversa](https://forum.inductiveautomation.com/u/qversa)\
**Post date:** [January 15, 2015, 6:01pm UTC](https://forum.inductiveautomation.com/t/hasrole-not-working-in-component-script/9549/1 "2015-01-15T18:01:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [January 15, 2015, 6:08pm UTC](https://forum.inductiveautomation.com/t/hasrole-not-working-in-component-script/9549/2 "2015-01-15T18:08:46Z")

</div>

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](https://support.inductiveautomation.com/usermanuals/ignition/system_user_getuser.htm)

---

<div class="post-metadata">

**Author:** ![qversa](https://avatars.discourse-cdn.com/v4/letter/q/35a633/32.png) [@qversa](https://forum.inductiveautomation.com/u/qversa)\
**Post date:** [January 16, 2015, 4:18am UTC](https://forum.inductiveautomation.com/t/hasrole-not-working-in-component-script/9549/3 "2015-01-16T04:18:34Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [January 20, 2015, 3:40pm UTC](https://forum.inductiveautomation.com/t/hasrole-not-working-in-component-script/9549/4 "2015-01-20T15:40:53Z")

</div>

[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:](https://us1.discourse-cdn.com/inductiveautomation/original/1X/2026e28c0c425034352c1b75807edf956adc75f4.gif ":thumb\_right:")
