hasRole expression not working?

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

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

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?

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

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

I can't find fill_background property

or Appearance....

It doesn't exist on perspective.

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

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:

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

Have not tested the hasRole expression in Perspective.

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

hasRole("Administrator)

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

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

What am I missing?
Thanks

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?

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

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"

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

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

2 Likes

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

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

1 Like

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.

3 Likes

Manual says other arguments are [optional]

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.

1 Like