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;
282 $form = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'name' => 'uluser' ) );
283 $form .= Xml
::hidden( 'title', 'Special:Userrights' );
284 $form .= '<fieldset><legend>' . wfMsgHtml( 'userrights-lookup-user' ) . '</legend>';
285 $form .= '<p>' . Xml
::inputLabel( wfMsg( 'userrights-user-editname' ), 'user', 'username', 30, $this->mTarget
) . '</p>';
286 $form .= '<p>' . Xml
::submitButton( wfMsg( 'editusergroup' ) ) . '</p>';
287 $form .= '</fieldset>';
289 $wgOut->addHTML( $form );
293 * Go through used and available groups and return the ones that this
294 * form will be able to manipulate based on the current user's system
297 * @param $groups Array: list of groups the given user is in
298 * @return Array: Tuple of addable, then removable groups
300 protected function splitGroups( $groups ) {
301 global $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
302 list($addable, $removable) = array_values( $this->changeableGroups() );
304 $removable = array_intersect(
305 array_merge($this->isself ?
$wgGroupsRemoveFromSelf : array(), $removable),
306 $groups ); // Can't remove groups the user doesn't have
307 $addable = array_diff(
308 array_merge($this->isself ?
$wgGroupsAddToSelf : array(), $addable),
309 $groups ); // Can't add groups the user does have
311 return array( $addable, $removable );
315 * Show the form to edit group memberships.
317 * @todo make all CSS-y and semantic
318 * @param $user User or UserRightsProxy you're editing
319 * @param $groups Array: Array of groups the user is in
321 protected function showEditUserGroupsForm( $user, $groups ) {
322 global $wgOut, $wgUser;
324 list( $addable, $removable ) = $this->splitGroups( $groups );
327 foreach( $user->getGroups() as $group )
328 $list[] = self
::buildGroupLink( $group );
331 if( count( $list ) > 0 ) {
332 $grouplist = '<p>' . wfMsgHtml( 'userrights-groupsmember' ) . ' ' . implode( ', ', $list ) . '</p>';
335 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->escapeLocalURL(), 'name' => 'editGroup' ) ) .
336 Xml
::hidden( 'user', $user->getName() ) .
337 Xml
::hidden( 'wpEditToken', $wgUser->editToken( $user->getName() ) ) .
338 Xml
::openElement( 'fieldset' ) .
339 Xml
::element( 'legend', array(), wfMsg( 'userrights-editusergroup' ) ) .
340 wfMsgExt( 'editinguser', array( 'parse' ),
341 wfEscapeWikiText( $user->getName() ) ) .
343 $this->explainRights() .
350 <td width='50%'>" . $this->removeSelect( $removable ) . "</td>
351 <td width='50%'>" . $this->addSelect( $addable ) . "</td>
357 $wgOut->parse( wfMsg('userrights-groupshelp') ) .
362 Xml
::label( wfMsg( 'userrights-reason' ), 'wpReason' ) .
365 Xml
::input( 'user-reason', 60, false, array( 'id' => 'wpReason', 'maxlength' => 255 ) ) .
371 Xml
::submitButton( wfMsg( 'saveusergroups' ), array( 'name' => 'saveusergroups' ) ) .
375 Xml
::closeElement( 'fieldset' ) .
376 Xml
::closeElement( 'form' ) . "\n"
381 * Format a link to a group description page
383 * @param string $group
386 private static function buildGroupLink( $group ) {
387 static $cache = array();
388 if( !isset( $cache[$group] ) )
389 $cache[$group] = User
::makeGroupLinkHtml( $group, User
::getGroupMember( $group ) );
390 return $cache[$group];
394 * Prepare a list of groups the user is able to add and remove
398 private function explainRights() {
399 global $wgUser, $wgLang;
402 list( $add, $remove, $addself, $rmself ) = array_values( $this->changeableGroups() );
404 if( count( $add ) > 0 )
405 $out[] = wfMsgExt( 'userrights-available-add', 'parseinline',
406 $wgLang->listToText( $add ), count( $add ) );
407 if( count( $remove ) > 0 )
408 $out[] = wfMsgExt( 'userrights-available-remove', 'parseinline',
409 $wgLang->listToText( $remove ), count( $add ) );
410 if( count( $addself ) > 0 )
411 $out[] = wfMsgExt( 'userrights-available-add-self', 'parseinline',
412 $wgLang->listToText( $addself ), count( $addself ) );
413 if( count( $rmself ) > 0 )
414 $out[] = wfMsgExt( 'userrights-available-remove-self', 'parseinline',
415 $wgLang->listToText( $rmself ), count( $rmself ) );
417 return count( $out ) > 0
418 ?
implode( '<br />', $out )
419 : wfMsgExt( 'userrights-available-none', 'parseinline' );
423 * Adds the <select> thingie where you can select what groups to remove
425 * @param array $groups The groups that can be removed
426 * @return string XHTML <select> element
428 private function removeSelect( $groups ) {
429 return $this->doSelect( $groups, 'removable' );
433 * Adds the <select> thingie where you can select what groups to add
435 * @param array $groups The groups that can be added
436 * @return string XHTML <select> element
438 private function addSelect( $groups ) {
439 return $this->doSelect( $groups, 'available' );
443 * Adds the <select> thingie where you can select what groups to add/remove
445 * @param array $groups The groups that can be added/removed
446 * @param string $name 'removable' or 'available'
447 * @return string XHTML <select> element
449 private function doSelect( $groups, $name ) {
450 $ret = wfMsgHtml( "{$this->mName}-groups$name" ) .
451 Xml
::openElement( 'select', array(
452 'name' => "{$name}[]",
453 'multiple' => 'multiple',
455 'style' => 'width: 100%;'
458 foreach ($groups as $group) {
459 $ret .= Xml
::element( 'option', array( 'value' => $group ), User
::getGroupName( $group ) );
461 $ret .= Xml
::closeElement( 'select' );
466 * @param string $group The name of the group to check
467 * @return bool Can we remove the group?
469 private function canRemove( $group ) {
470 // $this->changeableGroups()['remove'] doesn't work, of course. Thanks,
472 $groups = $this->changeableGroups();
473 return in_array( $group, $groups['remove'] );
477 * @param string $group The name of the group to check
478 * @return bool Can we add the group?
480 private function canAdd( $group ) {
481 $groups = $this->changeableGroups();
482 return in_array( $group, $groups['add'] );
486 * Returns an array of the groups that the user can add/remove.
488 * @return Array array( 'add' => array( addablegroups ), 'remove' => array( removablegroups ) )
490 function changeableGroups() {
491 global $wgUser, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
493 if( $wgUser->isAllowed( 'userrights' ) ) {
494 // This group gives the right to modify everything (reverse-
495 // compatibility with old "userrights lets you change
497 // Using array_merge to make the groups reindexed
498 $all = array_merge( User
::getAllGroups() );
502 'add-self' => array(),
503 'remove-self' => array()
507 // Okay, it's not so simple, we will have to go through the arrays
511 'add-self' => $wgGroupsAddToSelf,
512 'remove-self' => $wgGroupsRemoveFromSelf);
513 $addergroups = $wgUser->getEffectiveGroups();
515 foreach ($addergroups as $addergroup) {
516 $groups = array_merge_recursive(
517 $groups, $this->changeableByGroup($addergroup)
519 $groups['add'] = array_unique( $groups['add'] );
520 $groups['remove'] = array_unique( $groups['remove'] );
526 * Returns an array of the groups that a particular group can add/remove.
528 * @param String $group The group to check for whether it can add/remove
529 * @return Array array( 'add' => array( addablegroups ), 'remove' => array( removablegroups ) )
531 private function changeableByGroup( $group ) {
532 global $wgAddGroups, $wgRemoveGroups;
534 $groups = array( 'add' => array(), 'remove' => array() );
535 if( empty($wgAddGroups[$group]) ) {
536 // Don't add anything to $groups
537 } elseif( $wgAddGroups[$group] === true ) {
538 // You get everything
539 $groups['add'] = User
::getAllGroups();
540 } elseif( is_array($wgAddGroups[$group]) ) {
541 $groups['add'] = $wgAddGroups[$group];
544 // Same thing for remove
545 if( empty($wgRemoveGroups[$group]) ) {
546 } elseif($wgRemoveGroups[$group] === true ) {
547 $groups['remove'] = User
::getAllGroups();
548 } elseif( is_array($wgRemoveGroups[$group]) ) {
549 $groups['remove'] = $wgRemoveGroups[$group];
555 * Show a rights log fragment for the specified user
557 * @param User $user User to show log for
558 * @param OutputPage $output OutputPage to use
560 protected function showLogFragment( $user, $output ) {
561 $viewer = new LogViewer(
566 'page' => $user->getUserPage()->getPrefixedText(),
571 $output->addHtml( "<h2>" . htmlspecialchars( LogPage
::logName( 'rights' ) ) . "</h2>\n" );
572 $viewer->showList( $output );