Monday 12 October 2015

Checking security group membership

These few lines of code will tell the current user whether he/she belongs to a specified security group. I use it extensively with my AutoKeys macro to enable keystrokes to do different things for Admins users and regular users. Note that if you're planning to ask the same question many times in your application, it might be an idea to define a Boolean global variable and set it once with this function, then subsequently refer to the variable, as the function can be a little slow, especially if you have lots of security groups. Anyway, it takes one parameter - the security group name - and returns True if the current user is a member of that group, False otherwise.

Function fctnUserGroup(GroupName As String) As Integer
    Dim w As Workspace, u As User, i As Integer
    fctnUserGroup = False
    Set w = DBEngine.Workspaces(0)
    Set u = w.Users(CurrentUser())
    For i = 0 To u.Groups.Count - 1
        If u.Groups(i).Name = GroupName Then
            fctnUserGroup = True
            Exit Function
        End If
    Next i
End Function

No comments:

Post a Comment