Author Topic: Using Active Directory to validate a user name ...  (Read 1147 times)

0 Members and 1 Guest are viewing this topic.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Using Active Directory to validate a user name ...
« on: March 28, 2017, 11:58:36 AM »
As the thread title indicates I needed to use Active Directory to validate a user name.

I wrote a quick & dirty function that is working for me ...

Code: [Select]
(defun _UserExists ( domain user / conn cmd rs result )

    ;;  Translated from:
    ;;
    ;;      http://gallery.technet.microsoft.com/scriptcenter/13e06435-c105-48d4-9128-29303b1d36f5.
    ;;     
    ;;      Note: Substituted <LDAP://domain.local> for <LDAP://dc=fabrikam,dc=com>
    ;;
    ;;  Original sample VBScript Code from web ^ page:
    ;;
    ;;      strUserName = "kenmyer"
    ;;      dtStart = TimeValue(Now())
    ;;      Set objConnection = CreateObject("ADODB.Connection")
    ;;      objConnection.Open "Provider=ADsDSOObject;"
    ;;       
    ;;      Set objCommand = CreateObject("ADODB.Command")
    ;;      objCommand.ActiveConnection = objConnection
    ;;       
    ;;      objCommand.CommandText = _
    ;;          "<LDAP://dc=fabrikam,dc=com>;(&(objectCategory=User)" & _
    ;;               "(samAccountName=" & strUserName & "));samAccountName;subtree"
    ;;         
    ;;      Set objRecordSet = objCommand.Execute
    ;;       
    ;;      If objRecordset.RecordCount = 0 Then
    ;;          WScript.Echo "sAMAccountName: " & strUserName & " does not exist."
    ;;      Else
    ;;          WScript.Echo strUserName & " exists."
    ;;      End If
    ;;       
    ;;      objConnection.Close
    ;;
    ;;  Examples:
    ;;
    ;;      (_UserExists "mydomain" "eclapton") >> T
    ;;      (_UserExists "mydomain" "bogus")    >> nil

    (setq
        conn (vlax-create-object "ADODB.Connection")
        cmd  (vlax-create-object "ADODB.Command")
    )

    (vlax-invoke conn 'Open "Provider=ADsDSOObject;")

    (vlax-put cmd 'ActiveConnection conn)

    (vlax-put cmd 'CommandText
        (strcat
            "<LDAP://" domain ".local>;(&(objectCategory=User)"
            "(samAccountName=" user "));samAccountName;subtree"
        )
    )

    (setq result
        (and
            (setq rs (vl-catch-all-apply 'vlax-invoke (list cmd 'Execute)))
            (eq 'vla-object (type rs))
            (< 0 (vlax-get rs 'RecordCount))
        )
    )

    (vl-catch-all-apply 'vlax-invoke (list conn 'Close))
    (vl-catch-all-apply 'vlax-release-object (list rs))
    (vl-catch-all-apply 'vlax-release-object (list cmd))
    (vl-catch-all-apply 'vlax-release-object (list conn))

    result
     
)

For what it's worth .... cheers.

Edit: Tightened up the code slightly because I'm helplessly retentive.
« Last Edit: March 28, 2017, 01:14:46 PM by MP »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Using Active Directory to validate a user name ...
« Reply #1 on: March 28, 2017, 12:59:38 PM »
Very cool - thanks for sharing MP.  :-)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Using Active Directory to validate a user name ...
« Reply #2 on: March 28, 2017, 01:15:44 PM »
Merci. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst