MediaWiki  REL1_20
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 
00048         public function userCanChangeRights( $user, $checkIfSelf = true ) {
00049                 $available = $this->changeableGroups();
00050                 if ( $user->getId() == 0 ) {
00051                         return false;
00052                 }
00053                 return !empty( $available['add'] )
00054                         || !empty( $available['remove'] )
00055                         || ( ( $this->isself || !$checkIfSelf ) &&
00056                                 ( !empty( $available['add-self'] )
00057                                  || !empty( $available['remove-self'] ) ) );
00058         }
00059 
00066         public function execute( $par ) {
00067                 // If the visitor doesn't have permissions to assign or remove
00068                 // any groups, it's a bit silly to give them the user search prompt.
00069 
00070                 $user = $this->getUser();
00071 
00072                 /*
00073                  * If the user is blocked and they only have "partial" access
00074                  * (e.g. they don't have the userrights permission), then don't
00075                  * allow them to use Special:UserRights.
00076                  */
00077                 if( $user->isBlocked() && !$user->isAllowed( 'userrights' ) ) {
00078                         throw new UserBlockedError( $user->getBlock() );
00079                 }
00080 
00081                 $request = $this->getRequest();
00082 
00083                 if( $par !== null ) {
00084                         $this->mTarget = $par;
00085                 } else {
00086                         $this->mTarget = $request->getVal( 'user' );
00087                 }
00088 
00089                 $available = $this->changeableGroups();
00090 
00091                 if ( $this->mTarget === null ) {
00092                         /*
00093                          * If the user specified no target, and they can only
00094                          * edit their own groups, automatically set them as the
00095                          * target.
00096                          */
00097                         if ( !count( $available['add'] ) && !count( $available['remove'] ) )
00098                                 $this->mTarget = $user->getName();
00099                 }
00100 
00101                 if ( User::getCanonicalName( $this->mTarget ) == $user->getName() ) {
00102                         $this->isself = true;
00103                 }
00104 
00105                 if( !$this->userCanChangeRights( $user, true ) ) {
00106                         // @todo FIXME: There may be intermediate groups we can mention.
00107                         $msg = $user->isAnon() ? 'userrights-nologin' : 'userrights-notallowed';
00108                         throw new PermissionsError( null, array( array( $msg ) ) );
00109                 }
00110 
00111                 $this->checkReadOnly();
00112 
00113                 $this->setHeaders();
00114                 $this->outputHeader();
00115 
00116                 $out = $this->getOutput();
00117                 $out->addModuleStyles( 'mediawiki.special' );
00118 
00119                 // show the general form
00120                 if ( count( $available['add'] ) || count( $available['remove'] ) ) {
00121                         $this->switchForm();
00122                 }
00123 
00124                 if( $request->wasPosted() ) {
00125                         // save settings
00126                         if( $request->getCheck( 'saveusergroups' ) ) {
00127                                 $reason = $request->getVal( 'user-reason' );
00128                                 $tok = $request->getVal( 'wpEditToken' );
00129                                 if( $user->matchEditToken( $tok, $this->mTarget ) ) {
00130                                         $this->saveUserGroups(
00131                                                 $this->mTarget,
00132                                                 $reason
00133                                         );
00134 
00135                                         $out->redirect( $this->getSuccessURL() );
00136                                         return;
00137                                 }
00138                         }
00139                 }
00140 
00141                 // show some more forms
00142                 if( $this->mTarget !== null ) {
00143                         $this->editUserGroupsForm( $this->mTarget );
00144                 }
00145         }
00146 
00147         function getSuccessURL() {
00148                 return $this->getTitle( $this->mTarget )->getFullURL();
00149         }
00150 
00159         function saveUserGroups( $username, $reason = '' ) {
00160                 $status = $this->fetchUser( $username );
00161                 if( !$status->isOK() ) {
00162                         $this->getOutput()->addWikiText( $status->getWikiText() );
00163                         return;
00164                 } else {
00165                         $user = $status->value;
00166                 }
00167 
00168                 $allgroups = $this->getAllGroups();
00169                 $addgroup = array();
00170                 $removegroup = array();
00171 
00172                 // This could possibly create a highly unlikely race condition if permissions are changed between
00173                 //  when the form is loaded and when the form is saved. Ignoring it for the moment.
00174                 foreach ( $allgroups as $group ) {
00175                         // We'll tell it to remove all unchecked groups, and add all checked groups.
00176                         // Later on, this gets filtered for what can actually be removed
00177                         if ( $this->getRequest()->getCheck( "wpGroup-$group" ) ) {
00178                                 $addgroup[] = $group;
00179                         } else {
00180                                 $removegroup[] = $group;
00181                         }
00182                 }
00183 
00184                 $this->doSaveUserGroups( $user, $addgroup, $removegroup, $reason );
00185         }
00186 
00196         function doSaveUserGroups( $user, $add, $remove, $reason = '' ) {
00197                 // Validate input set...
00198                 $isself = ( $user->getName() == $this->getUser()->getName() );
00199                 $groups = $user->getGroups();
00200                 $changeable = $this->changeableGroups();
00201                 $addable = array_merge( $changeable['add'], $isself ? $changeable['add-self'] : array() );
00202                 $removable = array_merge( $changeable['remove'], $isself ? $changeable['remove-self'] : array() );
00203 
00204                 $remove = array_unique(
00205                         array_intersect( (array)$remove, $removable, $groups ) );
00206                 $add = array_unique( array_diff(
00207                         array_intersect( (array)$add, $addable ),
00208                         $groups )
00209                 );
00210 
00211                 $oldGroups = $user->getGroups();
00212                 $newGroups = $oldGroups;
00213 
00214                 // remove then add groups
00215                 if( $remove ) {
00216                         $newGroups = array_diff( $newGroups, $remove );
00217                         foreach( $remove as $group ) {
00218                                 $user->removeGroup( $group );
00219                         }
00220                 }
00221                 if( $add ) {
00222                         $newGroups = array_merge( $newGroups, $add );
00223                         foreach( $add as $group ) {
00224                                 $user->addGroup( $group );
00225                         }
00226                 }
00227                 $newGroups = array_unique( $newGroups );
00228 
00229                 // Ensure that caches are cleared
00230                 $user->invalidateCache();
00231 
00232                 wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) );
00233                 wfDebug( 'newGroups: ' . print_r( $newGroups, true ) );
00234                 wfRunHooks( 'UserRights', array( &$user, $add, $remove ) );
00235 
00236                 if( $newGroups != $oldGroups ) {
00237                         $this->addLogEntry( $user, $oldGroups, $newGroups, $reason );
00238                 }
00239                 return array( $add, $remove );
00240         }
00241 
00242 
00246         function addLogEntry( $user, $oldGroups, $newGroups, $reason ) {
00247                 $log = new LogPage( 'rights' );
00248 
00249                 $log->addEntry( 'rights',
00250                         $user->getUserPage(),
00251                         $reason,
00252                         array(
00253                                 $this->makeGroupNameListForLog( $oldGroups ),
00254                                 $this->makeGroupNameListForLog( $newGroups )
00255                         )
00256                 );
00257         }
00258 
00263         function editUserGroupsForm( $username ) {
00264                 $status = $this->fetchUser( $username );
00265                 if( !$status->isOK() ) {
00266                         $this->getOutput()->addWikiText( $status->getWikiText() );
00267                         return;
00268                 } else {
00269                         $user = $status->value;
00270                 }
00271 
00272                 $groups = $user->getGroups();
00273 
00274                 $this->showEditUserGroupsForm( $user, $groups );
00275 
00276                 // This isn't really ideal logging behavior, but let's not hide the
00277                 // interwiki logs if we're using them as is.
00278                 $this->showLogFragment( $user, $this->getOutput() );
00279         }
00280 
00288         public function fetchUser( $username ) {
00289                 global $wgUserrightsInterwikiDelimiter;
00290 
00291                 $parts = explode( $wgUserrightsInterwikiDelimiter, $username );
00292                 if( count( $parts ) < 2 ) {
00293                         $name = trim( $username );
00294                         $database = '';
00295                 } else {
00296                         list( $name, $database ) = array_map( 'trim', $parts );
00297 
00298                         if( $database == wfWikiID() ) {
00299                                 $database = '';
00300                         } else {
00301                                 if( !$this->getUser()->isAllowed( 'userrights-interwiki' ) ) {
00302                                         return Status::newFatal( 'userrights-no-interwiki' );
00303                                 }
00304                                 if( !UserRightsProxy::validDatabase( $database ) ) {
00305                                         return Status::newFatal( 'userrights-nodatabase', $database );
00306                                 }
00307                         }
00308                 }
00309 
00310                 if( $name === '' ) {
00311                         return Status::newFatal( 'nouserspecified' );
00312                 }
00313 
00314                 if( $name[0] == '#' ) {
00315                         // Numeric ID can be specified...
00316                         // We'll do a lookup for the name internally.
00317                         $id = intval( substr( $name, 1 ) );
00318 
00319                         if( $database == '' ) {
00320                                 $name = User::whoIs( $id );
00321                         } else {
00322                                 $name = UserRightsProxy::whoIs( $database, $id );
00323                         }
00324 
00325                         if( !$name ) {
00326                                 return Status::newFatal( 'noname' );
00327                         }
00328                 } else {
00329                         $name = User::getCanonicalName( $name );
00330                         if( $name === false ) {
00331                                 // invalid name
00332                                 return Status::newFatal( 'nosuchusershort', $username );
00333                         }
00334                 }
00335 
00336                 if( $database == '' ) {
00337                         $user = User::newFromName( $name );
00338                 } else {
00339                         $user = UserRightsProxy::newFromName( $database, $name );
00340                 }
00341 
00342                 if( !$user || $user->isAnon() ) {
00343                         return Status::newFatal( 'nosuchusershort', $username );
00344                 }
00345 
00346                 return Status::newGood( $user );
00347         }
00348 
00349         function makeGroupNameList( $ids ) {
00350                 if( empty( $ids ) ) {
00351                         return $this->msg( 'rightsnone' )->inContentLanguage()->text();
00352                 } else {
00353                         return implode( ', ', $ids );
00354                 }
00355         }
00356 
00357         function makeGroupNameListForLog( $ids ) {
00358                 if( empty( $ids ) ) {
00359                         return '';
00360                 } else {
00361                         return $this->makeGroupNameList( $ids );
00362                 }
00363         }
00364 
00368         function switchForm() {
00369                 global $wgScript;
00370                 $this->getOutput()->addHTML(
00371                         Html::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'name' => 'uluser', 'id' => 'mw-userrights-form1' ) ) .
00372                         Html::hidden( 'title',  $this->getTitle()->getPrefixedText() ) .
00373                         Xml::fieldset( $this->msg( 'userrights-lookup-user' )->text() ) .
00374                         Xml::inputLabel( $this->msg( 'userrights-user-editname' )->text(), 'user', 'username', 30, str_replace( '_', ' ', $this->mTarget ) ) . ' ' .
00375                         Xml::submitButton( $this->msg( 'editusergroup' )->text() ) .
00376                         Html::closeElement( 'fieldset' ) .
00377                         Html::closeElement( 'form' ) . "\n"
00378                 );
00379         }
00380 
00389         protected function splitGroups( $groups ) {
00390                 list( $addable, $removable, $addself, $removeself ) = array_values( $this->changeableGroups() );
00391 
00392                 $removable = array_intersect(
00393                         array_merge( $this->isself ? $removeself : array(), $removable ),
00394                         $groups
00395                 ); // Can't remove groups the user doesn't have
00396                 $addable = array_diff(
00397                         array_merge( $this->isself ? $addself : array(), $addable ),
00398                         $groups
00399                 ); // Can't add groups the user does have
00400 
00401                 return array( $addable, $removable );
00402         }
00403 
00410         protected function showEditUserGroupsForm( $user, $groups ) {
00411                 $list = array();
00412                 foreach( $groups as $group ) {
00413                         $list[] = self::buildGroupLink( $group );
00414                 }
00415 
00416                 $autolist = array();
00417                 if ( $user instanceof User ) {
00418                         foreach( Autopromote::getAutopromoteGroups( $user ) as $group ) {
00419                                 $autolist[] = self::buildGroupLink( $group );
00420                         }
00421                 }
00422 
00423                 $grouplist = '';
00424                 $count = count( $list );
00425                 if( $count > 0 ) {
00426                         $grouplist = $this->msg( 'userrights-groupsmember', $count, $user->getName() )->parse();
00427                         $grouplist = '<p>' . $grouplist  . ' ' . $this->getLanguage()->listToText( $list ) . "</p>\n";
00428                 }
00429                 $count = count( $autolist );
00430                 if( $count > 0 ) {
00431                         $autogrouplistintro = $this->msg( 'userrights-groupsmember-auto', $count, $user->getName() )->parse();
00432                         $grouplist .= '<p>' . $autogrouplistintro  . ' ' . $this->getLanguage()->listToText( $autolist ) . "</p>\n";
00433                 }
00434 
00435                 $userToolLinks = Linker::userToolLinks(
00436                                 $user->getId(),
00437                                 $user->getName(),
00438                                 false, /* default for redContribsWhenNoEdits */
00439                                 Linker::TOOL_LINKS_EMAIL /* Add "send e-mail" link */
00440                 );
00441 
00442                 $this->getOutput()->addHTML(
00443                         Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL(), 'name' => 'editGroup', 'id' => 'mw-userrights-form2' ) ) .
00444                         Html::hidden( 'user', $this->mTarget ) .
00445                         Html::hidden( 'wpEditToken', $this->getUser()->getEditToken( $this->mTarget ) ) .
00446                         Xml::openElement( 'fieldset' ) .
00447                         Xml::element( 'legend', array(), $this->msg( 'userrights-editusergroup', $user->getName() )->text() ) .
00448                         $this->msg( 'editinguser' )->params( wfEscapeWikiText( $user->getName() ) )->rawParams( $userToolLinks )->parse() .
00449                         $this->msg( 'userrights-groups-help', $user->getName() )->parse() .
00450                         $grouplist .
00451                         Xml::tags( 'p', null, $this->groupCheckboxes( $groups, $user ) ) .
00452                         Xml::openElement( 'table', array( 'id' => 'mw-userrights-table-outer' ) ) .
00453                                 "<tr>
00454                                         <td class='mw-label'>" .
00455                                                 Xml::label( $this->msg( 'userrights-reason' )->text(), 'wpReason' ) .
00456                                         "</td>
00457                                         <td class='mw-input'>" .
00458                                                 Xml::input( 'user-reason', 60, $this->getRequest()->getVal( 'user-reason', false ),
00459                                                         array( 'id' => 'wpReason', 'maxlength' => 255 ) ) .
00460                                         "</td>
00461                                 </tr>
00462                                 <tr>
00463                                         <td></td>
00464                                         <td class='mw-submit'>" .
00465                                                 Xml::submitButton( $this->msg( 'saveusergroups' )->text(),
00466                                                         array( 'name' => 'saveusergroups' ) + Linker::tooltipAndAccesskeyAttribs( 'userrights-set' ) ) .
00467                                         "</td>
00468                                 </tr>" .
00469                         Xml::closeElement( 'table' ) . "\n" .
00470                         Xml::closeElement( 'fieldset' ) .
00471                         Xml::closeElement( 'form' ) . "\n"
00472                 );
00473         }
00474 
00481         private static function buildGroupLink( $group ) {
00482                 static $cache = array();
00483                 if( !isset( $cache[$group] ) )
00484                         $cache[$group] = User::makeGroupLinkHtml( $group, htmlspecialchars( User::getGroupName( $group ) ) );
00485                 return $cache[$group];
00486         }
00487 
00492         protected static function getAllGroups() {
00493                 return User::getAllGroups();
00494         }
00495 
00504         private function groupCheckboxes( $usergroups, $user ) {
00505                 $allgroups = $this->getAllGroups();
00506                 $ret = '';
00507 
00508                 # Put all column info into an associative array so that extensions can
00509                 # more easily manage it.
00510                 $columns = array( 'unchangeable' => array(), 'changeable' => array() );
00511 
00512                 foreach( $allgroups as $group ) {
00513                         $set = in_array( $group, $usergroups );
00514                         # Should the checkbox be disabled?
00515                         $disabled = !(
00516                                 ( $set && $this->canRemove( $group ) ) ||
00517                                 ( !$set && $this->canAdd( $group ) ) );
00518                         # Do we need to point out that this action is irreversible?
00519                         $irreversible = !$disabled && (
00520                                 ( $set && !$this->canAdd( $group ) ) ||
00521                                 ( !$set && !$this->canRemove( $group ) ) );
00522 
00523                         $checkbox = array(
00524                                 'set' => $set,
00525                                 'disabled' => $disabled,
00526                                 'irreversible' => $irreversible
00527                         );
00528 
00529                         if( $disabled ) {
00530                                 $columns['unchangeable'][$group] = $checkbox;
00531                         } else {
00532                                 $columns['changeable'][$group] = $checkbox;
00533                         }
00534                 }
00535 
00536                 # Build the HTML table
00537                 $ret .= Xml::openElement( 'table', array( 'class' => 'mw-userrights-groups' ) ) .
00538                         "<tr>\n";
00539                 foreach( $columns as $name => $column ) {
00540                         if( $column === array() )
00541                                 continue;
00542                         $ret .= Xml::element( 'th', null, $this->msg( 'userrights-' . $name . '-col', count( $column ) )->text() );
00543                 }
00544                 $ret.= "</tr>\n<tr>\n";
00545                 foreach( $columns as $column ) {
00546                         if( $column === array() )
00547                                 continue;
00548                         $ret .= "\t<td style='vertical-align:top;'>\n";
00549                         foreach( $column as $group => $checkbox ) {
00550                                 $attr = $checkbox['disabled'] ? array( 'disabled' => 'disabled' ) : array();
00551 
00552                                 $member = User::getGroupMember( $group, $user->getName() );
00553                                 if ( $checkbox['irreversible'] ) {
00554                                         $text = $this->msg( 'userrights-irreversible-marker', $member )->escaped();
00555                                 } else {
00556                                         $text = htmlspecialchars( $member );
00557                                 }
00558                                 $checkboxHtml = Xml::checkLabel( $text, "wpGroup-" . $group,
00559                                         "wpGroup-" . $group, $checkbox['set'], $attr );
00560                                 $ret .= "\t\t" . ( $checkbox['disabled']
00561                                         ? Xml::tags( 'span', array( 'class' => 'mw-userrights-disabled' ), $checkboxHtml )
00562                                         : $checkboxHtml
00563                                 ) . "<br />\n";
00564                         }
00565                         $ret .= "\t</td>\n";
00566                 }
00567                 $ret .= Xml::closeElement( 'tr' ) . Xml::closeElement( 'table' );
00568 
00569                 return $ret;
00570         }
00571 
00576         private function canRemove( $group ) {
00577                 // $this->changeableGroups()['remove'] doesn't work, of course. Thanks,
00578                 // PHP.
00579                 $groups = $this->changeableGroups();
00580                 return in_array( $group, $groups['remove'] ) || ( $this->isself && in_array( $group, $groups['remove-self'] ) );
00581         }
00582 
00587         private function canAdd( $group ) {
00588                 $groups = $this->changeableGroups();
00589                 return in_array( $group, $groups['add'] ) || ( $this->isself && in_array( $group, $groups['add-self'] ) );
00590         }
00591 
00597         function changeableGroups() {
00598                 return $this->getUser()->changeableGroups();
00599         }
00600 
00607         protected function showLogFragment( $user, $output ) {
00608                 $rightsLogPage = new LogPage( 'rights' );
00609                 $output->addHTML( Xml::element( 'h2', null, $rightsLogPage->getName()->text() ) );
00610                 LogEventsList::showLogExtract( $output, 'rights', $user->getUserPage() );
00611         }
00612 }