Created
February 6, 2012 09:43
-
-
Save chdemko/1751107 to your computer and use it in GitHub Desktop.
setUserGroups
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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