PrincipalsDAO.iterateAll(null, 100, _onEach, function(err) { assert.ok(!err, JSON.stringify(err, null, 4)); assert.ok(foundUser, 'Expected to find the user we just created'); foundUser = false; /*! * Verifies that we only get the principalId and displayName of each principalRow */ var _onEach = function(principalRows, done) { // Ensure we only get the principalId and displayName of the principal _.each(principalRows, function(principalRow) { assert.equal(_.keys(principalRow).length, 2, 'Expected to have only two keys on the principal row, the principal id and displayName'); assert.ok(principalRow.principalId, 'Expected the row to have principalId'); // Remember whether or not we found the user if (principalRow.principalId === mrvisser.id) { // Verify the displayName is accurate assert.equal(principalRow.displayName, mrvisser.displayName); foundUser = true; } }); done(); }; // Do the same thing but fetch the principalId and the displayName, and ensure they match PrincipalsDAO.iterateAll(['principalId', 'displayName'], 100, _onEach, function(err) { assert.ok(!err, JSON.stringify(err, null, 4)); assert.ok(foundUser, 'Expected to find the user we just created'); callback(); }); });
TestsUtil.generateTestUsers(camAdminRestContext, 1, function(err, users) { assert.ok(!err); var mrvisser = users[_.keys(users)[0]].user; var mrvisserRestCtx = users[_.keys(users)[0]].restContext; var foundUser = false; /*! * Verifies that each principal row only has the principalId */ var _onEach = function(principalRows, done) { // Ensure we only get the principalId of the users _.each(principalRows, function(principalRow) { assert.equal(_.keys(principalRow).length, 1, 'Expected to have only one key on the principal row, the principal id'); assert.ok(principalRow.principalId, 'Expected the row to have principalId'); // Remember whether or not we found the principal if (principalRow.principalId === mrvisser.id) { foundUser = true; } }); done(); }; // Ensure that passing in `null` will result in only the principalId being returned from the principal rows PrincipalsDAO.iterateAll(null, 100, _onEach, function(err) { assert.ok(!err, JSON.stringify(err, null, 4)); assert.ok(foundUser, 'Expected to find the user we just created'); foundUser = false; /*! * Verifies that we only get the principalId and displayName of each principalRow */ var _onEach = function(principalRows, done) { // Ensure we only get the principalId and displayName of the principal _.each(principalRows, function(principalRow) { assert.equal(_.keys(principalRow).length, 2, 'Expected to have only two keys on the principal row, the principal id and displayName'); assert.ok(principalRow.principalId, 'Expected the row to have principalId'); // Remember whether or not we found the user if (principalRow.principalId === mrvisser.id) { // Verify the displayName is accurate assert.equal(principalRow.displayName, mrvisser.displayName); foundUser = true; } }); done(); }; // Do the same thing but fetch the principalId and the displayName, and ensure they match PrincipalsDAO.iterateAll(['principalId', 'displayName'], 100, _onEach, function(err) { assert.ok(!err, JSON.stringify(err, null, 4)); assert.ok(foundUser, 'Expected to find the user we just created'); callback(); }); }); });
TestsUtil.generateTestUsers(camAdminRestContext, 1, function(err, users, createdUser) { assert.ok(!err); var numPrincipalsAfter = 0; var hasNewPrincipal = false; // Count the principals we have now, and ensure we iterate over the new user PrincipalsDAO.iterateAll(null, 1000, function(principalRows, done) { if (principalRows) { numPrincipalsAfter += principalRows.length; _.each(principalRows, function(principalRow) { if (principalRow.principalId === createdUser.user.id) { hasNewPrincipal = true; } }); } return done(); }, function(err) { assert.ok(!err); assert.strictEqual(numPrincipalsOrig + 1, numPrincipalsAfter); assert.ok(hasNewPrincipal); return callback(); }); });
SearchAPI.registerReindexAllHandler('principal', function(callback) { /*! * Handles each iteration of the PrincipalsDAO iterate all method, firing tasks for all principals to * be reindexed. * * @see PrincipalsDAO#iterateAll */ var _onEach = function(principalRows, done) { // Aggregate group and user reindexing task resources var groupResources = []; var userResources = []; _.each(principalRows, function(principal) { var principalId = principal.principalId; if (principalId) { if (PrincipalsUtil.isGroup(principalId)) { groupResources.push({ 'id': principalId, 'opts': { 'indexResource': true, 'indexMembers': true, 'indexMemberships': true } }); } else { userResources.push({ 'id': principalId, 'opts': { 'indexResource': true, 'indexMemberships': true } }); } } }); log().info('Firing re-indexing task for %s users and %s groups.', userResources.length, groupResources.length); if (userResources.length > 0) { MQ.submit(SearchConstants.mq.TASK_INDEX_DOCUMENT, { 'resourceType': 'user', 'resources': userResources }); } if (groupResources.length > 0) { MQ.submit(SearchConstants.mq.TASK_INDEX_DOCUMENT, { 'resourceType': 'group', 'resources': groupResources }); } done(); }; PrincipalsDAO.iterateAll(null, 1000, _onEach, callback); });
TestsUtil.generateTestUsers(camAdminRestContext, 1, function(err, user) { assert.ok(!err); user = _.values(user)[0]; var testUsername = TestsUtil.generateTestUserId(); // Stores how many principals were in the database before we created a new one var numPrincipalsOrig = 0; // Count how many principals we currently have in the database PrincipalsDAO.iterateAll(null, 1000, function(principalRows, done) { if (principalRows) { numPrincipalsOrig += principalRows.length; } return done(); }, function(err) { assert.ok(!err); // Create one new one, and ensure the new number of principals is numPrincipalsOrig + 1 TestsUtil.generateTestUsers(camAdminRestContext, 1, function(err, users, createdUser) { assert.ok(!err); var numPrincipalsAfter = 0; var hasNewPrincipal = false; // Count the principals we have now, and ensure we iterate over the new user PrincipalsDAO.iterateAll(null, 1000, function(principalRows, done) { if (principalRows) { numPrincipalsAfter += principalRows.length; _.each(principalRows, function(principalRow) { if (principalRow.principalId === createdUser.user.id) { hasNewPrincipal = true; } }); } return done(); }, function(err) { assert.ok(!err); assert.strictEqual(numPrincipalsOrig + 1, numPrincipalsAfter); assert.ok(hasNewPrincipal); return callback(); }); }); }); });
const _getAllUsersForTenant = function(tenantAlias, callback) { const userHashes = []; PrincipalsDAO.iterateAll( ['principalId', 'tenantAlias', 'email', 'displayName'], 100, // eslint-disable-next-line no-use-before-define _aggregateUsers, err => { if (err) { log().error({ err }, 'Failed to iterate all users'); return callback(err); } log().info('Found %s users for specified tenant', userHashes.length); return callback(null, userHashes); } ); /*! * Filter users down to those that are part of the specified tenant. Then add them to the `userHashes` array * * @param {Object[]} principalHashes The principals to filter and aggregate * @param {Function} callback Will be invoked when the principals are aggregated */ function _aggregateUsers(principalHashes, callback) { log().info('Checking %s principals for tenancy', principalHashes.length); _.chain(principalHashes) .filter(principalHash => { return ( principalHash.tenantAlias === tenantAlias && PrincipalsDAO.isUser(principalHash.principalId) ); }) .each(userHash => { log().info('Adding user %s to tenant users', userHash.displayName); userHashes.push(userHash); }); return callback(); } };
/** * Page through all the users in the system and find tenancies with users * * @param {String} tenants The list of tenants to filter * @param {Function} callback Invoked when users have been collected * @param {Object} callback.err An error that occurred, if any * @param {Object[]} callback.tenantsWithPrincipals An array of tenant aliases * @api private */ function _getTenantsWithPrincipals(tenants, callback) { let tenantsWithPrincipals = []; PrincipalsDAO.iterateAll( ['principalId', 'tenantAlias'], 100, _addToTenantsWithPrincipals, err => { if (err) { log().error({ err }, 'Failed to iterate all users'); return callback(err); } tenantsWithPrincipals = _.uniq(tenantsWithPrincipals); log().info('Found %s tenants with at least one user', tenantsWithPrincipals.length); return callback(null, tenantsWithPrincipals); } ); /*! * Add each user's `tenantAlias` to `tenantsWithPrincipals` array. * * @param {Object[]} principalHashes The principals to filter and aggregate * @param {Function} callback Will be invoked when the principals are aggregated */ function _addToTenantsWithPrincipals(rows, callback) { _.each(rows, principalHash => { const { tenantAlias } = principalHash; if ( !_.contains(tenantsWithPrincipals, tenantAlias) && PrincipalsDAO.isUser(principalHash.principalId) ) { log().info('Adding tenant %s to tenants with users', tenantAlias); tenantsWithPrincipals.push(tenantAlias); } }); return callback(); } }
SearchAPI.registerReindexAllHandler('principal', function(callback) { /*! * Handles each iteration of the PrincipalsDAO iterate all method, firing tasks for all principals to * be reindexed. * * @see PrincipalsDAO#iterateAll */ var _onEach = function(principalRows, done) { // Aggregate group and user reindexing task resources var groupResources = []; var userResources = []; _.each(principalRows, function(principal) { var principalId = principal.principalId; if (principalId) { if (AuthzUtil.isGroupId(principalId)) { groupResources.push({'id': principalId}); } else if (AuthzUtil.isUserId(principalId)) { userResources.push({'id': principalId}); } } }); log().info('Firing re-indexing task for %s users and %s groups', userResources.length, groupResources.length); if (!_.isEmpty(userResources)) { SearchAPI.postIndexTask('user', userResources, {'resource': true, 'children': true}); } if (!_.isEmpty(groupResources)) { SearchAPI.postIndexTask('group', groupResources, {'resource': true, 'children': true}); } return done(); }; return PrincipalsDAO.iterateAll(null, 100, _onEach, callback); });