Skip to content

Commit

Permalink
Merge pull request #9 from ennosol/master
Browse files Browse the repository at this point in the history
updateGroups method added
  • Loading branch information
jenssegers committed Feb 9, 2015
2 parents 4ef3366 + 8115399 commit 52c12d5
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/Jenssegers/Mongodb/Sentry/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,61 @@ public function removeGroup(GroupInterface $group)
return true;
}

/**
* Updates the user to the given group(s).
*
* @param \Illuminate\Database\Eloquent\Collection $groups
* @param bool $remove
* @return bool
*/
public function updateGroups($groups, $remove = true)
{
$newGroupIds = array();
$removeGroupIds = array();
$existingGroupIds = array();
foreach ($groups as $group)
{
if (is_object($group))
{
$newGroupIds[] = $group->getId();
if ( ! $this->addGroup($group))
{
return false;
}
}
else
{
$newGroupIds[] = $groups->getId();
if ( ! $this->addGroup($groups))
{
return false;
}
break;
}
}
if ($remove)
{
foreach ($this->groups as $userGroup)
{
$existingGroupIds[] = $userGroup->getId();
}
$removeGroupIds = array_diff($existingGroupIds, $newGroupIds);
if ($removeGroupIds)
{
self::$groupProviderModel = self::$groupProviderModel ?: new GroupProvider;
}
foreach ($removeGroupIds as $id)
{
$group = self::$groupProviderModel->findById($id);
if ( ! $this->removeGroup($group))
{
return false;
}
}
}
return true;
}

/**
* See if the user is in the given group.
*
Expand Down

0 comments on commit 52c12d5

Please sign in to comment.