async'ify the groups code
This commit is contained in:
+29
-31
@@ -14,7 +14,7 @@ exports = module.exports = {
|
||||
sync
|
||||
};
|
||||
|
||||
var assert = require('assert'),
|
||||
const assert = require('assert'),
|
||||
async = require('async'),
|
||||
auditSource = require('./auditsource.js'),
|
||||
BoxError = require('./boxerror.js'),
|
||||
@@ -23,9 +23,11 @@ var assert = require('assert'),
|
||||
groups = require('./groups.js'),
|
||||
ldap = require('ldapjs'),
|
||||
once = require('once'),
|
||||
safe = require('safetydance'),
|
||||
settings = require('./settings.js'),
|
||||
tasks = require('./tasks.js'),
|
||||
users = require('./users.js');
|
||||
users = require('./users.js'),
|
||||
util = require('util');
|
||||
|
||||
function injectPrivateFields(newConfig, currentConfig) {
|
||||
if (newConfig.bindPassword === constants.SECRET_PLACEHOLDER) newConfig.bindPassword = currentConfig.bindPassword;
|
||||
@@ -445,11 +447,11 @@ function syncGroups(externalLdapConfig, progressCallback, callback) {
|
||||
let step = 30/(ldapGroups.length+1); // ensure no divide by 0
|
||||
|
||||
// we ignore all non internal errors here and just log them for now
|
||||
async.eachSeries(ldapGroups, function (ldapGroup, iteratorCallback) {
|
||||
async.eachSeries(ldapGroups, async function (ldapGroup) {
|
||||
var groupName = ldapGroup[externalLdapConfig.groupnameField];
|
||||
if (!groupName) return iteratorCallback();
|
||||
if (!groupName) return;
|
||||
// some servers return empty array for unknown properties :-/
|
||||
if (typeof groupName !== 'string') return iteratorCallback();
|
||||
if (typeof groupName !== 'string') return;
|
||||
|
||||
// groups are lowercase
|
||||
groupName = groupName.toLowerCase();
|
||||
@@ -457,22 +459,17 @@ function syncGroups(externalLdapConfig, progressCallback, callback) {
|
||||
percent += step;
|
||||
progressCallback({ percent, message: `Syncing... ${groupName}` });
|
||||
|
||||
groups.getByName(groupName, function (error, result) {
|
||||
if (error && error.reason !== BoxError.NOT_FOUND) return iteratorCallback(error);
|
||||
let [error, result] = await safe(groups.getByName(groupName));
|
||||
if (error && error.reason !== BoxError.NOT_FOUND) throw error;
|
||||
|
||||
if (!result) {
|
||||
debug(`[adding group] groupname=${groupName}`);
|
||||
if (!result) {
|
||||
debug(`[adding group] groupname=${groupName}`);
|
||||
|
||||
groups.create(groupName, 'ldap', function (error) {
|
||||
if (error) debug('syncGroups: Failed to create group', groupName, error);
|
||||
iteratorCallback();
|
||||
});
|
||||
} else {
|
||||
debug(`[up-to-date group] groupname=${groupName}`);
|
||||
|
||||
iteratorCallback();
|
||||
}
|
||||
});
|
||||
[error] = await safe(groups.add({ name: groupName, source: 'ldap' }));
|
||||
if (error) debug('syncGroups: Failed to create group', groupName, error);
|
||||
} else {
|
||||
debug(`[up-to-date group] groupname=${groupName}`);
|
||||
}
|
||||
}, function (error) {
|
||||
if (error) return callback(error);
|
||||
|
||||
@@ -494,20 +491,22 @@ function syncGroupUsers(externalLdapConfig, progressCallback, callback) {
|
||||
return callback(null, []);
|
||||
}
|
||||
|
||||
groups.getAll(function (error, result) {
|
||||
const getAllGroups = util.callbackify(groups.getAll);
|
||||
|
||||
getAllGroups(function (error, result) {
|
||||
if (error) return callback(error);
|
||||
|
||||
var ldapGroups = result.filter(function (g) { return g.source === 'ldap'; });
|
||||
const ldapGroups = result.filter(function (g) { return g.source === 'ldap'; });
|
||||
debug(`Found ${ldapGroups.length} groups to sync users`);
|
||||
|
||||
async.eachSeries(ldapGroups, function (group, iteratorCallback) {
|
||||
async.eachSeries(ldapGroups, function (group, iteratorDone) {
|
||||
debug(`Sync users for group ${group.name}`);
|
||||
|
||||
ldapGroupSearch(externalLdapConfig, {}, function (error, result) {
|
||||
if (error) return callback(error);
|
||||
if (error) return iteratorDone(error);
|
||||
if (!result || result.length === 0) {
|
||||
debug(`syncGroupUsers: Unable to find group ${group.name} ignoring for now.`);
|
||||
return callback();
|
||||
return iteratorDone();
|
||||
}
|
||||
|
||||
// since our group names are lowercase we cannot use potentially case matching ldap filters
|
||||
@@ -518,7 +517,7 @@ function syncGroupUsers(externalLdapConfig, progressCallback, callback) {
|
||||
|
||||
if (!found) {
|
||||
debug(`syncGroupUsers: Unable to find group ${group.name} ignoring for now.`);
|
||||
return callback();
|
||||
return iteratorDone();
|
||||
}
|
||||
|
||||
var ldapGroupMembers = found.member || found.uniqueMember || [];
|
||||
@@ -540,21 +539,20 @@ function syncGroupUsers(externalLdapConfig, progressCallback, callback) {
|
||||
const username = result[externalLdapConfig.usernameField];
|
||||
if (!username) return iteratorCallback();
|
||||
|
||||
users.getByUsername(username, function (error, result) {
|
||||
users.getByUsername(username, async function (error, result) {
|
||||
if (error) {
|
||||
debug(`syncGroupUsers: Failed to get user by username ${username}`, error);
|
||||
return iteratorCallback();
|
||||
}
|
||||
|
||||
groups.addMember(group.id, result.id, function (error) {
|
||||
if (error && error.reason !== BoxError.ALREADY_EXISTS) debug('syncGroupUsers: Failed to add member', error);
|
||||
iteratorCallback();
|
||||
});
|
||||
[error] = await safe(groups.addMember(group.id, result.id));
|
||||
if (error && error.reason !== BoxError.ALREADY_EXISTS) debug('syncGroupUsers: Failed to add member', error);
|
||||
iteratorCallback();
|
||||
});
|
||||
});
|
||||
}, function (error) {
|
||||
if (error) debug('syncGroupUsers: ', error);
|
||||
iteratorCallback();
|
||||
iteratorDone();
|
||||
});
|
||||
});
|
||||
}, callback);
|
||||
|
||||
Reference in New Issue
Block a user