3 * Implements Special:Userrights
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
25 * Special page to allow managing user group membership
27 * @ingroup SpecialPage
29 class UserrightsPage
extends SpecialPage
{
30 # The target of the local right-adjuster's interest. Can be gotten from
31 # either a GET parameter or a subpage-style parameter, so have a member
34 protected $isself = false;
36 public function __construct() {
37 parent
::__construct( 'Userrights' );
40 public function isRestricted() {
44 public function userCanExecute( User
$user ) {
45 return $this->userCanChangeRights( $user, false );
48 public function userCanChangeRights( $user, $checkIfSelf = true ) {
49 $available = $this->changeableGroups();
50 return !empty( $available['add'] )
51 ||
!empty( $available['remove'] )
52 ||
( ( $this->isself ||
!$checkIfSelf ) &&
53 ( !empty( $available['add-self'] )
54 ||
!empty( $available['remove-self'] ) ) );
58 * Manage forms to be shown according to posted data.
59 * Depending on the submit button used, call a form or a save function.
61 * @param $par Mixed: string if any subpage provided, else null
63 public function execute( $par ) {
64 // If the visitor doesn't have permissions to assign or remove
65 // any groups, it's a bit silly to give them the user search prompt.
66 global $wgUser, $wgRequest, $wgOut;
69 $this->mTarget
= $par;
71 $this->mTarget
= $wgRequest->getVal( 'user' );
75 * If the user is blocked and they only have "partial" access
76 * (e.g. they don't have the userrights permission), then don't
77 * allow them to use Special:UserRights.
79 if( $wgUser->isBlocked() && !$wgUser->isAllowed( 'userrights' ) ) {
80 $wgOut->blockedPage();
84 $available = $this->changeableGroups();
86 if ( $this->mTarget
=== null ) {
88 * If the user specified no target, and they can only
89 * edit their own groups, automatically set them as the
92 if ( !count( $available['add'] ) && !count( $available['remove'] ) )
93 $this->mTarget
= $wgUser->getName();
96 if ( User
::getCanonicalName( $this->mTarget
) == $wgUser->getName() ) {
100 if( !$this->userCanChangeRights( $wgUser, true ) ) {
101 // fixme... there may be intermediate groups we can mention.
102 $wgOut->showPermissionsErrorPage( array( array(
104 ?
'userrights-nologin'
105 : 'userrights-notallowed' ) ) );
109 if ( wfReadOnly() ) {
110 $wgOut->readOnlyPage();
114 $this->outputHeader();
115 $wgOut->addModuleStyles( 'mediawiki.special' );
118 // show the general form
119 if ( count( $available['add'] ) ||
count( $available['remove'] ) ) {
123 if( $wgRequest->wasPosted() ) {
125 if( $wgRequest->getCheck( 'saveusergroups' ) ) {
126 $reason = $wgRequest->getVal( 'user-reason' );
127 $tok = $wgRequest->getVal( 'wpEditToken' );
128 if( $wgUser->matchEditToken( $tok, $this->mTarget
) ) {
129 $this->saveUserGroups(
134 $url = $this->getSuccessURL();
135 $wgOut->redirect( $url );
141 // show some more forms
142 if( $this->mTarget
!== null ) {
143 $this->editUserGroupsForm( $this->mTarget
);
147 function getSuccessURL() {
148 return $this->getTitle( $this->mTarget
)->getFullURL();
152 * Save user groups changes in the database.
153 * Data comes from the editUserGroupsForm() form function
155 * @param $username String: username to apply changes to.
156 * @param $reason String: reason for group change
159 function saveUserGroups( $username, $reason = '' ) {
160 global $wgRequest, $wgOut;
162 $status = $this->fetchUser( $username );
163 if( !$status->isOK() ) {
164 $wgOut->addWikiText( $status->getWikiText() );
167 $user = $status->value
;
170 $allgroups = $this->getAllGroups();
172 $removegroup = array();
174 // This could possibly create a highly unlikely race condition if permissions are changed between
175 // when the form is loaded and when the form is saved. Ignoring it for the moment.
176 foreach ( $allgroups as $group ) {
177 // We'll tell it to remove all unchecked groups, and add all checked groups.
178 // Later on, this gets filtered for what can actually be removed
179 if ( $wgRequest->getCheck( "wpGroup-$group" ) ) {
180 $addgroup[] = $group;
182 $removegroup[] = $group;
186 $this->doSaveUserGroups( $user, $addgroup, $removegroup, $reason );
190 * Save user groups changes in the database.
192 * @param $user User object
193 * @param $add Array of groups to add
194 * @param $remove Array of groups to remove
195 * @param $reason String: reason for group change
196 * @return Array: Tuple of added, then removed groups
198 function doSaveUserGroups( $user, $add, $remove, $reason = '' ) {
201 // Validate input set...
202 $isself = ( $user->getName() == $wgUser->getName() );
203 $groups = $user->getGroups();
204 $changeable = $this->changeableGroups();
205 $addable = array_merge( $changeable['add'], $isself ?
$changeable['add-self'] : array() );
206 $removable = array_merge( $changeable['remove'], $isself ?
$changeable['remove-self'] : array() );
208 $remove = array_unique(
209 array_intersect( (array)$remove, $removable, $groups ) );
210 $add = array_unique( array_diff(
211 array_intersect( (array)$add, $addable ),
215 $oldGroups = $user->getGroups();
216 $newGroups = $oldGroups;
218 // remove then add groups
220 $newGroups = array_diff( $newGroups, $remove );
221 foreach( $remove as $group ) {
222 $user->removeGroup( $group );
226 $newGroups = array_merge( $newGroups, $add );
227 foreach( $add as $group ) {
228 $user->addGroup( $group );
231 $newGroups = array_unique( $newGroups );
233 // Ensure that caches are cleared
234 $user->invalidateCache( true );
236 wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) );
237 wfDebug( 'newGroups: ' . print_r( $newGroups, true ) );
238 wfRunHooks( 'UserRights', array( &$user, $add, $remove ) );
240 if( $newGroups != $oldGroups ) {
241 $this->addLogEntry( $user, $oldGroups, $newGroups, $reason );
243 return array( $add, $remove );
248 * Add a rights log entry for an action.
250 function addLogEntry( $user, $oldGroups, $newGroups, $reason ) {
251 $log = new LogPage( 'rights' );
253 $log->addEntry( 'rights',
254 $user->getUserPage(),
257 $this->makeGroupNameListForLog( $oldGroups ),
258 $this->makeGroupNameListForLog( $newGroups )
264 * Edit user groups membership
265 * @param $username String: name of the user.
267 function editUserGroupsForm( $username ) {
270 $status = $this->fetchUser( $username );
271 if( !$status->isOK() ) {
272 $wgOut->addWikiText( $status->getWikiText() );
275 $user = $status->value
;
278 $groups = $user->getGroups();
280 $this->showEditUserGroupsForm( $user, $groups );
282 // This isn't really ideal logging behavior, but let's not hide the
283 // interwiki logs if we're using them as is.
284 $this->showLogFragment( $user, $wgOut );
288 * Normalize the input username, which may be local or remote, and
289 * return a user (or proxy) object for manipulating it.
291 * Side effects: error output for invalid access
292 * @return Status object
294 public function fetchUser( $username ) {
295 global $wgUser, $wgUserrightsInterwikiDelimiter;
297 $parts = explode( $wgUserrightsInterwikiDelimiter, $username );
298 if( count( $parts ) < 2 ) {
299 $name = trim( $username );
302 list( $name, $database ) = array_map( 'trim', $parts );
304 if( $database == wfWikiID() ) {
307 if( !$wgUser->isAllowed( 'userrights-interwiki' ) ) {
308 return Status
::newFatal( 'userrights-no-interwiki' );
310 if( !UserRightsProxy
::validDatabase( $database ) ) {
311 return Status
::newFatal( 'userrights-nodatabase', $database );
317 return Status
::newFatal( 'nouserspecified' );
320 if( $name[0] == '#' ) {
321 // Numeric ID can be specified...
322 // We'll do a lookup for the name internally.
323 $id = intval( substr( $name, 1 ) );
325 if( $database == '' ) {
326 $name = User
::whoIs( $id );
328 $name = UserRightsProxy
::whoIs( $database, $id );
332 return Status
::newFatal( 'noname' );
335 $name = User
::getCanonicalName( $name );
336 if( $name === false ) {
338 return Status
::newFatal( 'nosuchusershort', $username );
342 if( $database == '' ) {
343 $user = User
::newFromName( $name );
345 $user = UserRightsProxy
::newFromName( $database, $name );
348 if( !$user ||
$user->isAnon() ) {
349 return Status
::newFatal( 'nosuchusershort', $username );
352 return Status
::newGood( $user );
355 function makeGroupNameList( $ids ) {
356 if( empty( $ids ) ) {
357 return wfMsgForContent( 'rightsnone' );
359 return implode( ', ', $ids );
363 function makeGroupNameListForLog( $ids ) {
364 if( empty( $ids ) ) {
367 return $this->makeGroupNameList( $ids );
372 * Output a form to allow searching for a user
374 function switchForm() {
375 global $wgOut, $wgScript;
377 Html
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'name' => 'uluser', 'id' => 'mw-userrights-form1' ) ) .
378 Html
::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
379 Xml
::fieldset( wfMsg( 'userrights-lookup-user' ) ) .
380 Xml
::inputLabel( wfMsg( 'userrights-user-editname' ), 'user', 'username', 30, str_replace( '_', ' ', $this->mTarget
) ) . ' ' .
381 Xml
::submitButton( wfMsg( 'editusergroup' ) ) .
382 Html
::closeElement( 'fieldset' ) .
383 Html
::closeElement( 'form' ) . "\n"
388 * Go through used and available groups and return the ones that this
389 * form will be able to manipulate based on the current user's system
392 * @param $groups Array: list of groups the given user is in
393 * @return Array: Tuple of addable, then removable groups
395 protected function splitGroups( $groups ) {
396 list( $addable, $removable, $addself, $removeself ) = array_values( $this->changeableGroups() );
398 $removable = array_intersect(
399 array_merge( $this->isself ?
$removeself : array(), $removable ),
401 ); // Can't remove groups the user doesn't have
402 $addable = array_diff(
403 array_merge( $this->isself ?
$addself : array(), $addable ),
405 ); // Can't add groups the user does have
407 return array( $addable, $removable );
411 * Show the form to edit group memberships.
413 * @param $user User or UserRightsProxy you're editing
414 * @param $groups Array: Array of groups the user is in
416 protected function showEditUserGroupsForm( $user, $groups ) {
417 global $wgOut, $wgUser, $wgLang, $wgRequest;
420 foreach( $groups as $group ) {
421 $list[] = self
::buildGroupLink( $group );
425 if ( $user instanceof User
) {
426 foreach( Autopromote
::getAutopromoteGroups( $user ) as $group ) {
427 $autolist[] = self
::buildGroupLink( $group );
432 $count = count( $list );
434 $grouplist = wfMessage( 'userrights-groupsmember', $count)->parse();
435 $grouplist = '<p>' . $grouplist . ' ' . $wgLang->listToText( $list ) . "</p>\n";
437 $count = count( $autolist );
439 $autogrouplistintro = wfMessage( 'userrights-groupsmember-auto', $count)->parse();
440 $grouplist .= '<p>' . $autogrouplistintro . ' ' . $wgLang->listToText( $autolist ) . "</p>\n";
443 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL(), 'name' => 'editGroup', 'id' => 'mw-userrights-form2' ) ) .
444 Html
::hidden( 'user', $this->mTarget
) .
445 Html
::hidden( 'wpEditToken', $wgUser->editToken( $this->mTarget
) ) .
446 Xml
::openElement( 'fieldset' ) .
447 Xml
::element( 'legend', array(), wfMsg( 'userrights-editusergroup' ) ) .
448 wfMsgExt( 'editinguser', array( 'parse' ), wfEscapeWikiText( $user->getName() ) ) .
449 wfMsgExt( 'userrights-groups-help', array( 'parse' ) ) .
451 Xml
::tags( 'p', null, $this->groupCheckboxes( $groups ) ) .
452 Xml
::openElement( 'table', array( 'border' => '0', 'id' => 'mw-userrights-table-outer' ) ) .
454 <td class='mw-label'>" .
455 Xml
::label( wfMsg( 'userrights-reason' ), 'wpReason' ) .
457 <td class='mw-input'>" .
458 Xml
::input( 'user-reason', 60, $wgRequest->getVal( 'user-reason', false ),
459 array( 'id' => 'wpReason', 'maxlength' => 255 ) ) .
464 <td class='mw-submit'>" .
465 Xml
::submitButton( wfMsg( 'saveusergroups' ),
466 array( 'name' => 'saveusergroups' ) +
$wgUser->getSkin()->tooltipAndAccessKeyAttribs( 'userrights-set' ) ) .
469 Xml
::closeElement( 'table' ) . "\n" .
470 Xml
::closeElement( 'fieldset' ) .
471 Xml
::closeElement( 'form' ) . "\n"
476 * Format a link to a group description page
478 * @param $group string
481 private static function buildGroupLink( $group ) {
482 static $cache = array();
483 if( !isset( $cache[$group] ) )
484 $cache[$group] = User
::makeGroupLinkHtml( $group, htmlspecialchars( User
::getGroupName( $group ) ) );
485 return $cache[$group];
489 * Returns an array of all groups that may be edited
490 * @return array Array of groups that may be edited.
492 protected static function getAllGroups() {
493 return User
::getAllGroups();
497 * Adds a table with checkboxes where you can select what groups to add/remove
499 * @param $usergroups Array: groups the user belongs to
500 * @return string XHTML table element with checkboxes
502 private function groupCheckboxes( $usergroups ) {
503 $allgroups = $this->getAllGroups();
506 # Put all column info into an associative array so that extensions can
507 # more easily manage it.
508 $columns = array( 'unchangeable' => array(), 'changeable' => array() );
510 foreach( $allgroups as $group ) {
511 $set = in_array( $group, $usergroups );
512 # Should the checkbox be disabled?
514 ( $set && $this->canRemove( $group ) ) ||
515 ( !$set && $this->canAdd( $group ) ) );
516 # Do we need to point out that this action is irreversible?
517 $irreversible = !$disabled && (
518 ( $set && !$this->canAdd( $group ) ) ||
519 ( !$set && !$this->canRemove( $group ) ) );
523 'disabled' => $disabled,
524 'irreversible' => $irreversible
528 $columns['unchangeable'][$group] = $checkbox;
530 $columns['changeable'][$group] = $checkbox;
534 # Build the HTML table
535 $ret .= Xml
::openElement( 'table', array( 'border' => '0', 'class' => 'mw-userrights-groups' ) ) .
537 foreach( $columns as $name => $column ) {
538 if( $column === array() )
540 $ret .= Xml
::element( 'th', null, wfMessage( 'userrights-' . $name . '-col', count( $column ) )->text() );
542 $ret.= "</tr>\n<tr>\n";
543 foreach( $columns as $column ) {
544 if( $column === array() )
546 $ret .= "\t<td style='vertical-align:top;'>\n";
547 foreach( $column as $group => $checkbox ) {
548 $attr = $checkbox['disabled'] ?
array( 'disabled' => 'disabled' ) : array();
550 if ( $checkbox['irreversible'] ) {
551 $text = htmlspecialchars( wfMsg( 'userrights-irreversible-marker',
552 User
::getGroupMember( $group ) ) );
554 $text = htmlspecialchars( User
::getGroupMember( $group ) );
556 $checkboxHtml = Xml
::checkLabel( $text, "wpGroup-" . $group,
557 "wpGroup-" . $group, $checkbox['set'], $attr );
558 $ret .= "\t\t" . ( $checkbox['disabled']
559 ? Xml
::tags( 'span', array( 'class' => 'mw-userrights-disabled' ), $checkboxHtml )
565 $ret .= Xml
::closeElement( 'tr' ) . Xml
::closeElement( 'table' );
571 * @param $group String: the name of the group to check
572 * @return bool Can we remove the group?
574 private function canRemove( $group ) {
575 // $this->changeableGroups()['remove'] doesn't work, of course. Thanks,
577 $groups = $this->changeableGroups();
578 return in_array( $group, $groups['remove'] ) ||
( $this->isself
&& in_array( $group, $groups['remove-self'] ) );
582 * @param $group string: the name of the group to check
583 * @return bool Can we add the group?
585 private function canAdd( $group ) {
586 $groups = $this->changeableGroups();
587 return in_array( $group, $groups['add'] ) ||
( $this->isself
&& in_array( $group, $groups['add-self'] ) );
591 * Returns $wgUser->changeableGroups()
593 * @return Array array( 'add' => array( addablegroups ), 'remove' => array( removablegroups ) , 'add-self' => array( addablegroups to self), 'remove-self' => array( removable groups from self) )
595 function changeableGroups() {
597 return $wgUser->changeableGroups();
601 * Show a rights log fragment for the specified user
603 * @param $user User to show log for
604 * @param $output OutputPage to use
606 protected function showLogFragment( $user, $output ) {
607 $output->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'rights' ) . "\n" ) );
608 LogEventsList
::showLogExtract( $output, 'rights', $user->getUserPage()->getPrefixedText() );