MediaWiki
REL1_21
|
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 00246 function addLogEntry( $user, $oldGroups, $newGroups, $reason ) { 00247 $logEntry = new ManualLogEntry( 'rights', 'rights' ); 00248 $logEntry->setPerformer( $this->getUser() ); 00249 $logEntry->setTarget( $user->getUserPage() ); 00250 $logEntry->setComment( $reason ); 00251 $logEntry->setParameters( array( 00252 '4::oldgroups' => $oldGroups, 00253 '5::newgroups' => $newGroups, 00254 ) ); 00255 $logid = $logEntry->insert(); 00256 $logEntry->publish( $logid ); 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 00364 function makeGroupNameListForLog( $ids ) { 00365 wfDeprecated( __METHOD__, '1.21' ); 00366 00367 if( empty( $ids ) ) { 00368 return ''; 00369 } else { 00370 return $this->makeGroupNameList( $ids ); 00371 } 00372 } 00373 00377 function switchForm() { 00378 global $wgScript; 00379 $this->getOutput()->addHTML( 00380 Html::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'name' => 'uluser', 'id' => 'mw-userrights-form1' ) ) . 00381 Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . 00382 Xml::fieldset( $this->msg( 'userrights-lookup-user' )->text() ) . 00383 Xml::inputLabel( $this->msg( 'userrights-user-editname' )->text(), 'user', 'username', 30, str_replace( '_', ' ', $this->mTarget ) ) . ' ' . 00384 Xml::submitButton( $this->msg( 'editusergroup' )->text() ) . 00385 Html::closeElement( 'fieldset' ) . 00386 Html::closeElement( 'form' ) . "\n" 00387 ); 00388 } 00389 00398 protected function splitGroups( $groups ) { 00399 list( $addable, $removable, $addself, $removeself ) = array_values( $this->changeableGroups() ); 00400 00401 $removable = array_intersect( 00402 array_merge( $this->isself ? $removeself : array(), $removable ), 00403 $groups 00404 ); // Can't remove groups the user doesn't have 00405 $addable = array_diff( 00406 array_merge( $this->isself ? $addself : array(), $addable ), 00407 $groups 00408 ); // Can't add groups the user does have 00409 00410 return array( $addable, $removable ); 00411 } 00412 00419 protected function showEditUserGroupsForm( $user, $groups ) { 00420 $list = array(); 00421 $membersList = array(); 00422 foreach( $groups as $group ) { 00423 $list[] = self::buildGroupLink( $group ); 00424 $membersList[] = self::buildGroupMemberLink( $group ); 00425 } 00426 00427 $autoList = array(); 00428 $autoMembersList = array(); 00429 if ( $user instanceof User ) { 00430 foreach( Autopromote::getAutopromoteGroups( $user ) as $group ) { 00431 $autoList[] = self::buildGroupLink( $group ); 00432 $autoMembersList[] = self::buildGroupMemberLink( $group ); 00433 } 00434 } 00435 00436 $language = $this->getLanguage(); 00437 $displayedList = $this->msg( 'userrights-groupsmember-type', 00438 $language->listToText( $list ), 00439 $language->listToText( $membersList ) 00440 )->plain(); 00441 $displayedAutolist = $this->msg( 'userrights-groupsmember-type', 00442 $language->listToText( $autoList ), 00443 $language->listToText( $autoMembersList ) 00444 )->plain(); 00445 00446 $grouplist = ''; 00447 $count = count( $list ); 00448 if ( $count > 0 ) { 00449 $grouplist = $this->msg( 'userrights-groupsmember', $count, $user->getName() )->parse(); 00450 $grouplist = '<p>' . $grouplist . ' ' . $displayedList . "</p>\n"; 00451 } 00452 $count = count( $autoList ); 00453 if ( $count > 0 ) { 00454 $autogrouplistintro = $this->msg( 'userrights-groupsmember-auto', $count, $user->getName() )->parse(); 00455 $grouplist .= '<p>' . $autogrouplistintro . ' ' . $displayedAutolist . "</p>\n"; 00456 } 00457 00458 $userToolLinks = Linker::userToolLinks( 00459 $user->getId(), 00460 $user->getName(), 00461 false, /* default for redContribsWhenNoEdits */ 00462 Linker::TOOL_LINKS_EMAIL /* Add "send e-mail" link */ 00463 ); 00464 00465 $this->getOutput()->addHTML( 00466 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL(), 'name' => 'editGroup', 'id' => 'mw-userrights-form2' ) ) . 00467 Html::hidden( 'user', $this->mTarget ) . 00468 Html::hidden( 'wpEditToken', $this->getUser()->getEditToken( $this->mTarget ) ) . 00469 Xml::openElement( 'fieldset' ) . 00470 Xml::element( 'legend', array(), $this->msg( 'userrights-editusergroup', $user->getName() )->text() ) . 00471 $this->msg( 'editinguser' )->params( wfEscapeWikiText( $user->getName() ) )->rawParams( $userToolLinks )->parse() . 00472 $this->msg( 'userrights-groups-help', $user->getName() )->parse() . 00473 $grouplist . 00474 Xml::tags( 'p', null, $this->groupCheckboxes( $groups, $user ) ) . 00475 Xml::openElement( 'table', array( 'id' => 'mw-userrights-table-outer' ) ) . 00476 "<tr> 00477 <td class='mw-label'>" . 00478 Xml::label( $this->msg( 'userrights-reason' )->text(), 'wpReason' ) . 00479 "</td> 00480 <td class='mw-input'>" . 00481 Xml::input( 'user-reason', 60, $this->getRequest()->getVal( 'user-reason', false ), 00482 array( 'id' => 'wpReason', 'maxlength' => 255 ) ) . 00483 "</td> 00484 </tr> 00485 <tr> 00486 <td></td> 00487 <td class='mw-submit'>" . 00488 Xml::submitButton( $this->msg( 'saveusergroups' )->text(), 00489 array( 'name' => 'saveusergroups' ) + Linker::tooltipAndAccesskeyAttribs( 'userrights-set' ) ) . 00490 "</td> 00491 </tr>" . 00492 Xml::closeElement( 'table' ) . "\n" . 00493 Xml::closeElement( 'fieldset' ) . 00494 Xml::closeElement( 'form' ) . "\n" 00495 ); 00496 } 00497 00504 private static function buildGroupLink( $group ) { 00505 return User::makeGroupLinkHtml( $group, User::getGroupName( $group ) ); 00506 } 00507 00514 private static function buildGroupMemberLink( $group ) { 00515 return User::makeGroupLinkHtml( $group, User::getGroupMember( $group ) ); 00516 } 00517 00522 protected static function getAllGroups() { 00523 return User::getAllGroups(); 00524 } 00525 00534 private function groupCheckboxes( $usergroups, $user ) { 00535 $allgroups = $this->getAllGroups(); 00536 $ret = ''; 00537 00538 # Put all column info into an associative array so that extensions can 00539 # more easily manage it. 00540 $columns = array( 'unchangeable' => array(), 'changeable' => array() ); 00541 00542 foreach( $allgroups as $group ) { 00543 $set = in_array( $group, $usergroups ); 00544 # Should the checkbox be disabled? 00545 $disabled = !( 00546 ( $set && $this->canRemove( $group ) ) || 00547 ( !$set && $this->canAdd( $group ) ) ); 00548 # Do we need to point out that this action is irreversible? 00549 $irreversible = !$disabled && ( 00550 ( $set && !$this->canAdd( $group ) ) || 00551 ( !$set && !$this->canRemove( $group ) ) ); 00552 00553 $checkbox = array( 00554 'set' => $set, 00555 'disabled' => $disabled, 00556 'irreversible' => $irreversible 00557 ); 00558 00559 if( $disabled ) { 00560 $columns['unchangeable'][$group] = $checkbox; 00561 } else { 00562 $columns['changeable'][$group] = $checkbox; 00563 } 00564 } 00565 00566 # Build the HTML table 00567 $ret .= Xml::openElement( 'table', array( 'class' => 'mw-userrights-groups' ) ) . 00568 "<tr>\n"; 00569 foreach( $columns as $name => $column ) { 00570 if( $column === array() ) 00571 continue; 00572 $ret .= Xml::element( 'th', null, $this->msg( 'userrights-' . $name . '-col', count( $column ) )->text() ); 00573 } 00574 $ret .= "</tr>\n<tr>\n"; 00575 foreach( $columns as $column ) { 00576 if( $column === array() ) 00577 continue; 00578 $ret .= "\t<td style='vertical-align:top;'>\n"; 00579 foreach( $column as $group => $checkbox ) { 00580 $attr = $checkbox['disabled'] ? array( 'disabled' => 'disabled' ) : array(); 00581 00582 $member = User::getGroupMember( $group, $user->getName() ); 00583 if ( $checkbox['irreversible'] ) { 00584 $text = $this->msg( 'userrights-irreversible-marker', $member )->escaped(); 00585 } else { 00586 $text = htmlspecialchars( $member ); 00587 } 00588 $checkboxHtml = Xml::checkLabel( $text, "wpGroup-" . $group, 00589 "wpGroup-" . $group, $checkbox['set'], $attr ); 00590 $ret .= "\t\t" . ( $checkbox['disabled'] 00591 ? Xml::tags( 'span', array( 'class' => 'mw-userrights-disabled' ), $checkboxHtml ) 00592 : $checkboxHtml 00593 ) . "<br />\n"; 00594 } 00595 $ret .= "\t</td>\n"; 00596 } 00597 $ret .= Xml::closeElement( 'tr' ) . Xml::closeElement( 'table' ); 00598 00599 return $ret; 00600 } 00601 00606 private function canRemove( $group ) { 00607 // $this->changeableGroups()['remove'] doesn't work, of course. Thanks, 00608 // PHP. 00609 $groups = $this->changeableGroups(); 00610 return in_array( $group, $groups['remove'] ) || ( $this->isself && in_array( $group, $groups['remove-self'] ) ); 00611 } 00612 00617 private function canAdd( $group ) { 00618 $groups = $this->changeableGroups(); 00619 return in_array( $group, $groups['add'] ) || ( $this->isself && in_array( $group, $groups['add-self'] ) ); 00620 } 00621 00627 function changeableGroups() { 00628 return $this->getUser()->changeableGroups(); 00629 } 00630 00637 protected function showLogFragment( $user, $output ) { 00638 $rightsLogPage = new LogPage( 'rights' ); 00639 $output->addHTML( Xml::element( 'h2', null, $rightsLogPage->getName()->text() ) ); 00640 LogEventsList::showLogExtract( $output, 'rights', $user->getUserPage() ); 00641 } 00642 00643 protected function getGroupName() { 00644 return 'users'; 00645 } 00646 }