00001 <?php
00013 class UserrightsPage extends SpecialPage {
00014 # The target of the local right-adjuster's interest. Can be gotten from
00015 # either a GET parameter or a subpage-style parameter, so have a member
00016 # variable for it.
00017 protected $mTarget;
00018 protected $isself = false;
00019
00020 public function __construct() {
00021 parent::__construct( 'Userrights' );
00022 }
00023
00024 public function isRestricted() {
00025 return true;
00026 }
00027
00028 public function userCanExecute( $user ) {
00029 return $this->userCanChangeRights( $user, false );
00030 }
00031
00032 public function userCanChangeRights( $user, $checkIfSelf = true ) {
00033 $available = $this->changeableGroups();
00034 return !empty( $available['add'] )
00035 or !empty( $available['remove'] )
00036 or ( ( $this->isself || !$checkIfSelf ) and
00037 ( !empty( $available['add-self'] )
00038 or !empty( $available['remove-self'] ) ) );
00039 }
00040
00047 public function execute( $par ) {
00048
00049
00050 global $wgUser, $wgRequest, $wgOut;
00051
00052 if( $par ) {
00053 $this->mTarget = $par;
00054 } else {
00055 $this->mTarget = $wgRequest->getVal( 'user' );
00056 }
00057
00058
00059
00060
00061
00062
00063 if( $wgUser->isBlocked() && !$wgUser->isAllowed( 'userrights' ) ) {
00064 $wgOut->blockedPage();
00065 return;
00066 }
00067
00068 $available = $this->changeableGroups();
00069
00070 if ( !$this->mTarget ) {
00071
00072
00073
00074
00075
00076 if ( !count( $available['add'] ) && !count( $available['remove'] ) )
00077 $this->mTarget = $wgUser->getName();
00078 }
00079
00080 if ( $this->mTarget == $wgUser->getName() )
00081 $this->isself = true;
00082
00083 if( !$this->userCanChangeRights( $wgUser, true ) ) {
00084
00085 $wgOut->showPermissionsErrorPage( array( array(
00086 $wgUser->isAnon()
00087 ? 'userrights-nologin'
00088 : 'userrights-notallowed' ) ) );
00089 return;
00090 }
00091
00092 if ( wfReadOnly() ) {
00093 $wgOut->readOnlyPage();
00094 return;
00095 }
00096
00097 $this->outputHeader();
00098
00099 $this->setHeaders();
00100
00101
00102 if ( count( $available['add'] ) || count( $available['remove'] ) )
00103 $this->switchForm();
00104
00105 if( $wgRequest->wasPosted() ) {
00106
00107 if( $wgRequest->getCheck( 'saveusergroups' ) ) {
00108 $reason = $wgRequest->getVal( 'user-reason' );
00109 $tok = $wgRequest->getVal( 'wpEditToken' );
00110 if( $wgUser->matchEditToken( $tok, $this->mTarget ) ) {
00111 $this->saveUserGroups(
00112 $this->mTarget,
00113 $reason
00114 );
00115
00116 $url = $this->getSuccessURL();
00117 $wgOut->redirect( $url );
00118 return;
00119 }
00120 }
00121 }
00122
00123
00124 if( $this->mTarget ) {
00125 $this->editUserGroupsForm( $this->mTarget );
00126 }
00127 }
00128
00129 function getSuccessURL() {
00130 return $this->getTitle( $this->mTarget )->getFullURL();
00131 }
00132
00141 function saveUserGroups( $username, $reason = '' ) {
00142 global $wgRequest, $wgUser, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
00143
00144 $user = $this->fetchUser( $username );
00145 if( $user instanceof WikiErrorMsg ) {
00146 $wgOut->addWikiMsgArray( $user->getMessageKey(), $user->getMessageArgs() );
00147 return;
00148 }
00149
00150 $allgroups = $this->getAllGroups();
00151 $addgroup = array();
00152 $removegroup = array();
00153
00154
00155
00156 foreach ( $allgroups as $group ) {
00157
00158
00159 if ( $wgRequest->getCheck( "wpGroup-$group" ) ) {
00160 $addgroup[] = $group;
00161 } else {
00162 $removegroup[] = $group;
00163 }
00164 }
00165
00166 $this->doSaveUserGroups( $user, $addgroup, $removegroup, $reason );
00167 }
00168
00178 function doSaveUserGroups( $user, $add, $remove, $reason = '' ) {
00179 global $wgUser;
00180
00181
00182 $isself = ( $user->getName() == $wgUser->getName() );
00183 $groups = $user->getGroups();
00184 $changeable = $this->changeableGroups();
00185 $addable = array_merge( $changeable['add'], $isself ? $changeable['add-self'] : array() );
00186 $removable = array_merge( $changeable['remove'], $isself ? $changeable['remove-self'] : array() );
00187
00188 $remove = array_unique(
00189 array_intersect( (array)$remove, $removable, $groups ) );
00190 $add = array_unique( array_diff(
00191 array_intersect( (array)$add, $addable ),
00192 $groups )
00193 );
00194
00195 $oldGroups = $user->getGroups();
00196 $newGroups = $oldGroups;
00197
00198
00199 if( $remove ) {
00200 $newGroups = array_diff( $newGroups, $remove );
00201 foreach( $remove as $group ) {
00202 $user->removeGroup( $group );
00203 }
00204 }
00205 if( $add ) {
00206 $newGroups = array_merge( $newGroups, $add );
00207 foreach( $add as $group ) {
00208 $user->addGroup( $group );
00209 }
00210 }
00211 $newGroups = array_unique( $newGroups );
00212
00213
00214 $user->invalidateCache();
00215
00216 wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) );
00217 wfDebug( 'newGroups: ' . print_r( $newGroups, true ) );
00218 wfRunHooks( 'UserRights', array( &$user, $add, $remove ) );
00219
00220 if( $newGroups != $oldGroups ) {
00221 $this->addLogEntry( $user, $oldGroups, $newGroups, $reason );
00222 }
00223 return array( $add, $remove );
00224 }
00225
00226
00230 function addLogEntry( $user, $oldGroups, $newGroups, $reason ) {
00231 $log = new LogPage( 'rights' );
00232
00233 $log->addEntry( 'rights',
00234 $user->getUserPage(),
00235 $reason,
00236 array(
00237 $this->makeGroupNameListForLog( $oldGroups ),
00238 $this->makeGroupNameListForLog( $newGroups )
00239 )
00240 );
00241 }
00242
00247 function editUserGroupsForm( $username ) {
00248 global $wgOut;
00249
00250 $user = $this->fetchUser( $username );
00251 if( $user instanceof WikiErrorMsg ) {
00252 $wgOut->addWikiMsgArray( $user->getMessageKey(), $user->getMessageArgs() );
00253 return;
00254 }
00255
00256 $groups = $user->getGroups();
00257
00258 $this->showEditUserGroupsForm( $user, $groups );
00259
00260
00261
00262 $this->showLogFragment( $user, $wgOut );
00263 }
00264
00272 public function fetchUser( $username ) {
00273 global $wgUser, $wgUserrightsInterwikiDelimiter;
00274
00275 $parts = explode( $wgUserrightsInterwikiDelimiter, $username );
00276 if( count( $parts ) < 2 ) {
00277 $name = trim( $username );
00278 $database = '';
00279 } else {
00280 list( $name, $database ) = array_map( 'trim', $parts );
00281
00282 if( $database == wfWikiID() ) {
00283 $database = '';
00284 } else {
00285 if( !$wgUser->isAllowed( 'userrights-interwiki' ) ) {
00286 return new WikiErrorMsg( 'userrights-no-interwiki' );
00287 }
00288 if( !UserRightsProxy::validDatabase( $database ) ) {
00289 return new WikiErrorMsg( 'userrights-nodatabase', $database );
00290 }
00291 }
00292 }
00293
00294 if( $name == '' ) {
00295 return new WikiErrorMsg( 'nouserspecified' );
00296 }
00297
00298 if( $name{0} == '#' ) {
00299
00300
00301 $id = intval( substr( $name, 1 ) );
00302
00303 if( $database == '' ) {
00304 $name = User::whoIs( $id );
00305 } else {
00306 $name = UserRightsProxy::whoIs( $database, $id );
00307 }
00308
00309 if( !$name ) {
00310 return new WikiErrorMsg( 'noname' );
00311 }
00312 } else {
00313 $name = User::getCanonicalName( $name );
00314 if( !$name ) {
00315
00316 return new WikiErrorMsg( 'nosuchusershort', $username );
00317 }
00318 }
00319
00320 if( $database == '' ) {
00321 $user = User::newFromName( $name );
00322 } else {
00323 $user = UserRightsProxy::newFromName( $database, $name );
00324 }
00325
00326 if( !$user || $user->isAnon() ) {
00327 return new WikiErrorMsg( 'nosuchusershort', $username );
00328 }
00329
00330 return $user;
00331 }
00332
00333 function makeGroupNameList( $ids ) {
00334 if( empty( $ids ) ) {
00335 return wfMsgForContent( 'rightsnone' );
00336 } else {
00337 return implode( ', ', $ids );
00338 }
00339 }
00340
00341 function makeGroupNameListForLog( $ids ) {
00342 if( empty( $ids ) ) {
00343 return '';
00344 } else {
00345 return $this->makeGroupNameList( $ids );
00346 }
00347 }
00348
00352 function switchForm() {
00353 global $wgOut, $wgScript;
00354 $wgOut->addHTML(
00355 Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'name' => 'uluser', 'id' => 'mw-userrights-form1' ) ) .
00356 Xml::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
00357 Xml::openElement( 'fieldset' ) .
00358 Xml::element( 'legend', array(), wfMsg( 'userrights-lookup-user' ) ) .
00359 Xml::inputLabel( wfMsg( 'userrights-user-editname' ), 'user', 'username', 30, $this->mTarget ) . ' ' .
00360 Xml::submitButton( wfMsg( 'editusergroup' ) ) .
00361 Xml::closeElement( 'fieldset' ) .
00362 Xml::closeElement( 'form' ) . "\n"
00363 );
00364 }
00365
00374 protected function splitGroups( $groups ) {
00375 list( $addable, $removable, $addself, $removeself ) = array_values( $this->changeableGroups() );
00376
00377 $removable = array_intersect(
00378 array_merge( $this->isself ? $removeself : array(), $removable ),
00379 $groups
00380 );
00381 $addable = array_diff(
00382 array_merge( $this->isself ? $addself : array(), $addable ),
00383 $groups
00384 );
00385
00386 return array( $addable, $removable );
00387 }
00388
00395 protected function showEditUserGroupsForm( $user, $groups ) {
00396 global $wgOut, $wgUser, $wgLang;
00397
00398 $list = array();
00399 foreach( $groups as $group )
00400 $list[] = self::buildGroupLink( $group );
00401
00402 $autolist = array();
00403 if ( $user instanceof User ) {
00404 foreach( Autopromote::getAutopromoteGroups( $user ) as $group ) {
00405 $autolist[] = self::buildGroupLink( $group );
00406 }
00407 }
00408
00409 $grouplist = '';
00410 if( count( $list ) > 0 ) {
00411 $grouplist = wfMsgHtml( 'userrights-groupsmember' );
00412 $grouplist = '<p>' . $grouplist . ' ' . $wgLang->listToText( $list ) . "</p>\n";
00413 }
00414 if( count( $autolist ) > 0 ) {
00415 $autogrouplistintro = wfMsgHtml( 'userrights-groupsmember-auto' );
00416 $grouplist .= '<p>' . $autogrouplistintro . ' ' . $wgLang->listToText( $autolist ) . "</p>\n";
00417 }
00418 $wgOut->addHTML(
00419 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL(), 'name' => 'editGroup', 'id' => 'mw-userrights-form2' ) ) .
00420 Xml::hidden( 'user', $this->mTarget ) .
00421 Xml::hidden( 'wpEditToken', $wgUser->editToken( $this->mTarget ) ) .
00422 Xml::openElement( 'fieldset' ) .
00423 Xml::element( 'legend', array(), wfMsg( 'userrights-editusergroup' ) ) .
00424 wfMsgExt( 'editinguser', array( 'parse' ), wfEscapeWikiText( $user->getName() ) ) .
00425 wfMsgExt( 'userrights-groups-help', array( 'parse' ) ) .
00426 $grouplist .
00427 Xml::tags( 'p', null, $this->groupCheckboxes( $groups ) ) .
00428 Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-userrights-table-outer' ) ) .
00429 "<tr>
00430 <td class='mw-label'>" .
00431 Xml::label( wfMsg( 'userrights-reason' ), 'wpReason' ) .
00432 "</td>
00433 <td class='mw-input'>" .
00434 Xml::input( 'user-reason', 60, false, array( 'id' => 'wpReason', 'maxlength' => 255 ) ) .
00435 "</td>
00436 </tr>
00437 <tr>
00438 <td></td>
00439 <td class='mw-submit'>" .
00440 Xml::submitButton( wfMsg( 'saveusergroups' ), array( 'name' => 'saveusergroups', 'accesskey' => 's' ) ) .
00441 "</td>
00442 </tr>" .
00443 Xml::closeElement( 'table' ) . "\n" .
00444 Xml::closeElement( 'fieldset' ) .
00445 Xml::closeElement( 'form' ) . "\n"
00446 );
00447 }
00448
00455 private static function buildGroupLink( $group ) {
00456 static $cache = array();
00457 if( !isset( $cache[$group] ) )
00458 $cache[$group] = User::makeGroupLinkHtml( $group, htmlspecialchars( User::getGroupName( $group ) ) );
00459 return $cache[$group];
00460 }
00461
00466 protected static function getAllGroups() {
00467 return User::getAllGroups();
00468 }
00469
00476 private function groupCheckboxes( $usergroups ) {
00477 $allgroups = $this->getAllGroups();
00478 $ret = '';
00479
00480 # Put all column info into an associative array so that extensions can
00481 # more easily manage it.
00482 $columns = array( 'unchangeable' => array(), 'changeable' => array() );
00483
00484 foreach( $allgroups as $group ) {
00485 $set = in_array( $group, $usergroups );
00486 # Should the checkbox be disabled?
00487 $disabled = !(
00488 ( $set && $this->canRemove( $group ) ) ||
00489 ( !$set && $this->canAdd( $group ) ) );
00490 # Do we need to point out that this action is irreversible?
00491 $irreversible = !$disabled && (
00492 ( $set && !$this->canAdd( $group ) ) ||
00493 ( !$set && !$this->canRemove( $group ) ) );
00494
00495 $checkbox = array(
00496 'set' => $set,
00497 'disabled' => $disabled,
00498 'irreversible' => $irreversible
00499 );
00500
00501 if( $disabled ) {
00502 $columns['unchangeable'][$group] = $checkbox;
00503 } else {
00504 $columns['changeable'][$group] = $checkbox;
00505 }
00506 }
00507
00508 # Build the HTML table
00509 $ret .= Xml::openElement( 'table', array( 'border' => '0', 'class' => 'mw-userrights-groups' ) ) .
00510 "<tr>\n";
00511 foreach( $columns as $name => $column ) {
00512 if( $column === array() )
00513 continue;
00514 $ret .= xml::element( 'th', null, wfMsg( 'userrights-' . $name . '-col' ) );
00515 }
00516 $ret.= "</tr>\n<tr>\n";
00517 foreach( $columns as $column ) {
00518 if( $column === array() )
00519 continue;
00520 $ret .= "\t<td style='vertical-align:top;'>\n";
00521 foreach( $column as $group => $checkbox ) {
00522 $attr = $checkbox['disabled'] ? array( 'disabled' => 'disabled' ) : array();
00523
00524 if ( $checkbox['irreversible'] ) {
00525 $text = htmlspecialchars( wfMsg( 'userrights-irreversible-marker',
00526 User::getGroupMember( $group ) ) );
00527 } else {
00528 $text = htmlspecialchars( User::getGroupMember( $group ) );
00529 }
00530 $checkboxHtml = Xml::checkLabel( $text, "wpGroup-" . $group,
00531 "wpGroup-" . $group, $checkbox['set'], $attr );
00532 $ret .= "\t\t" . ( $checkbox['disabled']
00533 ? Xml::tags( 'span', array( 'class' => 'mw-userrights-disabled' ), $checkboxHtml )
00534 : $checkboxHtml
00535 ) . "<br />\n";
00536 }
00537 $ret .= "\t</td>\n";
00538 }
00539 $ret .= Xml::closeElement( 'tr' ) . Xml::closeElement( 'table' );
00540
00541 return $ret;
00542 }
00543
00548 private function canRemove( $group ) {
00549
00550
00551 $groups = $this->changeableGroups();
00552 return in_array( $group, $groups['remove'] ) || ( $this->isself && in_array( $group, $groups['remove-self'] ) );
00553 }
00554
00559 private function canAdd( $group ) {
00560 $groups = $this->changeableGroups();
00561 return in_array( $group, $groups['add'] ) || ( $this->isself && in_array( $group, $groups['add-self'] ) );
00562 }
00563
00569 function changeableGroups() {
00570 global $wgUser;
00571 return $wgUser->changeableGroups();
00572 }
00573
00580 protected function showLogFragment( $user, $output ) {
00581 $output->addHTML( Xml::element( 'h2', null, LogPage::logName( 'rights' ) . "\n" ) );
00582 LogEventsList::showLogExtract( $output, 'rights', $user->getUserPage()->getPrefixedText() );
00583 }
00584 }