UtterAccess HomeUtterAccess Wiki

Welcome Guest ( Log In | Register )

Custom Search
Edit Discussion
> LocalUser    
(Difference between revisions)
Revision as of 05:57, 25 February 2012
DatAdrenaline (Talk | contribs)

← Previous diff
Revision as of 06:00, 25 February 2012
DatAdrenaline (Talk | contribs)

Next diff →
Line 1: Line 1:
 +==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. 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.
Line 99: Line 100:
End Function[code] End Function[code]
 +
 +==Example Usage==
 +From the debug window:
 +
 +[code]? LocalUser.UserName
 +someUserName
 +
 +? LocalUser.ComputerName
 +someComputerName[/code]

Revision as of 06:00, 25 February 2012

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

UserName: 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[code]

==Example Usage==
From the debug window:

[code]? LocalUser.UserName
someUserName

? LocalUser.ComputerName
someComputerName
Edit Discussion
Custom Search


Thank you for your support!
Disclaimers