# Perspective: Error using hasRole(...) with only 1 argument

**URL:** <https://forum.inductiveautomation.com/t/perspective-error-using-hasrole-with-only-1-argument/26628>\
**Category:** Ignition Early Access\
**Created:** [July 17, 2019, 3:03pm UTC](https://forum.inductiveautomation.com/t/perspective-error-using-hasrole-with-only-1-argument/26628 "2019-07-17T15:03:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [July 17, 2019, 3:03pm UTC](https://forum.inductiveautomation.com/t/perspective-error-using-hasrole-with-only-1-argument/26628/1 "2019-07-17T15:03:26Z")

</div>

v8.0.2

I’m sure this is a bug, as the function description says that the 2nd 2 arguments are optional, and this works in Vision:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/2X/e/eb6c1daf6eede3e8846d8628d979a2750087408e.png)

---

<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:** [July 17, 2019, 3:07pm UTC](https://forum.inductiveautomation.com/t/perspective-error-using-hasrole-with-only-1-argument/26628/2 "2019-07-17T15:07:07Z")

</div>

Since this is Perspective, it’s running in the gateway scope, which does require all the args.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [July 17, 2019, 3:11pm UTC](https://forum.inductiveautomation.com/t/perspective-error-using-hasrole-with-only-1-argument/26628/3 "2019-07-17T15:11:58Z")

</div>

Hmm, I’ve added these from the system tags but now I’m getting another error:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/2X/4/4c31ec6aa3a3ba3ce47facaad67e615af4443aa0.png)

---

<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:** [July 17, 2019, 3:15pm UTC](https://forum.inductiveautomation.com/t/perspective-error-using-hasrole-with-only-1-argument/26628/4 "2019-07-17T15:15:21Z")

</div>

Hmm. Best I can tell that error would happen if the user source by that name was not found.

---

<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:** [July 17, 2019, 3:39pm UTC](https://forum.inductiveautomation.com/t/perspective-error-using-hasrole-with-only-1-argument/26628/5 "2019-07-17T15:39:33Z")

</div>

`[System]Client` tags _also_ don’t exist in Perspective, since you’re on the gateway scope. You would use the appropriate session properties, instead - but `hasRole` probably isn’t the function you should be using at all in Perspective.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [July 17, 2019, 3:46pm UTC](https://forum.inductiveautomation.com/t/perspective-error-using-hasrole-with-only-1-argument/26628/6 "2019-07-17T15:46:16Z")

</div>

How would you disable a button in Perspective if a user doesn’t have a particular role? Is this where you’d use security levels and the identity providers? Which on that note, I can’t seem to add any security levels into my identity providers’ security levels. I can add them into the general Security Levels section, but not when I click on the Identity provider and click security levels from there. I just get:

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

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/2X/f/f596d16e629433223efad65d4eab6621f70a5867.png)

---

<div class="post-metadata">

**Author:** ![osolorzano](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/osolorzano/32/6432_2.png) [@osolorzano](https://forum.inductiveautomation.com/u/osolorzano)\
**Post date:** [July 17, 2019, 4:02pm UTC](https://forum.inductiveautomation.com/t/perspective-error-using-hasrole-with-only-1-argument/26628/7 "2019-07-17T16:02:50Z")

</div>

What you could do is use a property binding to `session.props.auth.user.roles` and then a simple script transform to check whether the role your user has is in the list of roles:

```auto
if value is not None:
	return 'Operator' in value
return False

```

A similar binding/script could probably also make use of the `session.props.auth.securityLevels` session property.

---

<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:** [July 17, 2019, 4:05pm UTC](https://forum.inductiveautomation.com/t/perspective-error-using-hasrole-with-only-1-argument/26628/8 "2019-07-17T16:05:35Z")

</div>

There’s also the new `isAuthorized` expression/scripting function(s):  
[https://docs.inductiveautomation.com/display/DOC80/isAuthorized](https://docs.inductiveautomation.com/display/DOC80/isAuthorized)

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [July 17, 2019, 4:07pm UTC](https://forum.inductiveautomation.com/t/perspective-error-using-hasrole-with-only-1-argument/26628/9 "2019-07-17T16:07:29Z")

</div>

I don’t think that can check for assigned roles though; correct me if I’m wrong, but it just checks that a user has been authenticated to an IP

---

<div class="post-metadata">

**Author:** ![chi](https://avatars.discourse-cdn.com/v4/letter/c/0ea827/32.png) [@chi](https://forum.inductiveautomation.com/u/chi)\
**Post date:** [July 17, 2019, 4:24pm UTC](https://forum.inductiveautomation.com/t/perspective-error-using-hasrole-with-only-1-argument/26628/10 "2019-07-17T16:24:54Z")

</div>

~~I’m not 100% sure, but i think the security levels suddenly appeared for configuration after i used security levels for a view.  
\*\*I’m not sure that ‘use before configure’ is really intended by IA, but it worked for me.~~  
Ok, i have to correct myself. The workflow for using Security Levels has two steps:

First, you have to create a custom level under ‘Security Levels’:  
 ![levels](https://us1.discourse-cdn.com/inductiveautomation/original/2X/1/1978f05f65bb113b918157eecdc4936d6a3edbe5.png)  
The rules for this levels can then be configured in the Identity Provider:

 ![rules](https://us1.discourse-cdn.com/inductiveautomation/original/2X/d/d467a86dc4c037109c61d9b887d95341eead1d1c.png)

According to the docs, it should also be possible to add role names direct under Authenticated/Roles without the need for special rules.

---

<div class="post-metadata">

**Author:** ![derrick.marlow](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/derrick.marlow/32/14675_2.png) [@derrick.marlow](https://forum.inductiveautomation.com/u/derrick.marlow)\
**Post date:** [May 6, 2020, 6:46pm UTC](https://forum.inductiveautomation.com/t/perspective-error-using-hasrole-with-only-1-argument/26628/11 "2020-05-06T18:46:29Z")

</div>

Something like this worked for me:

if({session.props.auth.user.userName} = null, false, if(hasRole(‘Operator’,{session.props.auth.user.userName}, ‘default’), true, false))

However, I still can’t figure out how to reference the user source in the session properties. If the source changed from default it would break…

---

<div class="post-metadata">

**Author:** ![Ric\_Walker](https://avatars.discourse-cdn.com/v4/letter/r/ac8455/32.png) [@Ric\_Walker](https://forum.inductiveautomation.com/u/Ric_Walker)\
**Post date:** [September 18, 2020, 10:49pm UTC](https://forum.inductiveautomation.com/t/perspective-error-using-hasrole-with-only-1-argument/26628/12 "2020-09-18T22:49:57Z")

</div>

Well, the format goes: **hasRole(role[, username][, usersource])**.

So, what **should** be valid is:  
**hasRole(“Operator”, {session.props.auth.user.userName}, {session.props.auth.idp})**

When put into a Label expression as:  
“Operator” + " " + {session.props.auth.user.userName} + " " + {session.props.auth.idp}  
it returns:  
“Operator admin default” (or what ever your current is and whatever your current is).

However, the example is actually written:  
// This is an example using a username and userSource:  
**hasRole(“Administrator”, “bob”, “default”)**

Notice how the ‘user’ and ‘usersource’ both have the double quotes?

In SESSION CUSTOM create 2 Custom Properties with the expression binding  
• SessionUser : ‘"’ + {session.props.auth.user.userName} + ‘"’ : gives–\> “admin”  
• SessionIdp : ‘"’ + {session.props.auth.idp} + ‘"’ : gives–\> “default”  
Notice that there is ‘single quote’ ‘double quote’ ‘single quote’ which puts the double quotes into the value.  
Now put in:  
**hasRole(“Operator”, {session.custom.SessionUser}, {session.custom.SessionIdp})**

Sigh. _This should have worked._  
It should have evaluated, but instead I got the " ! Error\_ExpressionEval " again as well. ☹

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [September 20, 2020, 6:09am UTC](https://forum.inductiveautomation.com/t/perspective-error-using-hasrole-with-only-1-argument/26628/13 "2020-09-20T06:09:31Z")

</div>

Why are you forcing the double quotes?  
The example only has double quotes to display that the value is a String.

 ![Screen Shot 2020-09-19 at 11.08.12PM](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/8/18395e0c1980fb08e9f053449bcb72dd09a8b1d1.png)

---

<div class="post-metadata">

**Author:** ![Ric\_Walker](https://avatars.discourse-cdn.com/v4/letter/r/ac8455/32.png) [@Ric\_Walker](https://forum.inductiveautomation.com/u/Ric_Walker)\
**Post date:** [September 21, 2020, 2:37pm UTC](https://forum.inductiveautomation.com/t/perspective-error-using-hasrole-with-only-1-argument/26628/14 "2020-09-21T14:37:15Z")

</div>

Because the code Snippet in the Ignition 8 User Manual under “hasRole” shows the user and idp with quotes.  
Either way, we don’t get “true” or “false”, as in your _VISION_ example. In Perspective, we only get “Error\_ExpressionEval” in the results.

As far as I can tell, in Perspective the ‘hasRole’ function does not work, and it both should and needs to.  
By the way, I’m on version 8.0.13.

How did you add the picture? I would put that in if I knew how.  
However, this gives me an idea to try…

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [September 21, 2020, 3:28pm UTC](https://forum.inductiveautomation.com/t/perspective-error-using-hasrole-with-only-1-argument/26628/15 "2020-09-21T15:28:04Z")

</div>

I try to avoid double quotes if I can help it, but your original ‘shoulda worked’ works for me:

`hasRole('Operator', {session.props.auth.user.userName}, {session.props.auth.idp})`

---

<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:** [September 21, 2020, 3:35pm UTC](https://forum.inductiveautomation.com/t/perspective-error-using-hasrole-with-only-1-argument/26628/16 "2020-09-21T15:35:32Z")

</div>

> [@Ric\_Walker](#):
>
> `**hasRole(“Operator”, {session.props.auth.user.userName}, {session.props.auth.idp})**`

This will work, but what is _most likely_ causing your Expression Eval Error is that your Perspective project isn't requiring login, so `session.props.auth` is either an empty object or doesn't exist when you first start the session. This _is_ the correct expression syntax (you should still use `isAuthorized`), but you probably want to wrap it in the `try` function evaluating to false to catch the case when _no_ user is present. OR, make your Perspective project require login.

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [September 21, 2020, 3:53pm UTC](https://forum.inductiveautomation.com/t/perspective-error-using-hasrole-with-only-1-argument/26628/17 "2020-09-21T15:53:20Z")

</div>

> [@Ric\_Walker](#):
>
> How did you add the picture?

In the post controls, there is an upload option.  
 ![Screen Shot 2020-09-21 at 8.47.42AM](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/6/368e14130e2b53c4842d2c12eaf8eb0a0df05bc2.png)

If you hover over the warning in the expression dialog, what does the error say? Given your example, I'm betting it is something about an unexpected character.

---

<div class="post-metadata">

**Author:** ![Ric\_Walker](https://avatars.discourse-cdn.com/v4/letter/r/ac8455/32.png) [@Ric\_Walker](https://forum.inductiveautomation.com/u/Ric_Walker)\
**Post date:** [September 21, 2020, 4:13pm UTC](https://forum.inductiveautomation.com/t/perspective-error-using-hasrole-with-only-1-argument/26628/18 "2020-09-21T16:13:38Z")

</div>

My Prospective project already does require login. Has from the beginning, long before I tried to use ‘hasRole’.

However, everyone, I figured out how to get it to work. The third parameter, for Perspective, is not the ‘idp’ that is for the perspective project, but you must use the ‘User, Roles’ that your ‘Identity Provider’ is pointing to. For some unknown reason, that info is not being passed thru.

I learned more about the error “Error\_ExpressionEval”: The error description, which finally popped up when pointing to the “!” is  
• "Error\_ExpressionEval(“Error retrieving users from gateway.”)

The problem with all these examples is that both the ‘Identity Provider’ and the ‘Users, Roles’ name are both “default”. So if you have names defined per project (multiple projects on server for SYSTEM INTEGRTORS and PROGRAMMERS), and the two names are not identical (say for example ‘Identity Provider’ name of ‘CompanyIdentity’ and ‘User, Roles’ name of ‘CompanyUsers’), then that is where the confusion in the ‘hasRole’ comes from. The help menu item needs to be updated.  
Also, a way to have the Perspective session ‘Users, Roles’ name needs to be added along with the ‘idp’ parameter. I don’t know yet how to make a custom that can drill down into the Identity Provider settings to get that info.

To Clarify what I’ve said, as it sounds a little confusing even to me. Example:

**Identity Provider** name of “CompanyIdentity”. The setting ‘User Roles \*’ points to “CompanyUsers”  
**User, Roles** name of “CompanyUsers”.

In this situation, the proper Perspective format is:  
**hasRole(“Operator”, {session.props.auth.user.userName}, “CompanyUsers”)**

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [September 21, 2020, 9:09pm UTC](https://forum.inductiveautomation.com/t/perspective-error-using-hasrole-with-only-1-argument/26628/19 "2020-09-21T21:09:29Z")

</div>

> [@Ric\_Walker](#):
>
> How did you add the picture?

Screen capture to clipboard (eg printscreen, snipping tool, greenshot, etc) then simply paste it into the forum post. Ahh the benefits of using a modern forum backbone 🙂

---

<div class="post-metadata">

**Author:** ![Ric\_Walker](https://avatars.discourse-cdn.com/v4/letter/r/ac8455/32.png) [@Ric\_Walker](https://forum.inductiveautomation.com/u/Ric_Walker)\
**Post date:** [September 21, 2020, 10:31pm UTC](https://forum.inductiveautomation.com/t/perspective-error-using-hasrole-with-only-1-argument/26628/20 "2020-09-21T22:31:10Z")

</div>

Paste it to here? Really? That easy? Wish the other forum I post on was that easy. There you have to save the picture to a hosting service (like OneDrive) and then go thru hoops to get an embed path, etc.

Thanks. 🙂

[Next page](https://forum.inductiveautomation.com/t/perspective-error-using-hasrole-with-only-1-argument/26628.md?page=2)
