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
{
31 * The target of the local right-adjuster's interest. Can be gotten from
32 * either a GET parameter or a subpage-style parameter, so have a member
34 * @var null|string $mTarget
38 * @var null|User $mFetchedUser The user object of the target username or null.
40 protected $mFetchedUser = null;
41 protected $isself = false;
43 public function __construct() {
44 parent
::__construct( 'Userrights' );
47 public function doesWrites() {
52 * Check whether the current user (from context) can change the target user's rights.
54 * @param User $targetUser User whose rights are being changed
55 * @param bool $checkIfSelf If false, assume that the current user can add/remove groups defined
56 * in $wgGroupsAddToSelf / $wgGroupsRemoveFromSelf, without checking if it's the same as target
60 public function userCanChangeRights( $targetUser, $checkIfSelf = true ) {
61 $isself = $this->getUser()->equals( $targetUser );
63 $available = $this->changeableGroups();
64 if ( $targetUser->getId() == 0 ) {
68 return !empty( $available['add'] )
69 ||
!empty( $available['remove'] )
70 ||
( ( $isself ||
!$checkIfSelf ) &&
71 ( !empty( $available['add-self'] )
72 ||
!empty( $available['remove-self'] ) ) );
76 * Manage forms to be shown according to posted data.
77 * Depending on the submit button used, call a form or a save function.
79 * @param string|null $par String if any subpage provided, else null
80 * @throws UserBlockedError|PermissionsError
82 public function execute( $par ) {
83 $user = $this->getUser();
84 $request = $this->getRequest();
85 $session = $request->getSession();
86 $out = $this->getOutput();
88 $out->addModules( [ 'mediawiki.special.userrights' ] );
90 $this->mTarget
= $par ??
$request->getVal( 'user' );
92 if ( is_string( $this->mTarget
) ) {
93 $this->mTarget
= trim( $this->mTarget
);
96 if ( $this->mTarget
!== null && User
::getCanonicalName( $this->mTarget
) === $user->getName() ) {
100 $fetchedStatus = $this->fetchUser( $this->mTarget
, true );
101 if ( $fetchedStatus->isOK() ) {
102 $this->mFetchedUser
= $fetchedStatus->value
;
103 if ( $this->mFetchedUser
instanceof User
) {
104 // Set the 'relevant user' in the skin, so it displays links like Contributions,
105 // User logs, UserRights, etc.
106 $this->getSkin()->setRelevantUser( $this->mFetchedUser
);
110 // show a successbox, if the user rights was saved successfully
112 $session->get( 'specialUserrightsSaveSuccess' ) &&
113 $this->mFetchedUser
!== null
115 // Remove session data for the success message
116 $session->remove( 'specialUserrightsSaveSuccess' );
118 $out->addModuleStyles( 'mediawiki.notification.convertmessagebox.styles' );
123 'class' => 'mw-notify-success successbox',
124 'id' => 'mw-preferences-success',
125 'data-mw-autohide' => 'false',
130 $this->msg( 'savedrights', $this->mFetchedUser
->getName() )->text()
137 $this->outputHeader();
139 $out->addModuleStyles( 'mediawiki.special' );
140 $this->addHelpLink( 'Help:Assigning permissions' );
145 $request->wasPosted() &&
146 $request->getCheck( 'saveusergroups' ) &&
147 $this->mTarget
!== null &&
148 $user->matchEditToken( $request->getVal( 'wpEditToken' ), $this->mTarget
)
151 * If the user is blocked and they only have "partial" access
152 * (e.g. they don't have the userrights permission), then don't
153 * allow them to change any user rights.
155 if ( $user->isBlocked() && !$user->isAllowed( 'userrights' ) ) {
156 throw new UserBlockedError( $user->getBlock() );
159 $this->checkReadOnly();
162 if ( !$fetchedStatus->isOK() ) {
163 $this->getOutput()->addWikiText( $fetchedStatus->getWikiText() );
168 $targetUser = $this->mFetchedUser
;
169 if ( $targetUser instanceof User
) { // UserRightsProxy doesn't have this method (T63252)
170 $targetUser->clearInstanceCache(); // T40989
173 if ( $request->getVal( 'conflictcheck-originalgroups' )
174 !== implode( ',', $targetUser->getGroups() )
176 $out->addWikiMsg( 'userrights-conflict' );
178 $status = $this->saveUserGroups(
180 $request->getVal( 'user-reason' ),
184 if ( $status->isOK() ) {
185 // Set session data for the success message
186 $session->set( 'specialUserrightsSaveSuccess', 1 );
188 $out->redirect( $this->getSuccessURL() );
191 // Print an error message and redisplay the form
192 $out->addWikiText( '<div class="error">' . $status->getWikiText() . '</div>' );
197 // show some more forms
198 if ( $this->mTarget
!== null ) {
199 $this->editUserGroupsForm( $this->mTarget
);
203 function getSuccessURL() {
204 return $this->getPageTitle( $this->mTarget
)->getFullURL();
208 * Returns true if this user rights form can set and change user group expiries.
209 * Subclasses may wish to override this to return false.
213 public function canProcessExpiries() {
218 * Converts a user group membership expiry string into a timestamp. Words like
219 * 'existing' or 'other' should have been filtered out before calling this
222 * @param string $expiry
223 * @return string|null|false A string containing a valid timestamp, or null
224 * if the expiry is infinite, or false if the timestamp is not valid
226 public static function expiryToTimestamp( $expiry ) {
227 if ( wfIsInfinity( $expiry ) ) {
231 $unix = strtotime( $expiry );
233 if ( !$unix ||
$unix === -1 ) {
237 // @todo FIXME: Non-qualified absolute times are not in users specified timezone
238 // and there isn't notice about it in the ui (see ProtectionForm::getExpiry)
239 return wfTimestamp( TS_MW
, $unix );
243 * Save user groups changes in the database.
244 * Data comes from the editUserGroupsForm() form function
246 * @param string $username Username to apply changes to.
247 * @param string $reason Reason for group change
248 * @param User|UserRightsProxy $user Target user object.
251 protected function saveUserGroups( $username, $reason, $user ) {
252 $allgroups = $this->getAllGroups();
254 $groupExpiries = []; // associative array of (group name => expiry)
256 $existingUGMs = $user->getGroupMemberships();
258 // This could possibly create a highly unlikely race condition if permissions are changed between
259 // when the form is loaded and when the form is saved. Ignoring it for the moment.
260 foreach ( $allgroups as $group ) {
261 // We'll tell it to remove all unchecked groups, and add all checked groups.
262 // Later on, this gets filtered for what can actually be removed
263 if ( $this->getRequest()->getCheck( "wpGroup-$group" ) ) {
264 $addgroup[] = $group;
266 if ( $this->canProcessExpiries() ) {
267 // read the expiry information from the request
268 $expiryDropdown = $this->getRequest()->getVal( "wpExpiry-$group" );
269 if ( $expiryDropdown === 'existing' ) {
273 if ( $expiryDropdown === 'other' ) {
274 $expiryValue = $this->getRequest()->getVal( "wpExpiry-$group-other" );
276 $expiryValue = $expiryDropdown;
279 // validate the expiry
280 $groupExpiries[$group] = self
::expiryToTimestamp( $expiryValue );
282 if ( $groupExpiries[$group] === false ) {
283 return Status
::newFatal( 'userrights-invalid-expiry', $group );
286 // not allowed to have things expiring in the past
287 if ( $groupExpiries[$group] && $groupExpiries[$group] < wfTimestampNow() ) {
288 return Status
::newFatal( 'userrights-expiry-in-past', $group );
291 // if the user can only add this group (not remove it), the expiry time
292 // cannot be brought forward (T156784)
293 if ( !$this->canRemove( $group ) &&
294 isset( $existingUGMs[$group] ) &&
295 ( $existingUGMs[$group]->getExpiry() ?
: 'infinity' ) >
296 ( $groupExpiries[$group] ?
: 'infinity' )
298 return Status
::newFatal( 'userrights-cannot-shorten-expiry', $group );
302 $removegroup[] = $group;
306 $this->doSaveUserGroups( $user, $addgroup, $removegroup, $reason, [], $groupExpiries );
308 return Status
::newGood();
312 * Save user groups changes in the database. This function does not throw errors;
313 * instead, it ignores groups that the performer does not have permission to set.
315 * @param User|UserRightsProxy $user
316 * @param array $add Array of groups to add
317 * @param array $remove Array of groups to remove
318 * @param string $reason Reason for group change
319 * @param array $tags Array of change tags to add to the log entry
320 * @param array $groupExpiries Associative array of (group name => expiry),
321 * containing only those groups that are to have new expiry values set
322 * @return array Tuple of added, then removed groups
324 function doSaveUserGroups( $user, array $add, array $remove, $reason = '',
325 array $tags = [], array $groupExpiries = []
327 // Validate input set...
328 $isself = $user->getName() == $this->getUser()->getName();
329 $groups = $user->getGroups();
330 $ugms = $user->getGroupMemberships();
331 $changeable = $this->changeableGroups();
332 $addable = array_merge( $changeable['add'], $isself ?
$changeable['add-self'] : [] );
333 $removable = array_merge( $changeable['remove'], $isself ?
$changeable['remove-self'] : [] );
335 $remove = array_unique(
336 array_intersect( (array)$remove, $removable, $groups ) );
337 $add = array_intersect( (array)$add, $addable );
339 // add only groups that are not already present or that need their expiry updated,
340 // UNLESS the user can only add this group (not remove it) and the expiry time
341 // is being brought forward (T156784)
342 $add = array_filter( $add,
343 function ( $group ) use ( $groups, $groupExpiries, $removable, $ugms ) {
344 if ( isset( $groupExpiries[$group] ) &&
345 !in_array( $group, $removable ) &&
346 isset( $ugms[$group] ) &&
347 ( $ugms[$group]->getExpiry() ?
: 'infinity' ) >
348 ( $groupExpiries[$group] ?
: 'infinity' )
352 return !in_array( $group, $groups ) ||
array_key_exists( $group, $groupExpiries );
355 Hooks
::run( 'ChangeUserGroups', [ $this->getUser(), $user, &$add, &$remove ] );
357 $oldGroups = $groups;
358 $oldUGMs = $user->getGroupMemberships();
359 $newGroups = $oldGroups;
361 // Remove groups, then add new ones/update expiries of existing ones
363 foreach ( $remove as $index => $group ) {
364 if ( !$user->removeGroup( $group ) ) {
365 unset( $remove[$index] );
368 $newGroups = array_diff( $newGroups, $remove );
371 foreach ( $add as $index => $group ) {
372 $expiry = $groupExpiries[$group] ??
null;
373 if ( !$user->addGroup( $group, $expiry ) ) {
374 unset( $add[$index] );
377 $newGroups = array_merge( $newGroups, $add );
379 $newGroups = array_unique( $newGroups );
380 $newUGMs = $user->getGroupMemberships();
382 // Ensure that caches are cleared
383 $user->invalidateCache();
385 // update groups in external authentication database
386 Hooks
::run( 'UserGroupsChanged', [ $user, $add, $remove, $this->getUser(),
387 $reason, $oldUGMs, $newUGMs ] );
388 MediaWiki\Auth\AuthManager
::callLegacyAuthPlugin(
389 'updateExternalDBGroups', [ $user, $add, $remove ]
392 wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) . "\n" );
393 wfDebug( 'newGroups: ' . print_r( $newGroups, true ) . "\n" );
394 wfDebug( 'oldUGMs: ' . print_r( $oldUGMs, true ) . "\n" );
395 wfDebug( 'newUGMs: ' . print_r( $newUGMs, true ) . "\n" );
396 // Deprecated in favor of UserGroupsChanged hook
397 Hooks
::run( 'UserRights', [ &$user, $add, $remove ], '1.26' );
399 // Only add a log entry if something actually changed
400 if ( $newGroups != $oldGroups ||
$newUGMs != $oldUGMs ) {
401 $this->addLogEntry( $user, $oldGroups, $newGroups, $reason, $tags, $oldUGMs, $newUGMs );
404 return [ $add, $remove ];
408 * Serialise a UserGroupMembership object for storage in the log_params section
409 * of the logging table. Only keeps essential data, removing redundant fields.
411 * @param UserGroupMembership|null $ugm May be null if things get borked
414 protected static function serialiseUgmForLog( $ugm ) {
415 if ( !$ugm instanceof UserGroupMembership
) {
418 return [ 'expiry' => $ugm->getExpiry() ];
422 * Add a rights log entry for an action.
423 * @param User|UserRightsProxy $user
424 * @param array $oldGroups
425 * @param array $newGroups
426 * @param string $reason
427 * @param array $tags Change tags for the log entry
428 * @param array $oldUGMs Associative array of (group name => UserGroupMembership)
429 * @param array $newUGMs Associative array of (group name => UserGroupMembership)
431 protected function addLogEntry( $user, array $oldGroups, array $newGroups, $reason,
432 array $tags, array $oldUGMs, array $newUGMs
434 // make sure $oldUGMs and $newUGMs are in the same order, and serialise
435 // each UGM object to a simplified array
436 $oldUGMs = array_map( function ( $group ) use ( $oldUGMs ) {
437 return isset( $oldUGMs[$group] ) ?
438 self
::serialiseUgmForLog( $oldUGMs[$group] ) :
441 $newUGMs = array_map( function ( $group ) use ( $newUGMs ) {
442 return isset( $newUGMs[$group] ) ?
443 self
::serialiseUgmForLog( $newUGMs[$group] ) :
447 $logEntry = new ManualLogEntry( 'rights', 'rights' );
448 $logEntry->setPerformer( $this->getUser() );
449 $logEntry->setTarget( $user->getUserPage() );
450 $logEntry->setComment( $reason );
451 $logEntry->setParameters( [
452 '4::oldgroups' => $oldGroups,
453 '5::newgroups' => $newGroups,
454 'oldmetadata' => $oldUGMs,
455 'newmetadata' => $newUGMs,
457 $logid = $logEntry->insert();
458 if ( count( $tags ) ) {
459 $logEntry->setTags( $tags );
461 $logEntry->publish( $logid );
465 * Edit user groups membership
466 * @param string $username Name of the user.
468 function editUserGroupsForm( $username ) {
469 $status = $this->fetchUser( $username, true );
470 if ( !$status->isOK() ) {
471 $this->getOutput()->addWikiText( $status->getWikiText() );
475 $user = $status->value
;
478 $groups = $user->getGroups();
479 $groupMemberships = $user->getGroupMemberships();
480 $this->showEditUserGroupsForm( $user, $groups, $groupMemberships );
482 // This isn't really ideal logging behavior, but let's not hide the
483 // interwiki logs if we're using them as is.
484 $this->showLogFragment( $user, $this->getOutput() );
488 * Normalize the input username, which may be local or remote, and
489 * return a user (or proxy) object for manipulating it.
491 * Side effects: error output for invalid access
492 * @param string $username
493 * @param bool $writing
496 public function fetchUser( $username, $writing = true ) {
497 $parts = explode( $this->getConfig()->get( 'UserrightsInterwikiDelimiter' ), $username );
498 if ( count( $parts ) < 2 ) {
499 $name = trim( $username );
502 list( $name, $database ) = array_map( 'trim', $parts );
504 if ( $database == wfWikiID() ) {
507 if ( $writing && !$this->getUser()->isAllowed( 'userrights-interwiki' ) ) {
508 return Status
::newFatal( 'userrights-no-interwiki' );
510 if ( !UserRightsProxy
::validDatabase( $database ) ) {
511 return Status
::newFatal( 'userrights-nodatabase', $database );
516 if ( $name === '' ) {
517 return Status
::newFatal( 'nouserspecified' );
520 if ( $name[0] == '#' ) {
521 // Numeric ID can be specified...
522 // We'll do a lookup for the name internally.
523 $id = intval( substr( $name, 1 ) );
525 if ( $database == '' ) {
526 $name = User
::whoIs( $id );
528 $name = UserRightsProxy
::whoIs( $database, $id );
532 return Status
::newFatal( 'noname' );
535 $name = User
::getCanonicalName( $name );
536 if ( $name === false ) {
538 return Status
::newFatal( 'nosuchusershort', $username );
542 if ( $database == '' ) {
543 $user = User
::newFromName( $name );
545 $user = UserRightsProxy
::newFromName( $database, $name );
548 if ( !$user ||
$user->isAnon() ) {
549 return Status
::newFatal( 'nosuchusershort', $username );
552 return Status
::newGood( $user );
562 public function makeGroupNameList( $ids ) {
563 if ( empty( $ids ) ) {
564 return $this->msg( 'rightsnone' )->inContentLanguage()->text();
566 return implode( ', ', $ids );
571 * Output a form to allow searching for a user
573 function switchForm() {
574 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
576 $this->getOutput()->addHTML(
581 'action' => wfScript(),
583 'id' => 'mw-userrights-form1'
586 Html
::hidden( 'title', $this->getPageTitle()->getPrefixedText() ) .
587 Xml
::fieldset( $this->msg( 'userrights-lookup-user' )->text() ) .
589 $this->msg( 'userrights-user-editname' )->text(),
593 str_replace( '_', ' ', $this->mTarget
),
595 'class' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
597 // Set autofocus on blank input and error input
598 $this->mFetchedUser
=== null ?
[ 'autofocus' => '' ] : []
602 $this->msg( 'editusergroup' )->text()
604 Html
::closeElement( 'fieldset' ) .
605 Html
::closeElement( 'form' ) . "\n"
610 * Show the form to edit group memberships.
612 * @param User|UserRightsProxy $user User or UserRightsProxy you're editing
613 * @param array $groups Array of groups the user is in. Not used by this implementation
614 * anymore, but kept for backward compatibility with subclasses
615 * @param array $groupMemberships Associative array of (group name => UserGroupMembership
616 * object) containing the groups the user is in
618 protected function showEditUserGroupsForm( $user, $groups, $groupMemberships ) {
619 $list = $membersList = $tempList = $tempMembersList = [];
620 foreach ( $groupMemberships as $ugm ) {
621 $linkG = UserGroupMembership
::getLink( $ugm, $this->getContext(), 'html' );
622 $linkM = UserGroupMembership
::getLink( $ugm, $this->getContext(), 'html',
624 if ( $ugm->getExpiry() ) {
625 $tempList[] = $linkG;
626 $tempMembersList[] = $linkM;
629 $membersList[] = $linkM;
635 $autoMembersList = [];
636 if ( $user instanceof User
) {
637 foreach ( Autopromote
::getAutopromoteGroups( $user ) as $group ) {
638 $autoList[] = UserGroupMembership
::getLink( $group, $this->getContext(), 'html' );
639 $autoMembersList[] = UserGroupMembership
::getLink( $group, $this->getContext(),
640 'html', $user->getName() );
644 $language = $this->getLanguage();
645 $displayedList = $this->msg( 'userrights-groupsmember-type' )
647 $language->commaList( array_merge( $tempList, $list ) ),
648 $language->commaList( array_merge( $tempMembersList, $membersList ) )
650 $displayedAutolist = $this->msg( 'userrights-groupsmember-type' )
652 $language->commaList( $autoList ),
653 $language->commaList( $autoMembersList )
657 $count = count( $list ) +
count( $tempList );
659 $grouplist = $this->msg( 'userrights-groupsmember' )
660 ->numParams( $count )
661 ->params( $user->getName() )
663 $grouplist = '<p>' . $grouplist . ' ' . $displayedList . "</p>\n";
666 $count = count( $autoList );
668 $autogrouplistintro = $this->msg( 'userrights-groupsmember-auto' )
669 ->numParams( $count )
670 ->params( $user->getName() )
672 $grouplist .= '<p>' . $autogrouplistintro . ' ' . $displayedAutolist . "</p>\n";
675 $userToolLinks = Linker
::userToolLinks(
678 false, /* default for redContribsWhenNoEdits */
679 Linker
::TOOL_LINKS_EMAIL
/* Add "send e-mail" link */
682 list( $groupCheckboxes, $canChangeAny ) =
683 $this->groupCheckboxes( $groupMemberships, $user );
684 $this->getOutput()->addHTML(
689 'action' => $this->getPageTitle()->getLocalURL(),
690 'name' => 'editGroup',
691 'id' => 'mw-userrights-form2'
694 Html
::hidden( 'user', $this->mTarget
) .
695 Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken( $this->mTarget
) ) .
697 'conflictcheck-originalgroups',
698 implode( ',', $user->getGroups() )
699 ) . // Conflict detection
700 Xml
::openElement( 'fieldset' ) .
705 $canChangeAny ?
'userrights-editusergroup' : 'userrights-viewusergroup',
710 $canChangeAny ?
'editinguser' : 'viewinguserrights'
711 )->params( wfEscapeWikiText( $user->getName() ) )
712 ->rawParams( $userToolLinks )->parse()
714 if ( $canChangeAny ) {
715 $conf = $this->getConfig();
716 $oldCommentSchema = $conf->get( 'CommentTableSchemaMigrationStage' ) === MIGRATION_OLD
;
717 $this->getOutput()->addHTML(
718 $this->msg( 'userrights-groups-help', $user->getName() )->parse() .
721 Xml
::openElement( 'table', [ 'id' => 'mw-userrights-table-outer' ] ) .
723 <td class='mw-label'>" .
724 Xml
::label( $this->msg( 'userrights-reason' )->text(), 'wpReason' ) .
726 <td class='mw-input'>" .
727 Xml
::input( 'user-reason', 60, $this->getRequest()->getVal( 'user-reason', false ), [
729 // HTML maxlength uses "UTF-16 code units", which means that characters outside BMP
730 // (e.g. emojis) count for two each. This limit is overridden in JS to instead count
731 // Unicode codepoints (or 255 UTF-8 bytes for old schema).
732 'maxlength' => $oldCommentSchema ?
255 : CommentStore
::COMMENT_CHARACTER_LIMIT
,
738 <td class='mw-submit'>" .
739 Xml
::submitButton( $this->msg( 'saveusergroups', $user->getName() )->text(),
740 [ 'name' => 'saveusergroups' ] +
741 Linker
::tooltipAndAccesskeyAttribs( 'userrights-set' )
745 Xml
::closeElement( 'table' ) . "\n"
748 $this->getOutput()->addHTML( $grouplist );
750 $this->getOutput()->addHTML(
751 Xml
::closeElement( 'fieldset' ) .
752 Xml
::closeElement( 'form' ) . "\n"
757 * Returns an array of all groups that may be edited
758 * @return array Array of groups that may be edited.
760 protected static function getAllGroups() {
761 return User
::getAllGroups();
765 * Adds a table with checkboxes where you can select what groups to add/remove
767 * @param UserGroupMembership[] $usergroups Associative array of (group name as string =>
768 * UserGroupMembership object) for groups the user belongs to
770 * @return Array with 2 elements: the XHTML table element with checkxboes, and
771 * whether any groups are changeable
773 private function groupCheckboxes( $usergroups, $user ) {
774 $allgroups = $this->getAllGroups();
777 // Get the list of preset expiry times from the system message
778 $expiryOptionsMsg = $this->msg( 'userrights-expiry-options' )->inContentLanguage();
779 $expiryOptions = $expiryOptionsMsg->isDisabled() ?
781 explode( ',', $expiryOptionsMsg->text() );
783 // Put all column info into an associative array so that extensions can
784 // more easily manage it.
785 $columns = [ 'unchangeable' => [], 'changeable' => [] ];
787 foreach ( $allgroups as $group ) {
788 $set = isset( $usergroups[$group] );
789 // Users who can add the group, but not remove it, can only lengthen
790 // expiries, not shorten them. So they should only see the expiry
791 // dropdown if the group currently has a finite expiry
792 $canOnlyLengthenExpiry = ( $set && $this->canAdd( $group ) &&
793 !$this->canRemove( $group ) && $usergroups[$group]->getExpiry() );
794 // Should the checkbox be disabled?
795 $disabledCheckbox = !(
796 ( $set && $this->canRemove( $group ) ) ||
797 ( !$set && $this->canAdd( $group ) ) );
798 // Should the expiry elements be disabled?
799 $disabledExpiry = $disabledCheckbox && !$canOnlyLengthenExpiry;
800 // Do we need to point out that this action is irreversible?
801 $irreversible = !$disabledCheckbox && (
802 ( $set && !$this->canAdd( $group ) ) ||
803 ( !$set && !$this->canRemove( $group ) ) );
807 'disabled' => $disabledCheckbox,
808 'disabled-expiry' => $disabledExpiry,
809 'irreversible' => $irreversible
812 if ( $disabledCheckbox && $disabledExpiry ) {
813 $columns['unchangeable'][$group] = $checkbox;
815 $columns['changeable'][$group] = $checkbox;
819 // Build the HTML table
820 $ret .= Xml
::openElement( 'table', [ 'class' => 'mw-userrights-groups' ] ) .
822 foreach ( $columns as $name => $column ) {
823 if ( $column === [] ) {
826 // Messages: userrights-changeable-col, userrights-unchangeable-col
827 $ret .= Xml
::element(
830 $this->msg( 'userrights-' . $name . '-col', count( $column ) )->text()
834 $ret .= "</tr>\n<tr>\n";
835 foreach ( $columns as $column ) {
836 if ( $column === [] ) {
839 $ret .= "\t<td style='vertical-align:top;'>\n";
840 foreach ( $column as $group => $checkbox ) {
841 $attr = [ 'class' => 'mw-userrights-groupcheckbox' ];
842 if ( $checkbox['disabled'] ) {
843 $attr['disabled'] = 'disabled';
846 $member = UserGroupMembership
::getGroupMemberName( $group, $user->getName() );
847 if ( $checkbox['irreversible'] ) {
848 $text = $this->msg( 'userrights-irreversible-marker', $member )->text();
849 } elseif ( $checkbox['disabled'] && !$checkbox['disabled-expiry'] ) {
850 $text = $this->msg( 'userrights-no-shorten-expiry-marker', $member )->text();
854 $checkboxHtml = Xml
::checkLabel( $text, "wpGroup-" . $group,
855 "wpGroup-" . $group, $checkbox['set'], $attr );
857 if ( $this->canProcessExpiries() ) {
858 $uiUser = $this->getUser();
859 $uiLanguage = $this->getLanguage();
861 $currentExpiry = isset( $usergroups[$group] ) ?
862 $usergroups[$group]->getExpiry() :
865 // If the user can't modify the expiry, print the current expiry below
866 // it in plain text. Otherwise provide UI to set/change the expiry
867 if ( $checkbox['set'] &&
868 ( $checkbox['irreversible'] ||
$checkbox['disabled-expiry'] )
870 if ( $currentExpiry ) {
871 $expiryFormatted = $uiLanguage->userTimeAndDate( $currentExpiry, $uiUser );
872 $expiryFormattedD = $uiLanguage->userDate( $currentExpiry, $uiUser );
873 $expiryFormattedT = $uiLanguage->userTime( $currentExpiry, $uiUser );
874 $expiryHtml = $this->msg( 'userrights-expiry-current' )->params(
875 $expiryFormatted, $expiryFormattedD, $expiryFormattedT )->text();
877 $expiryHtml = $this->msg( 'userrights-expiry-none' )->text();
879 // T171345: Add a hidden form element so that other groups can still be manipulated,
880 // otherwise saving errors out with an invalid expiry time for this group.
881 $expiryHtml .= Html
::hidden( "wpExpiry-$group",
882 $currentExpiry ?
'existing' : 'infinite' );
883 $expiryHtml .= "<br />\n";
885 $expiryHtml = Xml
::element( 'span', null,
886 $this->msg( 'userrights-expiry' )->text() );
887 $expiryHtml .= Xml
::openElement( 'span' );
889 // add a form element to set the expiry date
890 $expiryFormOptions = new XmlSelect(
892 "mw-input-wpExpiry-$group", // forward compatibility with HTMLForm
893 $currentExpiry ?
'existing' : 'infinite'
895 if ( $checkbox['disabled-expiry'] ) {
896 $expiryFormOptions->setAttribute( 'disabled', 'disabled' );
899 if ( $currentExpiry ) {
900 $timestamp = $uiLanguage->userTimeAndDate( $currentExpiry, $uiUser );
901 $d = $uiLanguage->userDate( $currentExpiry, $uiUser );
902 $t = $uiLanguage->userTime( $currentExpiry, $uiUser );
903 $existingExpiryMessage = $this->msg( 'userrights-expiry-existing',
904 $timestamp, $d, $t );
905 $expiryFormOptions->addOption( $existingExpiryMessage->text(), 'existing' );
908 $expiryFormOptions->addOption(
909 $this->msg( 'userrights-expiry-none' )->text(),
912 $expiryFormOptions->addOption(
913 $this->msg( 'userrights-expiry-othertime' )->text(),
916 foreach ( $expiryOptions as $option ) {
917 if ( strpos( $option, ":" ) === false ) {
918 $displayText = $value = $option;
920 list( $displayText, $value ) = explode( ":", $option );
922 $expiryFormOptions->addOption( $displayText, htmlspecialchars( $value ) );
925 // Add expiry dropdown
926 $expiryHtml .= $expiryFormOptions->getHTML() . '<br />';
928 // Add custom expiry field
930 'id' => "mw-input-wpExpiry-$group-other",
931 'class' => 'mw-userrights-expiryfield',
933 if ( $checkbox['disabled-expiry'] ) {
934 $attribs['disabled'] = 'disabled';
936 $expiryHtml .= Xml
::input( "wpExpiry-$group-other", 30, '', $attribs );
938 // If the user group is set but the checkbox is disabled, mimic a
939 // checked checkbox in the form submission
940 if ( $checkbox['set'] && $checkbox['disabled'] ) {
941 $expiryHtml .= Html
::hidden( "wpGroup-$group", 1 );
944 $expiryHtml .= Xml
::closeElement( 'span' );
948 'id' => "mw-userrights-nested-wpGroup-$group",
949 'class' => 'mw-userrights-nested',
951 $checkboxHtml .= "\t\t\t" . Xml
::tags( 'div', $divAttribs, $expiryHtml ) . "\n";
953 $ret .= "\t\t" . ( ( $checkbox['disabled'] && $checkbox['disabled-expiry'] )
954 ? Xml
::tags( 'div', [ 'class' => 'mw-userrights-disabled' ], $checkboxHtml )
955 : Xml
::tags( 'div', [], $checkboxHtml )
960 $ret .= Xml
::closeElement( 'tr' ) . Xml
::closeElement( 'table' );
962 return [ $ret, (bool)$columns['changeable'] ];
966 * @param string $group The name of the group to check
967 * @return bool Can we remove the group?
969 private function canRemove( $group ) {
970 $groups = $this->changeableGroups();
974 $groups['remove'] ) ||
( $this->isself
&& in_array( $group, $groups['remove-self'] )
979 * @param string $group The name of the group to check
980 * @return bool Can we add the group?
982 private function canAdd( $group ) {
983 $groups = $this->changeableGroups();
987 $groups['add'] ) ||
( $this->isself
&& in_array( $group, $groups['add-self'] )
992 * Returns $this->getUser()->changeableGroups()
994 * @return array Array(
995 * 'add' => array( addablegroups ),
996 * 'remove' => array( removablegroups ),
997 * 'add-self' => array( addablegroups to self ),
998 * 'remove-self' => array( removable groups from self )
1001 function changeableGroups() {
1002 return $this->getUser()->changeableGroups();
1006 * Show a rights log fragment for the specified user
1008 * @param User $user User to show log for
1009 * @param OutputPage $output OutputPage to use
1011 protected function showLogFragment( $user, $output ) {
1012 $rightsLogPage = new LogPage( 'rights' );
1013 $output->addHTML( Xml
::element( 'h2', null, $rightsLogPage->getName()->text() ) );
1014 LogEventsList
::showLogExtract( $output, 'rights', $user->getUserPage() );
1018 * Return an array of subpages beginning with $search that this special page will accept.
1020 * @param string $search Prefix to search for
1021 * @param int $limit Maximum number of results to return (usually 10)
1022 * @param int $offset Number of results to skip (usually 0)
1023 * @return string[] Matching subpages
1025 public function prefixSearchSubpages( $search, $limit, $offset ) {
1026 $user = User
::newFromName( $search );
1028 // No prefix suggestion for invalid user
1031 // Autocomplete subpage as user list - public to allow caching
1032 return UserNamePrefixSearch
::search( 'public', $search, $limit, $offset );
1035 protected function getGroupName() {