MediaWiki
REL1_23
|
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 00282 function addLogEntry( $user, $oldGroups, $newGroups, $reason ) { 00283 $logEntry = new ManualLogEntry( 'rights', 'rights' ); 00284 $logEntry->setPerformer( $this->getUser() ); 00285 $logEntry->setTarget( $user->getUserPage() ); 00286 $logEntry->setComment( $reason ); 00287 $logEntry->setParameters( array( 00288 '4::oldgroups' => $oldGroups, 00289 '5::newgroups' => $newGroups, 00290 ) ); 00291 $logid = $logEntry->insert(); 00292 $logEntry->publish( $logid ); 00293 } 00294 00299 function editUserGroupsForm( $username ) { 00300 $status = $this->fetchUser( $username ); 00301 if ( !$status->isOK() ) { 00302 $this->getOutput()->addWikiText( $status->getWikiText() ); 00303 00304 return; 00305 } else { 00306 $user = $status->value; 00307 } 00308 00309 $groups = $user->getGroups(); 00310 00311 $this->showEditUserGroupsForm( $user, $groups ); 00312 00313 // This isn't really ideal logging behavior, but let's not hide the 00314 // interwiki logs if we're using them as is. 00315 $this->showLogFragment( $user, $this->getOutput() ); 00316 } 00317 00326 public function fetchUser( $username ) { 00327 global $wgUserrightsInterwikiDelimiter; 00328 00329 $parts = explode( $wgUserrightsInterwikiDelimiter, $username ); 00330 if ( count( $parts ) < 2 ) { 00331 $name = trim( $username ); 00332 $database = ''; 00333 } else { 00334 list( $name, $database ) = array_map( 'trim', $parts ); 00335 00336 if ( $database == wfWikiID() ) { 00337 $database = ''; 00338 } else { 00339 if ( !$this->getUser()->isAllowed( 'userrights-interwiki' ) ) { 00340 return Status::newFatal( 'userrights-no-interwiki' ); 00341 } 00342 if ( !UserRightsProxy::validDatabase( $database ) ) { 00343 return Status::newFatal( 'userrights-nodatabase', $database ); 00344 } 00345 } 00346 } 00347 00348 if ( $name === '' ) { 00349 return Status::newFatal( 'nouserspecified' ); 00350 } 00351 00352 if ( $name[0] == '#' ) { 00353 // Numeric ID can be specified... 00354 // We'll do a lookup for the name internally. 00355 $id = intval( substr( $name, 1 ) ); 00356 00357 if ( $database == '' ) { 00358 $name = User::whoIs( $id ); 00359 } else { 00360 $name = UserRightsProxy::whoIs( $database, $id ); 00361 } 00362 00363 if ( !$name ) { 00364 return Status::newFatal( 'noname' ); 00365 } 00366 } else { 00367 $name = User::getCanonicalName( $name ); 00368 if ( $name === false ) { 00369 // invalid name 00370 return Status::newFatal( 'nosuchusershort', $username ); 00371 } 00372 } 00373 00374 if ( $database == '' ) { 00375 $user = User::newFromName( $name ); 00376 } else { 00377 $user = UserRightsProxy::newFromName( $database, $name ); 00378 } 00379 00380 if ( !$user || $user->isAnon() ) { 00381 return Status::newFatal( 'nosuchusershort', $username ); 00382 } 00383 00384 return Status::newGood( $user ); 00385 } 00386 00387 function makeGroupNameList( $ids ) { 00388 if ( empty( $ids ) ) { 00389 return $this->msg( 'rightsnone' )->inContentLanguage()->text(); 00390 } else { 00391 return implode( ', ', $ids ); 00392 } 00393 } 00394 00402 function makeGroupNameListForLog( $ids ) { 00403 wfDeprecated( __METHOD__, '1.21' ); 00404 00405 if ( empty( $ids ) ) { 00406 return ''; 00407 } else { 00408 return $this->makeGroupNameList( $ids ); 00409 } 00410 } 00411 00415 function switchForm() { 00416 global $wgScript; 00417 $this->getOutput()->addHTML( 00418 Html::openElement( 00419 'form', 00420 array( 00421 'method' => 'get', 00422 'action' => $wgScript, 00423 'name' => 'uluser', 00424 'id' => 'mw-userrights-form1' 00425 ) 00426 ) . 00427 Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() ) . 00428 Xml::fieldset( $this->msg( 'userrights-lookup-user' )->text() ) . 00429 Xml::inputLabel( 00430 $this->msg( 'userrights-user-editname' )->text(), 00431 'user', 00432 'username', 00433 30, 00434 str_replace( '_', ' ', $this->mTarget ), 00435 array( 'autofocus' => true ) 00436 ) . ' ' . 00437 Xml::submitButton( $this->msg( 'editusergroup' )->text() ) . 00438 Html::closeElement( 'fieldset' ) . 00439 Html::closeElement( 'form' ) . "\n" 00440 ); 00441 } 00442 00451 protected function splitGroups( $groups ) { 00452 list( $addable, $removable, $addself, $removeself ) = array_values( $this->changeableGroups() ); 00453 00454 $removable = array_intersect( 00455 array_merge( $this->isself ? $removeself : array(), $removable ), 00456 $groups 00457 ); // Can't remove groups the user doesn't have 00458 $addable = array_diff( 00459 array_merge( $this->isself ? $addself : array(), $addable ), 00460 $groups 00461 ); // Can't add groups the user does have 00462 00463 return array( $addable, $removable ); 00464 } 00465 00472 protected function showEditUserGroupsForm( $user, $groups ) { 00473 $list = array(); 00474 $membersList = array(); 00475 foreach ( $groups as $group ) { 00476 $list[] = self::buildGroupLink( $group ); 00477 $membersList[] = self::buildGroupMemberLink( $group ); 00478 } 00479 00480 $autoList = array(); 00481 $autoMembersList = array(); 00482 if ( $user instanceof User ) { 00483 foreach ( Autopromote::getAutopromoteGroups( $user ) as $group ) { 00484 $autoList[] = self::buildGroupLink( $group ); 00485 $autoMembersList[] = self::buildGroupMemberLink( $group ); 00486 } 00487 } 00488 00489 $language = $this->getLanguage(); 00490 $displayedList = $this->msg( 'userrights-groupsmember-type', 00491 $language->listToText( $list ), 00492 $language->listToText( $membersList ) 00493 )->plain(); 00494 $displayedAutolist = $this->msg( 'userrights-groupsmember-type', 00495 $language->listToText( $autoList ), 00496 $language->listToText( $autoMembersList ) 00497 )->plain(); 00498 00499 $grouplist = ''; 00500 $count = count( $list ); 00501 if ( $count > 0 ) { 00502 $grouplist = $this->msg( 'userrights-groupsmember', $count, $user->getName() )->parse(); 00503 $grouplist = '<p>' . $grouplist . ' ' . $displayedList . "</p>\n"; 00504 } 00505 00506 $count = count( $autoList ); 00507 if ( $count > 0 ) { 00508 $autogrouplistintro = $this->msg( 'userrights-groupsmember-auto', $count, $user->getName() ) 00509 ->parse(); 00510 $grouplist .= '<p>' . $autogrouplistintro . ' ' . $displayedAutolist . "</p>\n"; 00511 } 00512 00513 $userToolLinks = Linker::userToolLinks( 00514 $user->getId(), 00515 $user->getName(), 00516 false, /* default for redContribsWhenNoEdits */ 00517 Linker::TOOL_LINKS_EMAIL /* Add "send e-mail" link */ 00518 ); 00519 00520 $this->getOutput()->addHTML( 00521 Xml::openElement( 00522 'form', 00523 array( 00524 'method' => 'post', 00525 'action' => $this->getPageTitle()->getLocalURL(), 00526 'name' => 'editGroup', 00527 'id' => 'mw-userrights-form2' 00528 ) 00529 ) . 00530 Html::hidden( 'user', $this->mTarget ) . 00531 Html::hidden( 'wpEditToken', $this->getUser()->getEditToken( $this->mTarget ) ) . 00532 Html::hidden( 00533 'conflictcheck-originalgroups', 00534 implode( ',', $user->getGroups() ) 00535 ) . // Conflict detection 00536 Xml::openElement( 'fieldset' ) . 00537 Xml::element( 00538 'legend', 00539 array(), 00540 $this->msg( 'userrights-editusergroup', $user->getName() )->text() 00541 ) . 00542 $this->msg( 'editinguser' )->params( wfEscapeWikiText( $user->getName() ) ) 00543 ->rawParams( $userToolLinks )->parse() . 00544 $this->msg( 'userrights-groups-help', $user->getName() )->parse() . 00545 $grouplist . 00546 Xml::tags( 'p', null, $this->groupCheckboxes( $groups, $user ) ) . 00547 Xml::openElement( 'table', array( 'id' => 'mw-userrights-table-outer' ) ) . 00548 "<tr> 00549 <td class='mw-label'>" . 00550 Xml::label( $this->msg( 'userrights-reason' )->text(), 'wpReason' ) . 00551 "</td> 00552 <td class='mw-input'>" . 00553 Xml::input( 'user-reason', 60, $this->getRequest()->getVal( 'user-reason', false ), 00554 array( 'id' => 'wpReason', 'maxlength' => 255 ) ) . 00555 "</td> 00556 </tr> 00557 <tr> 00558 <td></td> 00559 <td class='mw-submit'>" . 00560 Xml::submitButton( $this->msg( 'saveusergroups' )->text(), 00561 array( 'name' => 'saveusergroups' ) + 00562 Linker::tooltipAndAccesskeyAttribs( 'userrights-set' ) 00563 ) . 00564 "</td> 00565 </tr>" . 00566 Xml::closeElement( 'table' ) . "\n" . 00567 Xml::closeElement( 'fieldset' ) . 00568 Xml::closeElement( 'form' ) . "\n" 00569 ); 00570 } 00571 00578 private static function buildGroupLink( $group ) { 00579 return User::makeGroupLinkHtml( $group, User::getGroupName( $group ) ); 00580 } 00581 00588 private static function buildGroupMemberLink( $group ) { 00589 return User::makeGroupLinkHtml( $group, User::getGroupMember( $group ) ); 00590 } 00591 00596 protected static function getAllGroups() { 00597 return User::getAllGroups(); 00598 } 00599 00608 private function groupCheckboxes( $usergroups, $user ) { 00609 $allgroups = $this->getAllGroups(); 00610 $ret = ''; 00611 00612 // Put all column info into an associative array so that extensions can 00613 // more easily manage it. 00614 $columns = array( 'unchangeable' => array(), 'changeable' => array() ); 00615 00616 foreach ( $allgroups as $group ) { 00617 $set = in_array( $group, $usergroups ); 00618 // Should the checkbox be disabled? 00619 $disabled = !( 00620 ( $set && $this->canRemove( $group ) ) || 00621 ( !$set && $this->canAdd( $group ) ) ); 00622 // Do we need to point out that this action is irreversible? 00623 $irreversible = !$disabled && ( 00624 ( $set && !$this->canAdd( $group ) ) || 00625 ( !$set && !$this->canRemove( $group ) ) ); 00626 00627 $checkbox = array( 00628 'set' => $set, 00629 'disabled' => $disabled, 00630 'irreversible' => $irreversible 00631 ); 00632 00633 if ( $disabled ) { 00634 $columns['unchangeable'][$group] = $checkbox; 00635 } else { 00636 $columns['changeable'][$group] = $checkbox; 00637 } 00638 } 00639 00640 // Build the HTML table 00641 $ret .= Xml::openElement( 'table', array( 'class' => 'mw-userrights-groups' ) ) . 00642 "<tr>\n"; 00643 foreach ( $columns as $name => $column ) { 00644 if ( $column === array() ) { 00645 continue; 00646 } 00647 // Messages: userrights-changeable-col, userrights-unchangeable-col 00648 $ret .= Xml::element( 00649 'th', 00650 null, 00651 $this->msg( 'userrights-' . $name . '-col', count( $column ) )->text() 00652 ); 00653 } 00654 00655 $ret .= "</tr>\n<tr>\n"; 00656 foreach ( $columns as $column ) { 00657 if ( $column === array() ) { 00658 continue; 00659 } 00660 $ret .= "\t<td style='vertical-align:top;'>\n"; 00661 foreach ( $column as $group => $checkbox ) { 00662 $attr = $checkbox['disabled'] ? array( 'disabled' => 'disabled' ) : array(); 00663 00664 $member = User::getGroupMember( $group, $user->getName() ); 00665 if ( $checkbox['irreversible'] ) { 00666 $text = $this->msg( 'userrights-irreversible-marker', $member )->escaped(); 00667 } else { 00668 $text = htmlspecialchars( $member ); 00669 } 00670 $checkboxHtml = Xml::checkLabel( $text, "wpGroup-" . $group, 00671 "wpGroup-" . $group, $checkbox['set'], $attr ); 00672 $ret .= "\t\t" . ( $checkbox['disabled'] 00673 ? Xml::tags( 'span', array( 'class' => 'mw-userrights-disabled' ), $checkboxHtml ) 00674 : $checkboxHtml 00675 ) . "<br />\n"; 00676 } 00677 $ret .= "\t</td>\n"; 00678 } 00679 $ret .= Xml::closeElement( 'tr' ) . Xml::closeElement( 'table' ); 00680 00681 return $ret; 00682 } 00683 00688 private function canRemove( $group ) { 00689 // $this->changeableGroups()['remove'] doesn't work, of course. Thanks, PHP. 00690 $groups = $this->changeableGroups(); 00691 00692 return in_array( 00693 $group, 00694 $groups['remove'] ) || ( $this->isself && in_array( $group, $groups['remove-self'] ) 00695 ); 00696 } 00697 00702 private function canAdd( $group ) { 00703 $groups = $this->changeableGroups(); 00704 00705 return in_array( 00706 $group, 00707 $groups['add'] ) || ( $this->isself && in_array( $group, $groups['add-self'] ) 00708 ); 00709 } 00710 00721 function changeableGroups() { 00722 return $this->getUser()->changeableGroups(); 00723 } 00724 00731 protected function showLogFragment( $user, $output ) { 00732 $rightsLogPage = new LogPage( 'rights' ); 00733 $output->addHTML( Xml::element( 'h2', null, $rightsLogPage->getName()->text() ) ); 00734 LogEventsList::showLogExtract( $output, 'rights', $user->getUserPage() ); 00735 } 00736 00737 protected function getGroupName() { 00738 return 'users'; 00739 } 00740 }