3 * Special page to allow managing user group membership
10 * A class to manage user levels rights.
11 * @ingroup SpecialPage
13 class UserrightsPage
extends SpecialPage
{
14 # The target of the local right-adjuster's interest. Can be gotten from
15 # either a GET parameter or a subpage-style parameter, so have a member
18 protected $isself = false;
20 public function __construct() {
21 parent
::__construct( 'Userrights' );
24 public function isRestricted() {
28 public function userCanExecute( $user ) {
29 return $this->userCanChangeRights( $user, false );
32 public function userCanChangeRights( $user, $checkIfSelf = true ) {
33 $available = $this->changeableGroups();
34 return !empty( $available['add'] )
35 or !empty( $available['remove'] )
36 or ( ( $this->isself ||
!$checkIfSelf ) and
37 (!empty( $available['add-self'] )
38 or !empty( $available['remove-self'] )));
42 * Manage forms to be shown according to posted data.
43 * Depending on the submit button used, call a form or a save function.
45 * @param $par Mixed: string if any subpage provided, else null
47 function execute( $par ) {
48 // If the visitor doesn't have permissions to assign or remove
49 // any groups, it's a bit silly to give them the user search prompt.
50 global $wgUser, $wgRequest;
53 $this->mTarget
= $par;
55 $this->mTarget
= $wgRequest->getVal( 'user' );
58 if (!$this->mTarget
) {
60 * If the user specified no target, and they can only
61 * edit their own groups, automatically set them as the
64 $available = $this->changeableGroups();
65 if (empty($available['add']) && empty($available['remove']))
66 $this->mTarget
= $wgUser->getName();
69 if ($this->mTarget
== $wgUser->getName())
72 if( !$this->userCanChangeRights( $wgUser, true ) ) {
73 // fixme... there may be intermediate groups we can mention.
75 $wgOut->showPermissionsErrorPage( array(
77 ?
'userrights-nologin'
78 : 'userrights-notallowed' ) );
84 $wgOut->readOnlyPage();
88 $this->outputHeader();
92 // show the general form
95 if( $wgRequest->wasPosted() ) {
97 if( $wgRequest->getCheck( 'saveusergroups' ) ) {
98 $reason = $wgRequest->getVal( 'user-reason' );
99 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $this->mTarget
) ) {
100 $this->saveUserGroups(
108 // show some more forms
109 if( $this->mTarget
) {
110 $this->editUserGroupsForm( $this->mTarget
);
115 * Save user groups changes in the database.
116 * Data comes from the editUserGroupsForm() form function
118 * @param $username String: username to apply changes to.
119 * @param $reason String: reason for group change
122 function saveUserGroups( $username, $reason = '') {
123 global $wgRequest, $wgUser, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
125 $user = $this->fetchUser( $username );
130 $allgroups = $this->getAllGroups();
132 $removegroup = array();
134 // This could possibly create a highly unlikely race condition if permissions are changed between
135 // when the form is loaded and when the form is saved. Ignoring it for the moment.
136 foreach ($allgroups as $group) {
137 // We'll tell it to remove all unchecked groups, and add all checked groups.
138 // Later on, this gets filtered for what can actually be removed
139 if ($wgRequest->getCheck( "wpGroup-$group" )) {
140 $addgroup[] = $group;
142 $removegroup[] = $group;
146 // Validate input set...
147 $changeable = $this->changeableGroups();
148 $addable = array_merge( $changeable['add'], $this->isself ?
$changeable['add-self'] : array() );
149 $removable = array_merge( $changeable['remove'], $this->isself ?
$changeable['remove-self'] : array() );
151 $removegroup = array_unique(
152 array_intersect( (array)$removegroup, $removable ) );
153 $addgroup = array_unique(
154 array_intersect( (array)$addgroup, $addable ) );
156 $oldGroups = $user->getGroups();
157 $newGroups = $oldGroups;
158 // remove then add groups
160 $newGroups = array_diff($newGroups, $removegroup);
161 foreach( $removegroup as $group ) {
162 $user->removeGroup( $group );
166 $newGroups = array_merge($newGroups, $addgroup);
167 foreach( $addgroup as $group ) {
168 $user->addGroup( $group );
171 $newGroups = array_unique( $newGroups );
173 // Ensure that caches are cleared
174 $user->invalidateCache();
176 wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) );
177 wfDebug( 'newGroups: ' . print_r( $newGroups, true ) );
178 if( $user instanceof User
) {
180 wfRunHooks( 'UserRights', array( &$user, $addgroup, $removegroup ) );
183 if( $newGroups != $oldGroups ) {
184 $this->addLogEntry( $user, $oldGroups, $newGroups );
189 * Add a rights log entry for an action.
191 function addLogEntry( $user, $oldGroups, $newGroups ) {
193 $log = new LogPage( 'rights' );
195 $log->addEntry( 'rights',
196 $user->getUserPage(),
197 $wgRequest->getText( 'user-reason' ),
199 $this->makeGroupNameListForLog( $oldGroups ),
200 $this->makeGroupNameListForLog( $newGroups )
206 * Edit user groups membership
207 * @param $username String: name of the user.
209 function editUserGroupsForm( $username ) {
212 $user = $this->fetchUser( $username );
217 $groups = $user->getGroups();
219 $this->showEditUserGroupsForm( $user, $groups );
221 // This isn't really ideal logging behavior, but let's not hide the
222 // interwiki logs if we're using them as is.
223 $this->showLogFragment( $user, $wgOut );
227 * Normalize the input username, which may be local or remote, and
228 * return a user (or proxy) object for manipulating it.
230 * Side effects: error output for invalid access
231 * @return mixed User, UserRightsProxy, or null
233 function fetchUser( $username ) {
234 global $wgOut, $wgUser;
236 $parts = explode( '@', $username );
237 if( count( $parts ) < 2 ) {
238 $name = trim( $username );
241 list( $name, $database ) = array_map( 'trim', $parts );
243 if( !$wgUser->isAllowed( 'userrights-interwiki' ) ) {
244 $wgOut->addWikiMsg( 'userrights-no-interwiki' );
247 if( !UserRightsProxy
::validDatabase( $database ) ) {
248 $wgOut->addWikiMsg( 'userrights-nodatabase', $database );
254 $wgOut->addWikiMsg( 'nouserspecified' );
258 if( $name{0} == '#' ) {
259 // Numeric ID can be specified...
260 // We'll do a lookup for the name internally.
261 $id = intval( substr( $name, 1 ) );
263 if( $database == '' ) {
264 $name = User
::whoIs( $id );
266 $name = UserRightsProxy
::whoIs( $database, $id );
270 $wgOut->addWikiMsg( 'noname' );
275 if( $database == '' ) {
276 $user = User
::newFromName( $name );
278 $user = UserRightsProxy
::newFromName( $database, $name );
281 if( !$user ||
$user->isAnon() ) {
282 $wgOut->addWikiMsg( 'nosuchusershort', $username );
289 function makeGroupNameList( $ids ) {
290 if( empty( $ids ) ) {
291 return wfMsgForContent( 'rightsnone' );
293 return implode( ', ', $ids );
297 function makeGroupNameListForLog( $ids ) {
298 if( empty( $ids ) ) {
301 return $this->makeGroupNameList( $ids );
306 * Output a form to allow searching for a user
308 function switchForm() {
309 global $wgOut, $wgScript;
311 Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'name' => 'uluser', 'id' => 'mw-userrights-form1' ) ) .
312 Xml
::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
313 Xml
::openElement( 'fieldset' ) .
314 Xml
::element( 'legend', array(), wfMsg( 'userrights-lookup-user' ) ) .
315 Xml
::inputLabel( wfMsg( 'userrights-user-editname' ), 'user', 'username', 30, $this->mTarget
) . ' ' .
316 Xml
::submitButton( wfMsg( 'editusergroup' ) ) .
317 Xml
::closeElement( 'fieldset' ) .
318 Xml
::closeElement( 'form' ) . "\n"
323 * Go through used and available groups and return the ones that this
324 * form will be able to manipulate based on the current user's system
327 * @param $groups Array: list of groups the given user is in
328 * @return Array: Tuple of addable, then removable groups
330 protected function splitGroups( $groups ) {
331 list($addable, $removable, $addself, $removeself) = array_values( $this->changeableGroups() );
333 $removable = array_intersect(
334 array_merge( $this->isself ?
$removeself : array(), $removable ),
335 $groups ); // Can't remove groups the user doesn't have
336 $addable = array_diff(
337 array_merge( $this->isself ?
$addself : array(), $addable ),
338 $groups ); // Can't add groups the user does have
340 return array( $addable, $removable );
344 * Show the form to edit group memberships.
346 * @param $user User or UserRightsProxy you're editing
347 * @param $groups Array: Array of groups the user is in
349 protected function showEditUserGroupsForm( $user, $groups ) {
350 global $wgOut, $wgUser, $wgLang;
352 list( $addable, $removable ) = $this->splitGroups( $groups );
354 wfRunHooks( 'UserRights::showEditUserGroupsForm', array( &$user, &$addable, &$removable ) );
357 foreach( $user->getGroups() as $group )
358 $list[] = self
::buildGroupLink( $group );
361 if( count( $list ) > 0 ) {
362 $grouplist = wfMsgHtml( 'userrights-groupsmember' );
363 $grouplist = '<p>' . $grouplist . ' ' . $wgLang->listToText( $list ) . '</p>';
366 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL(), 'name' => 'editGroup', 'id' => 'mw-userrights-form2' ) ) .
367 Xml
::hidden( 'user', $this->mTarget
) .
368 Xml
::hidden( 'wpEditToken', $wgUser->editToken( $this->mTarget
) ) .
369 Xml
::openElement( 'fieldset' ) .
370 Xml
::element( 'legend', array(), wfMsg( 'userrights-editusergroup' ) ) .
371 wfMsgExt( 'editinguser', array( 'parse' ), wfEscapeWikiText( $user->getName() ) ) .
372 wfMsgExt( 'userrights-groups-help', array( 'parse' ) ) .
374 Xml
::tags( 'p', null, $this->groupCheckboxes( $groups ) ) .
375 Xml
::openElement( 'table', array( 'border' => '0', 'id' => 'mw-userrights-table-outer' ) ) .
377 <td class='mw-label'>" .
378 Xml
::label( wfMsg( 'userrights-reason' ), 'wpReason' ) .
380 <td class='mw-input'>" .
381 Xml
::input( 'user-reason', 60, false, array( 'id' => 'wpReason', 'maxlength' => 255 ) ) .
386 <td class='mw-submit'>" .
387 Xml
::submitButton( wfMsg( 'saveusergroups' ), array( 'name' => 'saveusergroups', 'accesskey' => 's' ) ) .
390 Xml
::closeElement( 'table' ) . "\n" .
391 Xml
::closeElement( 'fieldset' ) .
392 Xml
::closeElement( 'form' ) . "\n"
397 * Format a link to a group description page
399 * @param $group string
402 private static function buildGroupLink( $group ) {
403 static $cache = array();
404 if( !isset( $cache[$group] ) )
405 $cache[$group] = User
::makeGroupLinkHtml( $group, User
::getGroupName( $group ) );
406 return $cache[$group];
410 * Returns an array of all groups that may be edited
411 * @return array Array of groups that may be edited.
413 protected static function getAllGroups() {
414 return User
::getAllGroups();
418 * Adds a table with checkboxes where you can select what groups to add/remove
420 * @param $usergroups Array: groups the user belongs to
421 * @return string XHTML table element with checkboxes
423 private function groupCheckboxes( $usergroups ) {
424 $allgroups = $this->getAllGroups();
429 $unsettable_col = '';
431 foreach ($allgroups as $group) {
432 $set = in_array( $group, $usergroups );
433 # Should the checkbox be disabled?
435 ( $set && $this->canRemove( $group ) ) ||
436 ( !$set && $this->canAdd( $group ) ) );
437 # Do we need to point out that this action is irreversible?
438 $irreversible = !$disabled && (
439 ($set && !$this->canAdd( $group )) ||
440 (!$set && !$this->canRemove( $group ) ) );
442 $attr = $disabled ?
array( 'disabled' => 'disabled' ) : array();
443 $text = $irreversible
444 ?
wfMsgHtml( 'userrights-irreversible-marker', User
::getGroupMember( $group ) )
445 : User
::getGroupMember( $group );
446 $checkbox = Xml
::checkLabel( $text, "wpGroup-$group",
447 "wpGroup-$group", $set, $attr );
448 $checkbox = $disabled ? Xml
::tags( 'span', array( 'class' => 'mw-userrights-disabled' ), $checkbox ) : $checkbox;
451 $unsettable_col .= "$checkbox<br />\n";
453 $settable_col .= "$checkbox<br />\n";
458 $ret .= Xml
::openElement( 'table', array( 'border' => '0', 'class' => 'mw-userrights-groups' ) ) .
461 if( $settable_col !== '' ) {
462 $ret .= xml
::element( 'th', null, wfMsg( 'userrights-changeable-col' ) );
464 if( $unsettable_col !== '' ) {
465 $ret .= xml
::element( 'th', null, wfMsg( 'userrights-unchangeable-col' ) );
470 if( $settable_col !== '' ) {
472 " <td style='vertical-align:top;'>
477 if( $unsettable_col !== '' ) {
479 " <td style='vertical-align:top;'>
484 $ret .= Xml
::closeElement( 'tr' ) . Xml
::closeElement( 'table' );
491 * @param $group String: the name of the group to check
492 * @return bool Can we remove the group?
494 private function canRemove( $group ) {
495 // $this->changeableGroups()['remove'] doesn't work, of course. Thanks,
497 $groups = $this->changeableGroups();
498 return in_array( $group, $groups['remove'] ) ||
($this->isself
&& in_array( $group, $groups['remove-self'] ));
502 * @param $group string: the name of the group to check
503 * @return bool Can we add the group?
505 private function canAdd( $group ) {
506 $groups = $this->changeableGroups();
507 return in_array( $group, $groups['add'] ) ||
($this->isself
&& in_array( $group, $groups['add-self'] ));
511 * Returns an array of the groups that the user can add/remove.
513 * @return Array array( 'add' => array( addablegroups ), 'remove' => array( removablegroups ) , 'add-self' => array( addablegroups to self), 'remove-self' => array( removable groups from self) )
515 function changeableGroups() {
518 if( $wgUser->isAllowed( 'userrights' ) ) {
519 // This group gives the right to modify everything (reverse-
520 // compatibility with old "userrights lets you change
522 // Using array_merge to make the groups reindexed
523 $all = array_merge( User
::getAllGroups() );
527 'add-self' => array(),
528 'remove-self' => array()
532 // Okay, it's not so simple, we will have to go through the arrays
536 'add-self' => array(),
537 'remove-self' => array() );
538 $addergroups = $wgUser->getEffectiveGroups();
540 foreach ($addergroups as $addergroup) {
541 $groups = array_merge_recursive(
542 $groups, $this->changeableByGroup($addergroup)
544 $groups['add'] = array_unique( $groups['add'] );
545 $groups['remove'] = array_unique( $groups['remove'] );
546 $groups['add-self'] = array_unique( $groups['add-self'] );
547 $groups['remove-self'] = array_unique( $groups['remove-self'] );
550 // Run a hook because we can
551 wfRunHooks( 'UserrightsChangeableGroups', array( $this, $wgUser, $addergroups, &$groups ) );
557 * Returns an array of the groups that a particular group can add/remove.
559 * @param $group String: the group to check for whether it can add/remove
560 * @return Array array( 'add' => array( addablegroups ), 'remove' => array( removablegroups ) , 'add-self' => array( addablegroups to self), 'remove-self' => array( removable groups from self) )
562 private function changeableByGroup( $group ) {
563 global $wgAddGroups, $wgRemoveGroups, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
565 $groups = array( 'add' => array(), 'remove' => array(), 'add-self' => array(), 'remove-self' => array() );
566 if( empty($wgAddGroups[$group]) ) {
567 // Don't add anything to $groups
568 } elseif( $wgAddGroups[$group] === true ) {
569 // You get everything
570 $groups['add'] = User
::getAllGroups();
571 } elseif( is_array($wgAddGroups[$group]) ) {
572 $groups['add'] = $wgAddGroups[$group];
575 // Same thing for remove
576 if( empty($wgRemoveGroups[$group]) ) {
577 } elseif($wgRemoveGroups[$group] === true ) {
578 $groups['remove'] = User
::getAllGroups();
579 } elseif( is_array($wgRemoveGroups[$group]) ) {
580 $groups['remove'] = $wgRemoveGroups[$group];
583 // Re-map numeric keys of AddToSelf/RemoveFromSelf to the 'user' key for backwards compatibility
584 if( empty($wgGroupsAddToSelf['user']) ||
$wgGroupsAddToSelf['user'] !== true ) {
585 foreach($wgGroupsAddToSelf as $key => $value) {
587 $wgGroupsAddToSelf['user'][] = $value;
592 if( empty($wgGroupsRemoveFromSelf['user']) ||
$wgGroupsRemoveFromSelf['user'] !== true ) {
593 foreach($wgGroupsRemoveFromSelf as $key => $value) {
595 $wgGroupsRemoveFromSelf['user'][] = $value;
600 // Now figure out what groups the user can add to him/herself
601 if( empty($wgGroupsAddToSelf[$group]) ) {
602 } elseif( $wgGroupsAddToSelf[$group] === true ) {
603 // No idea WHY this would be used, but it's there
604 $groups['add-self'] = User
::getAllGroups();
605 } elseif( is_array($wgGroupsAddToSelf[$group]) ) {
606 $groups['add-self'] = $wgGroupsAddToSelf[$group];
609 if( empty($wgGroupsRemoveFromSelf[$group]) ) {
610 } elseif( $wgGroupsRemoveFromSelf[$group] === true ) {
611 $groups['remove-self'] = User
::getAllGroups();
612 } elseif( is_array($wgGroupsRemoveFromSelf[$group]) ) {
613 $groups['remove-self'] = $wgGroupsRemoveFromSelf[$group];
620 * Show a rights log fragment for the specified user
622 * @param $user User to show log for
623 * @param $output OutputPage to use
625 protected function showLogFragment( $user, $output ) {
626 $output->addHtml( Xml
::element( 'h2', null, LogPage
::logName( 'rights' ) . "\n" ) );
627 LogEventsList
::showLogExtract( $output, 'rights', $user->getUserPage()->getPrefixedText() );