From 57620326a770ed1ee66db345e14f2c88139b2b91 Mon Sep 17 00:00:00 2001 From: Rodrigo Nascimento Date: Tue, 2 Jan 2018 16:47:20 -0200 Subject: [PATCH] Merge pull request #9309 from RocketChat/hotfix/ldap-import [FIX] LDAP/AD is not importing all users --- packages/rocketchat-ldap/server/ldap.js | 56 +++++++++++++++++-------- 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/packages/rocketchat-ldap/server/ldap.js b/packages/rocketchat-ldap/server/ldap.js index 5c9bfe25bb68..894d00264bed 100644 --- a/packages/rocketchat-ldap/server/ldap.js +++ b/packages/rocketchat-ldap/server/ldap.js @@ -371,6 +371,17 @@ export default class LDAP { searchAllPaged(BaseDN, options, page) { this.bindIfNecessary(); + const processPage = ({entries, title, end, next}) => { + logger.search.info(title); + // Force LDAP idle to wait the record processing + this.client._updateIdle(true); + page(null, entries, {end, next: () => { + // Reset idle timer + this.client._updateIdle(); + next && next(); + }}); + }; + this.client.search(BaseDN, options, (error, res) => { if (error) { logger.search.error(error); @@ -392,33 +403,42 @@ export default class LDAP { entries.push(this.extractLdapEntryData(entry)); if (entries.length >= internalPageSize) { - logger.search.info('Internal Page'); - this.client._updateIdle(true); - page(null, entries, {end: false, next: () => { - // Reset idle timer - this.client._updateIdle(); - }}); + processPage({ + entries, + title: 'Internal Page', + end: false + }); entries = []; } }); res.on('page', (result, next) => { if (!next) { - logger.search.debug('Final Page'); this.client._updateIdle(true); - page(null, entries, {end: true, next: () => { - // Reset idle timer - this.client._updateIdle(); - }}); + processPage({ + entries, + title: 'Final Page', + end: true + }); } else if (entries.length) { logger.search.info('Page'); - // Force LDAP idle to wait the record processing - this.client._updateIdle(true); - page(null, entries, {end: !next, next: () => { - // Reset idle timer - this.client._updateIdle(); - next(); - }}); + processPage({ + entries, + title: 'Page', + end: false, + next + }); + entries = []; + } + }); + + res.on('end', () => { + if (entries.length) { + processPage({ + entries, + title: 'Final Page', + end: true + }); entries = []; } });