MediaWiki  REL1_24
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 
00059         return !empty( $available['add'] )
00060             || !empty( $available['remove'] )
00061             || ( ( $this->isself || !$checkIfSelf ) &&
00062                 ( !empty( $available['add-self'] )
00063                     || !empty( $available['remove-self'] ) ) );
00064     }
00065 
00073     public function execute( $par ) {
00074         // If the visitor doesn't have permissions to assign or remove
00075         // any groups, it's a bit silly to give them the user search prompt.
00076 
00077         $user = $this->getUser();
00078 
00079         /*
00080          * If the user is blocked and they only have "partial" access
00081          * (e.g. they don't have the userrights permission), then don't
00082          * allow them to use Special:UserRights.
00083          */
00084         if ( $user->isBlocked() && !$user->isAllowed( 'userrights' ) ) {
00085             throw new UserBlockedError( $user->getBlock() );
00086         }
00087 
00088         $request = $this->getRequest();
00089 
00090         if ( $par !== null ) {
00091             $this->mTarget = $par;
00092         } else {
00093             $this->mTarget = $request->getVal( 'user' );
00094         }
00095 
00096         $available = $this->changeableGroups();
00097 
00098         if ( $this->mTarget === null ) {
00099             /*
00100              * If the user specified no target, and they can only
00101              * edit their own groups, automatically set them as the
00102              * target.
00103              */
00104             if ( !count( $available['add'] ) && !count( $available['remove'] ) ) {
00105                 $this->mTarget = $user->getName();
00106             }
00107         }
00108 
00109         if ( User::getCanonicalName( $this->mTarget ) == $user->getName() ) {
00110             $this->isself = true;
00111         }
00112 
00113         if ( !$this->userCanChangeRights( $user, true ) ) {
00114             if ( $this->isself && $request->getCheck( 'success' ) ) {
00115                 // bug 48609: if the user just removed its own rights, this would
00116                 // leads it in a "permissions error" page. In that case, show a
00117                 // message that it can't anymore use this page instead of an error
00118                 $this->setHeaders();
00119                 $out = $this->getOutput();
00120                 $out->wrapWikiMsg( "<div class=\"successbox\">\n$1\n</div>", 'userrights-removed-self' );
00121                 $out->returnToMain();
00122 
00123                 return;
00124             }
00125 
00126             // @todo FIXME: There may be intermediate groups we can mention.
00127             $msg = $user->isAnon() ? 'userrights-nologin' : 'userrights-notallowed';
00128             throw new PermissionsError( null, array( array( $msg ) ) );
00129         }
00130 
00131         $this->checkReadOnly();
00132 
00133         $this->setHeaders();
00134         $this->outputHeader();
00135 
00136         $out = $this->getOutput();
00137         $out->addModuleStyles( 'mediawiki.special' );
00138 
00139         // show the general form
00140         if ( count( $available['add'] ) || count( $available['remove'] ) ) {
00141             $this->switchForm();
00142         }
00143 
00144         if (
00145             $request->wasPosted() &&
00146             $request->getCheck( 'saveusergroups' ) &&
00147             $user->matchEditToken( $request->getVal( 'wpEditToken' ), $this->mTarget )
00148         ) {
00149             // save settings
00150             $status = $this->fetchUser( $this->mTarget );
00151             if ( !$status->isOK() ) {
00152                 $this->getOutput()->addWikiText( $status->getWikiText() );
00153 
00154                 return;
00155             }
00156 
00157             $targetUser = $status->value;
00158             if ( $targetUser instanceof User ) { // UserRightsProxy doesn't have this method (bug 61252)
00159                 $targetUser->clearInstanceCache(); // bug 38989
00160             }
00161 
00162             if ( $request->getVal( 'conflictcheck-originalgroups' )
00163                 !== implode( ',', $targetUser->getGroups() )
00164             ) {
00165                 $out->addWikiMsg( 'userrights-conflict' );
00166             } else {
00167                 $this->saveUserGroups(
00168                     $this->mTarget,
00169                     $request->getVal( 'user-reason' ),
00170                     $targetUser
00171                 );
00172 
00173                 $out->redirect( $this->getSuccessURL() );
00174 
00175                 return;
00176             }
00177         }
00178 
00179         // show some more forms
00180         if ( $this->mTarget !== null ) {
00181             $this->editUserGroupsForm( $this->mTarget );
00182         }
00183     }
00184 
00185     function getSuccessURL() {
00186         return $this->getPageTitle( $this->mTarget )->getFullURL( array( 'success' => 1 ) );
00187     }
00188 
00198     function saveUserGroups( $username, $reason, $user ) {
00199         $allgroups = $this->getAllGroups();
00200         $addgroup = array();
00201         $removegroup = array();
00202 
00203         // This could possibly create a highly unlikely race condition if permissions are changed between
00204         //  when the form is loaded and when the form is saved. Ignoring it for the moment.
00205         foreach ( $allgroups as $group ) {
00206             // We'll tell it to remove all unchecked groups, and add all checked groups.
00207             // Later on, this gets filtered for what can actually be removed
00208             if ( $this->getRequest()->getCheck( "wpGroup-$group" ) ) {
00209                 $addgroup[] = $group;
00210             } else {
00211                 $removegroup[] = $group;
00212             }
00213         }
00214 
00215         $this->doSaveUserGroups( $user, $addgroup, $removegroup, $reason );
00216     }
00217 
00227     function doSaveUserGroups( $user, $add, $remove, $reason = '' ) {
00228         global $wgAuth;
00229 
00230         // Validate input set...
00231         $isself = ( $user->getName() == $this->getUser()->getName() );
00232         $groups = $user->getGroups();
00233         $changeable = $this->changeableGroups();
00234         $addable = array_merge( $changeable['add'], $isself ? $changeable['add-self'] : array() );
00235         $removable = array_merge( $changeable['remove'], $isself ? $changeable['remove-self'] : array() );
00236 
00237         $remove = array_unique(
00238             array_intersect( (array)$remove, $removable, $groups ) );
00239         $add = array_unique( array_diff(
00240             array_intersect( (array)$add, $addable ),
00241             $groups )
00242         );
00243 
00244         $oldGroups = $user->getGroups();
00245         $newGroups = $oldGroups;
00246 
00247         // remove then add groups
00248         if ( $remove ) {
00249             $newGroups = array_diff( $newGroups, $remove );
00250             foreach ( $remove as $group ) {
00251                 $user->removeGroup( $group );
00252             }
00253         }
00254         if ( $add ) {
00255             $newGroups = array_merge( $newGroups, $add );
00256             foreach ( $add as $group ) {
00257                 $user->addGroup( $group );
00258             }
00259         }
00260         $newGroups = array_unique( $newGroups );
00261 
00262         // Ensure that caches are cleared
00263         $user->invalidateCache();
00264 
00265         // update groups in external authentication database
00266         $wgAuth->updateExternalDBGroups( $user, $add, $remove );
00267 
00268         wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) . "\n" );
00269         wfDebug( 'newGroups: ' . print_r( $newGroups, true ) . "\n" );
00270         wfRunHooks( 'UserRights', array( &$user, $add, $remove ) );
00271 
00272         if ( $newGroups != $oldGroups ) {
00273             $this->addLogEntry( $user, $oldGroups, $newGroups, $reason );
00274         }
00275 
00276         return array( $add, $remove );
00277     }
00278 
00286     function addLogEntry( $user, $oldGroups, $newGroups, $reason ) {
00287         $logEntry = new ManualLogEntry( 'rights', 'rights' );
00288         $logEntry->setPerformer( $this->getUser() );
00289         $logEntry->setTarget( $user->getUserPage() );
00290         $logEntry->setComment( $reason );
00291         $logEntry->setParameters( array(
00292             '4::oldgroups' => $oldGroups,
00293             '5::newgroups' => $newGroups,
00294         ) );
00295         $logid = $logEntry->insert();
00296         $logEntry->publish( $logid );
00297     }
00298 
00303     function editUserGroupsForm( $username ) {
00304         $status = $this->fetchUser( $username );
00305         if ( !$status->isOK() ) {
00306             $this->getOutput()->addWikiText( $status->getWikiText() );
00307 
00308             return;
00309         } else {
00310             $user = $status->value;
00311         }
00312 
00313         $groups = $user->getGroups();
00314 
00315         $this->showEditUserGroupsForm( $user, $groups );
00316 
00317         // This isn't really ideal logging behavior, but let's not hide the
00318         // interwiki logs if we're using them as is.
00319         $this->showLogFragment( $user, $this->getOutput() );
00320     }
00321 
00330     public function fetchUser( $username ) {
00331         $parts = explode( $this->getConfig()->get( 'UserrightsInterwikiDelimiter' ), $username );
00332         if ( count( $parts ) < 2 ) {
00333             $name = trim( $username );
00334             $database = '';
00335         } else {
00336             list( $name, $database ) = array_map( 'trim', $parts );
00337 
00338             if ( $database == wfWikiID() ) {
00339                 $database = '';
00340             } else {
00341                 if ( !$this->getUser()->isAllowed( 'userrights-interwiki' ) ) {
00342                     return Status::newFatal( 'userrights-no-interwiki' );
00343                 }
00344                 if ( !UserRightsProxy::validDatabase( $database ) ) {
00345                     return Status::newFatal( 'userrights-nodatabase', $database );
00346                 }
00347             }
00348         }
00349 
00350         if ( $name === '' ) {
00351             return Status::newFatal( 'nouserspecified' );
00352         }
00353 
00354         if ( $name[0] == '#' ) {
00355             // Numeric ID can be specified...
00356             // We'll do a lookup for the name internally.
00357             $id = intval( substr( $name, 1 ) );
00358 
00359             if ( $database == '' ) {
00360                 $name = User::whoIs( $id );
00361             } else {
00362                 $name = UserRightsProxy::whoIs( $database, $id );
00363             }
00364 
00365             if ( !$name ) {
00366                 return Status::newFatal( 'noname' );
00367             }
00368         } else {
00369             $name = User::getCanonicalName( $name );
00370             if ( $name === false ) {
00371                 // invalid name
00372                 return Status::newFatal( 'nosuchusershort', $username );
00373             }
00374         }
00375 
00376         if ( $database == '' ) {
00377             $user = User::newFromName( $name );
00378         } else {
00379             $user = UserRightsProxy::newFromName( $database, $name );
00380         }
00381 
00382         if ( !$user || $user->isAnon() ) {
00383             return Status::newFatal( 'nosuchusershort', $username );
00384         }
00385 
00386         return Status::newGood( $user );
00387     }
00388 
00389     function makeGroupNameList( $ids ) {
00390         if ( empty( $ids ) ) {
00391             return $this->msg( 'rightsnone' )->inContentLanguage()->text();
00392         } else {
00393             return implode( ', ', $ids );
00394         }
00395     }
00396 
00404     function makeGroupNameListForLog( $ids ) {
00405         wfDeprecated( __METHOD__, '1.21' );
00406 
00407         if ( empty( $ids ) ) {
00408             return '';
00409         } else {
00410             return $this->makeGroupNameList( $ids );
00411         }
00412     }
00413 
00417     function switchForm() {
00418         $this->getOutput()->addHTML(
00419             Html::openElement(
00420                 'form',
00421                 array(
00422                     'method' => 'get',
00423                     'action' => wfScript(),
00424                     'name' => 'uluser',
00425                     'id' => 'mw-userrights-form1'
00426                 )
00427             ) .
00428             Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() ) .
00429             Xml::fieldset( $this->msg( 'userrights-lookup-user' )->text() ) .
00430             Xml::inputLabel(
00431                 $this->msg( 'userrights-user-editname' )->text(),
00432                 'user',
00433                 'username',
00434                 30,
00435                 str_replace( '_', ' ', $this->mTarget ),
00436                 array( 'autofocus' => true )
00437             ) . ' ' .
00438             Xml::submitButton( $this->msg( 'editusergroup' )->text() ) .
00439             Html::closeElement( 'fieldset' ) .
00440             Html::closeElement( 'form' ) . "\n"
00441         );
00442     }
00443 
00452     protected function splitGroups( $groups ) {
00453         list( $addable, $removable, $addself, $removeself ) = array_values( $this->changeableGroups() );
00454 
00455         $removable = array_intersect(
00456             array_merge( $this->isself ? $removeself : array(), $removable ),
00457             $groups
00458         ); // Can't remove groups the user doesn't have
00459         $addable = array_diff(
00460             array_merge( $this->isself ? $addself : array(), $addable ),
00461             $groups
00462         ); // Can't add groups the user does have
00463 
00464         return array( $addable, $removable );
00465     }
00466 
00473     protected function showEditUserGroupsForm( $user, $groups ) {
00474         $list = array();
00475         $membersList = array();
00476         foreach ( $groups as $group ) {
00477             $list[] = self::buildGroupLink( $group );
00478             $membersList[] = self::buildGroupMemberLink( $group );
00479         }
00480 
00481         $autoList = array();
00482         $autoMembersList = array();
00483         if ( $user instanceof User ) {
00484             foreach ( Autopromote::getAutopromoteGroups( $user ) as $group ) {
00485                 $autoList[] = self::buildGroupLink( $group );
00486                 $autoMembersList[] = self::buildGroupMemberLink( $group );
00487             }
00488         }
00489 
00490         $language = $this->getLanguage();
00491         $displayedList = $this->msg( 'userrights-groupsmember-type',
00492             $language->listToText( $list ),
00493             $language->listToText( $membersList )
00494         )->plain();
00495         $displayedAutolist = $this->msg( 'userrights-groupsmember-type',
00496             $language->listToText( $autoList ),
00497             $language->listToText( $autoMembersList )
00498         )->plain();
00499 
00500         $grouplist = '';
00501         $count = count( $list );
00502         if ( $count > 0 ) {
00503             $grouplist = $this->msg( 'userrights-groupsmember', $count, $user->getName() )->parse();
00504             $grouplist = '<p>' . $grouplist . ' ' . $displayedList . "</p>\n";
00505         }
00506 
00507         $count = count( $autoList );
00508         if ( $count > 0 ) {
00509             $autogrouplistintro = $this->msg( 'userrights-groupsmember-auto', $count, $user->getName() )
00510                 ->parse();
00511             $grouplist .= '<p>' . $autogrouplistintro . ' ' . $displayedAutolist . "</p>\n";
00512         }
00513 
00514         $userToolLinks = Linker::userToolLinks(
00515             $user->getId(),
00516             $user->getName(),
00517             false, /* default for redContribsWhenNoEdits */
00518             Linker::TOOL_LINKS_EMAIL /* Add "send e-mail" link */
00519         );
00520 
00521         $this->getOutput()->addHTML(
00522             Xml::openElement(
00523                 'form',
00524                 array(
00525                     'method' => 'post',
00526                     'action' => $this->getPageTitle()->getLocalURL(),
00527                     'name' => 'editGroup',
00528                     'id' => 'mw-userrights-form2'
00529                 )
00530             ) .
00531             Html::hidden( 'user', $this->mTarget ) .
00532             Html::hidden( 'wpEditToken', $this->getUser()->getEditToken( $this->mTarget ) ) .
00533             Html::hidden(
00534                 'conflictcheck-originalgroups',
00535                 implode( ',', $user->getGroups() )
00536             ) . // Conflict detection
00537             Xml::openElement( 'fieldset' ) .
00538             Xml::element(
00539                 'legend',
00540                 array(),
00541                 $this->msg( 'userrights-editusergroup', $user->getName() )->text()
00542             ) .
00543             $this->msg( 'editinguser' )->params( wfEscapeWikiText( $user->getName() ) )
00544                 ->rawParams( $userToolLinks )->parse() .
00545             $this->msg( 'userrights-groups-help', $user->getName() )->parse() .
00546             $grouplist .
00547             $this->groupCheckboxes( $groups, $user ) .
00548             Xml::openElement( 'table', array( 'id' => 'mw-userrights-table-outer' ) ) .
00549                 "<tr>
00550                     <td class='mw-label'>" .
00551                         Xml::label( $this->msg( 'userrights-reason' )->text(), 'wpReason' ) .
00552                     "</td>
00553                     <td class='mw-input'>" .
00554                         Xml::input( 'user-reason', 60, $this->getRequest()->getVal( 'user-reason', false ),
00555                             array( 'id' => 'wpReason', 'maxlength' => 255 ) ) .
00556                     "</td>
00557                 </tr>
00558                 <tr>
00559                     <td></td>
00560                     <td class='mw-submit'>" .
00561                         Xml::submitButton( $this->msg( 'saveusergroups' )->text(),
00562                             array( 'name' => 'saveusergroups' ) +
00563                                 Linker::tooltipAndAccesskeyAttribs( 'userrights-set' )
00564                         ) .
00565                     "</td>
00566                 </tr>" .
00567             Xml::closeElement( 'table' ) . "\n" .
00568             Xml::closeElement( 'fieldset' ) .
00569             Xml::closeElement( 'form' ) . "\n"
00570         );
00571     }
00572 
00579     private static function buildGroupLink( $group ) {
00580         return User::makeGroupLinkHtml( $group, User::getGroupName( $group ) );
00581     }
00582 
00589     private static function buildGroupMemberLink( $group ) {
00590         return User::makeGroupLinkHtml( $group, User::getGroupMember( $group ) );
00591     }
00592 
00597     protected static function getAllGroups() {
00598         return User::getAllGroups();
00599     }
00600 
00609     private function groupCheckboxes( $usergroups, $user ) {
00610         $allgroups = $this->getAllGroups();
00611         $ret = '';
00612 
00613         // Put all column info into an associative array so that extensions can
00614         // more easily manage it.
00615         $columns = array( 'unchangeable' => array(), 'changeable' => array() );
00616 
00617         foreach ( $allgroups as $group ) {
00618             $set = in_array( $group, $usergroups );
00619             // Should the checkbox be disabled?
00620             $disabled = !(
00621                 ( $set && $this->canRemove( $group ) ) ||
00622                 ( !$set && $this->canAdd( $group ) ) );
00623             // Do we need to point out that this action is irreversible?
00624             $irreversible = !$disabled && (
00625                 ( $set && !$this->canAdd( $group ) ) ||
00626                 ( !$set && !$this->canRemove( $group ) ) );
00627 
00628             $checkbox = array(
00629                 'set' => $set,
00630                 'disabled' => $disabled,
00631                 'irreversible' => $irreversible
00632             );
00633 
00634             if ( $disabled ) {
00635                 $columns['unchangeable'][$group] = $checkbox;
00636             } else {
00637                 $columns['changeable'][$group] = $checkbox;
00638             }
00639         }
00640 
00641         // Build the HTML table
00642         $ret .= Xml::openElement( 'table', array( 'class' => 'mw-userrights-groups' ) ) .
00643             "<tr>\n";
00644         foreach ( $columns as $name => $column ) {
00645             if ( $column === array() ) {
00646                 continue;
00647             }
00648             // Messages: userrights-changeable-col, userrights-unchangeable-col
00649             $ret .= Xml::element(
00650                 'th',
00651                 null,
00652                 $this->msg( 'userrights-' . $name . '-col', count( $column ) )->text()
00653             );
00654         }
00655 
00656         $ret .= "</tr>\n<tr>\n";
00657         foreach ( $columns as $column ) {
00658             if ( $column === array() ) {
00659                 continue;
00660             }
00661             $ret .= "\t<td style='vertical-align:top;'>\n";
00662             foreach ( $column as $group => $checkbox ) {
00663                 $attr = $checkbox['disabled'] ? array( 'disabled' => 'disabled' ) : array();
00664 
00665                 $member = User::getGroupMember( $group, $user->getName() );
00666                 if ( $checkbox['irreversible'] ) {
00667                     $text = $this->msg( 'userrights-irreversible-marker', $member )->escaped();
00668                 } else {
00669                     $text = htmlspecialchars( $member );
00670                 }
00671                 $checkboxHtml = Xml::checkLabel( $text, "wpGroup-" . $group,
00672                     "wpGroup-" . $group, $checkbox['set'], $attr );
00673                 $ret .= "\t\t" . ( $checkbox['disabled']
00674                     ? Xml::tags( 'span', array( 'class' => 'mw-userrights-disabled' ), $checkboxHtml )
00675                     : $checkboxHtml
00676                 ) . "<br />\n";
00677             }
00678             $ret .= "\t</td>\n";
00679         }
00680         $ret .= Xml::closeElement( 'tr' ) . Xml::closeElement( 'table' );
00681 
00682         return $ret;
00683     }
00684 
00689     private function canRemove( $group ) {
00690         // $this->changeableGroups()['remove'] doesn't work, of course. Thanks, PHP.
00691         $groups = $this->changeableGroups();
00692 
00693         return in_array(
00694             $group,
00695             $groups['remove'] ) || ( $this->isself && in_array( $group, $groups['remove-self'] )
00696         );
00697     }
00698 
00703     private function canAdd( $group ) {
00704         $groups = $this->changeableGroups();
00705 
00706         return in_array(
00707             $group,
00708             $groups['add'] ) || ( $this->isself && in_array( $group, $groups['add-self'] )
00709         );
00710     }
00711 
00722     function changeableGroups() {
00723         return $this->getUser()->changeableGroups();
00724     }
00725 
00732     protected function showLogFragment( $user, $output ) {
00733         $rightsLogPage = new LogPage( 'rights' );
00734         $output->addHTML( Xml::element( 'h2', null, $rightsLogPage->getName()->text() ) );
00735         LogEventsList::showLogExtract( $output, 'rights', $user->getUserPage() );
00736     }
00737 
00738     protected function getGroupName() {
00739         return 'users';
00740     }
00741 }