Back to UserHelper class

Method removeUserFromGroup

public static bool
removeUserFromGroup
(mixed $userId, mixed $groupId)
Method to remove a user from a group.
Parameters
  • int $userId The id of the user.
  • int $groupId The id of the group.
Returns
  • bool True on success
Since
  • 1.7.0
Class: UserHelper
Project: Joomla

Method removeUserFromGroup - Source code

/**
 * Method to remove a user from a group.
 *
 * @param   integer  $userId   The id of the user.
 * @param   integer  $groupId  The id of the group.
 *
 * @return  boolean  True on success
 *
 * @since   1.7.0
 */
public static function removeUserFromGroup($userId, $groupId)
{
    // Get the user object.
    $user = User::getInstance((int) $userId);
    // Remove the user from the group if necessary.
    $key = array_search($groupId, $user->groups);
    if ($key !== false) {
        unset($user->groups[$key]);
        $user->groups = array_values($user->groups);
        // Store the user object.
        $user->save();
    }
    // Set the group data for any preloaded user objects.
    $temp = Factory::getUser((int) $userId);
    $temp->groups = $user->groups;
    // Set the group data for the user object in the session.
    $temp = Factory::getUser();
    if ($temp->id == $userId) {
        $temp->groups = $user->groups;
    }
    return true;
}