4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 * http://www.gnu.org/copyleft/gpl.html
21 * Special page to allow managing user group membership
24 * @ingroup SpecialPage
28 * A class to manage user levels rights.
29 * @ingroup SpecialPage
31 class UserrightsPage
extends SpecialPage
{
32 # The target of the local right-adjuster's interest. Can be gotten from
33 # either a GET parameter or a subpage-style parameter, so have a member
36 protected $isself = false;
38 public function __construct() {
39 parent
::__construct( 'Userrights' );
42 public function isRestricted() {
46 public function userCanExecute( $user ) {
47 return $this->userCanChangeRights( $user, false );
50 public function userCanChangeRights( $user, $checkIfSelf = true ) {
51 $available = $this->changeableGroups();
52 return !empty( $available['add'] )
53 ||
!empty( $available['remove'] )
54 ||
( ( $this->isself ||
!$checkIfSelf ) &&
55 ( !empty( $available['add-self'] )
56 ||
!empty( $available['remove-self'] ) ) );
60 * Manage forms to be shown according to posted data.
61 * Depending on the submit button used, call a form or a save function.
63 * @param $par Mixed: string if any subpage provided, else null
65 public function execute( $par ) {
66 // If the visitor doesn't have permissions to assign or remove
67 // any groups, it's a bit silly to give them the user search prompt.
68 global $wgUser, $wgRequest, $wgOut;
71 $this->mTarget
= $par;
73 $this->mTarget
= $wgRequest->getVal( 'user' );
77 * If the user is blocked and they only have "partial" access
78 * (e.g. they don't have the userrights permission), then don't
79 * allow them to use Special:UserRights.
81 if( $wgUser->isBlocked() && !$wgUser->isAllowed( 'userrights' ) ) {
82 $wgOut->blockedPage();
86 $available = $this->changeableGroups();
88 if ( $this->mTarget
=== null ) {
90 * If the user specified no target, and they can only
91 * edit their own groups, automatically set them as the
94 if ( !count( $available['add'] ) && !count( $available['remove'] ) )
95 $this->mTarget
= $wgUser->getName();
98 if ( User
::getCanonicalName( $this->mTarget
) == $wgUser->getName() ) {
102 if( !$this->userCanChangeRights( $wgUser, true ) ) {
103 // fixme... there may be intermediate groups we can mention.
104 $wgOut->showPermissionsErrorPage( array( array(
106 ?
'userrights-nologin'
107 : 'userrights-notallowed' ) ) );
111 if ( wfReadOnly() ) {
112 $wgOut->readOnlyPage();
116 $this->outputHeader();
120 // show the general form
121 if ( count( $available['add'] ) ||
count( $available['remove'] ) ) {
125 if( $wgRequest->wasPosted() ) {
127 if( $wgRequest->getCheck( 'saveusergroups' ) ) {
128 $reason = $wgRequest->getVal( 'user-reason' );
129 $tok = $wgRequest->getVal( 'wpEditToken' );
130 if( $wgUser->matchEditToken( $tok, $this->mTarget
) ) {
131 $this->saveUserGroups(
136 $url = $this->getSuccessURL();
137 $wgOut->redirect( $url );
143 // show some more forms
144 if( $this->mTarget
!== null ) {
145 $this->editUserGroupsForm( $this->mTarget
);
149 function getSuccessURL() {
150 return $this->getTitle( $this->mTarget
)->getFullURL();
154 * Save user groups changes in the database.
155 * Data comes from the editUserGroupsForm() form function
157 * @param $username String: username to apply changes to.
158 * @param $reason String: reason for group change
161 function saveUserGroups( $username, $reason = '' ) {
162 global $wgRequest, $wgUser, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
164 $status = $this->fetchUser( $username );
165 if( !$status->isOK() ) {
166 $wgOut->addWikiText( $status->getWikiText() );
169 $user = $status->value
;
172 $allgroups = $this->getAllGroups();
174 $removegroup = array();
176 // This could possibly create a highly unlikely race condition if permissions are changed between
177 // when the form is loaded and when the form is saved. Ignoring it for the moment.
178 foreach ( $allgroups as $group ) {
179 // We'll tell it to remove all unchecked groups, and add all checked groups.
180 // Later on, this gets filtered for what can actually be removed
181 if ( $wgRequest->getCheck( "wpGroup-$group" ) ) {
182 $addgroup[] = $group;
184 $removegroup[] = $group;
188 $this->doSaveUserGroups( $user, $addgroup, $removegroup, $reason );
192 * Save user groups changes in the database.
194 * @param $user User object
195 * @param $add Array of groups to add
196 * @param $remove Array of groups to remove
197 * @param $reason String: reason for group change
198 * @return Array: Tuple of added, then removed groups
200 function doSaveUserGroups( $user, $add, $remove, $reason = '' ) {
203 // Validate input set...
204 $isself = ( $user->getName() == $wgUser->getName() );
205 $groups = $user->getGroups();
206 $changeable = $this->changeableGroups();
207 $addable = array_merge( $changeable['add'], $isself ?
$changeable['add-self'] : array() );
208 $removable = array_merge( $changeable['remove'], $isself ?
$changeable['remove-self'] : array() );
210 $remove = array_unique(
211 array_intersect( (array)$remove, $removable, $groups ) );
212 $add = array_unique( array_diff(
213 array_intersect( (array)$add, $addable ),
217 $oldGroups = $user->getGroups();
218 $newGroups = $oldGroups;
220 // remove then add groups
222 $newGroups = array_diff( $newGroups, $remove );
223 foreach( $remove as $group ) {
224 $user->removeGroup( $group );
228 $newGroups = array_merge( $newGroups, $add );
229 foreach( $add as $group ) {
230 $user->addGroup( $group );
233 $newGroups = array_unique( $newGroups );
235 // Ensure that caches are cleared
236 $user->invalidateCache();
238 wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) );
239 wfDebug( 'newGroups: ' . print_r( $newGroups, true ) );
240 wfRunHooks( 'UserRights', array( &$user, $add, $remove ) );
242 if( $newGroups != $oldGroups ) {
243 $this->addLogEntry( $user, $oldGroups, $newGroups, $reason );
245 return array( $add, $remove );
250 * Add a rights log entry for an action.
252 function addLogEntry( $user, $oldGroups, $newGroups, $reason ) {
253 $log = new LogPage( 'rights' );
255 $log->addEntry( 'rights',
256 $user->getUserPage(),
259 $this->makeGroupNameListForLog( $oldGroups ),
260 $this->makeGroupNameListForLog( $newGroups )
266 * Edit user groups membership
267 * @param $username String: name of the user.
269 function editUserGroupsForm( $username ) {
272 $status = $this->fetchUser( $username );
273 if( !$status->isOK() ) {
274 $wgOut->addWikiText( $status->getWikiText() );
277 $user = $status->value
;
280 $groups = $user->getGroups();
282 $this->showEditUserGroupsForm( $user, $groups );
284 // This isn't really ideal logging behavior, but let's not hide the
285 // interwiki logs if we're using them as is.
286 $this->showLogFragment( $user, $wgOut );
290 * Normalize the input username, which may be local or remote, and
291 * return a user (or proxy) object for manipulating it.
293 * Side effects: error output for invalid access
294 * @return Status object
296 public function fetchUser( $username ) {
297 global $wgUser, $wgUserrightsInterwikiDelimiter;
299 $parts = explode( $wgUserrightsInterwikiDelimiter, $username );
300 if( count( $parts ) < 2 ) {
301 $name = trim( $username );
304 list( $name, $database ) = array_map( 'trim', $parts );
306 if( $database == wfWikiID() ) {
309 if( !$wgUser->isAllowed( 'userrights-interwiki' ) ) {
310 return Status
::newFatal( 'userrights-no-interwiki' );
312 if( !UserRightsProxy
::validDatabase( $database ) ) {
313 return Status
::newFatal( 'userrights-nodatabase', $database );
319 return Status
::newFatal( 'nouserspecified' );
322 if( $name{0} == '#' ) {
323 // Numeric ID can be specified...
324 // We'll do a lookup for the name internally.
325 $id = intval( substr( $name, 1 ) );
327 if( $database == '' ) {
328 $name = User
::whoIs( $id );
330 $name = UserRightsProxy
::whoIs( $database, $id );
334 return Status
::newFatal( 'noname' );
337 $name = User
::getCanonicalName( $name );
338 if( $name === false ) {
340 return Status
::newFatal( 'nosuchusershort', $username );
344 if( $database == '' ) {
345 $user = User
::newFromName( $name );
347 $user = UserRightsProxy
::newFromName( $database, $name );
350 if( !$user ||
$user->isAnon() ) {
351 return Status
::newFatal( 'nosuchusershort', $username );
354 return Status
::newGood( $user );
357 function makeGroupNameList( $ids ) {
358 if( empty( $ids ) ) {
359 return wfMsgForContent( 'rightsnone' );
361 return implode( ', ', $ids );
365 function makeGroupNameListForLog( $ids ) {
366 if( empty( $ids ) ) {
369 return $this->makeGroupNameList( $ids );
374 * Output a form to allow searching for a user
376 function switchForm() {
377 global $wgOut, $wgScript;
379 Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'name' => 'uluser', 'id' => 'mw-userrights-form1' ) ) .
380 Xml
::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
381 Xml
::openElement( 'fieldset' ) .
382 Xml
::element( 'legend', array(), wfMsg( 'userrights-lookup-user' ) ) .
383 Xml
::inputLabel( wfMsg( 'userrights-user-editname' ), 'user', 'username', 30, $this->mTarget
) . ' ' .
384 Xml
::submitButton( wfMsg( 'editusergroup' ) ) .
385 Xml
::closeElement( 'fieldset' ) .
386 Xml
::closeElement( 'form' ) . "\n"
391 * Go through used and available groups and return the ones that this
392 * form will be able to manipulate based on the current user's system
395 * @param $groups Array: list of groups the given user is in
396 * @return Array: Tuple of addable, then removable groups
398 protected function splitGroups( $groups ) {
399 list( $addable, $removable, $addself, $removeself ) = array_values( $this->changeableGroups() );
401 $removable = array_intersect(
402 array_merge( $this->isself ?
$removeself : array(), $removable ),
404 ); // Can't remove groups the user doesn't have
405 $addable = array_diff(
406 array_merge( $this->isself ?
$addself : array(), $addable ),
408 ); // Can't add groups the user does have
410 return array( $addable, $removable );
414 * Show the form to edit group memberships.
416 * @param $user User or UserRightsProxy you're editing
417 * @param $groups Array: Array of groups the user is in
419 protected function showEditUserGroupsForm( $user, $groups ) {
420 global $wgOut, $wgUser, $wgLang;
423 foreach( $groups as $group ) {
424 $list[] = self
::buildGroupLink( $group );
428 if ( $user instanceof User
) {
429 foreach( Autopromote
::getAutopromoteGroups( $user ) as $group ) {
430 $autolist[] = self
::buildGroupLink( $group );
435 if( count( $list ) > 0 ) {
436 $grouplist = wfMsgHtml( 'userrights-groupsmember' );
437 $grouplist = '<p>' . $grouplist . ' ' . $wgLang->listToText( $list ) . "</p>\n";
439 if( count( $autolist ) > 0 ) {
440 $autogrouplistintro = wfMsgHtml( 'userrights-groupsmember-auto' );
441 $grouplist .= '<p>' . $autogrouplistintro . ' ' . $wgLang->listToText( $autolist ) . "</p>\n";
444 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL(), 'name' => 'editGroup', 'id' => 'mw-userrights-form2' ) ) .
445 Xml
::hidden( 'user', $this->mTarget
) .
446 Xml
::hidden( 'wpEditToken', $wgUser->editToken( $this->mTarget
) ) .
447 Xml
::openElement( 'fieldset' ) .
448 Xml
::element( 'legend', array(), wfMsg( 'userrights-editusergroup' ) ) .
449 wfMsgExt( 'editinguser', array( 'parse' ), wfEscapeWikiText( $user->getName() ) ) .
450 wfMsgExt( 'userrights-groups-help', array( 'parse' ) ) .
452 Xml
::tags( 'p', null, $this->groupCheckboxes( $groups ) ) .
453 Xml
::openElement( 'table', array( 'border' => '0', 'id' => 'mw-userrights-table-outer' ) ) .
455 <td class='mw-label'>" .
456 Xml
::label( wfMsg( 'userrights-reason' ), 'wpReason' ) .
458 <td class='mw-input'>" .
459 Xml
::input( 'user-reason', 60, false, array( 'id' => 'wpReason', 'maxlength' => 255 ) ) .
464 <td class='mw-submit'>" .
465 Xml
::submitButton( wfMsg( 'saveusergroups' ),
466 array( 'name' => 'saveusergroups' ) +
$wgUser->getSkin()->tooltipAndAccessKeyAttribs( 'userrights-set' ) ) .
469 Xml
::closeElement( 'table' ) . "\n" .
470 Xml
::closeElement( 'fieldset' ) .
471 Xml
::closeElement( 'form' ) . "\n"
476 * Format a link to a group description page
478 * @param $group string
481 private static function buildGroupLink( $group ) {
482 static $cache = array();
483 if( !isset( $cache[$group] ) )
484 $cache[$group] = User
::makeGroupLinkHtml( $group, htmlspecialchars( User
::getGroupName( $group ) ) );
485 return $cache[$group];
489 * Returns an array of all groups that may be edited
490 * @return array Array of groups that may be edited.
492 protected static function getAllGroups() {
493 return User
::getAllGroups();
497 * Adds a table with checkboxes where you can select what groups to add/remove
499 * @param $usergroups Array: groups the user belongs to
500 * @return string XHTML table element with checkboxes
502 private function groupCheckboxes( $usergroups ) {
503 $allgroups = $this->getAllGroups();
506 # Put all column info into an associative array so that extensions can
507 # more easily manage it.
508 $columns = array( 'unchangeable' => array(), 'changeable' => array() );
510 foreach( $allgroups as $group ) {
511 $set = in_array( $group, $usergroups );
512 # Should the checkbox be disabled?
514 ( $set && $this->canRemove( $group ) ) ||
515 ( !$set && $this->canAdd( $group ) ) );
516 # Do we need to point out that this action is irreversible?
517 $irreversible = !$disabled && (
518 ( $set && !$this->canAdd( $group ) ) ||
519 ( !$set && !$this->canRemove( $group ) ) );
523 'disabled' => $disabled,
524 'irreversible' => $irreversible
528 $columns['unchangeable'][$group] = $checkbox;
530 $columns['changeable'][$group] = $checkbox;
534 # Build the HTML table
535 $ret .= Xml
::openElement( 'table', array( 'border' => '0', 'class' => 'mw-userrights-groups' ) ) .
537 foreach( $columns as $name => $column ) {
538 if( $column === array() )
540 $ret .= xml
::element( 'th', null, wfMsg( 'userrights-' . $name . '-col' ) );
542 $ret.= "</tr>\n<tr>\n";
543 foreach( $columns as $column ) {
544 if( $column === array() )
546 $ret .= "\t<td style='vertical-align:top;'>\n";
547 foreach( $column as $group => $checkbox ) {
548 $attr = $checkbox['disabled'] ?
array( 'disabled' => 'disabled' ) : array();
550 if ( $checkbox['irreversible'] ) {
551 $text = htmlspecialchars( wfMsg( 'userrights-irreversible-marker',
552 User
::getGroupMember( $group ) ) );
554 $text = htmlspecialchars( User
::getGroupMember( $group ) );
556 $checkboxHtml = Xml
::checkLabel( $text, "wpGroup-" . $group,
557 "wpGroup-" . $group, $checkbox['set'], $attr );
558 $ret .= "\t\t" . ( $checkbox['disabled']
559 ? Xml
::tags( 'span', array( 'class' => 'mw-userrights-disabled' ), $checkboxHtml )
565 $ret .= Xml
::closeElement( 'tr' ) . Xml
::closeElement( 'table' );
571 * @param $group String: the name of the group to check
572 * @return bool Can we remove the group?
574 private function canRemove( $group ) {
575 // $this->changeableGroups()['remove'] doesn't work, of course. Thanks,
577 $groups = $this->changeableGroups();
578 return in_array( $group, $groups['remove'] ) ||
( $this->isself
&& in_array( $group, $groups['remove-self'] ) );
582 * @param $group string: the name of the group to check
583 * @return bool Can we add the group?
585 private function canAdd( $group ) {
586 $groups = $this->changeableGroups();
587 return in_array( $group, $groups['add'] ) ||
( $this->isself
&& in_array( $group, $groups['add-self'] ) );
591 * Returns $wgUser->changeableGroups()
593 * @return Array array( 'add' => array( addablegroups ), 'remove' => array( removablegroups ) , 'add-self' => array( addablegroups to self), 'remove-self' => array( removable groups from self) )
595 function changeableGroups() {
597 return $wgUser->changeableGroups();
601 * Show a rights log fragment for the specified user
603 * @param $user User to show log for
604 * @param $output OutputPage to use
606 protected function showLogFragment( $user, $output ) {
607 $output->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'rights' ) . "\n" ) );
608 LogEventsList
::showLogExtract( $output, 'rights', $user->getUserPage()->getPrefixedText() );