Reduce number of SQL queries when doing groups sync
This commit is contained in:
@@ -16,8 +16,8 @@ class GroupSync extends Base
|
|||||||
* Synchronize group membership
|
* Synchronize group membership
|
||||||
*
|
*
|
||||||
* @access public
|
* @access public
|
||||||
* @param integer $userId
|
* @param integer $userId
|
||||||
* @param array $externalGroupIds
|
* @param string[] $externalGroupIds
|
||||||
*/
|
*/
|
||||||
public function synchronize($userId, array $externalGroupIds)
|
public function synchronize($userId, array $externalGroupIds)
|
||||||
{
|
{
|
||||||
@@ -30,21 +30,18 @@ class GroupSync extends Base
|
|||||||
* Add missing groups to the user
|
* Add missing groups to the user
|
||||||
*
|
*
|
||||||
* @access protected
|
* @access protected
|
||||||
* @param integer $userId
|
* @param integer $userId
|
||||||
* @param array $userGroups
|
* @param array $userGroups
|
||||||
* @param array $externalGroupIds
|
* @param string[] $externalGroupIds
|
||||||
*/
|
*/
|
||||||
protected function addGroups($userId, array $userGroups, array $externalGroupIds)
|
protected function addGroups($userId, array $userGroups, array $externalGroupIds)
|
||||||
{
|
{
|
||||||
$userGroupIds = array_column($userGroups, 'external_id', 'external_id');
|
$userGroupIds = array_column($userGroups, 'external_id', 'external_id');
|
||||||
|
$externalGroups = $this->groupModel->getByExternalIds($externalGroupIds);
|
||||||
|
|
||||||
foreach ($externalGroupIds as $externalGroupId) {
|
foreach ($externalGroups as $externalGroup) {
|
||||||
if (! isset($userGroupIds[$externalGroupId])) {
|
if (! isset($userGroupIds[$externalGroup['external_id']])) {
|
||||||
$group = $this->groupModel->getByExternalId($externalGroupId);
|
$this->groupMemberModel->addUser($externalGroup['id'], $userId);
|
||||||
|
|
||||||
if (! empty($group)) {
|
|
||||||
$this->groupMemberModel->addUser($group['id'], $userId);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -53,9 +50,9 @@ class GroupSync extends Base
|
|||||||
* Remove groups from the user
|
* Remove groups from the user
|
||||||
*
|
*
|
||||||
* @access protected
|
* @access protected
|
||||||
* @param integer $userId
|
* @param integer $userId
|
||||||
* @param array $userGroups
|
* @param array $userGroups
|
||||||
* @param array $externalGroupIds
|
* @param string[] $externalGroupIds
|
||||||
*/
|
*/
|
||||||
protected function removeGroups($userId, array $userGroups, array $externalGroupIds)
|
protected function removeGroups($userId, array $userGroups, array $externalGroupIds)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -45,10 +45,10 @@ class GroupModel extends Base
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a specific group by external id
|
* Get a specific group by externalID
|
||||||
*
|
*
|
||||||
* @access public
|
* @access public
|
||||||
* @param integer $external_id
|
* @param string $external_id
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getByExternalId($external_id)
|
public function getByExternalId($external_id)
|
||||||
@@ -56,6 +56,22 @@ class GroupModel extends Base
|
|||||||
return $this->db->table(self::TABLE)->eq('external_id', $external_id)->findOne();
|
return $this->db->table(self::TABLE)->eq('external_id', $external_id)->findOne();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get specific groups by externalIDs
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @param string[] $external_ids
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getByExternalIds(array $external_ids)
|
||||||
|
{
|
||||||
|
if (empty($external_ids)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->db->table(self::TABLE)->in('external_id', $external_ids)->findAll();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all groups
|
* Get all groups
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user