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(
100 $wgRequest->getArray( 'removable' ),
101 $wgRequest->getArray( 'available' ),
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 string $username Username to apply changes to.
119 * @param array $removegroup id of groups to be removed.
120 * @param array $addgroup id of groups to be added.
121 * @param string $reason Reason for group change
124 function saveUserGroups( $username, $removegroup, $addgroup, $reason = '') {
125 global $wgUser, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
127 $user = $this->fetchUser( $username );
132 // Validate input set...
133 $changeable = $this->changeableGroups();
134 if ($wgUser->getId() != 0 && $wgUser->getId() == $user->getId()) {
135 $addable = array_merge($changeable['add'], $wgGroupsAddToSelf);
136 $removable = array_merge($changeable['remove'], $wgGroupsRemoveFromSelf);
138 $addable = $changeable['add'];
139 $removable = $changeable['remove'];
142 $removegroup = array_unique(
143 array_intersect( (array)$removegroup, $removable ) );
144 $addgroup = array_unique(
145 array_intersect( (array)$addgroup, $addable ) );
147 $oldGroups = $user->getGroups();
148 $newGroups = $oldGroups;
149 // remove then add groups
151 $newGroups = array_diff($newGroups, $removegroup);
152 foreach( $removegroup as $group ) {
153 $user->removeGroup( $group );
157 $newGroups = array_merge($newGroups, $addgroup);
158 foreach( $addgroup as $group ) {
159 $user->addGroup( $group );
162 $newGroups = array_unique( $newGroups );
164 // Ensure that caches are cleared
165 $user->invalidateCache();
167 wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) );
168 wfDebug( 'newGroups: ' . print_r( $newGroups, true ) );
169 if( $user instanceof User
) {
171 wfRunHooks( 'UserRights', array( &$user, $addgroup, $removegroup ) );
174 if( $newGroups != $oldGroups ) {
175 $log = new LogPage( 'rights' );
178 $log->addEntry( 'rights',
179 $user->getUserPage(),
180 $wgRequest->getText( 'user-reason' ),
182 $this->makeGroupNameList( $oldGroups ),
183 $this->makeGroupNameList( $newGroups )
190 * Edit user groups membership
191 * @param string $username Name of the user.
193 function editUserGroupsForm( $username ) {
196 $user = $this->fetchUser( $username );
201 $groups = $user->getGroups();
203 $this->showEditUserGroupsForm( $user, $groups );
205 // This isn't really ideal logging behavior, but let's not hide the
206 // interwiki logs if we're using them as is.
207 $this->showLogFragment( $user, $wgOut );
211 * Normalize the input username, which may be local or remote, and
212 * return a user (or proxy) object for manipulating it.
214 * Side effects: error output for invalid access
215 * @return mixed User, UserRightsProxy, or null
217 function fetchUser( $username ) {
218 global $wgOut, $wgUser;
220 $parts = explode( '@', $username );
221 if( count( $parts ) < 2 ) {
222 $name = trim( $username );
225 list( $name, $database ) = array_map( 'trim', $parts );
227 if( !$wgUser->isAllowed( 'userrights-interwiki' ) ) {
228 $wgOut->addWikiMsg( 'userrights-no-interwiki' );
231 if( !UserRightsProxy
::validDatabase( $database ) ) {
232 $wgOut->addWikiMsg( 'userrights-nodatabase', $database );
238 $wgOut->addWikiMsg( 'nouserspecified' );
242 if( $name{0} == '#' ) {
243 // Numeric ID can be specified...
244 // We'll do a lookup for the name internally.
245 $id = intval( substr( $name, 1 ) );
247 if( $database == '' ) {
248 $name = User
::whoIs( $id );
250 $name = UserRightsProxy
::whoIs( $database, $id );
254 $wgOut->addWikiMsg( 'noname' );
259 if( $database == '' ) {
260 $user = User
::newFromName( $name );
262 $user = UserRightsProxy
::newFromName( $database, $name );
265 if( !$user ||
$user->isAnon() ) {
266 $wgOut->addWikiMsg( 'nosuchusershort', $username );
273 function makeGroupNameList( $ids ) {
274 return implode( ', ', $ids );
278 * Output a form to allow searching for a user
280 function switchForm() {
281 global $wgOut, $wgScript;
283 Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'name' => 'uluser', 'id' => 'mw-userrights-form1' ) ) .
284 Xml
::hidden( 'title', 'Special:Userrights' ) .
285 Xml
::openElement( 'fieldset' ) .
286 Xml
::element( 'legend', array(), wfMsg( 'userrights-lookup-user' ) ) .
287 Xml
::inputLabel( wfMsg( 'userrights-user-editname' ), 'user', 'username', 30, $this->mTarget
) . ' ' .
288 Xml
::submitButton( wfMsg( 'editusergroup' ) ) .
289 Xml
::closeElement( 'fieldset' ) .
290 Xml
::closeElement( 'form' ) . "\n"
295 * Go through used and available groups and return the ones that this
296 * form will be able to manipulate based on the current user's system
299 * @param $groups Array: list of groups the given user is in
300 * @return Array: Tuple of addable, then removable groups
302 protected function splitGroups( $groups ) {
303 global $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
304 list($addable, $removable) = array_values( $this->changeableGroups() );
306 $removable = array_intersect(
307 array_merge($this->isself ?
$wgGroupsRemoveFromSelf : array(), $removable),
308 $groups ); // Can't remove groups the user doesn't have
309 $addable = array_diff(
310 array_merge($this->isself ?
$wgGroupsAddToSelf : array(), $addable),
311 $groups ); // Can't add groups the user does have
313 return array( $addable, $removable );
317 * Show the form to edit group memberships.
319 * @param $user User or UserRightsProxy you're editing
320 * @param $groups Array: Array of groups the user is in
322 protected function showEditUserGroupsForm( $user, $groups ) {
323 global $wgOut, $wgUser;
325 list( $addable, $removable ) = $this->splitGroups( $groups );
328 foreach( $user->getGroups() as $group )
329 $list[] = self
::buildGroupLink( $group );
332 if( count( $list ) > 0 ) {
333 $grouplist = '<p>' . wfMsgHtml( 'userrights-groupsmember' ) . ' ' . implode( ', ', $list ) . '</p>';
336 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL(), 'name' => 'editGroup', 'id' => 'mw-userrights-form2' ) ) .
337 Xml
::hidden( 'user', $this->mTarget
) .
338 Xml
::hidden( 'wpEditToken', $wgUser->editToken( $this->mTarget
) ) .
339 Xml
::openElement( 'fieldset' ) .
340 Xml
::element( 'legend', array(), wfMsg( 'userrights-editusergroup' ) ) .
341 wfMsgExt( 'editinguser', array( 'parse' ), wfEscapeWikiText( $user->getName() ) ) .
343 $this->explainRights() .
344 Xml
::openElement( 'table', array( 'border' => '0', 'id' => 'mw-userrights-table-outer' ) ) .
348 Xml
::openElement( 'table', array( 'style' => 'width:400px;', 'id' => 'mw-userrights-table-inner' ) ) .
350 <td style='width:50%;'>" .
351 $this->removeSelect( $removable ) .
353 <td style='width:50%;'>" .
354 $this->addSelect( $addable ) .
357 Xml
::closeElement( 'table' ) .
361 $wgOut->parse( wfMsg( 'userrights-groupshelp' ) ) .
366 Xml
::label( wfMsg( 'userrights-reason' ), 'wpReason' ) .
369 Xml
::input( 'user-reason', 60, false, array( 'id' => 'wpReason', 'maxlength' => 255 ) ) .
375 Xml
::submitButton( wfMsg( 'saveusergroups' ), array( 'name' => 'saveusergroups' ) ) .
378 Xml
::closeElement( 'table' ) . "\n" .
379 Xml
::closeElement( 'fieldset' ) .
380 Xml
::closeElement( 'form' ) . "\n"
385 * Format a link to a group description page
387 * @param string $group
390 private static function buildGroupLink( $group ) {
391 static $cache = array();
392 if( !isset( $cache[$group] ) )
393 $cache[$group] = User
::makeGroupLinkHtml( $group, User
::getGroupMember( $group ) );
394 return $cache[$group];
398 * Prepare a list of groups the user is able to add and remove
402 private function explainRights() {
403 global $wgUser, $wgLang;
406 list( $add, $remove, $addself, $rmself ) = array_values( $this->changeableGroups() );
408 if( count( $add ) > 0 )
409 $out[] = wfMsgExt( 'userrights-available-add', 'parseinline',
410 $wgLang->listToText( $add ), count( $add ) );
411 if( count( $remove ) > 0 )
412 $out[] = wfMsgExt( 'userrights-available-remove', 'parseinline',
413 $wgLang->listToText( $remove ), count( $add ) );
414 if( count( $addself ) > 0 )
415 $out[] = wfMsgExt( 'userrights-available-add-self', 'parseinline',
416 $wgLang->listToText( $addself ), count( $addself ) );
417 if( count( $rmself ) > 0 )
418 $out[] = wfMsgExt( 'userrights-available-remove-self', 'parseinline',
419 $wgLang->listToText( $rmself ), count( $rmself ) );
421 return count( $out ) > 0
422 ?
implode( '<br />', $out )
423 : wfMsgExt( 'userrights-available-none', 'parseinline' );
427 * Adds the <select> thingie where you can select what groups to remove
429 * @param array $groups The groups that can be removed
430 * @return string XHTML <select> element
432 private function removeSelect( $groups ) {
433 return $this->doSelect( $groups, 'removable' );
437 * Adds the <select> thingie where you can select what groups to add
439 * @param array $groups The groups that can be added
440 * @return string XHTML <select> element
442 private function addSelect( $groups ) {
443 return $this->doSelect( $groups, 'available' );
447 * Adds the <select> thingie where you can select what groups to add/remove
449 * @param array $groups The groups that can be added/removed
450 * @param string $name 'removable' or 'available'
451 * @return string XHTML <select> element
453 private function doSelect( $groups, $name ) {
454 $ret = wfMsgHtml( "{$this->mName}-groups$name" ) .
455 Xml
::openElement( 'select', array(
456 'name' => "{$name}[]",
457 'multiple' => 'multiple',
459 'style' => 'width: 100%;'
462 foreach ($groups as $group) {
463 $ret .= Xml
::element( 'option', array( 'value' => $group ), User
::getGroupName( $group ) );
465 $ret .= Xml
::closeElement( 'select' );
470 * @param string $group The name of the group to check
471 * @return bool Can we remove the group?
473 private function canRemove( $group ) {
474 // $this->changeableGroups()['remove'] doesn't work, of course. Thanks,
476 $groups = $this->changeableGroups();
477 return in_array( $group, $groups['remove'] );
481 * @param string $group The name of the group to check
482 * @return bool Can we add the group?
484 private function canAdd( $group ) {
485 $groups = $this->changeableGroups();
486 return in_array( $group, $groups['add'] );
490 * Returns an array of the groups that the user can add/remove.
492 * @return Array array( 'add' => array( addablegroups ), 'remove' => array( removablegroups ) )
494 function changeableGroups() {
495 global $wgUser, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
497 if( $wgUser->isAllowed( 'userrights' ) ) {
498 // This group gives the right to modify everything (reverse-
499 // compatibility with old "userrights lets you change
501 // Using array_merge to make the groups reindexed
502 $all = array_merge( User
::getAllGroups() );
506 'add-self' => array(),
507 'remove-self' => array()
511 // Okay, it's not so simple, we will have to go through the arrays
515 'add-self' => $wgGroupsAddToSelf,
516 'remove-self' => $wgGroupsRemoveFromSelf);
517 $addergroups = $wgUser->getEffectiveGroups();
519 foreach ($addergroups as $addergroup) {
520 $groups = array_merge_recursive(
521 $groups, $this->changeableByGroup($addergroup)
523 $groups['add'] = array_unique( $groups['add'] );
524 $groups['remove'] = array_unique( $groups['remove'] );
530 * Returns an array of the groups that a particular group can add/remove.
532 * @param String $group The group to check for whether it can add/remove
533 * @return Array array( 'add' => array( addablegroups ), 'remove' => array( removablegroups ) )
535 private function changeableByGroup( $group ) {
536 global $wgAddGroups, $wgRemoveGroups;
538 $groups = array( 'add' => array(), 'remove' => array() );
539 if( empty($wgAddGroups[$group]) ) {
540 // Don't add anything to $groups
541 } elseif( $wgAddGroups[$group] === true ) {
542 // You get everything
543 $groups['add'] = User
::getAllGroups();
544 } elseif( is_array($wgAddGroups[$group]) ) {
545 $groups['add'] = $wgAddGroups[$group];
548 // Same thing for remove
549 if( empty($wgRemoveGroups[$group]) ) {
550 } elseif($wgRemoveGroups[$group] === true ) {
551 $groups['remove'] = User
::getAllGroups();
552 } elseif( is_array($wgRemoveGroups[$group]) ) {
553 $groups['remove'] = $wgRemoveGroups[$group];
559 * Show a rights log fragment for the specified user
561 * @param User $user User to show log for
562 * @param OutputPage $output OutputPage to use
564 protected function showLogFragment( $user, $output ) {
565 $viewer = new LogViewer(
570 'page' => $user->getUserPage()->getPrefixedText(),
575 $output->addHtml( Xml
::element( 'h2', null, LogPage
::logName( 'rights' ) . "\n" ) );
576 $viewer->showList( $output );