From 6abc02cb884d4384f6892082373eab50c805486d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Tue, 1 Dec 2015 12:48:23 +0100 Subject: [PATCH] Proposal: add enumeration function to IUserManager which simply calls a callbask for each user in all backends --- apps/dav/command/syncsystemaddressbook.php | 37 ++++++++++------------ lib/private/user/manager.php | 34 +++++++++++++++++--- lib/public/iusermanager.php | 7 ++++ 3 files changed, 54 insertions(+), 24 deletions(-) diff --git a/apps/dav/command/syncsystemaddressbook.php b/apps/dav/command/syncsystemaddressbook.php index 288616d065b..bb2896abc60 100644 --- a/apps/dav/command/syncsystemaddressbook.php +++ b/apps/dav/command/syncsystemaddressbook.php @@ -7,6 +7,7 @@ use OCA\DAV\CardDAV\Converter; use OCA\DAV\Connector\Sabre\Principal; use OCP\IConfig; use OCP\IDBConnection; +use OCP\IUser; use OCP\IUserManager; use Sabre\CardDAV\Plugin; use Sabre\VObject\Component\VCard; @@ -69,28 +70,24 @@ class SyncSystemAddressBook extends Command { $output->writeln('Syncing users ...'); $progress = new ProgressBar($output); $progress->start(); - $page = 0; - foreach( $this->userManager->getBackends() as $backend) { - $users = $backend->getUsers('', 50, $page++); - foreach($users as $user) { - $user = $this->userManager->get($user); - $name = $user->getBackendClassName(); - $userId = $user->getUID(); - - $cardId = "$name:$userId.vcf"; - $card = $this->backend->getCard($systemAddressBook['id'], $cardId); - if ($card === false) { - $vCard = $converter->createCardFromUser($user); - $this->backend->createCard($systemAddressBook['id'], $cardId, $vCard->serialize()); - } else { - $vCard = Reader::read($card['carddata']); - if ($converter->updateCard($vCard, $user)) { - $this->backend->updateCard($systemAddressBook['id'], $cardId, $vCard->serialize()); - } + $this->userManager->callForAllUsers(function($user) use ($systemAddressBook, $converter, $progress) { + /** @var IUser $user */ + $name = $user->getBackendClassName(); + $userId = $user->getUID(); + + $cardId = "$name:$userId.vcf"; + $card = $this->backend->getCard($systemAddressBook['id'], $cardId); + if ($card === false) { + $vCard = $converter->createCardFromUser($user); + $this->backend->createCard($systemAddressBook['id'], $cardId, $vCard->serialize()); + } else { + $vCard = Reader::read($card['carddata']); + if ($converter->updateCard($vCard, $user)) { + $this->backend->updateCard($systemAddressBook['id'], $cardId, $vCard->serialize()); } - $progress->advance(); } - } + $progress->advance(); + }); $progress->finish(); $output->writeln(''); } diff --git a/lib/private/user/manager.php b/lib/private/user/manager.php index 109f08f47a0..8d93b998d99 100644 --- a/lib/private/user/manager.php +++ b/lib/private/user/manager.php @@ -294,21 +294,47 @@ class Manager extends PublicEmitter implements IUserManager { $userCountStatistics = array(); foreach ($this->backends as $backend) { if ($backend->implementsActions(\OC_User_Backend::COUNT_USERS)) { - $backendusers = $backend->countUsers(); - if($backendusers !== false) { + $backendUsers = $backend->countUsers(); + if($backendUsers !== false) { if($backend instanceof \OCP\IUserBackend) { $name = $backend->getBackendName(); } else { $name = get_class($backend); } if(isset($userCountStatistics[$name])) { - $userCountStatistics[$name] += $backendusers; + $userCountStatistics[$name] += $backendUsers; } else { - $userCountStatistics[$name] = $backendusers; + $userCountStatistics[$name] = $backendUsers; } } } } return $userCountStatistics; } + + /** + * The callback is executed for each user on each backend. + * If the callback returns false no further users will be retrieved. + * + * @param \Closure $callback + * @return void + * @since 9.0.0 + */ + public function callForAllUsers(\Closure $callback, $search = '') { + foreach($this->getBackends() as $backend) { + $limit = 50; + $offset = 0; + do { + $users = $backend->getUsers($search, $limit, $offset); + foreach ($users as $user) { + $user = $this->get($user); + $return = $callback($user); + if ($return === false) { + break; + } + } + $offset += $limit; + } while (count($users) >= $limit); + } + } } diff --git a/lib/public/iusermanager.php b/lib/public/iusermanager.php index e3857d6231a..3a0abaca7b7 100644 --- a/lib/public/iusermanager.php +++ b/lib/public/iusermanager.php @@ -134,4 +134,11 @@ interface IUserManager { * @since 8.0.0 */ public function countUsers(); + + /** + * @param \Closure $callback + * @return void + * @since 9.0.0 + */ + public function callForAllUsers (\Closure $callback, $search = ''); }