Friday, February 20, 2009

Xpages, hide whens formulas, and user roles

I needed to hide a button and since you cant simply put in @isnotmember("[Admin]";@userroles)
i had to find a different way to find out if the user was a member of that role, here is what i came up with.

//get the database
db1 = session.getDatabase("server1","database.nsf")

//get the acl
acl1 = db1.getACL()

//get the current users access
personalentry = acl1.getEntry(session.getEffectiveUserName())

//check if role is enabled
personalentry.isRoleEnabled("Admin")

this will return a true or false and calculate the hide when

6 comments:

Anonymous said...

Hi Mark,

Your approach is probably shorter, but I wanted to point out another way to access roles that might be useful:

there is a DirectoryUser object that can provide the roles for a user. Here's an example:

var roles = context.getUser().getRoles().toArray();

Then you can loop through the array or use @IsMember to check if the role exists for the user.

-John

Anonymous said...

Another issue with .isRoleEnabled is that this only supports users listed explicitly in the ACL. In addition to John's approach, here's what I've been using:

database.queryAccessRoles(session.getEffectiveUserName()).contains('[RoleName]')

Since .queryAccessRoles returns a vector, there's no need to loop through an array; you can just chain .contains and pass it the role name you're validating.

Anonymous said...

Looks like the CSS cut off the end of the code example:

.contains('[RoleName]')

Mark Hughes said...

Changed theme for better css

Anonymous said...

@Tim, very cool. So, since getRoles() returns a java.util.List, I can just use the contains() also. Such as:
context.getUser().getRoles().contains('[RoleName]');

John

Anonymous said...

@John, I like yours better... it's shorter. :)

Post a Comment