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() {
243 $form = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $this->getTitle()->escapeLocalURL(), 'name' => 'uluser' ) );
244 $form .= '<fieldset><legend>' . wfMsgHtml( 'userrights-lookup-user' ) . '</legend>';
245 $form .= '<p>' . Xml
::inputLabel( wfMsg( 'userrights-user-editname' ), 'user', 'username', 30, $this->mTarget
) . '</p>';
246 $form .= '<p>' . Xml
::submitButton( wfMsg( 'editusergroup' ) ) . '</p>';
247 $form .= '</fieldset>';
249 $wgOut->addHTML( $form );
253 * Go through used and available groups and return the ones that this
254 * form will be able to manipulate based on the current user's system
257 * @param $groups Array: list of groups the given user is in
258 * @return Array: Tuple of addable, then removable groups
260 protected function splitGroups( $groups ) {
261 list($addable, $removable) = array_values( $this->changeableGroups() );
262 $removable = array_intersect($removable, $groups ); // Can't remove groups the user doesn't have
263 $addable = array_diff( $addable, $groups ); // Can't add groups the user does have
265 return array( $addable, $removable );
269 * Show the form to edit group memberships.
271 * @todo make all CSS-y and semantic
272 * @param $user User or UserRightsProxy you're editing
273 * @param $groups Array: Array of groups the user is in
275 protected function showEditUserGroupsForm( $user, $groups ) {
276 global $wgOut, $wgUser;
278 list( $addable, $removable ) = $this->splitGroups( $groups );
281 foreach( $user->getGroups() as $group )
282 $list[] = self
::buildGroupLink( $group );
285 if( count( $list ) > 0 ) {
286 $grouplist = '<p>' . wfMsgHtml( 'userrights-groupsmember' ) . ' ' . implode( ', ', $list ) . '</p>';
289 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->escapeLocalURL(), 'name' => 'editGroup' ) ) .
290 Xml
::hidden( 'user', $user->getName() ) .
291 Xml
::hidden( 'wpEditToken', $wgUser->editToken( $user->getName() ) ) .
292 Xml
::openElement( 'fieldset' ) .
293 Xml
::element( 'legend', array(), wfMsg( 'userrights-editusergroup' ) ) .
294 wfMsgExt( 'editinguser', array( 'parse' ),
295 wfEscapeWikiText( $user->getName() ) ) .
297 $this->explainRights() .
304 <td width='50%'>" . $this->removeSelect( $removable ) . "</td>
305 <td width='50%'>" . $this->addSelect( $addable ) . "</td>
311 $wgOut->parse( wfMsg('userrights-groupshelp') ) .
316 Xml
::label( wfMsg( 'userrights-reason' ), 'wpReason' ) .
319 Xml
::input( 'user-reason', 60, false, array( 'id' => 'wpReason', 'maxlength' => 255 ) ) .
325 Xml
::submitButton( wfMsg( 'saveusergroups' ), array( 'name' => 'saveusergroups' ) ) .
329 Xml
::closeElement( 'fieldset' ) .
330 Xml
::closeElement( 'form' ) . "\n"
335 * Format a link to a group description page
337 * @param string $group
340 private static function buildGroupLink( $group ) {
341 static $cache = array();
342 if( !isset( $cache[$group] ) )
343 $cache[$group] = User
::makeGroupLinkHtml( $group, User
::getGroupMember( $group ) );
344 return $cache[$group];
348 * Prepare a list of groups the user is able to add and remove
352 private function explainRights() {
353 global $wgUser, $wgLang;
356 list( $add, $remove ) = array_values( $this->changeableGroups() );
358 if( count( $add ) > 0 )
359 $out[] = wfMsgExt( 'userrights-available-add', 'parseinline', $wgLang->listToText( $add ), count( $add ) );
360 if( count( $remove ) > 0 )
361 $out[] = wfMsgExt( 'userrights-available-remove', 'parseinline', $wgLang->listToText( $remove ), count( $add ) );
363 return count( $out ) > 0
364 ?
implode( '<br />', $out )
365 : wfMsgExt( 'userrights-available-none', 'parseinline' );
369 * Adds the <select> thingie where you can select what groups to remove
371 * @param array $groups The groups that can be removed
372 * @return string XHTML <select> element
374 private function removeSelect( $groups ) {
375 return $this->doSelect( $groups, 'removable' );
379 * Adds the <select> thingie where you can select what groups to add
381 * @param array $groups The groups that can be added
382 * @return string XHTML <select> element
384 private function addSelect( $groups ) {
385 return $this->doSelect( $groups, 'available' );
389 * Adds the <select> thingie where you can select what groups to add/remove
391 * @param array $groups The groups that can be added/removed
392 * @param string $name 'removable' or 'available'
393 * @return string XHTML <select> element
395 private function doSelect( $groups, $name ) {
396 $ret = wfMsgHtml( "{$this->mName}-groups$name" ) .
397 Xml
::openElement( 'select', array(
398 'name' => "{$name}[]",
399 'multiple' => 'multiple',
401 'style' => 'width: 100%;'
404 foreach ($groups as $group) {
405 $ret .= Xml
::element( 'option', array( 'value' => $group ), User
::getGroupName( $group ) );
407 $ret .= Xml
::closeElement( 'select' );
412 * @param string $group The name of the group to check
413 * @return bool Can we remove the group?
415 private function canRemove( $group ) {
416 // $this->changeableGroups()['remove'] doesn't work, of course. Thanks,
418 $groups = $this->changeableGroups();
419 return in_array( $group, $groups['remove'] );
423 * @param string $group The name of the group to check
424 * @return bool Can we add the group?
426 private function canAdd( $group ) {
427 $groups = $this->changeableGroups();
428 return in_array( $group, $groups['add'] );
432 * Returns an array of the groups that the user can add/remove.
434 * @return Array array( 'add' => array( addablegroups ), 'remove' => array( removablegroups ) )
436 function changeableGroups() {
439 if( $wgUser->isAllowed( 'userrights' ) ) {
440 // This group gives the right to modify everything (reverse-
441 // compatibility with old "userrights lets you change
443 // Using array_merge to make the groups reindexed
444 $all = array_merge( User
::getAllGroups() );
451 // Okay, it's not so simple, we will have to go through the arrays
452 $groups = array( 'add' => array(), 'remove' => array() );
453 $addergroups = $wgUser->getEffectiveGroups();
455 foreach ($addergroups as $addergroup) {
456 $groups = array_merge_recursive(
457 $groups, $this->changeableByGroup($addergroup)
459 $groups['add'] = array_unique( $groups['add'] );
460 $groups['remove'] = array_unique( $groups['remove'] );
466 * Returns an array of the groups that a particular group can add/remove.
468 * @param String $group The group to check for whether it can add/remove
469 * @return Array array( 'add' => array( addablegroups ), 'remove' => array( removablegroups ) )
471 private function changeableByGroup( $group ) {
472 global $wgAddGroups, $wgRemoveGroups;
474 $groups = array( 'add' => array(), 'remove' => array() );
475 if( empty($wgAddGroups[$group]) ) {
476 // Don't add anything to $groups
477 } elseif( $wgAddGroups[$group] === true ) {
478 // You get everything
479 $groups['add'] = User
::getAllGroups();
480 } elseif( is_array($wgAddGroups[$group]) ) {
481 $groups['add'] = $wgAddGroups[$group];
484 // Same thing for remove
485 if( empty($wgRemoveGroups[$group]) ) {
486 } elseif($wgRemoveGroups[$group] === true ) {
487 $groups['remove'] = User
::getAllGroups();
488 } elseif( is_array($wgRemoveGroups[$group]) ) {
489 $groups['remove'] = $wgRemoveGroups[$group];
495 * Show a rights log fragment for the specified user
497 * @param User $user User to show log for
498 * @param OutputPage $output OutputPage to use
500 protected function showLogFragment( $user, $output ) {
501 $viewer = new LogViewer(
506 'page' => $user->getUserPage()->getPrefixedText(),
511 $output->addHtml( "<h2>" . htmlspecialchars( LogPage
::logName( 'rights' ) ) . "</h2>\n" );
512 $viewer->showList( $output );