Danial
Danial

Reputation: 703

Search in Ldap.js

I am trying to use the search method of Ldap.js in my node.js code. But it doesn't work. Here is my code:

searchFunc : function (){
            console.log('inside search');
            client.bind('cn=Manager,dc=foo,dc=com', kredito231, function(err) {
                if (err) {
                    console.log(err);
                    client.unbind();
                    return;
                }

                var opts = {
                    filter: (('Email=*@foo.com'))
                } ;
                 //This search works correct:
                //client.search( 'cn=x,ou=users' + ',' + 'dc=foo,dc=com', function(err,res){
                //This one doesn't work. But everything is done according api
                client.search('dc=foo,dc=com', opts, function(err, res) {
                    res.on('searchEntry', function(entry) {
                        console.log('hit');
                        console.log('entry: ' + JSON.stringify(entry.object));
                    });
                    res.on('searchReference', function(referral) {
                        console.log('referral: ' + referral.uris.join());
                    });
                    res.on('error', function(err) {
                        console.log('searchFailed') ;
                        console.error('error: ' + err.message);
                    });
                    res.on('end', function(result) {
                        console.log('4') ;
                        console.log('status: ' + result.status);
                    });
                });

            });

        }

When I use the search method by with dn name, it returns the correct object with its attributes (res.on('searchEntry', function(entry) part executed, because it can find the record in Ldap). But when I use client.search('dc=foo,dc=com', opts, function(err, res) with opt defined above, it always goes to branch 4: res.on('end', function(result) and never returns an error status of 0.

API documentation of Ldap.

Upvotes: 3

Views: 8093

Answers (2)

KARTHIKEYAN.A
KARTHIKEYAN.A

Reputation: 20108

In the following way we can able to search user data

function searchUser() {
    var opts = {
         filter: '(objectClass=*)',  //simple search
        //  filter: '(&(uid=2)(sn=John))',// and search
        // filter: '(|(uid=2)(sn=John)(cn=Smith))', // or search
        scope: 'sub',
        attributes: ['sn']
    };

    client.search('ou=users,ou=system', opts, function (err, res) {
        if (err) {
            console.log("Error in search " + err)
        } else {
            res.on('searchEntry', function (entry) {
                console.log('entry: ' + JSON.stringify(entry.object));
            });
            res.on('searchReference', function (referral) {
                console.log('referral: ' + referral.uris.join());
            });
            res.on('error', function (err) {
                console.error('error: ' + err.message);
            });
            res.on('end', function (result) {
                console.log('status: ' + result.status);
            });
        }
    });
}

Upvotes: 0

Yuvika
Yuvika

Reputation: 6132

This does not work for dc=foo,dc=com because that entry in the LDAP directory does not have the attribute Email and hence your filter does not match. The entry 'cn=x,ou=users,dc=foo,dc=com' in LDAP directory probably has this attribute which is why it works.

Upvotes: 2

Related Questions