Check User's Role in Agent Before Running Agent

Mindwatering Incorporated

Author: Tripp Black

Created: 11/02/2005 at 04:53 PM

 

Category:
Notes Developer Tips
Agents, LotusScript

Issue:
Before you let a user run an agent, you want to verify the user has the rights to run the agent by verifying a role.

Solutions:
There are several ways to check/verify this. Each does a piece of the pie.

1. Set agent to run as AgentList or Schedule (Never). Then just hide button that runs the agent with the normal @IsMember formula code when applicable.

2. Check for the role before running or in the agent.

The easiest way is to check is to hide the button or link with formula:
Dim aclcheckflag As Variant
aclcheckflag =Evaluate({@IsMember("[admin]"; @UserRoles)})
If (aclcheckflag (0)=1) Then
...
End If


Below is an LotusScript function w/o using an Evaluate:

%REM
Function ACLIsMember
Description: Returns true if aclStr is member of db ACL
%END REM
Function ACLIsMember(db As NotesDatabase, userNm As String, roleNm As String) As Integer
Dim roleFound As Integer
Dim roles As Variant

On Error GoTo FErrorHandler

roleFound = 0

If (db Is Nothing) Then
' return false
ACLIsMember = roleFound
Exit Function
End If
If Not (db.Isopen) Then
' verify is open/allowed access
ACLIsMember = roleFound
Exit Function
End If

roles = db.QueryAccessRoles( userNm )

ForAll role In roles
If (role = roleNm) Then
roleFound = 1
ACLIsMember = roleFound
Exit Function
End If
End ForAll

FExit:
ACLIsMember = roleFound
Exit Function
FErrorHandler:
ACLIsMember = roleFound
Resume FExit
End Function



Better than using an equals, switch to Like in the LotusScript function:
Someone in Notes.net posted this option of using like for the comparison. Still pass db.QueryAccessRoles(s.UserName) as vArray.

Function isMember(vStr As String, vArray As Variant) As Boolean
For x = 0 To Ubound(vArray)
If vArray(x) Like vStr Then
isMember = True
Exit Function
End If
Next
isMember = False
End Function



Another Version:
(Obviously, the Like substitution can be made here, too.)

Function LSIsmMemberAppRole(nm As String, rolenm As String) As Integer
Dim s As New NotesSession
Dim db As NotesDatabase ' this app
Dim dbACL As NotesACL ' this app acl
Dim dbACLE As NotesACLEntry ' entry in acl

On Error Goto FErrorHandler

LSIsmMemberAppRole = 0
' do checks
If (nm = "" Or rolenm = "") Then
Exit Function
End If
' get acl entry, and check for roles
Set db = s.CurrentDatabase
Set dbAcl = db.ACL
Set dbACLE = dbAcl.GetEntry( nm )
Forall r In dbACLE.Roles
If ( Cstr(r) Like rolenm ) Then
' found, return true = 1
LSIsmMemberAppRole = 0
Exit Function
End If
End Forall

FExit:
' not in roles, if made it this far
Exit Function
FErrorHandler:
Print "(LSIsMemberAppRole) Unexpected Error: " & Cstr(Err) & ", " & Error$ & ", line: " & Cstr(Erl) & "."
Resume FExit
End Function


previous page