All files / node-activedirectory/lib/services service.findGroup.js

23.21% Statements 13/56
0% Branches 0/44
0% Functions 0/4
23.21% Lines 13/56

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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 1011x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x                                                                                                                                                                               1x
const _                                 = require('underscore');
const Group                             = require('../models/group');
const joinAttributes                    = require('./internal/service.joinAttributes');
const getRequiredLdapAttributesForGroup = require('./internal/service.getRequiredLdapAttributesForUser');
const includeGroupMembershipFor         = require('./internal/service.includeGroupMembershipFor');
const search                            = require('./internal/service.search');
const truncateLogOutput                 = require('./internal/service.truncateLogOutput');
const pickAttributes                    = require('./internal/service.pickAttributes');
const getGroupQueryFilter               = require('./internal/service.getGroupQueryFilter');
const getGroupMembershipForDN           = require('./service.getGroupMembershipForDn');
 
const log                                = require('./internal/service.log');
let defaultAttributes                  = require('../configs/config.defaultAttributes');
 
 
/**
 * Retrieves the specified group.
 *
 * @public
 * @param {Object} [opts] Optional LDAP query string parameters to execute. { scope: '', filter: '', attributes: [ '', '', ... ], sizeLimit: 0, timelimit: 0 }
 * @param {String} groupName The group (cn) to retrieve information about. Optionally can pass in the distinguishedName (dn) of the group to retrieve.
 * @param {Function} callback The callback to execute when completed. callback(err: {Object}, group: {Group})
 */
function findGroup(opts, groupName, callback) {
    if (typeof (groupName) === 'function' || !groupName) {
        callback = groupName;
        groupName = opts;
        opts = undefined;
    }
    if (typeof (opts) === 'string') {
        groupName = opts;
        opts = undefined;
    }
    var self = this;
    return new Promise((resolve, reject) => {
        
        log.trace('findGroup(%j,%s)', opts, groupName);
    
        var localOpts = _.defaults(_.omit(opts || {}, 'attributes'), {
            filter: getGroupQueryFilter.call(self, groupName),
            scope: 'sub',
            attributes: joinAttributes((opts || {}).attributes || defaultAttributes.group, getRequiredLdapAttributesForGroup(opts))
        });
        search.call(self, localOpts, function onSearch(err, results) {
            // Ignore ECONNRESET ERRORS
            if (err) {
                if((err || {}).errno !== 'ECONNRESET'){
                    if(callback){
                        callback(err);
                    }
                    return reject(err);
                }
            }
    
            if ((!results) || (results.length === 0)) {
                log.warn('Group "%s" not found for query "%s"', groupName, truncateLogOutput(localOpts.filter));
                if (callback){
                    callback(null, {});
                } 
                return resolve({});
            }
    
            var group = new Group(pickAttributes(results[0], (opts || {}).attributes || defaultAttributes.group));
            log.info('%d group(s) found for query "%s". Returning first group: %j',
                results.length, truncateLogOutput(localOpts.filter), group);
            // Also retrieving user group memberships?
            if (includeGroupMembershipFor(opts, 'group')) {
                getGroupMembershipForDN.call(self, opts, group.dn, function (err, groups) {
                    if (err) {
                        if (callback){
                            callback(err);
                        } 
                        return reject(err);
                    }
    
                    group.groups = groups;
                    self.emit('group', group);
                    if (callback){
                        callback(null, group);
                    }
                    return resolve(group);
                });
            }
            else {
                self.emit('group', group);
                if(err){
                    if (callback){
                        callback(err, group);
                    }
                    return reject(err);                
                }
                if(callback){
                    callback(null, group);
                }
                return resolve(group);                
            }
        });
    });    
};
 
module.exports = findGroup;