MediaWiki  REL1_22
SpecialUserrights.php
Go to the documentation of this file.
00001 <?php
00029 class UserrightsPage extends SpecialPage {
00030     # The target of the local right-adjuster's interest.  Can be gotten from
00031     # either a GET parameter or a subpage-style parameter, so have a member
00032     # variable for it.
00033     protected $mTarget;
00034     protected $isself = false;
00035 
00036     public function __construct() {
00037         parent::__construct( 'Userrights' );
00038     }
00039 
00040     public function isRestricted() {
00041         return true;
00042     }
00043 
00044     public function userCanExecute( User $user ) {
00045         return $this->userCanChangeRights( $user, false );
00046     }
00047 
00053     public function userCanChangeRights( $user, $checkIfSelf = true ) {
00054         $available = $this->changeableGroups();
00055         if ( $user->getId() == 0 ) {
00056             return false;
00057         }
00058         return !empty( $available['add'] )
00059             || !empty( $available['remove'] )
00060             || ( ( $this->isself || !$checkIfSelf ) &&
00061                 ( !empty( $available['add-self'] )
00062                     || !empty( $available['remove-self'] ) ) );
00063     }
00064 
00072     public function execute( $par ) {
00073         // If the visitor doesn't have permissions to assign or remove
00074         // any groups, it's a bit silly to give them the user search prompt.
00075 
00076         $user = $this->getUser();
00077 
00078         /*
00079          * If the user is blocked and they only have "partial" access
00080          * (e.g. they don't have the userrights permission), then don't
00081          * allow them to use Special:UserRights.
00082          */
00083         if ( $user->isBlocked() && !$user->isAllowed( 'userrights' ) ) {
00084             throw new UserBlockedError( $user->getBlock() );
00085         }
00086 
00087         $request = $this->getRequest();
00088 
00089         if ( $par !== null ) {
00090             $this->mTarget = $par;
00091         } else {
00092             $this->mTarget = $request->getVal( 'user' );
00093         }
00094 
00095         $available = $this->changeableGroups();
00096 
00097         if ( $this->mTarget === null ) {
00098             /*
00099              * If the user specified no target, and they can only
00100              * edit their own groups, automatically set them as the
00101              * target.
00102              */
00103             if ( !count( $available['add'] ) && !count( $available['remove'] ) ) {
00104                 $this->mTarget = $user->getName();
00105             }
00106         }
00107 
00108         if ( User::getCanonicalName( $this->mTarget ) == $user->getName() ) {
00109             $this->isself = true;
00110         }
00111 
00112         if ( !$this->userCanChangeRights( $user, true ) ) {
00113             if ( $this->isself && $request->getCheck( 'success' ) ) {
00114                 // bug 48609: if the user just removed its own rights, this would
00115                 // leads it in a "permissions error" page. In that case, show a
00116                 // message that it can't anymore use this page instead of an error
00117                 $this->setHeaders();
00118                 $out = $this->getOutput();
00119                 $out->wrapWikiMsg( "<div class=\"successbox\">\n$1\n</div>", 'userrights-removed-self' );
00120                 $out->returnToMain();
00121                 return;
00122             }
00123 
00124             // @todo FIXME: There may be intermediate groups we can mention.
00125             $msg = $user->isAnon() ? 'userrights-nologin' : 'userrights-notallowed';
00126             throw new PermissionsError( null, array( array( $msg ) ) );
00127         }
00128 
00129         $this->checkReadOnly();
00130 
00131         $this->setHeaders();
00132         $this->outputHeader();
00133 
00134         $out = $this->getOutput();
00135         $out->addModuleStyles( 'mediawiki.special' );
00136 
00137         // show the general form
00138         if ( count( $available['add'] ) || count( $available['remove'] ) ) {
00139             $this->switchForm();
00140         }
00141 
00142         if (
00143             $request->wasPosted() &&
00144             $request->getCheck( 'saveusergroups' ) &&
00145             $user->matchEditToken( $request->getVal( 'wpEditToken' ), $this->mTarget )
00146         ) {
00147             // save settings
00148             $status = $this->fetchUser( $this->mTarget );
00149             if ( !$status->isOK() ) {
00150                 $this->getOutput()->addWikiText( $status->getWikiText() );
00151                 return;
00152             }
00153 
00154             $targetUser = $status->value;
00155 
00156             if ( $request->getVal( 'conflictcheck-originalgroups' ) !== implode( ',', $targetUser->getGroups() ) ) {
00157                 $out->addWikiMsg( 'userrights-conflict' );
00158             } else {
00159                 $this->saveUserGroups(
00160                     $this->mTarget,
00161                     $request->getVal( 'user-reason' ),
00162                     $targetUser
00163                 );
00164 
00165                 $out->redirect( $this->getSuccessURL() );
00166                 return;
00167             }
00168         }
00169 
00170         // show some more forms
00171         if ( $this->mTarget !== null ) {
00172             $this->editUserGroupsForm( $this->mTarget );
00173         }
00174     }
00175 
00176     function getSuccessURL() {
00177         return $this->getTitle( $this->mTarget )->getFullURL( array( 'success' => 1 ) );
00178     }
00179 
00189     function saveUserGroups( $username, $reason, $user ) {
00190         $allgroups = $this->getAllGroups();
00191         $addgroup = array();
00192         $removegroup = array();
00193 
00194         // This could possibly create a highly unlikely race condition if permissions are changed between
00195         //  when the form is loaded and when the form is saved. Ignoring it for the moment.
00196         foreach ( $allgroups as $group ) {
00197             // We'll tell it to remove all unchecked groups, and add all checked groups.
00198             // Later on, this gets filtered for what can actually be removed
00199             if ( $this->getRequest()->getCheck( "wpGroup-$group" ) ) {
00200                 $addgroup[] = $group;
00201             } else {
00202                 $removegroup[] = $group;
00203             }
00204         }
00205 
00206         $this->doSaveUserGroups( $user, $addgroup, $removegroup, $reason );
00207     }
00208 
00218     function doSaveUserGroups( $user, $add, $remove, $reason = '' ) {
00219         global $wgAuth;
00220 
00221         // Validate input set...
00222         $isself = ( $user->getName() == $this->getUser()->getName() );
00223         $groups = $user->getGroups();
00224         $changeable = $this->changeableGroups();
00225         $addable = array_merge( $changeable['add'], $isself ? $changeable['add-self'] : array() );
00226         $removable = array_merge( $changeable['remove'], $isself ? $changeable['remove-self'] : array() );
00227 
00228         $remove = array_unique(
00229             array_intersect( (array)$remove, $removable, $groups ) );
00230         $add = array_unique( array_diff(
00231             array_intersect( (array)$add, $addable ),
00232             $groups )
00233         );
00234 
00235         $oldGroups = $user->getGroups();
00236         $newGroups = $oldGroups;
00237 
00238         // remove then add groups
00239         if ( $remove ) {
00240             $newGroups = array_diff( $newGroups, $remove );
00241             foreach ( $remove as $group ) {
00242                 $user->removeGroup( $group );
00243             }
00244         }
00245         if ( $add ) {
00246             $newGroups = array_merge( $newGroups, $add );
00247             foreach ( $add as $group ) {
00248                 $user->addGroup( $group );
00249             }
00250         }
00251         $newGroups = array_unique( $newGroups );
00252 
00253         // Ensure that caches are cleared
00254         $user->invalidateCache();
00255 
00256         // update groups in external authentication database
00257         $wgAuth->updateExternalDBGroups( $user, $add, $remove );
00258 
00259         wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) );
00260         wfDebug( 'newGroups: ' . print_r( $newGroups, true ) );
00261         wfRunHooks( 'UserRights', array( &$user, $add, $remove ) );
00262 
00263         if ( $newGroups != $oldGroups ) {
00264             $this->addLogEntry( $user, $oldGroups, $newGroups, $reason );
00265         }
00266         return array( $add, $remove );
00267     }
00268 
00272     function addLogEntry( $user, $oldGroups, $newGroups, $reason ) {
00273         $logEntry = new ManualLogEntry( 'rights', 'rights' );
00274         $logEntry->setPerformer( $this->getUser() );
00275         $logEntry->setTarget( $user->getUserPage() );
00276         $logEntry->setComment( $reason );
00277         $logEntry->setParameters( array(
00278             '4::oldgroups' => $oldGroups,
00279             '5::newgroups' => $newGroups,
00280         ) );
00281         $logid = $logEntry->insert();
00282         $logEntry->publish( $logid );
00283     }
00284 
00289     function editUserGroupsForm( $username ) {
00290         $status = $this->fetchUser( $username );
00291         if ( !$status->isOK() ) {
00292             $this->getOutput()->addWikiText( $status->getWikiText() );
00293             return;
00294         } else {
00295             $user = $status->value;
00296         }
00297 
00298         $groups = $user->getGroups();
00299 
00300         $this->showEditUserGroupsForm( $user, $groups );
00301 
00302         // This isn't really ideal logging behavior, but let's not hide the
00303         // interwiki logs if we're using them as is.
00304         $this->showLogFragment( $user, $this->getOutput() );
00305     }
00306 
00315     public function fetchUser( $username ) {
00316         global $wgUserrightsInterwikiDelimiter;
00317 
00318         $parts = explode( $wgUserrightsInterwikiDelimiter, $username );
00319         if ( count( $parts ) < 2 ) {
00320             $name = trim( $username );
00321             $database = '';
00322         } else {
00323             list( $name, $database ) = array_map( 'trim', $parts );
00324 
00325             if ( $database == wfWikiID() ) {
00326                 $database = '';
00327             } else {
00328                 if ( !$this->getUser()->isAllowed( 'userrights-interwiki' ) ) {
00329                     return Status::newFatal( 'userrights-no-interwiki' );
00330                 }
00331                 if ( !UserRightsProxy::validDatabase( $database ) ) {
00332                     return Status::newFatal( 'userrights-nodatabase', $database );
00333                 }
00334             }
00335         }
00336 
00337         if ( $name === '' ) {
00338             return Status::newFatal( 'nouserspecified' );
00339         }
00340 
00341         if ( $name[0] == '#' ) {
00342             // Numeric ID can be specified...
00343             // We'll do a lookup for the name internally.
00344             $id = intval( substr( $name, 1 ) );
00345 
00346             if ( $database == '' ) {
00347                 $name = User::whoIs( $id );
00348             } else {
00349                 $name = UserRightsProxy::whoIs( $database, $id );
00350             }
00351 
00352             if ( !$name ) {
00353                 return Status::newFatal( 'noname' );
00354             }
00355         } else {
00356             $name = User::getCanonicalName( $name );
00357             if ( $name === false ) {
00358                 // invalid name
00359                 return Status::newFatal( 'nosuchusershort', $username );
00360             }
00361         }
00362 
00363         if ( $database == '' ) {
00364             $user = User::newFromName( $name );
00365         } else {
00366             $user = UserRightsProxy::newFromName( $database, $name );
00367         }
00368 
00369         if ( !$user || $user->isAnon() ) {
00370             return Status::newFatal( 'nosuchusershort', $username );
00371         }
00372 
00373         return Status::newGood( $user );
00374     }
00375 
00376     function makeGroupNameList( $ids ) {
00377         if ( empty( $ids ) ) {
00378             return $this->msg( 'rightsnone' )->inContentLanguage()->text();
00379         } else {
00380             return implode( ', ', $ids );
00381         }
00382     }
00383 
00391     function makeGroupNameListForLog( $ids ) {
00392         wfDeprecated( __METHOD__, '1.21' );
00393 
00394         if ( empty( $ids ) ) {
00395             return '';
00396         } else {
00397             return $this->makeGroupNameList( $ids );
00398         }
00399     }
00400 
00404     function switchForm() {
00405         global $wgScript;
00406         $this->getOutput()->addHTML(
00407             Html::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'name' => 'uluser', 'id' => 'mw-userrights-form1' ) ) .
00408             Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
00409             Xml::fieldset( $this->msg( 'userrights-lookup-user' )->text() ) .
00410             Xml::inputLabel( $this->msg( 'userrights-user-editname' )->text(), 'user', 'username', 30, str_replace( '_', ' ', $this->mTarget ), array( 'autofocus' => true ) ) . ' ' .
00411             Xml::submitButton( $this->msg( 'editusergroup' )->text() ) .
00412             Html::closeElement( 'fieldset' ) .
00413             Html::closeElement( 'form' ) . "\n"
00414         );
00415     }
00416 
00425     protected function splitGroups( $groups ) {
00426         list( $addable, $removable, $addself, $removeself ) = array_values( $this->changeableGroups() );
00427 
00428         $removable = array_intersect(
00429             array_merge( $this->isself ? $removeself : array(), $removable ),
00430             $groups
00431         ); // Can't remove groups the user doesn't have
00432         $addable = array_diff(
00433             array_merge( $this->isself ? $addself : array(), $addable ),
00434             $groups
00435         ); // Can't add groups the user does have
00436 
00437         return array( $addable, $removable );
00438     }
00439 
00446     protected function showEditUserGroupsForm( $user, $groups ) {
00447         $list = array();
00448         $membersList = array();
00449         foreach ( $groups as $group ) {
00450             $list[] = self::buildGroupLink( $group );
00451             $membersList[] = self::buildGroupMemberLink( $group );
00452         }
00453 
00454         $autoList = array();
00455         $autoMembersList = array();
00456         if ( $user instanceof User ) {
00457             foreach ( Autopromote::getAutopromoteGroups( $user ) as $group ) {
00458                 $autoList[] = self::buildGroupLink( $group );
00459                 $autoMembersList[] = self::buildGroupMemberLink( $group );
00460             }
00461         }
00462 
00463         $language = $this->getLanguage();
00464         $displayedList = $this->msg( 'userrights-groupsmember-type',
00465             $language->listToText( $list ),
00466             $language->listToText( $membersList )
00467         )->plain();
00468         $displayedAutolist = $this->msg( 'userrights-groupsmember-type',
00469             $language->listToText( $autoList ),
00470             $language->listToText( $autoMembersList )
00471         )->plain();
00472 
00473         $grouplist = '';
00474         $count = count( $list );
00475         if ( $count > 0 ) {
00476             $grouplist = $this->msg( 'userrights-groupsmember', $count, $user->getName() )->parse();
00477             $grouplist = '<p>' . $grouplist . ' ' . $displayedList . "</p>\n";
00478         }
00479         $count = count( $autoList );
00480         if ( $count > 0 ) {
00481             $autogrouplistintro = $this->msg( 'userrights-groupsmember-auto', $count, $user->getName() )->parse();
00482             $grouplist .= '<p>' . $autogrouplistintro . ' ' . $displayedAutolist . "</p>\n";
00483         }
00484 
00485         $userToolLinks = Linker::userToolLinks(
00486                 $user->getId(),
00487                 $user->getName(),
00488                 false, /* default for redContribsWhenNoEdits */
00489                 Linker::TOOL_LINKS_EMAIL /* Add "send e-mail" link */
00490         );
00491 
00492         $this->getOutput()->addHTML(
00493             Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL(), 'name' => 'editGroup', 'id' => 'mw-userrights-form2' ) ) .
00494             Html::hidden( 'user', $this->mTarget ) .
00495             Html::hidden( 'wpEditToken', $this->getUser()->getEditToken( $this->mTarget ) ) .
00496             Html::hidden( 'conflictcheck-originalgroups', implode( ',', $user->getGroups() ) ) . // Conflict detection
00497             Xml::openElement( 'fieldset' ) .
00498             Xml::element( 'legend', array(), $this->msg( 'userrights-editusergroup', $user->getName() )->text() ) .
00499             $this->msg( 'editinguser' )->params( wfEscapeWikiText( $user->getName() ) )->rawParams( $userToolLinks )->parse() .
00500             $this->msg( 'userrights-groups-help', $user->getName() )->parse() .
00501             $grouplist .
00502             Xml::tags( 'p', null, $this->groupCheckboxes( $groups, $user ) ) .
00503             Xml::openElement( 'table', array( 'id' => 'mw-userrights-table-outer' ) ) .
00504                 "<tr>
00505                     <td class='mw-label'>" .
00506                         Xml::label( $this->msg( 'userrights-reason' )->text(), 'wpReason' ) .
00507                     "</td>
00508                     <td class='mw-input'>" .
00509                         Xml::input( 'user-reason', 60, $this->getRequest()->getVal( 'user-reason', false ),
00510                             array( 'id' => 'wpReason', 'maxlength' => 255 ) ) .
00511                     "</td>
00512                 </tr>
00513                 <tr>
00514                     <td></td>
00515                     <td class='mw-submit'>" .
00516                         Xml::submitButton( $this->msg( 'saveusergroups' )->text(),
00517                             array( 'name' => 'saveusergroups' ) + Linker::tooltipAndAccesskeyAttribs( 'userrights-set' ) ) .
00518                     "</td>
00519                 </tr>" .
00520             Xml::closeElement( 'table' ) . "\n" .
00521             Xml::closeElement( 'fieldset' ) .
00522             Xml::closeElement( 'form' ) . "\n"
00523         );
00524     }
00525 
00532     private static function buildGroupLink( $group ) {
00533         return User::makeGroupLinkHtml( $group, User::getGroupName( $group ) );
00534     }
00535 
00542     private static function buildGroupMemberLink( $group ) {
00543         return User::makeGroupLinkHtml( $group, User::getGroupMember( $group ) );
00544     }
00545 
00550     protected static function getAllGroups() {
00551         return User::getAllGroups();
00552     }
00553 
00562     private function groupCheckboxes( $usergroups, $user ) {
00563         $allgroups = $this->getAllGroups();
00564         $ret = '';
00565 
00566         // Put all column info into an associative array so that extensions can
00567         // more easily manage it.
00568         $columns = array( 'unchangeable' => array(), 'changeable' => array() );
00569 
00570         foreach ( $allgroups as $group ) {
00571             $set = in_array( $group, $usergroups );
00572             // Should the checkbox be disabled?
00573             $disabled = !(
00574                 ( $set && $this->canRemove( $group ) ) ||
00575                 ( !$set && $this->canAdd( $group ) ) );
00576             // Do we need to point out that this action is irreversible?
00577             $irreversible = !$disabled && (
00578                 ( $set && !$this->canAdd( $group ) ) ||
00579                 ( !$set && !$this->canRemove( $group ) ) );
00580 
00581             $checkbox = array(
00582                 'set' => $set,
00583                 'disabled' => $disabled,
00584                 'irreversible' => $irreversible
00585             );
00586 
00587             if ( $disabled ) {
00588                 $columns['unchangeable'][$group] = $checkbox;
00589             } else {
00590                 $columns['changeable'][$group] = $checkbox;
00591             }
00592         }
00593 
00594         // Build the HTML table
00595         $ret .= Xml::openElement( 'table', array( 'class' => 'mw-userrights-groups' ) ) .
00596             "<tr>\n";
00597         foreach ( $columns as $name => $column ) {
00598             if ( $column === array() ) {
00599                 continue;
00600             }
00601             // Messages: userrights-changeable-col, userrights-unchangeable-col
00602             $ret .= Xml::element( 'th', null, $this->msg( 'userrights-' . $name . '-col', count( $column ) )->text() );
00603         }
00604         $ret .= "</tr>\n<tr>\n";
00605         foreach ( $columns as $column ) {
00606             if ( $column === array() ) {
00607                 continue;
00608             }
00609             $ret .= "\t<td style='vertical-align:top;'>\n";
00610             foreach ( $column as $group => $checkbox ) {
00611                 $attr = $checkbox['disabled'] ? array( 'disabled' => 'disabled' ) : array();
00612 
00613                 $member = User::getGroupMember( $group, $user->getName() );
00614                 if ( $checkbox['irreversible'] ) {
00615                     $text = $this->msg( 'userrights-irreversible-marker', $member )->escaped();
00616                 } else {
00617                     $text = htmlspecialchars( $member );
00618                 }
00619                 $checkboxHtml = Xml::checkLabel( $text, "wpGroup-" . $group,
00620                     "wpGroup-" . $group, $checkbox['set'], $attr );
00621                 $ret .= "\t\t" . ( $checkbox['disabled']
00622                     ? Xml::tags( 'span', array( 'class' => 'mw-userrights-disabled' ), $checkboxHtml )
00623                     : $checkboxHtml
00624                 ) . "<br />\n";
00625             }
00626             $ret .= "\t</td>\n";
00627         }
00628         $ret .= Xml::closeElement( 'tr' ) . Xml::closeElement( 'table' );
00629 
00630         return $ret;
00631     }
00632 
00637     private function canRemove( $group ) {
00638         // $this->changeableGroups()['remove'] doesn't work, of course. Thanks, PHP.
00639         $groups = $this->changeableGroups();
00640         return in_array( $group, $groups['remove'] ) || ( $this->isself && in_array( $group, $groups['remove-self'] ) );
00641     }
00642 
00647     private function canAdd( $group ) {
00648         $groups = $this->changeableGroups();
00649         return in_array( $group, $groups['add'] ) || ( $this->isself && in_array( $group, $groups['add-self'] ) );
00650     }
00651 
00657     function changeableGroups() {
00658         return $this->getUser()->changeableGroups();
00659     }
00660 
00667     protected function showLogFragment( $user, $output ) {
00668         $rightsLogPage = new LogPage( 'rights' );
00669         $output->addHTML( Xml::element( 'h2', null, $rightsLogPage->getName()->text() ) );
00670         LogEventsList::showLogExtract( $output, 'rights', $user->getUserPage() );
00671     }
00672 
00673     protected function getGroupName() {
00674         return 'users';
00675     }
00676 }