UtterAccess HomeUtterAccess Wiki

Welcome Guest ( Log In | Register )

Custom Search
Edit Discussion
> LocalUser    

Contents

Synopsis

This is a singleton class that can be used to return information about the currently logged in user. Class has been successful in 32bit and 64bit versions of Access.

Properties

Name: Returns the currently logged on user (the LocalUser).
ComputerName: Returns the computer name the LocalUser is using.
Domain: The domain name the LocalUser resides in.

Functions

Validate(strPassword): Validates a users password. Could be used after a period of inactivity or before a sensitive operation of your app.
IsMemberOf(strGroupname): Returns a boolean indicating if LocalUser is a member of the passed in Active Directory group.
Groups(): Returns a list of all the groups the LocalUser is a member of.

How to implement

Copy the code below to create a text file with the name LocalUser.cls. Then IMPORT the file into your VBA project.

Code

CODE
VERSION 1.0 CLASS
BEGIN
 MultiUse = -1  'True
END
Attribute VB_Name = "LocalUser"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False

Option Compare Database
Option Explicit

Private strName As String
Private strDomain As String
Private strComputerName As String

Private Sub Class_Initialize()

   If Not Me Is LocalUser Then
       Err.Raise vbObjectError, "LocalUser", "LocalUser cannot have multiple instances."
   End If
   
   'Set the properties
   With CreateObject("wscript.network")
       strName = .UserName
       strDomain = .UserDomain
       strComputerName = .ComputerName
   End With
   
End Sub

Public Property Get Name()
   Name = strName
End Property

Public Property Get Domain()
   Domain = strDomain
End Property

Public Property Get ComputerName()
   ComputerName = strComputerName
End Property

Public Function Validate(strPassword As String) As Boolean
'Validate WindowsUser active directory password
   
   Dim oADobject As Object 'IADsOpenDSObject
   Set oADobject = GetObject("WinNT:")
   
   On Error Resume Next
   oADobject.OpenDSObject "WinNT://" & strDomain, strName, strPassword, &O1 '&O1 = ADS_SECURE_AUTHENTICATION
   
   Validate = (Err = 0)
   
End Function

Public Function IsMemberOf(strGroupname As String) As Boolean
'Returns a boolean indicating if the current windows user is a member of the passed group name.
   
   IsMemberOf = Groups Like "*|" & strGroupname & "|*"
   
End Function

Public Function Groups() As String
'Returns a delimited string of all the ad groups the current windows user is a member of

   Dim sGroupName As String _
       , sUsersGroupList As String
   Dim oTarget As Object
   Dim vGroup As Variant
   
   ' Bind to Computer object
   Set oTarget = GetObject("WinNT://" & strDomain & "/" & strName & ",user")
   
   For Each vGroup In oTarget.Groups
       sUsersGroupList = sUsersGroupList & "|" & vGroup.Name
   Next
   
   sUsersGroupList = UCase(sUsersGroupList)
   
   Groups = sUsersGroupList & "|"
   
End Function

Example Usage

From the immediate window:

CODE
? LocalUser.Name
someUserName

? LocalUser.ComputerName
someComputerName


Creative Commons License
LocalUser by UtterAccess Wiki is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Editing and revision of the content is freely encouraged; for details, see Expected Usage.

Edit Discussion
Custom Search
Thank you for your support!
This page has been accessed 7,971 times.  This page was last modified 16:31, 25 February 2012 by Brent Spaulding.   Disclaimers