All files / node-activedirectory/lib/services/internal service.getDistinguishedNames.js

24% Statements 6/25
0% Branches 0/14
0% Functions 0/3
25% Lines 6/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54  1x 1x 1x 1x   1x                                                                                             1x
 
const _                                 = require('underscore');
const joinAttributes                    = require('./service.joinAttributes');
const search                            = require('./service.search');
const truncateLogOutput                 = require('./service.truncateLogOutput');
 
let log                                 = require('./service.log');
 
 
 
/**
 * For the specified filter, return the distinguishedName (dn) of all the matched entries.
 *
 * @private
 * @param {Object} [opts] Optional LDAP query string parameters to execute. { scope: '', filter: '', attributes: [ '', '', ... ], sizeLimit: 0, timelimit: 0 }
 * @params {Object|String} filter The LDAP filter to execute. Optionally a custom LDAP query object can be specified. { scope: '', filter: '', attributes: [ '', '', ... ], sizeLimit: 0, timelimit: 0 }
 * @param {Function} callback The callback to execute when completed. callback(err: {Object}, dns: {Array[String]})
 */
function getDistinguishedNames(opts, filter, callback) {
    var self = this;
 
    if (typeof (filter) === 'function') {
        callback = filter;
        filter = opts;
        opts = undefined;
    }
    if (typeof (opts) === 'string') {
        filter = opts;
        opts = undefined;
    }
    log.trace('getDistinguishedNames(%j,%j)', opts, filter);
 
    opts = _.defaults(_.omit(opts || {}, 'attributes'), {
        filter: filter,
        scope: 'sub',
        attributes: joinAttributes((opts || {}).attributes || [], ['dn'])
    });
    search.call(self, opts, function (err, results) {
        if (err) {
            if (callback) callback(err);
            return;
        }
 
        // Extract just the DN from the results
        var dns = _.map(results, function (result) {
            return (result.dn);
        });
        log.info('%d distinguishedName(s) found for LDAP query: "%s". Results: %j',
            results.length, truncateLogOutput(opts.filter), results);
        callback(null, dns);
    });
}
 
module.exports = getDistinguishedNames;