ad-promise
Advanced tools
Comparing version 1.5.4 to 1.5.5
@@ -9,3 +9,3 @@ | ||
* @private | ||
* @param {String} username The samAccountName or userPrincipalName (email) of the user. | ||
* @param {String|String[]} username The samAccountName(s) or userPrincipalName(s) (email) of the user. | ||
* @returns {String} | ||
@@ -16,11 +16,31 @@ */ | ||
var self = this; | ||
let filter; | ||
// if username is acually an array of multiple usernames create an or query | ||
if (!username) return ('(objectCategory=User)'); | ||
if (isDistinguishedName.call(self, username)) { | ||
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 + ')))'); | ||
} | ||
return ('(&(objectCategory=User)(|(sAMAccountName=' + username + ')(userPrincipalName=' + username + ')))'); | ||
} | ||
module.exports = getUserQueryFilter; |
@@ -28,84 +28,93 @@ | ||
function search (baseDN, opts, callback) { | ||
let searchStarted = new Date(); | ||
let self = this; | ||
let results = []; | ||
let isDone = false; | ||
return new Promise((resolve, reject) => { | ||
let searchStarted = new Date(); | ||
let self = this; | ||
let results = []; | ||
let isDone = false; | ||
if (typeof (opts) === 'function') { | ||
callback = opts; | ||
opts = baseDN; | ||
baseDN = undefined; | ||
} | ||
if (typeof (baseDN) === 'object') { | ||
opts = baseDN; | ||
baseDN = undefined; | ||
} | ||
opts || (opts = {}); | ||
baseDN || (baseDN = opts.baseDN) || (baseDN = self.baseDN); | ||
log.trace('search(%s,%j)', baseDN, opts); | ||
if (typeof (opts) === 'function') { | ||
callback = opts; | ||
opts = baseDN; | ||
baseDN = undefined; | ||
} | ||
if (typeof (baseDN) === 'object') { | ||
opts = baseDN; | ||
baseDN = undefined; | ||
} | ||
opts || (opts = {}); | ||
baseDN || (baseDN = opts.baseDN) || (baseDN = self.baseDN); | ||
log.trace('search(%s,%j)', baseDN, opts); | ||
var controls = opts.controls || (opts.controls = []); | ||
// Add paging results control by default if not already added. | ||
if (!_.any(controls, function (control) { return (control instanceof ldap.PagedResultsControl); })) { | ||
log.debug('Adding PagedResultControl to search (%s) with filter "%s" for %j', | ||
baseDN, truncateLogOutput(opts.filter), _.any(opts.attributes) ? opts.attributes : '[*]'); | ||
controls.push(new ldap.PagedResultsControl({ value: { size: defaultPageSize } })); | ||
} | ||
if (opts.includeDeleted) { | ||
if (!_.any(controls, function (control) { return (control.type === '1.2.840.113556.1.4.417'); })) { | ||
log.debug('Adding ShowDeletedOidControl(1.2.840.113556.1.4.417) to search (%s) with filter "%s" for %j', | ||
var controls = opts.controls || (opts.controls = []); | ||
// Add paging results control by default if not already added. | ||
if (!_.any(controls, function (control) { return (control instanceof ldap.PagedResultsControl); })) { | ||
log.debug('Adding PagedResultControl to search (%s) with filter "%s" for %j', | ||
baseDN, truncateLogOutput(opts.filter), _.any(opts.attributes) ? opts.attributes : '[*]'); | ||
controls.push(new ldap.Control({ type: '1.2.840.113556.1.4.417', criticality: true })); | ||
controls.push(new ldap.PagedResultsControl({ value: { size: defaultPageSize } })); | ||
} | ||
} | ||
if (opts.includeDeleted) { | ||
if (!_.any(controls, function (control) { return (control.type === '1.2.840.113556.1.4.417'); })) { | ||
log.debug('Adding ShowDeletedOidControl(1.2.840.113556.1.4.417) to search (%s) with filter "%s" for %j', | ||
baseDN, truncateLogOutput(opts.filter), _.any(opts.attributes) ? opts.attributes : '[*]'); | ||
controls.push(new ldap.Control({ type: '1.2.840.113556.1.4.417', criticality: true })); | ||
} | ||
} | ||
log.debug('Querying active directory (%s) with filter "%s" for %j', | ||
baseDN, | ||
truncateLogOutput(opts.filter), | ||
_.any(opts.attributes) ? opts.attributes : '[*]' | ||
); | ||
// We want to limit the total of the searches, we will use [true] as InputValues as we don't use it anyways in the function | ||
let s = limitpromises(Input => { | ||
return new Promise((resolve, reject) => { | ||
var client = createClient.call(self, null, opts); | ||
client.on('error', err => { | ||
onClientError(err, client, searchStarted, baseDN, opts, results, resolve, reject) | ||
}); | ||
client.search(baseDN, getLdapOpts(opts), controls, function onSearch(err, res) { | ||
if (err) { | ||
reject(err); | ||
} | ||
log.debug('Querying active directory (%s) with filter "%s" for %j', | ||
baseDN, | ||
truncateLogOutput(opts.filter), | ||
_.any(opts.attributes) ? opts.attributes : '[*]' | ||
); | ||
res.on('searchEntry', entry => { | ||
onSearchEntry(entry, client, baseDN, self, opts, isDone, results, resolve, reject); | ||
}); | ||
res.on('searchReference', ref => { | ||
onReferralChase(self, client, ref, opts, controls, results, resolve, reject); | ||
// We want to limit the total of the searches, we will use [true] as InputValues as we don't use it anyways in the function | ||
let s = limitpromises(Input => { | ||
return new Promise((resolve, reject) => { | ||
var client = createClient.call(self, null, opts); | ||
client.on('error', err => { | ||
onClientError(err, client, searchStarted, baseDN, opts, results, resolve, reject) | ||
}); | ||
res.on('error', function (err) { | ||
onClientError(err, client, searchStarted, baseDN, opts, results, resolve, reject); | ||
client.search(baseDN, getLdapOpts(opts), controls, function onSearch(err, res) { | ||
if (err) { | ||
reject(err); | ||
} | ||
res.on('searchEntry', entry => { | ||
onSearchEntry(entry, client, baseDN, self, opts, isDone, results, resolve, reject); | ||
}); | ||
res.on('searchReference', ref => { | ||
onReferralChase(self, client, ref, opts, controls, results, resolve, reject); | ||
}); | ||
res.on('error', function (err) { | ||
onClientError(err, client, searchStarted, baseDN, opts, results, resolve, reject); | ||
}); | ||
res.on('end', function (result) { | ||
isDone = true; // Flag that the primary query is complete | ||
onSearchEnd(client, baseDN, opts, results, resolve, reject); | ||
}); | ||
}); | ||
res.on('end', function (result) { | ||
isDone = true; // Flag that the primary query is complete | ||
onSearchEnd(client, baseDN, opts, results, resolve, reject); | ||
}); | ||
}); | ||
}, [true], self.opts.maxSearchesAtOnce || maxPromiseConfig.maxSearchesAtOnce, "searches", maxPromiseConfig.searchTimeoutAndReject); | ||
}); | ||
}); | ||
}, [true], self.opts.maxSearchesAtOnce || maxPromiseConfig.maxSearchesAtOnce, "searches", maxPromiseConfig.searchTimeoutAndReject); | ||
Promise.all(s.map(r => {return r.result})).then(results => { | ||
for(index in results){ | ||
callback(null, results[index]); | ||
} | ||
}, err => { | ||
callback(err); | ||
}); | ||
Promise.all(s.map(r => {return r.result})).then(results => { | ||
if(callback){ | ||
callback(null, results[0]); | ||
} | ||
return resolve(results[0]); | ||
}, err => { | ||
if(callback){ | ||
callback(err); | ||
} | ||
return reject(err); | ||
}); | ||
}) | ||
} | ||
module.exports = search; |
@@ -10,2 +10,3 @@ const _ = require('underscore'); | ||
const getCompoundFilter = require('./internal/service.getCompoundFilter'); | ||
const getUserQueryFilter = require('./internal/service.getUserQueryFilter'); | ||
const isUserResult = require('./internal/service.isUserResult'); | ||
@@ -38,2 +39,6 @@ const getGroupMembershipForDN = require('./service.getGroupMembershipForDn'); | ||
} | ||
if(typeof(opts) === 'object'){ | ||
opts.filter = getUserQueryFilter(opts); | ||
} | ||
if ((typeof (opts) === 'string') && (opts)) { | ||
@@ -44,6 +49,9 @@ opts = { | ||
} | ||
log.trace('findUsers(%j,%s)', opts, includeMembership); | ||
var localOpts = _.defaults(_.omit(opts || {}, 'attributes'), { | ||
filter: '(&' + defaultUserFilter + ')', | ||
filter: opts.filter, | ||
scope: 'sub', | ||
@@ -50,0 +58,0 @@ attributes: joinAttributes((opts || {}).attributes || defaultAttributes.user || [], |
{ | ||
"author": "Relief Melone (relief.melone@gmail.com)", | ||
"name": "ad-promise", | ||
"version": "1.5.4", | ||
"version": "1.5.5", | ||
"description": "This is a fork of the gheeres node-activedirectory. It fixes some issues with timeouts with very large AD-Groups as well as returning also promises so you won't have to use callbacks", | ||
@@ -6,0 +6,0 @@ "main": "index.js", |
@@ -480,3 +480,3 @@ AD-Promise for Node | ||
__Arguments__ | ||
* opts - Optional parameters to extend or override functionality. See [optional parameters](#opts). If only a string is provided, then the string is assumed to be an LDAP filter that will be appended as the last parameter in the default LDAP filter. | ||
* opts - Optional parameters to extend or override functionality. See [optional parameters](#opts). If only a string is provided, then the string is assumed to be an LDAP filter that will be appended as the last parameter in the default LDAP filter. If an Array is provided, AD-Promise will assume its multiple sAMAccountNames. | ||
* callback - The callback to execute when completed. callback(err: {Object}, users: {Array[User]}) | ||
@@ -487,5 +487,7 @@ | ||
```js | ||
var query = 'cn=*George*'; | ||
let query = 'cn=*George*'; | ||
let usernames = ["username1", "username2"]; | ||
var ad = new ActiveDirectory(config); | ||
const ad = new ActiveDirectory(config); | ||
// Working with a query | ||
ad.findUsers(query, function(err, users) { | ||
@@ -502,2 +504,15 @@ if (err) { | ||
}); | ||
// Working with an array of usernames | ||
ad.findUsers(usernames, function(err, users) { | ||
if (err) { | ||
console.log('ERROR: ' +JSON.stringify(err)); | ||
return; | ||
} | ||
if ((! users) || (users.length == 0)) console.log('No users found.'); | ||
else { | ||
console.log('findUsers: '+JSON.stringify(users)); | ||
} | ||
}); | ||
``` | ||
@@ -504,0 +519,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
222959
4330
809