I’m not a Member
Posted by Theo Heselmans on June 28th, 2007
I hate it when I still see @formula code that uses @IsMember.
You usually see this in combinations like (e.g. as a hide-when formula):
@IsMember("[Admin]";@UserRoles)
A much cleaner approach is:
@UserRoles="[Admin]"
It get's worse, if one has to check for multiple roles:
@IsMember("[Sales]";@UserRoles) | @IsMember("[Marketing]";@UserRoles)
Far more elegant (and easier to read):
@UserRoles*="[Sales]":"[Marketing]"
Note the *= (asterisk equal operator) instead of = (equal operator).
This is absolutely necessary, because you are comparing 2 lists.
In this case either roles is sufficient to pass the test.
Of course, replace @IsNotMember by negating the comparison:
!@UserRoles="[Admin]"
Always put the ! (exclamation mark) in front of the comparison.
!= might have a different effect when comparing lists.
Category: Domino/Notes | Technorati: Show-n-Tell Thursday, SnTT
Comments (5)
Nice post. I took your approach for a spin and published the results here: { Link }
:-) stw
Great timing Theo. I always have to go back to the help file to keep the @IsMember/@IsNotMember logic straight when doing @UserRoles checks. Only today I had to remember to reverse the paramaters between the two functions, e.g.
@IsMember("[Admin]"; @UserRoles) vs.
@IsNotMember(@UserRoles; "[Admin]")
And only then after forgetting to reverse them and trying to figure out why my roles were not being recognized. Doh!
Your @UserRoles="[Admin]" vs !@UserRoles="[Admin]" approach is SO much easier to write correctly without wondering if you got it right. I like it so much I just replaced all the relevant calls in SuperNTF.
Thanks!
@Kevin:
The switching of the parameters was also one of my issues !
Just another tip: @userroles is a pretty heavy function, as it has to do a request to the server every time you use it. I usually end up putting a 'Roles' computed for display field (multivalue, computed as @UserRoles) at the top om my form, and use this field for my 'hide-when's.
Maybe nice to point out that you can also use the powerful @UserNamesList-function.
This will also give you the groups of the user (next to the roles).
Note: you need a consistent ACL for this to work.
Even though the documentation is quirky and wouldn't lead you to believe this works, the following is a readable and clean approach for your hide-whens:
!@contains(@userroles;"[role1]":"[role2]")
You can also use this or the !IsMember approach in css by putting somethign like this:
<div style="display: <ComputedText>;">Hide me from anyone except admins</div>
ComputedText formula is:
@if(@contains(@userroles;"[role1]":"[role2]");"inline";"none")