Get All Users From The ACL Of A Notes Database

General Programming Tips-And-Tricks

HCL Notes (formerly IBM Notes) provides access to ACL through a pretty useful dialog window under Notes Client. The authorized user can perform several actions like viewing existing database users, their access levels and assigned roles. They can add and remove users and grant rights and roles to them as per the requirements.

Viewing and taking actions through this window is quite handy and serves a lot of purposes but there are times when the management requires the report for all users with their access levels, rights and roles for one or more databases in a text file or preferably in a Microsoft Excel sheet.

Following simple but useful code provides the same feature in, somewhat comprehensive manner.

The function below is a generic function which, when provided with a Notes Database, extracts all users, their access rights and roles from that database and returns the information in the form of an array (namesList). Most of the code is self-explanatory for the regular lotus script users. Note that we are interested in the ACL entries that are of type ‘Person’, not a server or a group.

Here’s the function. 

Function getUsersByDB(db As NotesDatabase) As Variant     

    ''Gets all users from the ACL of the specified database with their access levels and roles. 

    Dim session As New NotesSession     
    Dim acl As NotesACL 
    Dim aclEntryDb As NotesACLEntry     
    Dim userType As Integer 
    Dim namesList() As Variant 
    Dim currUserNdx  As Integer 
    Dim aclLevel As Integer 
    Dim aclLevelName As String     
    Dim personNotesName As NotesName 
    Dim roleNames As String 
    Dim FirstTime As Boolean 
   
    Set acl = db.acl 
    Set aclEntryDb = acl.GetFirstEntry     
    roleNames = "" 
    currUserNdx = -1     
  
    While Not aclEntryDb Is Nothing     
        roleName = "" 
        Set personNotesName = session.createName( aclEntryDb.Name) 
        userType = aclEntryDb.Usertype 

        If userType=1 Then        ''Person only             
            aclLevel = aclEntryDb.Level                     

            Select Case Cstr(aclLevel) 
            Case "0" : aclLevelName = "No Accees" 
            Case "1" : aclLevelName = "Depositor" 
            Case "2" : aclLevelName = "Reader" 
            Case "3" : aclLevelName = "Author" 
            Case "4" : aclLevelName = "Editor" 
            Case "5" : aclLevelName = "Designer" 
            Case "6" : aclLevelName = "Manager"             
            End Select 
             
            FirstTime = True 

            Forall role In aclentryDb.Roles         
                If FirstTime Then 
                    roleNames = role                 
                    FirstTime = False 
                Else 
                    roleNames = roleNames & ";" & role     
                End If 
            End Forall              

            If Trim(roleNames) <> "" Then 
                roleNames = "," & roleNames 
            End If              
           
            currUserNdx = currUserNdx + 1                         
            Redim Preserve namesList(currUserNdx) As Variant 
            namesList(currUserNdx) = personNotesName.Abbreviated  & "," & aclLevelName  & roleNames 

        End If 
         
        Set aclEntryDb = acl.GetNextEntry(aclEntryDb) 
    Wend     
     
    getUsersByDB = namesList     
End Function 

And here’s the code which invokes the above function, receives the information (UsersList) and writes that information in the format of a comma-separated values (CSV) in a text file. This file can then be opened as MS Excel document to prepare in a more presentable form. Note that you have to tell Excel a couple of things to “understand” the data layout, like the data is tab-delimited.

Dim workspace As New NotesUIWorkspace 
Dim session As New NotesSession     
Dim uidoc As NotesUIDocument        ''source document 
Dim ACLRole As String 
Dim UsersList As Variant 
Dim arr As Variant 
Dim db As NotesDatabase 

Set db = session.CurrentDatabase         
Set uidoc = workspace.CurrentDocument         

UsersList =  getUsersByDB(db) 

'Print Ubound(UsersList) 

FileNum% = Freefile() 
FileName = "D:\ACL.txt" 
Open FileName For Output As FileNum% 

For I = 0 To Ubound(UsersList) 
    Print #FileNum%, UsersList(I)    & Chr(10) 
Next 

Close FileNum%     

Leave a Reply

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