4 * Special page to allow managing user group membership
6 * @addtogroup SpecialPage
7 * @todo Use checkboxes or something, this list thing is incomprehensible to
12 * A class to manage user levels rights.
13 * @addtogroup SpecialPage
15 class UserrightsPage
extends SpecialPage
{
16 # The target of the local right-adjuster's interest. Can be gotten from
17 # either a GET parameter or a subpage-style parameter, so have a member
20 protected $isself = false;
22 public function __construct() {
23 parent
::__construct( 'Userrights' );
26 public function isRestricted() {
30 public function userCanExecute( $user ) {
31 $available = $this->changeableGroups();
32 return !empty( $available['add'] )
33 or !empty( $available['remove'] )
35 (!empty( $available['add-self'] )
36 or !empty( $available['remove-self'] )));
40 * Manage forms to be shown according to posted data.
41 * Depending on the submit button used, call a form or a save function.
43 * @param mixed $par String if any subpage provided, else null
45 function execute( $par ) {
46 // If the visitor doesn't have permissions to assign or remove
47 // any groups, it's a bit silly to give them the user search prompt.
48 global $wgUser, $wgRequest;
51 $this->mTarget
= $par;
53 $this->mTarget
= $wgRequest->getVal( 'user' );
56 if (!$this->mTarget
) {
58 * If the user specified no target, and they can only
59 * edit their own groups, automatically set them as the
62 $available = $this->changeableGroups();
63 if (empty($available['add']) && empty($available['remove']))
64 $this->mTarget
= $wgUser->getName();
67 if ($this->mTarget
== $wgUser->getName())
70 if( !$this->userCanExecute( $wgUser ) ) {
71 // fixme... there may be intermediate groups we can mention.
73 $wgOut->showPermissionsErrorPage( array(
75 ?
'userrights-nologin'
76 : 'userrights-notallowed' ) );
82 $wgOut->readOnlyPage();
86 $this->outputHeader();
90 // show the general form
93 if( $wgRequest->wasPosted() ) {
95 if( $wgRequest->getCheck( 'saveusergroups' ) ) {
96 $reason = $wgRequest->getVal( 'user-reason' );
97 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $this->mTarget
) ) {
98 $this->saveUserGroups(
106 // show some more forms
107 if( $this->mTarget
) {
108 $this->editUserGroupsForm( $this->mTarget
);
113 * Save user groups changes in the database.
114 * Data comes from the editUserGroupsForm() form function
116 * @param string $username Username to apply changes to.
117 * @param string $reason Reason for group change
120 function saveUserGroups( $username, $reason = '') {
121 global $wgRequest, $wgUser, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
123 $user = $this->fetchUser( $username );
128 $allgroups = User
::getAllGroups();
130 $removegroup = array();
132 // This could possibly create a highly unlikely race condition if permissions are changed between
133 // when the form is loaded and when the form is saved. Ignoring it for the moment.
134 foreach ($allgroups as $group) {
135 // We'll tell it to remove all unchecked groups, and add all checked groups.
136 // Later on, this gets filtered for what can actually be removed
137 if ($wgRequest->getCheck( "wpGroup-$group" )) {
138 $addgroup[] = $group;
140 $removegroup[] = $group;
144 // Validate input set...
145 $changeable = $this->changeableGroups();
146 if ($wgUser->getId() != 0 && $wgUser->getId() == $user->getId()) {
147 $addable = array_merge($changeable['add'], $wgGroupsAddToSelf);
148 $removable = array_merge($changeable['remove'], $wgGroupsRemoveFromSelf);
150 $addable = $changeable['add'];
151 $removable = $changeable['remove'];
154 $removegroup = array_unique(
155 array_intersect( (array)$removegroup, $removable ) );
156 $addgroup = array_unique(
157 array_intersect( (array)$addgroup, $addable ) );
159 $oldGroups = $user->getGroups();
160 $newGroups = $oldGroups;
161 // remove then add groups
163 $newGroups = array_diff($newGroups, $removegroup);
164 foreach( $removegroup as $group ) {
165 $user->removeGroup( $group );
169 $newGroups = array_merge($newGroups, $addgroup);
170 foreach( $addgroup as $group ) {
171 $user->addGroup( $group );
174 $newGroups = array_unique( $newGroups );
176 // Ensure that caches are cleared
177 $user->invalidateCache();
179 wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) );
180 wfDebug( 'newGroups: ' . print_r( $newGroups, true ) );
181 if( $user instanceof User
) {
183 wfRunHooks( 'UserRights', array( &$user, $addgroup, $removegroup ) );
186 if( $newGroups != $oldGroups ) {
187 $log = new LogPage( 'rights' );
189 $log->addEntry( 'rights',
190 $user->getUserPage(),
191 $wgRequest->getText( 'user-reason' ),
193 $this->makeGroupNameList( $oldGroups ),
194 $this->makeGroupNameList( $newGroups )
201 * Edit user groups membership
202 * @param string $username Name of the user.
204 function editUserGroupsForm( $username ) {
207 $user = $this->fetchUser( $username );
212 $groups = $user->getGroups();
214 $this->showEditUserGroupsForm( $user, $groups );
216 // This isn't really ideal logging behavior, but let's not hide the
217 // interwiki logs if we're using them as is.
218 $this->showLogFragment( $user, $wgOut );
222 * Normalize the input username, which may be local or remote, and
223 * return a user (or proxy) object for manipulating it.
225 * Side effects: error output for invalid access
226 * @return mixed User, UserRightsProxy, or null
228 function fetchUser( $username ) {
229 global $wgOut, $wgUser;
231 $parts = explode( '@', $username );
232 if( count( $parts ) < 2 ) {
233 $name = trim( $username );
236 list( $name, $database ) = array_map( 'trim', $parts );
238 if( !$wgUser->isAllowed( 'userrights-interwiki' ) ) {
239 $wgOut->addWikiMsg( 'userrights-no-interwiki' );
242 if( !UserRightsProxy
::validDatabase( $database ) ) {
243 $wgOut->addWikiMsg( 'userrights-nodatabase', $database );
249 $wgOut->addWikiMsg( 'nouserspecified' );
253 if( $name{0} == '#' ) {
254 // Numeric ID can be specified...
255 // We'll do a lookup for the name internally.
256 $id = intval( substr( $name, 1 ) );
258 if( $database == '' ) {
259 $name = User
::whoIs( $id );
261 $name = UserRightsProxy
::whoIs( $database, $id );
265 $wgOut->addWikiMsg( 'noname' );
270 if( $database == '' ) {
271 $user = User
::newFromName( $name );
273 $user = UserRightsProxy
::newFromName( $database, $name );
276 if( !$user ||
$user->isAnon() ) {
277 $wgOut->addWikiMsg( 'nosuchusershort', $username );
284 function makeGroupNameList( $ids ) {
285 return implode( ', ', $ids );
289 * Output a form to allow searching for a user
291 function switchForm() {
292 global $wgOut, $wgScript;
294 Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'name' => 'uluser', 'id' => 'mw-userrights-form1' ) ) .
295 Xml
::hidden( 'title', 'Special:Userrights' ) .
296 Xml
::openElement( 'fieldset' ) .
297 Xml
::element( 'legend', array(), wfMsg( 'userrights-lookup-user' ) ) .
298 Xml
::inputLabel( wfMsg( 'userrights-user-editname' ), 'user', 'username', 30, $this->mTarget
) . ' ' .
299 Xml
::submitButton( wfMsg( 'editusergroup' ) ) .
300 Xml
::closeElement( 'fieldset' ) .
301 Xml
::closeElement( 'form' ) . "\n"
306 * Go through used and available groups and return the ones that this
307 * form will be able to manipulate based on the current user's system
310 * @param $groups Array: list of groups the given user is in
311 * @return Array: Tuple of addable, then removable groups
313 protected function splitGroups( $groups ) {
314 global $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
315 list($addable, $removable) = array_values( $this->changeableGroups() );
317 $removable = array_intersect(
318 array_merge($this->isself ?
$wgGroupsRemoveFromSelf : array(), $removable),
319 $groups ); // Can't remove groups the user doesn't have
320 $addable = array_diff(
321 array_merge($this->isself ?
$wgGroupsAddToSelf : array(), $addable),
322 $groups ); // Can't add groups the user does have
324 return array( $addable, $removable );
328 * Show the form to edit group memberships.
330 * @param $user User or UserRightsProxy you're editing
331 * @param $groups Array: Array of groups the user is in
333 protected function showEditUserGroupsForm( $user, $groups ) {
334 global $wgOut, $wgUser;
336 list( $addable, $removable ) = $this->splitGroups( $groups );
339 foreach( $user->getGroups() as $group )
340 $list[] = self
::buildGroupLink( $group );
343 if( count( $list ) > 0 ) {
344 $grouplist = '<p>' . wfMsgHtml( 'userrights-groupsmember' ) . ' ' . implode( ', ', $list ) . '</p>';
347 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL(), 'name' => 'editGroup', 'id' => 'mw-userrights-form2' ) ) .
348 Xml
::hidden( 'user', $this->mTarget
) .
349 Xml
::hidden( 'wpEditToken', $wgUser->editToken( $this->mTarget
) ) .
350 Xml
::openElement( 'fieldset' ) .
351 Xml
::element( 'legend', array(), wfMsg( 'userrights-editusergroup' ) ) .
352 wfMsgExt( 'editinguser', array( 'parse' ), wfEscapeWikiText( $user->getName() ) ) .
354 Xml
::openElement( 'p') . $this->groupCheckboxes( $groups ) . Xml
::closeElement( 'p' ) .
355 Xml
::openElement( 'table', array( 'border' => '0', 'id' => 'mw-userrights-table-outer' ) ) .
358 $wgOut->parse( wfMsg( 'userrights-groups-help' ) ) .
363 Xml
::label( wfMsg( 'userrights-reason' ), 'wpReason' ) .
366 Xml
::input( 'user-reason', 60, false, array( 'id' => 'wpReason', 'maxlength' => 255 ) ) .
372 Xml
::submitButton( wfMsg( 'saveusergroups' ), array( 'name' => 'saveusergroups' ) ) .
375 Xml
::closeElement( 'table' ) . "\n" .
376 Xml
::closeElement( 'fieldset' ) .
377 Xml
::closeElement( 'form' ) . "\n"
382 * Format a link to a group description page
384 * @param string $group
387 private static function buildGroupLink( $group ) {
388 static $cache = array();
389 if( !isset( $cache[$group] ) )
390 $cache[$group] = User
::makeGroupLinkHtml( $group, User
::getGroupName( $group ) );
391 return $cache[$group];
395 * Adds the <select> thingie where you can select what groups to add/remove
397 * @param array $groups The groups that can be added/removed
398 * @param string $name 'removable' or 'available'
399 * @return string XHTML <select> element
401 private function groupCheckboxes( $usergroups ) {
402 $allgroups = User
::getAllGroups();
407 $unsettable_col = '';
409 foreach ($allgroups as $group) {
410 $set = in_array( $group, $usergroups );
412 ( $set && $this->canRemove( $group ) ) ||
413 ( !$set && $this->canAdd( $group ) ) );
415 $attr = $disabled ?
array( 'disabled' => 'disabled' ) : array();
416 $checkbox = wfCheckLabel( User
::getGroupMember( $group ), "wpGroup-$group",
417 "wpGroup-$group", $set, $attr );
418 $checkbox = $disabled ?
"<span class='mw-userrights-disabled'>$checkbox</span>" : $checkbox;
421 $unsettable_col .= "$checkbox<br/>\n";
423 $settable_col .= "$checkbox<br/>\n";
428 $ret .= '<table class="mw-userrights-groups">';
429 $ret .= '<tr><th>'.wfMsgHtml('userrights-changeable-col').'</th><th>'.wfMsgHtml('userrights-unchangeable-col').'</th></tr>';
430 $ret .= "<tr><td valign=\"top\">$settable_col</td><td valign=\"top\">$unsettable_col</td></tr>";
438 * @param string $group The name of the group to check
439 * @return bool Can we remove the group?
441 private function canRemove( $group ) {
442 // $this->changeableGroups()['remove'] doesn't work, of course. Thanks,
444 $groups = $this->changeableGroups();
445 return in_array( $group, $groups['remove'] ) ||
($this->isself
&& in_array( $group, $groups['remove-self'] ));
449 * @param string $group The name of the group to check
450 * @return bool Can we add the group?
452 private function canAdd( $group ) {
453 $groups = $this->changeableGroups();
454 return in_array( $group, $groups['add'] ) ||
($this->isself
&& in_array( $group, $groups['add-self'] ));
458 * Returns an array of the groups that the user can add/remove.
460 * @return Array array( 'add' => array( addablegroups ), 'remove' => array( removablegroups ) )
462 function changeableGroups() {
463 global $wgUser, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
465 if( $wgUser->isAllowed( 'userrights' ) ) {
466 // This group gives the right to modify everything (reverse-
467 // compatibility with old "userrights lets you change
469 // Using array_merge to make the groups reindexed
470 $all = array_merge( User
::getAllGroups() );
474 'add-self' => array(),
475 'remove-self' => array()
479 // Okay, it's not so simple, we will have to go through the arrays
483 'add-self' => $wgGroupsAddToSelf,
484 'remove-self' => $wgGroupsRemoveFromSelf);
485 $addergroups = $wgUser->getEffectiveGroups();
487 foreach ($addergroups as $addergroup) {
488 $groups = array_merge_recursive(
489 $groups, $this->changeableByGroup($addergroup)
491 $groups['add'] = array_unique( $groups['add'] );
492 $groups['remove'] = array_unique( $groups['remove'] );
498 * Returns an array of the groups that a particular group can add/remove.
500 * @param String $group The group to check for whether it can add/remove
501 * @return Array array( 'add' => array( addablegroups ), 'remove' => array( removablegroups ) )
503 private function changeableByGroup( $group ) {
504 global $wgAddGroups, $wgRemoveGroups;
506 $groups = array( 'add' => array(), 'remove' => array() );
507 if( empty($wgAddGroups[$group]) ) {
508 // Don't add anything to $groups
509 } elseif( $wgAddGroups[$group] === true ) {
510 // You get everything
511 $groups['add'] = User
::getAllGroups();
512 } elseif( is_array($wgAddGroups[$group]) ) {
513 $groups['add'] = $wgAddGroups[$group];
516 // Same thing for remove
517 if( empty($wgRemoveGroups[$group]) ) {
518 } elseif($wgRemoveGroups[$group] === true ) {
519 $groups['remove'] = User
::getAllGroups();
520 } elseif( is_array($wgRemoveGroups[$group]) ) {
521 $groups['remove'] = $wgRemoveGroups[$group];
527 * Show a rights log fragment for the specified user
529 * @param User $user User to show log for
530 * @param OutputPage $output OutputPage to use
532 protected function showLogFragment( $user, $output ) {
533 $viewer = new LogViewer(
538 'page' => $user->getUserPage()->getPrefixedText(),
543 $output->addHtml( Xml
::element( 'h2', null, LogPage
::logName( 'rights' ) . "\n" ) );
544 $viewer->showList( $output );