3 * Implements Special:Userrights
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
25 * Special page to allow managing user group membership
27 * @ingroup SpecialPage
29 class UserrightsPage
extends SpecialPage
{
30 # The target of the local right-adjuster's interest. Can be gotten from
31 # either a GET parameter or a subpage-style parameter, so have a member
34 protected $isself = false;
36 public function __construct() {
37 parent
::__construct( 'Userrights' );
40 public function isRestricted() {
44 public function userCanExecute( User
$user ) {
45 return $this->userCanChangeRights( $user, false );
50 * @param bool $checkIfSelf
53 public function userCanChangeRights( $user, $checkIfSelf = true ) {
54 $available = $this->changeableGroups();
55 if ( $user->getId() == 0 ) {
58 return !empty( $available['add'] )
59 ||
!empty( $available['remove'] )
60 ||
( ( $this->isself ||
!$checkIfSelf ) &&
61 ( !empty( $available['add-self'] )
62 ||
!empty( $available['remove-self'] ) ) );
66 * Manage forms to be shown according to posted data.
67 * Depending on the submit button used, call a form or a save function.
69 * @param $par Mixed: string if any subpage provided, else null
70 * @throws UserBlockedError|PermissionsError
72 public function execute( $par ) {
73 // If the visitor doesn't have permissions to assign or remove
74 // any groups, it's a bit silly to give them the user search prompt.
76 $user = $this->getUser();
79 * If the user is blocked and they only have "partial" access
80 * (e.g. they don't have the userrights permission), then don't
81 * allow them to use Special:UserRights.
83 if ( $user->isBlocked() && !$user->isAllowed( 'userrights' ) ) {
84 throw new UserBlockedError( $user->getBlock() );
87 $request = $this->getRequest();
89 if ( $par !== null ) {
90 $this->mTarget
= $par;
92 $this->mTarget
= $request->getVal( 'user' );
95 $available = $this->changeableGroups();
97 if ( $this->mTarget
=== null ) {
99 * If the user specified no target, and they can only
100 * edit their own groups, automatically set them as the
103 if ( !count( $available['add'] ) && !count( $available['remove'] ) ) {
104 $this->mTarget
= $user->getName();
108 if ( User
::getCanonicalName( $this->mTarget
) == $user->getName() ) {
109 $this->isself
= true;
112 if ( !$this->userCanChangeRights( $user, true ) ) {
113 // @todo FIXME: There may be intermediate groups we can mention.
114 $msg = $user->isAnon() ?
'userrights-nologin' : 'userrights-notallowed';
115 throw new PermissionsError( null, array( array( $msg ) ) );
118 $this->checkReadOnly();
121 $this->outputHeader();
123 $out = $this->getOutput();
124 $out->addModuleStyles( 'mediawiki.special' );
126 // show the general form
127 if ( count( $available['add'] ) ||
count( $available['remove'] ) ) {
132 $request->wasPosted() &&
133 $request->getCheck( 'saveusergroups' ) &&
134 $user->matchEditToken( $request->getVal( 'wpEditToken' ), $this->mTarget
)
137 $status = $this->fetchUser( $this->mTarget
);
138 if ( !$status->isOK() ) {
139 $this->getOutput()->addWikiText( $status->getWikiText() );
143 $targetUser = $status->value
;
145 if ( $request->getVal( 'conflictcheck-originalgroups' ) !== implode( ',', $targetUser->getGroups() ) ) {
146 $out->addWikiMsg( 'userrights-conflict' );
148 $this->saveUserGroups(
150 $request->getVal( 'user-reason' ),
154 $out->redirect( $this->getSuccessURL() );
159 // show some more forms
160 if ( $this->mTarget
!== null ) {
161 $this->editUserGroupsForm( $this->mTarget
);
165 function getSuccessURL() {
166 return $this->getTitle( $this->mTarget
)->getFullURL();
170 * Save user groups changes in the database.
171 * Data comes from the editUserGroupsForm() form function
173 * @param string $username username to apply changes to.
174 * @param string $reason reason for group change
175 * @param User|UserRightsProxy $user Target user object.
178 function saveUserGroups( $username, $reason, $user ) {
179 $allgroups = $this->getAllGroups();
181 $removegroup = array();
183 // This could possibly create a highly unlikely race condition if permissions are changed between
184 // when the form is loaded and when the form is saved. Ignoring it for the moment.
185 foreach ( $allgroups as $group ) {
186 // We'll tell it to remove all unchecked groups, and add all checked groups.
187 // Later on, this gets filtered for what can actually be removed
188 if ( $this->getRequest()->getCheck( "wpGroup-$group" ) ) {
189 $addgroup[] = $group;
191 $removegroup[] = $group;
195 $this->doSaveUserGroups( $user, $addgroup, $removegroup, $reason );
199 * Save user groups changes in the database.
201 * @param $user User object
202 * @param array $add of groups to add
203 * @param array $remove of groups to remove
204 * @param string $reason reason for group change
205 * @return Array: Tuple of added, then removed groups
207 function doSaveUserGroups( $user, $add, $remove, $reason = '' ) {
208 // Validate input set...
209 $isself = ( $user->getName() == $this->getUser()->getName() );
210 $groups = $user->getGroups();
211 $changeable = $this->changeableGroups();
212 $addable = array_merge( $changeable['add'], $isself ?
$changeable['add-self'] : array() );
213 $removable = array_merge( $changeable['remove'], $isself ?
$changeable['remove-self'] : array() );
215 $remove = array_unique(
216 array_intersect( (array)$remove, $removable, $groups ) );
217 $add = array_unique( array_diff(
218 array_intersect( (array)$add, $addable ),
222 $oldGroups = $user->getGroups();
223 $newGroups = $oldGroups;
225 // remove then add groups
227 $newGroups = array_diff( $newGroups, $remove );
228 foreach ( $remove as $group ) {
229 $user->removeGroup( $group );
233 $newGroups = array_merge( $newGroups, $add );
234 foreach ( $add as $group ) {
235 $user->addGroup( $group );
238 $newGroups = array_unique( $newGroups );
240 // Ensure that caches are cleared
241 $user->invalidateCache();
243 wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) );
244 wfDebug( 'newGroups: ' . print_r( $newGroups, true ) );
245 wfRunHooks( 'UserRights', array( &$user, $add, $remove ) );
247 if ( $newGroups != $oldGroups ) {
248 $this->addLogEntry( $user, $oldGroups, $newGroups, $reason );
250 return array( $add, $remove );
254 * Add a rights log entry for an action.
256 function addLogEntry( $user, $oldGroups, $newGroups, $reason ) {
257 $logEntry = new ManualLogEntry( 'rights', 'rights' );
258 $logEntry->setPerformer( $this->getUser() );
259 $logEntry->setTarget( $user->getUserPage() );
260 $logEntry->setComment( $reason );
261 $logEntry->setParameters( array(
262 '4::oldgroups' => $oldGroups,
263 '5::newgroups' => $newGroups,
265 $logid = $logEntry->insert();
266 $logEntry->publish( $logid );
270 * Edit user groups membership
271 * @param string $username name of the user.
273 function editUserGroupsForm( $username ) {
274 $status = $this->fetchUser( $username );
275 if ( !$status->isOK() ) {
276 $this->getOutput()->addWikiText( $status->getWikiText() );
279 $user = $status->value
;
282 $groups = $user->getGroups();
284 $this->showEditUserGroupsForm( $user, $groups );
286 // This isn't really ideal logging behavior, but let's not hide the
287 // interwiki logs if we're using them as is.
288 $this->showLogFragment( $user, $this->getOutput() );
292 * Normalize the input username, which may be local or remote, and
293 * return a user (or proxy) object for manipulating it.
295 * Side effects: error output for invalid access
296 * @param string $username
297 * @return Status object
299 public function fetchUser( $username ) {
300 global $wgUserrightsInterwikiDelimiter;
302 $parts = explode( $wgUserrightsInterwikiDelimiter, $username );
303 if ( count( $parts ) < 2 ) {
304 $name = trim( $username );
307 list( $name, $database ) = array_map( 'trim', $parts );
309 if ( $database == wfWikiID() ) {
312 if ( !$this->getUser()->isAllowed( 'userrights-interwiki' ) ) {
313 return Status
::newFatal( 'userrights-no-interwiki' );
315 if ( !UserRightsProxy
::validDatabase( $database ) ) {
316 return Status
::newFatal( 'userrights-nodatabase', $database );
321 if ( $name === '' ) {
322 return Status
::newFatal( 'nouserspecified' );
325 if ( $name[0] == '#' ) {
326 // Numeric ID can be specified...
327 // We'll do a lookup for the name internally.
328 $id = intval( substr( $name, 1 ) );
330 if ( $database == '' ) {
331 $name = User
::whoIs( $id );
333 $name = UserRightsProxy
::whoIs( $database, $id );
337 return Status
::newFatal( 'noname' );
340 $name = User
::getCanonicalName( $name );
341 if ( $name === false ) {
343 return Status
::newFatal( 'nosuchusershort', $username );
347 if ( $database == '' ) {
348 $user = User
::newFromName( $name );
350 $user = UserRightsProxy
::newFromName( $database, $name );
353 if ( !$user ||
$user->isAnon() ) {
354 return Status
::newFatal( 'nosuchusershort', $username );
357 return Status
::newGood( $user );
360 function makeGroupNameList( $ids ) {
361 if ( empty( $ids ) ) {
362 return $this->msg( 'rightsnone' )->inContentLanguage()->text();
364 return implode( ', ', $ids );
369 * Make a list of group names to be stored as parameter for log entries
371 * @deprecated in 1.21; use LogFormatter instead.
375 function makeGroupNameListForLog( $ids ) {
376 wfDeprecated( __METHOD__
, '1.21' );
378 if ( empty( $ids ) ) {
381 return $this->makeGroupNameList( $ids );
386 * Output a form to allow searching for a user
388 function switchForm() {
390 $this->getOutput()->addHTML(
391 Html
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'name' => 'uluser', 'id' => 'mw-userrights-form1' ) ) .
392 Html
::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
393 Xml
::fieldset( $this->msg( 'userrights-lookup-user' )->text() ) .
394 Xml
::inputLabel( $this->msg( 'userrights-user-editname' )->text(), 'user', 'username', 30, str_replace( '_', ' ', $this->mTarget
) ) . ' ' .
395 Xml
::submitButton( $this->msg( 'editusergroup' )->text() ) .
396 Html
::closeElement( 'fieldset' ) .
397 Html
::closeElement( 'form' ) . "\n"
402 * Go through used and available groups and return the ones that this
403 * form will be able to manipulate based on the current user's system
406 * @param array $groups list of groups the given user is in
407 * @return Array: Tuple of addable, then removable groups
409 protected function splitGroups( $groups ) {
410 list( $addable, $removable, $addself, $removeself ) = array_values( $this->changeableGroups() );
412 $removable = array_intersect(
413 array_merge( $this->isself ?
$removeself : array(), $removable ),
415 ); // Can't remove groups the user doesn't have
416 $addable = array_diff(
417 array_merge( $this->isself ?
$addself : array(), $addable ),
419 ); // Can't add groups the user does have
421 return array( $addable, $removable );
425 * Show the form to edit group memberships.
427 * @param $user User or UserRightsProxy you're editing
428 * @param $groups Array: Array of groups the user is in
430 protected function showEditUserGroupsForm( $user, $groups ) {
432 $membersList = array();
433 foreach ( $groups as $group ) {
434 $list[] = self
::buildGroupLink( $group );
435 $membersList[] = self
::buildGroupMemberLink( $group );
439 $autoMembersList = array();
440 if ( $user instanceof User
) {
441 foreach ( Autopromote
::getAutopromoteGroups( $user ) as $group ) {
442 $autoList[] = self
::buildGroupLink( $group );
443 $autoMembersList[] = self
::buildGroupMemberLink( $group );
447 $language = $this->getLanguage();
448 $displayedList = $this->msg( 'userrights-groupsmember-type',
449 $language->listToText( $list ),
450 $language->listToText( $membersList )
452 $displayedAutolist = $this->msg( 'userrights-groupsmember-type',
453 $language->listToText( $autoList ),
454 $language->listToText( $autoMembersList )
458 $count = count( $list );
460 $grouplist = $this->msg( 'userrights-groupsmember', $count, $user->getName() )->parse();
461 $grouplist = '<p>' . $grouplist . ' ' . $displayedList . "</p>\n";
463 $count = count( $autoList );
465 $autogrouplistintro = $this->msg( 'userrights-groupsmember-auto', $count, $user->getName() )->parse();
466 $grouplist .= '<p>' . $autogrouplistintro . ' ' . $displayedAutolist . "</p>\n";
469 $userToolLinks = Linker
::userToolLinks(
472 false, /* default for redContribsWhenNoEdits */
473 Linker
::TOOL_LINKS_EMAIL
/* Add "send e-mail" link */
476 $this->getOutput()->addHTML(
477 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL(), 'name' => 'editGroup', 'id' => 'mw-userrights-form2' ) ) .
478 Html
::hidden( 'user', $this->mTarget
) .
479 Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken( $this->mTarget
) ) .
480 Html
::hidden( 'conflictcheck-originalgroups', implode( ',', $user->getGroups() ) ) . // Conflict detection
481 Xml
::openElement( 'fieldset' ) .
482 Xml
::element( 'legend', array(), $this->msg( 'userrights-editusergroup', $user->getName() )->text() ) .
483 $this->msg( 'editinguser' )->params( wfEscapeWikiText( $user->getName() ) )->rawParams( $userToolLinks )->parse() .
484 $this->msg( 'userrights-groups-help', $user->getName() )->parse() .
486 Xml
::tags( 'p', null, $this->groupCheckboxes( $groups, $user ) ) .
487 Xml
::openElement( 'table', array( 'id' => 'mw-userrights-table-outer' ) ) .
489 <td class='mw-label'>" .
490 Xml
::label( $this->msg( 'userrights-reason' )->text(), 'wpReason' ) .
492 <td class='mw-input'>" .
493 Xml
::input( 'user-reason', 60, $this->getRequest()->getVal( 'user-reason', false ),
494 array( 'id' => 'wpReason', 'maxlength' => 255 ) ) .
499 <td class='mw-submit'>" .
500 Xml
::submitButton( $this->msg( 'saveusergroups' )->text(),
501 array( 'name' => 'saveusergroups' ) + Linker
::tooltipAndAccesskeyAttribs( 'userrights-set' ) ) .
504 Xml
::closeElement( 'table' ) . "\n" .
505 Xml
::closeElement( 'fieldset' ) .
506 Xml
::closeElement( 'form' ) . "\n"
511 * Format a link to a group description page
513 * @param $group string
516 private static function buildGroupLink( $group ) {
517 return User
::makeGroupLinkHtml( $group, User
::getGroupName( $group ) );
521 * Format a link to a group member description page
523 * @param $group string
526 private static function buildGroupMemberLink( $group ) {
527 return User
::makeGroupLinkHtml( $group, User
::getGroupMember( $group ) );
531 * Returns an array of all groups that may be edited
532 * @return array Array of groups that may be edited.
534 protected static function getAllGroups() {
535 return User
::getAllGroups();
539 * Adds a table with checkboxes where you can select what groups to add/remove
541 * @todo Just pass the username string?
542 * @param array $usergroups groups the user belongs to
543 * @param $user User a user object
544 * @return string XHTML table element with checkboxes
546 private function groupCheckboxes( $usergroups, $user ) {
547 $allgroups = $this->getAllGroups();
550 # Put all column info into an associative array so that extensions can
551 # more easily manage it.
552 $columns = array( 'unchangeable' => array(), 'changeable' => array() );
554 foreach ( $allgroups as $group ) {
555 $set = in_array( $group, $usergroups );
556 # Should the checkbox be disabled?
558 ( $set && $this->canRemove( $group ) ) ||
559 ( !$set && $this->canAdd( $group ) ) );
560 # Do we need to point out that this action is irreversible?
561 $irreversible = !$disabled && (
562 ( $set && !$this->canAdd( $group ) ) ||
563 ( !$set && !$this->canRemove( $group ) ) );
567 'disabled' => $disabled,
568 'irreversible' => $irreversible
572 $columns['unchangeable'][$group] = $checkbox;
574 $columns['changeable'][$group] = $checkbox;
578 # Build the HTML table
579 $ret .= Xml
::openElement( 'table', array( 'class' => 'mw-userrights-groups' ) ) .
581 foreach ( $columns as $name => $column ) {
582 if ( $column === array() ) {
585 $ret .= Xml
::element( 'th', null, $this->msg( 'userrights-' . $name . '-col', count( $column ) )->text() );
587 $ret .= "</tr>\n<tr>\n";
588 foreach ( $columns as $column ) {
589 if ( $column === array() ) {
592 $ret .= "\t<td style='vertical-align:top;'>\n";
593 foreach ( $column as $group => $checkbox ) {
594 $attr = $checkbox['disabled'] ?
array( 'disabled' => 'disabled' ) : array();
596 $member = User
::getGroupMember( $group, $user->getName() );
597 if ( $checkbox['irreversible'] ) {
598 $text = $this->msg( 'userrights-irreversible-marker', $member )->escaped();
600 $text = htmlspecialchars( $member );
602 $checkboxHtml = Xml
::checkLabel( $text, "wpGroup-" . $group,
603 "wpGroup-" . $group, $checkbox['set'], $attr );
604 $ret .= "\t\t" . ( $checkbox['disabled']
605 ? Xml
::tags( 'span', array( 'class' => 'mw-userrights-disabled' ), $checkboxHtml )
611 $ret .= Xml
::closeElement( 'tr' ) . Xml
::closeElement( 'table' );
617 * @param $group String: the name of the group to check
618 * @return bool Can we remove the group?
620 private function canRemove( $group ) {
621 // $this->changeableGroups()['remove'] doesn't work, of course. Thanks,
623 $groups = $this->changeableGroups();
624 return in_array( $group, $groups['remove'] ) ||
( $this->isself
&& in_array( $group, $groups['remove-self'] ) );
628 * @param string $group the name of the group to check
629 * @return bool Can we add the group?
631 private function canAdd( $group ) {
632 $groups = $this->changeableGroups();
633 return in_array( $group, $groups['add'] ) ||
( $this->isself
&& in_array( $group, $groups['add-self'] ) );
637 * Returns $this->getUser()->changeableGroups()
639 * @return Array array( 'add' => array( addablegroups ), 'remove' => array( removablegroups ), 'add-self' => array( addablegroups to self ), 'remove-self' => array( removable groups from self ) )
641 function changeableGroups() {
642 return $this->getUser()->changeableGroups();
646 * Show a rights log fragment for the specified user
648 * @param $user User to show log for
649 * @param $output OutputPage to use
651 protected function showLogFragment( $user, $output ) {
652 $rightsLogPage = new LogPage( 'rights' );
653 $output->addHTML( Xml
::element( 'h2', null, $rightsLogPage->getName()->text() ) );
654 LogEventsList
::showLogExtract( $output, 'rights', $user->getUserPage() );
657 protected function getGroupName() {