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