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
21 public function __construct() {
22 parent
::__construct( 'Userrights' );
25 public function isRestricted() {
29 public function userCanExecute( $user ) {
30 $available = $this->changeableGroups();
31 return !empty( $available['add'] ) or !empty( $available['remove'] );
35 * Manage forms to be shown according to posted data.
36 * Depending on the submit button used, call a form or a save function.
38 * @param mixed $par String if any subpage provided, else null
40 function execute( $par ) {
41 // If the visitor doesn't have permissions to assign or remove
42 // any groups, it's a bit silly to give them the user search prompt.
44 if( !$this->userCanExecute( $wgUser ) ) {
45 // fixme... there may be intermediate groups we can mention.
47 $wgOut->showPermissionsErrorPage( array(
49 ?
'userrights-nologin'
50 : 'userrights-notallowed' ) );
56 $this->mTarget
= $par;
58 $this->mTarget
= $wgRequest->getVal( 'user' );
63 // show the general form
66 if( $wgRequest->wasPosted() ) {
68 if( $wgRequest->getCheck( 'saveusergroups' ) ) {
69 $reason = $wgRequest->getVal( 'user-reason' );
70 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $this->mTarget
) ) {
71 $this->saveUserGroups(
73 $wgRequest->getArray( 'removable' ),
74 $wgRequest->getArray( 'available' ),
81 // show some more forms
82 if( $this->mTarget
) {
83 $this->editUserGroupsForm( $this->mTarget
);
88 * Save user groups changes in the database.
89 * Data comes from the editUserGroupsForm() form function
91 * @param string $username Username to apply changes to.
92 * @param array $removegroup id of groups to be removed.
93 * @param array $addgroup id of groups to be added.
94 * @param string $reason Reason for group change
97 function saveUserGroups( $username, $removegroup, $addgroup, $reason = '') {
98 $user = $this->fetchUser( $username );
103 // Validate input set...
104 $changeable = $this->changeableGroups();
105 $removegroup = array_unique(
106 array_intersect( (array)$removegroup, $changeable['remove'] ) );
107 $addgroup = array_unique(
108 array_intersect( (array)$addgroup, $changeable['add'] ) );
110 $oldGroups = $user->getGroups();
111 $newGroups = $oldGroups;
112 // remove then add groups
114 $newGroups = array_diff($newGroups, $removegroup);
115 foreach( $removegroup as $group ) {
116 $user->removeGroup( $group );
120 $newGroups = array_merge($newGroups, $addgroup);
121 foreach( $addgroup as $group ) {
122 $user->addGroup( $group );
125 $newGroups = array_unique( $newGroups );
127 // Ensure that caches are cleared
128 $user->invalidateCache();
130 wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) );
131 wfDebug( 'newGroups: ' . print_r( $newGroups, true ) );
132 if( $user instanceof User
) {
134 wfRunHooks( 'UserRights', array( &$user, $addgroup, $removegroup ) );
137 $log = new LogPage( 'rights' );
140 $log->addEntry( 'rights',
141 $user->getUserPage(),
142 $wgRequest->getText( 'user-reason' ),
144 $this->makeGroupNameList( $oldGroups ),
145 $this->makeGroupNameList( $newGroups )
151 * Edit user groups membership
152 * @param string $username Name of the user.
154 function editUserGroupsForm( $username ) {
157 $user = $this->fetchUser( $username );
162 $groups = $user->getGroups();
164 $this->showEditUserGroupsForm( $user, $groups );
166 // This isn't really ideal logging behavior, but let's not hide the
167 // interwiki logs if we're using them as is.
168 $this->showLogFragment( $user, $wgOut );
172 * Normalize the input username, which may be local or remote, and
173 * return a user (or proxy) object for manipulating it.
175 * Side effects: error output for invalid access
176 * @return mixed User, UserRightsProxy, or null
178 function fetchUser( $username ) {
179 global $wgOut, $wgUser;
181 $parts = explode( '@', $username );
182 if( count( $parts ) < 2 ) {
183 $name = trim( $username );
186 list( $name, $database ) = array_map( 'trim', $parts );
188 if( !$wgUser->isAllowed( 'userrights-interwiki' ) ) {
189 $wgOut->addWikiText( wfMsg( 'userrights-no-interwiki' ) );
192 if( !UserRightsProxy
::validDatabase( $database ) ) {
193 $wgOut->addWikiText( wfMsg( 'userrights-nodatabase', $database ) );
199 $wgOut->addWikiText( wfMsg( 'nouserspecified' ) );
203 if( $name{0} == '#' ) {
204 // Numeric ID can be specified...
205 // We'll do a lookup for the name internally.
206 $id = intval( substr( $name, 1 ) );
208 if( $database == '' ) {
209 $name = User
::whoIs( $id );
211 $name = UserRightsProxy
::whoIs( $database, $id );
215 $wgOut->addWikiText( wfMsg( 'noname' ) );
220 if( $database == '' ) {
221 $user = User
::newFromName( $name );
223 $user = UserRightsProxy
::newFromName( $database, $name );
226 if( !$user ||
$user->isAnon() ) {
227 $wgOut->addWikiText( wfMsg( 'nosuchusershort', wfEscapeWikiText( $username ) ) );
234 function makeGroupNameList( $ids ) {
235 return implode( ', ', $ids );
239 * Output a form to allow searching for a user
241 function switchForm() {
242 global $wgOut, $wgScript;
243 $form = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'name' => 'uluser' ) );
244 $form .= Xml
::hidden( 'title', 'Special:Userrights' );
245 $form .= '<fieldset><legend>' . wfMsgHtml( 'userrights-lookup-user' ) . '</legend>';
246 $form .= '<p>' . Xml
::inputLabel( wfMsg( 'userrights-user-editname' ), 'user', 'username', 30, $this->mTarget
) . '</p>';
247 $form .= '<p>' . Xml
::submitButton( wfMsg( 'editusergroup' ) ) . '</p>';
248 $form .= '</fieldset>';
250 $wgOut->addHTML( $form );
254 * Go through used and available groups and return the ones that this
255 * form will be able to manipulate based on the current user's system
258 * @param $groups Array: list of groups the given user is in
259 * @return Array: Tuple of addable, then removable groups
261 protected function splitGroups( $groups ) {
262 list($addable, $removable) = array_values( $this->changeableGroups() );
263 $removable = array_intersect($removable, $groups ); // Can't remove groups the user doesn't have
264 $addable = array_diff( $addable, $groups ); // Can't add groups the user does have
266 return array( $addable, $removable );
270 * Show the form to edit group memberships.
272 * @todo make all CSS-y and semantic
273 * @param $user User or UserRightsProxy you're editing
274 * @param $groups Array: Array of groups the user is in
276 protected function showEditUserGroupsForm( $user, $groups ) {
277 global $wgOut, $wgUser;
279 list( $addable, $removable ) = $this->splitGroups( $groups );
282 foreach( $user->getGroups() as $group )
283 $list[] = self
::buildGroupLink( $group );
286 if( count( $list ) > 0 ) {
287 $grouplist = '<p>' . wfMsgHtml( 'userrights-groupsmember' ) . ' ' . implode( ', ', $list ) . '</p>';
290 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->escapeLocalURL(), 'name' => 'editGroup' ) ) .
291 Xml
::hidden( 'user', $user->getName() ) .
292 Xml
::hidden( 'wpEditToken', $wgUser->editToken( $user->getName() ) ) .
293 Xml
::openElement( 'fieldset' ) .
294 Xml
::element( 'legend', array(), wfMsg( 'userrights-editusergroup' ) ) .
295 wfMsgExt( 'editinguser', array( 'parse' ),
296 wfEscapeWikiText( $user->getName() ) ) .
298 $this->explainRights() .
305 <td width='50%'>" . $this->removeSelect( $removable ) . "</td>
306 <td width='50%'>" . $this->addSelect( $addable ) . "</td>
312 $wgOut->parse( wfMsg('userrights-groupshelp') ) .
317 Xml
::label( wfMsg( 'userrights-reason' ), 'wpReason' ) .
320 Xml
::input( 'user-reason', 60, false, array( 'id' => 'wpReason', 'maxlength' => 255 ) ) .
326 Xml
::submitButton( wfMsg( 'saveusergroups' ), array( 'name' => 'saveusergroups' ) ) .
330 Xml
::closeElement( 'fieldset' ) .
331 Xml
::closeElement( 'form' ) . "\n"
336 * Format a link to a group description page
338 * @param string $group
341 private static function buildGroupLink( $group ) {
342 static $cache = array();
343 if( !isset( $cache[$group] ) )
344 $cache[$group] = User
::makeGroupLinkHtml( $group, User
::getGroupMember( $group ) );
345 return $cache[$group];
349 * Prepare a list of groups the user is able to add and remove
353 private function explainRights() {
354 global $wgUser, $wgLang;
357 list( $add, $remove ) = array_values( $this->changeableGroups() );
359 if( count( $add ) > 0 )
360 $out[] = wfMsgExt( 'userrights-available-add', 'parseinline', $wgLang->listToText( $add ), count( $add ) );
361 if( count( $remove ) > 0 )
362 $out[] = wfMsgExt( 'userrights-available-remove', 'parseinline', $wgLang->listToText( $remove ), count( $add ) );
364 return count( $out ) > 0
365 ?
implode( '<br />', $out )
366 : wfMsgExt( 'userrights-available-none', 'parseinline' );
370 * Adds the <select> thingie where you can select what groups to remove
372 * @param array $groups The groups that can be removed
373 * @return string XHTML <select> element
375 private function removeSelect( $groups ) {
376 return $this->doSelect( $groups, 'removable' );
380 * Adds the <select> thingie where you can select what groups to add
382 * @param array $groups The groups that can be added
383 * @return string XHTML <select> element
385 private function addSelect( $groups ) {
386 return $this->doSelect( $groups, 'available' );
390 * Adds the <select> thingie where you can select what groups to add/remove
392 * @param array $groups The groups that can be added/removed
393 * @param string $name 'removable' or 'available'
394 * @return string XHTML <select> element
396 private function doSelect( $groups, $name ) {
397 $ret = wfMsgHtml( "{$this->mName}-groups$name" ) .
398 Xml
::openElement( 'select', array(
399 'name' => "{$name}[]",
400 'multiple' => 'multiple',
402 'style' => 'width: 100%;'
405 foreach ($groups as $group) {
406 $ret .= Xml
::element( 'option', array( 'value' => $group ), User
::getGroupName( $group ) );
408 $ret .= Xml
::closeElement( 'select' );
413 * @param string $group The name of the group to check
414 * @return bool Can we remove the group?
416 private function canRemove( $group ) {
417 // $this->changeableGroups()['remove'] doesn't work, of course. Thanks,
419 $groups = $this->changeableGroups();
420 return in_array( $group, $groups['remove'] );
424 * @param string $group The name of the group to check
425 * @return bool Can we add the group?
427 private function canAdd( $group ) {
428 $groups = $this->changeableGroups();
429 return in_array( $group, $groups['add'] );
433 * Returns an array of the groups that the user can add/remove.
435 * @return Array array( 'add' => array( addablegroups ), 'remove' => array( removablegroups ) )
437 function changeableGroups() {
440 if( $wgUser->isAllowed( 'userrights' ) ) {
441 // This group gives the right to modify everything (reverse-
442 // compatibility with old "userrights lets you change
444 // Using array_merge to make the groups reindexed
445 $all = array_merge( User
::getAllGroups() );
452 // Okay, it's not so simple, we will have to go through the arrays
453 $groups = array( 'add' => array(), 'remove' => array() );
454 $addergroups = $wgUser->getEffectiveGroups();
456 foreach ($addergroups as $addergroup) {
457 $groups = array_merge_recursive(
458 $groups, $this->changeableByGroup($addergroup)
460 $groups['add'] = array_unique( $groups['add'] );
461 $groups['remove'] = array_unique( $groups['remove'] );
467 * Returns an array of the groups that a particular group can add/remove.
469 * @param String $group The group to check for whether it can add/remove
470 * @return Array array( 'add' => array( addablegroups ), 'remove' => array( removablegroups ) )
472 private function changeableByGroup( $group ) {
473 global $wgAddGroups, $wgRemoveGroups;
475 $groups = array( 'add' => array(), 'remove' => array() );
476 if( empty($wgAddGroups[$group]) ) {
477 // Don't add anything to $groups
478 } elseif( $wgAddGroups[$group] === true ) {
479 // You get everything
480 $groups['add'] = User
::getAllGroups();
481 } elseif( is_array($wgAddGroups[$group]) ) {
482 $groups['add'] = $wgAddGroups[$group];
485 // Same thing for remove
486 if( empty($wgRemoveGroups[$group]) ) {
487 } elseif($wgRemoveGroups[$group] === true ) {
488 $groups['remove'] = User
::getAllGroups();
489 } elseif( is_array($wgRemoveGroups[$group]) ) {
490 $groups['remove'] = $wgRemoveGroups[$group];
496 * Show a rights log fragment for the specified user
498 * @param User $user User to show log for
499 * @param OutputPage $output OutputPage to use
501 protected function showLogFragment( $user, $output ) {
502 $viewer = new LogViewer(
507 'page' => $user->getUserPage()->getPrefixedText(),
512 $output->addHtml( "<h2>" . htmlspecialchars( LogPage
::logName( 'rights' ) ) . "</h2>\n" );
513 $viewer->showList( $output );