Getting the list of users belonging to a role

Lotus provides the method QueryAccessRoles of the NotesDatabase class to get the roles of a user and there are no methods to get users belonging to a role, but you can get them by combining IsRoleEnabled of the NotesACLEntry class with the function getUsersByGroup that I wrote in a previous article.

Option Public
Option Declare

Sub Initialize

	Dim role As String
	Dim users As Variant
	
	role = "[role]"
	
	users = getUsersByRole(role)

	ForAll v In users
		Print CStr(v)
	End ForAll
	
End Sub

Function getUsersByGroup(groupName As String, usersTempList() As String) As Variant
	
	Static incrementIndex As Integer
	Static maxNestedGroup As Integer
	Static s As NotesSession
	Static db As NotesDatabase
	Static dbNames As NotesDatabase 
	Static vwGroup As NotesView
	Static vwPerson As NotesView
	Static counter As Integer
	Static nestedCounter As Integer
	Dim doc As NotesDocument
	Dim members As Variant
	Dim user As NotesName
	Dim userName As String
	
	If (s Is Nothing) Then
		Set s = New NotesSession
		Set db = s.Currentdatabase
		Set dbNames = s.Getdatabase(db.server, "names.nsf")
		Set vwGroup = dbNames.Getview("($VIMGroups)")
		vwGroup.Autoupdate = False
		Set vwPerson = dbNames.Getview("($VIMPeople)")
		vwPerson.Autoupdate = False
		counter = 0	
		nestedCounter = 0
		incrementIndex = 10
		maxNestedGroup = 100
	End If
	
	nestedCounter = nestedCounter + 1
	
	Set doc = vwPerson.Getdocumentbykey(groupName, True)

	If Not(doc Is Nothing) Then
		If (counter>UBound(usersTempList)) Then
			ReDim Preserve usersTempList(UBound(usersTempList)+incrementIndex)
		End If
		Set user = New NotesName(doc.FullName(0))
		userName = user.Abbreviated
		usersTempList(counter) = userName
		counter = counter + 1
	Else
		Set doc = vwGroup.Getdocumentbykey(groupName, True)
		If Not(doc Is Nothing And nestedCounter<maxNestedGroup) Then
			members = doc.Getitemvalue("Members")
			ForAll v In members
				Set user = New NotesName(CStr(v))
				userName = user.Abbreviated
				Call getUsersByGroup(userName, usersTempList)
			End ForAll
		End If
	End If
	
	If (nestedCounter=1 And UBound(usersTempList)>1) Then
		getUsersByGroup = FullTrim(ArrayUnique(usersTempList))
	End If
	nestedCounter= nestedCounter - 1

End Function

Function getUsersByRole(role As String) As Variant
	
	dim s As NotesSession
	dim db As NotesDatabase
	dim acl As NotesACL
	Dim aclEntry As NotesACLEntry
	ReDim usersTempList(10) As String	
	Dim users As Variant

	Set s = New NotesSession
	Set db = s.Currentdatabase
	Set acl = db.Acl

	Set aclEntry = acl.Getfirstentry()
	While Not(aclEntry Is Nothing)
		
		If (aclEntry.Isroleenabled(role)) Then
			users = getUsersByGroup(aclEntry.Name, usersTempList)
		End If
		
		Set aclEntry = acl.Getnextentry(aclEntry)
	Wend
	
	getUsersByRole = users
	
End Function

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.