Skip to content

Instantly share code, notes, and snippets.

@chdemko
Created February 6, 2012 09:43
Show Gist options
  • Save chdemko/1751107 to your computer and use it in GitHub Desktop.
Save chdemko/1751107 to your computer and use it in GitHub Desktop.
setUserGroups
public static function setUserGroups($userId, $groups)
{
// Get the user object.
$user = JUser::getInstance((int) $userId);
// Set the group ids.
JArrayHelper::toInteger($groups);
// Set the titles for the user groups.
foreach ($groups as $group)
{
$user->groups[$group] = $group;
}
// Store the user object.
if (!$user->save())
{
return new Exception($user->getError());
}
// Set the group data for any preloaded user objects.
$temp = JFactory::getUser((int) $userId);
$temp->groups = $user->groups;
// Set the group data for the user object in the session.
$temp = JFactory::getUser();
if ($temp->id == $userId)
{
$temp->groups = $user->groups;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment