Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've been trying to figure out the best way to query Active Directory with JavaScript. The code below is the best I can come up with.

I was hoping someone could/would review my JavaScript code to see if this is okay and/or if there is a better way to do any of this. All it does is query AD for some properties then create an array of objects representing each record.

The code works but this is my first attempt at querying AD using JS and thought some peer-review might be fruitful.

' create connection and command objects
var ADConnection = new ActiveXObject( "ADODB.connection" );
var ADCommand = new ActiveXObject( "ADODB.Command" );

' 600 second timeout
ADConnection.ConnectionTimeout = 600;
' not entirly sure as I just copied this from online
ADConnection.Open( "Data Source=Active Directory Provider;Provider=ADsDSOObject" );
ADCommand.ActiveConnection = ADConnection;
' return 10000 rows at a time
ADCommand.Properties( "Page Size" ) = 10000;
' i think this is defaulting to subtree
ADCommand.Properties( "Searchscope" ) = 2;
' 600 second time out (not sure why this is needed twice)
ADCommand.Properties( "Timeout" ) = 600;
' i don't want to cache results
ADCommand.Properties( "Cache Results" ) = false;
' not sure what all the options do but this one seems to work for what i need
ADCommand.Properties( "Chase Referrals" ) = 96;
ADCommand.CommandTimeout = 600;

' the LDAP query information
var ou = "DC=company,DC=com";
var filter = "....";
var fields = "name,mail";
var level = "sublevel";

' GC doesn't contain certain properties so for those have to use LDAP
var queryType = fields.match( /,(memberof|member),/ig ) ? "LDAP" : "GC";

' put the query together
ADCommand.CommandText = "<" + queryType + "://" + ou + ">;" + filter + ";" + fields + ";" + level;

' execute
var recordSet = ADCommand.Execute;

' get the fields that were pulled
fields = fields.split( "," );

' place to store the data into a JS friendly object
var data = [];

' iterate through the returned data
while(!recordSet.EOF)
{
    var rowResult = { "length" : fields.length };
    var i = fields.length;
    ' loop through all the fields we pulled
    while(i--)
    {
        ' get the field name
        var fieldName = fields[i];

        ' can't figure out how to access the data in directReports but if it exists then the person has direct reports
        if(fieldName == "directReports" && recordSet.Fields(fieldName).value != null)
            rowResult[fieldName] = true;
        else
            ' save the data
            rowResult[fieldName] = recordSet.Fields(fieldName).value;
    }

    ' save that row
    data.push(rowResult);

    ' move to next record
    recordSet.MoveNext;
}

' close the recordset
recordSet.Close();

When it's done `data is an object like so:

[
    {
        "length" : 2,
        "name": "dingo",
        "mail": "[email protected]"
    },
    {
        "length" : 2,
        "name": "bingo",
        "mail": "[email protected]"
    },
    {
        "length" : 2,
        "name": "pickle",
        "mail": "[email protected]"
    },
    ...
]
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.