Ejemplo n.º 1
0
 /**
  * Attempts to add the 'chatmoderator' group to the user whose name is provided
  * in 'userNameToPromote'.
  *
  * Returns true on success, returns an error message as a string on failure.
  */
 public static function promoteChatModerator($userNameToPromote, $promottingUser)
 {
     wfProfileIn(__METHOD__);
     $CHAT_MOD_GROUP = 'chatmoderator';
     $userToPromote = User::newFromName($userNameToPromote);
     if (!$userToPromote instanceof User) {
         $errorMsg = wfMsg('chat-err-invalid-username-chatmod', $userNameToPromote);
         wfProfileOut(__METHOD__);
         return $errorMsg;
     }
     // Check if the userToPromote is already in the chatmoderator group.
     $errorMsg = '';
     if (in_array($CHAT_MOD_GROUP, $userToPromote->getEffectiveGroups())) {
         $errorMsg = wfMsg("chat-err-already-chatmod", $userNameToPromote, $CHAT_MOD_GROUP);
     } else {
         $changeableGroups = $promottingUser->changeableGroups();
         $promottingUserName = $promottingUser->getName();
         $isSelf = $userToPromote->getName() == $promottingUserName;
         $addableGroups = array_merge($changeableGroups['add'], $isSelf ? $changeableGroups['add-self'] : array());
         if (in_array($CHAT_MOD_GROUP, $addableGroups)) {
             // Adding the group is allowed. Add the group, clear the cache, run necessary hooks, and log the change.
             $oldGroups = $userToPromote->getGroups();
             $userToPromote->addGroup($CHAT_MOD_GROUP);
             $userToPromote->invalidateCache();
             if ($userToPromote instanceof User) {
                 $removegroups = array();
                 $addgroups = array($CHAT_MOD_GROUP);
                 wfRunHooks('UserRights', array(&$userToPromote, $addgroups, $removegroups));
             }
             // Update user-rights log.
             $newGroups = array_merge($oldGroups, array($CHAT_MOD_GROUP));
             // Log the rights-change.
             Chat::addLogEntry($userToPromote, $promottingUser, array(Chat::makeGroupNameListForLog($oldGroups), Chat::makeGroupNameListForLog($newGroups)), 'chatmoderator');
         } else {
             $errorMsg = wfMsg("chat-err-no-permission-to-add-chatmod", $CHAT_MOD_GROUP);
         }
     }
     wfProfileOut(__METHOD__);
     return $errorMsg == "" ? true : $errorMsg;
 }