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

22.22% Statements 4/18
0% Branches 0/6
0% Functions 0/2
23.53% Lines 4/17

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  1x 1x 1x                                                                               1x
 
const parseDistinguishedName            = require('./service.parseDistinguishedName');
const isDistinguishedName               = require('./service.isDistinguishedName');
const log                                 = require('./service.log');
/**
 * Gets the ActiveDirectory LDAP query string for a user search.
 *
 * @private
 * @param {String|String[]} username The samAccountName(s) or userPrincipalName(s) (email) of the user.
 * @returns {String}
 */
function getUserQueryFilter(username) {
    log.trace('getUserQueryFilter(%s)', username);
    var self = this;
    let filter;
 
    // if username is acually an array of multiple usernames create an or query
    if (!username) return ('(objectCategory=User)');
    if(typeof(username) === 'object'){
        filter = '(|';
        for(ind in username){
            filter += getSingleQueryFilter(username[ind], self);
        }
        filter += ')';
    // if its a single username 
    } else {
        filter = getSingleQueryFilter(username, self);
    }
 
    return filter;
}
 
function getSingleQueryFilter(username, adObject){
    
    if (isDistinguishedName.call(adObject, username)) {
        return ('(&(objectCategory=User)(distinguishedName=' + parseDistinguishedName(username) + '))');
    } else {
        return ('(&(objectCategory=User)(|(sAMAccountName=' + username + ')(userPrincipalName=' + username + ')))');
    }
 
    
}
 
module.exports = getUserQueryFilter;