# hasRole expression not working?

**URL:** <https://forum.inductiveautomation.com/t/hasrole-expression-not-working/85987>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [February 20, 2024, 5:57am UTC](https://forum.inductiveautomation.com/t/hasrole-expression-not-working/85987 "2024-02-20T05:57:21Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![jrd](https://avatars.discourse-cdn.com/v4/letter/j/b9bd4f/32.png) [@jrd](https://forum.inductiveautomation.com/u/jrd)\
**Post date:** [February 20, 2024, 5:57am UTC](https://forum.inductiveautomation.com/t/hasrole-expression-not-working/85987/1 "2024-02-20T05:57:21Z")

</div>

I have a SIMPLE expression bound to the background color of a label  
The expression is

```auto
if( hasRole("Administrator") , "#FF0000","#00FF00")

```

I would expect the background color to change depending on whether  
When I replace hasRole("Administrator") with "True" or "False" then all good  
This when running in client

---

<div class="post-metadata">

**Author:** ![andreas](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/andreas/32/18981_2.png) [@andreas](https://forum.inductiveautomation.com/u/andreas)\
**Post date:** [February 20, 2024, 6:38am UTC](https://forum.inductiveautomation.com/t/hasrole-expression-not-working/85987/2 "2024-02-20T06:38:29Z")

</div>

Hi,

Just copied your expression into my test environment, and it seems to be working fine. Tested in both v. 8.1.23 and 8.1.37.

Have you remembered to enable the "Fill Background" property on the label?

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/7/d79550dae416583b7d82664bb76d4a13bc9f75e0.png)

Are you sure that your Administrator role actually is spelled that way, and that your logged in user has the Administrator role? 🙂 Throw in an extra label and connect it to "[System]Client/User/RolesString" to be sure.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [February 20, 2024, 8:22am UTC](https://forum.inductiveautomation.com/t/hasrole-expression-not-working/85987/3 "2024-02-20T08:22:11Z")

</div>

Andreas, the question is tagged Perspective which doesn't have a Fill Background property.

---

<div class="post-metadata">

**Author:** ![jrd](https://avatars.discourse-cdn.com/v4/letter/j/b9bd4f/32.png) [@jrd](https://forum.inductiveautomation.com/u/jrd)\
**Post date:** [February 20, 2024, 8:37am UTC](https://forum.inductiveautomation.com/t/hasrole-expression-not-working/85987/4 "2024-02-20T08:37:27Z")

</div>

I can't find fill\_background property

---

<div class="post-metadata">

**Author:** ![jrd](https://avatars.discourse-cdn.com/v4/letter/j/b9bd4f/32.png) [@jrd](https://forum.inductiveautomation.com/u/jrd)\
**Post date:** [February 20, 2024, 8:38am UTC](https://forum.inductiveautomation.com/t/hasrole-expression-not-working/85987/5 "2024-02-20T08:38:00Z")

</div>

or Appearance....

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [February 20, 2024, 8:42am UTC](https://forum.inductiveautomation.com/t/hasrole-expression-not-working/85987/6 "2024-02-20T08:42:14Z")

</div>

It doesn't exist on perspective.

Make sure the role is actually what you think it is.

---

<div class="post-metadata">

**Author:** ![andreas](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/andreas/32/18981_2.png) [@andreas](https://forum.inductiveautomation.com/u/andreas)\
**Post date:** [February 20, 2024, 9:32am UTC](https://forum.inductiveautomation.com/t/hasrole-expression-not-working/85987/7 "2024-02-20T09:32:46Z")

</div>

Ah my bad. Clearly my morning coffee was not strong enough, sorry about that.

In the case of Perspective, I guess I would just benefit from the "`session.props.auth.user.roles`" property to see if the "Administrator" role is in that value.

In the backgroundColor binding I would retrieve the "`session.props.auth.user.roles`" property, and e.g. use a script transform something like:

```auto
if "Administrator" in value:
	return "#00FF00"
else:
	return "#FF0000"

```

Have not tested the hasRole expression in Perspective.

---

<div class="post-metadata">

**Author:** ![jrd](https://avatars.discourse-cdn.com/v4/letter/j/b9bd4f/32.png) [@jrd](https://forum.inductiveautomation.com/u/jrd)\
**Post date:** [February 20, 2024, 9:40am UTC](https://forum.inductiveautomation.com/t/hasrole-expression-not-working/85987/8 "2024-02-20T09:40:04Z")

</div>

I created custom property on a View, named "whatever", with an expression binding

```auto
hasRole("Administrator)

```

According to the doco, this should return a Boolean, True or False  
But I get an error:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/3/a329ca06df55dfb01b51e3c20058678f23785537.png)

But as can be seen there is an "Error Configuration" message.

What am I missing?  
Thanks

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [February 20, 2024, 9:41am UTC](https://forum.inductiveautomation.com/t/hasrole-expression-not-working/85987/9 "2024-02-20T09:41:03Z")

</div>

You're missing the closing `"`.

You should be able to debug the authorization problem in the binding editor.

In Designer what is listed under `SESSION PROPS.auth.user.roles`?

---

<div class="post-metadata">

**Author:** ![jrd](https://avatars.discourse-cdn.com/v4/letter/j/b9bd4f/32.png) [@jrd](https://forum.inductiveautomation.com/u/jrd)\
**Post date:** [February 20, 2024, 11:01am UTC](https://forum.inductiveautomation.com/t/hasrole-expression-not-working/85987/10 "2024-02-20T11:01:12Z")

</div>

my bad - typo  
I added the " (cut and paste from doco, but same issue  
Under  
`SESSION PROPS.auth.user.roles` it has 'Administrator'

---

<div class="post-metadata">

**Author:** ![jrd](https://avatars.discourse-cdn.com/v4/letter/j/b9bd4f/32.png) [@jrd](https://forum.inductiveautomation.com/u/jrd)\
**Post date:** [February 20, 2024, 11:03am UTC](https://forum.inductiveautomation.com/t/hasrole-expression-not-working/85987/11 "2024-02-20T11:03:23Z")

</div>

The message says there is a "Configuration\_Error"  
If I mis-typed role as ""Fred Nurk Role", sureli I should get Boolean False, not "Configuration\_Error"

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [February 20, 2024, 11:20am UTC](https://forum.inductiveautomation.com/t/hasrole-expression-not-working/85987/12 "2024-02-20T11:20:47Z")

</div>

1. Hover over the Error\_Configuration label to read the error. Post it here.
2. Double-check your spelling of "Administrator" everywhere.

---

<div class="post-metadata">

**Author:** ![michael.flagler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/michael.flagler/32/26272_2.png) [@michael.flagler](https://forum.inductiveautomation.com/u/michael.flagler)\
**Post date:** [February 20, 2024, 2:48pm UTC](https://forum.inductiveautomation.com/t/hasrole-expression-not-working/85987/13 "2024-02-20T14:48:25Z")

</div>

I believe in Perspective you have to define the username and usersource since everything runs in Gateway scope.

> **[hasRole | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/appendix/expression-functions/users/hasRole)**
>
> Returns true if the user has the given role. The username and usersource parameters are optional in the client scope, but required in the Gateway scope.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [February 20, 2024, 3:11pm UTC](https://forum.inductiveautomation.com/t/hasrole-expression-not-working/85987/14 "2024-02-20T15:11:02Z")

</div>

Yep, I just tried and that's what the configuration error says:

> function `hasRole(role, username, usersource)` does not work with 1 argument

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [February 20, 2024, 4:15pm UTC](https://forum.inductiveautomation.com/t/hasrole-expression-not-working/85987/15 "2024-02-20T16:15:46Z")

</div>

I'm pretty sure in Perspective, you should be using `isAuthorized`, rather than `hasRole`, because all Perspective authentication is relative to security levels, rather than 'classic' user source roles.

> **[isAuthorized | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/appendix/expression-functions/users/isAuthorized)**
>
> Returns a qualified value with a boolean value which is true if the user in the current Session is authorized, false otherwise.

---

<div class="post-metadata">

**Author:** ![jrd](https://avatars.discourse-cdn.com/v4/letter/j/b9bd4f/32.png) [@jrd](https://forum.inductiveautomation.com/u/jrd)\
**Post date:** [February 20, 2024, 6:03pm UTC](https://forum.inductiveautomation.com/t/hasrole-expression-not-working/85987/16 "2024-02-20T18:03:01Z")

</div>

Manual says other arguments are [optional]

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [February 20, 2024, 6:06pm UTC](https://forum.inductiveautomation.com/t/hasrole-expression-not-working/85987/17 "2024-02-20T18:06:26Z")

</div>

Actually it doesn't.

> Returns true if the user has the given role. **The username and usersource parameters are optional in the client scope, but _required_ in the Gateway scope.**

They are required in Gateway scope, perspective runs in gateway scope.
