From c06a1108304eb31d15523fcd3caf78509eefd841 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Tue, 2 Oct 2018 15:15:23 -0700 Subject: Reduce number of SQL queries when doing groups sync --- app/Core/User/GroupSync.php | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) (limited to 'app/Core/User/GroupSync.php') diff --git a/app/Core/User/GroupSync.php b/app/Core/User/GroupSync.php index d0bb647b..0e2a87fe 100644 --- a/app/Core/User/GroupSync.php +++ b/app/Core/User/GroupSync.php @@ -16,8 +16,8 @@ class GroupSync extends Base * Synchronize group membership * * @access public - * @param integer $userId - * @param array $externalGroupIds + * @param integer $userId + * @param string[] $externalGroupIds */ public function synchronize($userId, array $externalGroupIds) { @@ -30,21 +30,18 @@ class GroupSync extends Base * Add missing groups to the user * * @access protected - * @param integer $userId - * @param array $userGroups - * @param array $externalGroupIds + * @param integer $userId + * @param array $userGroups + * @param string[] $externalGroupIds */ protected function addGroups($userId, array $userGroups, array $externalGroupIds) { $userGroupIds = array_column($userGroups, 'external_id', 'external_id'); + $externalGroups = $this->groupModel->getByExternalIds($externalGroupIds); - foreach ($externalGroupIds as $externalGroupId) { - if (! isset($userGroupIds[$externalGroupId])) { - $group = $this->groupModel->getByExternalId($externalGroupId); - - if (! empty($group)) { - $this->groupMemberModel->addUser($group['id'], $userId); - } + foreach ($externalGroups as $externalGroup) { + if (! isset($userGroupIds[$externalGroup['external_id']])) { + $this->groupMemberModel->addUser($externalGroup['id'], $userId); } } } @@ -53,9 +50,9 @@ class GroupSync extends Base * Remove groups from the user * * @access protected - * @param integer $userId - * @param array $userGroups - * @param array $externalGroupIds + * @param integer $userId + * @param array $userGroups + * @param string[] $externalGroupIds */ protected function removeGroups($userId, array $userGroups, array $externalGroupIds) { -- cgit v1.2.3