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