/** * Method to add a user to a group. * * @param integer $userId The id of the user. * @param integer $groupId The id of the group. * * @return boolean True on success * * @since 11.1 * @throws RuntimeException */ public static function addUserToGroup($userId, $groupId) { // Get the user object. $user = new User((int) $userId); // Add the user to the group if necessary. if (!in_array($groupId, $user->groups)) { // Get the title of the group. $db = Factory::getDbo(); $query = $db->getQuery(true); $query->select($db->quoteName('title')); $query->from($db->quoteName('#__usergroups')); $query->where($db->quoteName('id') . ' = ' . (int) $groupId); $db->setQuery($query); $title = $db->loadResult(); // If the group does not exist, return an exception. if (!$title) { throw new RuntimeException('Access Usergroup Invalid'); } // Add the group data to the user object. $user->groups[$title] = $groupId; // 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; }