Saturday, November 9, 2013

How to secure your Access application using Active Directory

If your network is part of an Active Directory domain, then you can easily use domain groups as a means of securing your Microsoft Access application.

The IsMember function determines if a logged in user is a member of a specific Active Directory group or not.

Function IsMember(strDomain As String, strGroup As String, strMember As String) As Boolean
    Dim adGrp As Object
    Dim strPath As String
    
    strPath = "WinNT://" & strDomain & "/"
    Set adGrp = GetObject(strPath & strGroup & ",group")
    IsMember = adGrp.IsMember(strPath & strMember)
    
End Function

If IsMember("mydomain.com", "Accounting", Environ("Username")) Then
    ' Allow access to sensitive information
Else
    ' Deny access to sensitive information 
End If

You can use the IsMember function to show or hide forms, to show or hide buttons, to add filters to SQL code, etc. Here are some code samples of ways you might secure your application using Active Directory groups:

This code hides and disables a button when the form loads:

Private Sub Form_Load()
    If IsMember("mydomain.com", "Domain Admins", Environ("Username")) Then
        Me.cmdLoadData.Visible = True
        Me.cmdLoadData.Enabled = True
    Else
        Me.cmdLoadData.Visible = False
        Me.cmdLoadData.Enabled = False
    End If
End Sub

You can also use the IsMember function as you build a dynamic SQL Statement. The following code allows Executives to see anyone's salary information but only allows others to see their own salary information.

    If IsMember("mydomain.com", "Executives", Environ("Username")) Then
        strSQL = "SELECT * FROM SalaryInfo"
    Else
         strSQL = "SELECT * FROM SalaryInfo" _
                        & " WHERE Username = '" & Environ("Username") & "'"
    End If









No comments:

Post a Comment