46 parent::__construct(
'Userrights' );
66 if ( $targetUser->getId() === 0 ) {
70 if ( $available[
'add'] || $available[
'remove'] ) {
75 if ( ( $available[
'add-self'] || $available[
'remove-self'] )
96 $session = $request->getSession();
99 $out->addModules( [
'mediawiki.special.userrights' ] );
101 $this->mTarget = $par ?? $request->getVal(
'user' );
103 if ( is_string( $this->mTarget ) ) {
104 $this->mTarget = trim( $this->mTarget );
108 $this->isself =
true;
111 $fetchedStatus = $this->
fetchUser( $this->mTarget,
true );
112 if ( $fetchedStatus->isOK() ) {
113 $this->mFetchedUser = $fetchedStatus->value;
114 if ( $this->mFetchedUser instanceof
User ) {
117 $this->
getSkin()->setRelevantUser( $this->mFetchedUser );
123 $session->get(
'specialUserrightsSaveSuccess' ) &&
124 $this->mFetchedUser !== null
127 $session->remove(
'specialUserrightsSaveSuccess' );
129 $out->addModuleStyles(
'mediawiki.notification.convertmessagebox.styles' );
134 'class' =>
'mw-notify-success successbox',
135 'id' =>
'mw-preferences-success',
136 'data-mw-autohide' =>
'false',
141 $this->
msg(
'savedrights', $this->mFetchedUser->getName() )->text()
150 $out->addModuleStyles(
'mediawiki.special' );
151 $this->
addHelpLink(
'Help:Assigning permissions' );
156 $request->wasPosted() &&
157 $request->getCheck(
'saveusergroups' ) &&
158 $this->mTarget !==
null &&
159 $user->matchEditToken( $request->getVal(
'wpEditToken' ),
$this->mTarget )
166 if ( !MediaWikiServices::getInstance()
167 ->getPermissionManager()
168 ->userHasRight( $user,
'userrights' )
170 $block = $user->getBlock();
171 if ( $block && $block->isSitewide() ) {
179 if ( !$fetchedStatus->isOK() ) {
180 $this->
getOutput()->addWikiTextAsInterface(
181 $fetchedStatus->getWikiText(
false,
false, $this->getLanguage() )
188 if ( $targetUser instanceof
User ) {
189 $targetUser->clearInstanceCache();
192 $conflictCheck = $request->getVal(
'conflictcheck-originalgroups' );
193 $conflictCheck = ( $conflictCheck ===
'' ) ? [] : explode(
',', $conflictCheck );
194 $userGroups = $targetUser->getGroups();
196 if ( $userGroups !== $conflictCheck ) {
197 $out->wrapWikiMsg(
'<span class="error">$1</span>',
'userrights-conflict' );
201 $request->getVal(
'user-reason' ),
207 $session->set(
'specialUserrightsSaveSuccess', 1 );
213 $out->wrapWikiTextAsInterface(
214 'error',
$status->getWikiText(
false,
false, $this->getLanguage() )
221 if ( $this->mTarget !==
null ) {
227 return $this->
getPageTitle( $this->mTarget )->getFullURL();
254 $unix = strtotime( $expiry );
256 if ( !$unix || $unix === -1 ) {
279 $existingUGMs = $user->getGroupMemberships();
283 foreach ( $allgroups as $group ) {
286 if ( $this->
getRequest()->getCheck(
"wpGroup-$group" ) ) {
287 $addgroup[] = $group;
291 $expiryDropdown = $this->
getRequest()->getVal(
"wpExpiry-$group" );
292 if ( $expiryDropdown ===
'existing' ) {
296 if ( $expiryDropdown ===
'other' ) {
297 $expiryValue = $this->
getRequest()->getVal(
"wpExpiry-$group-other" );
299 $expiryValue = $expiryDropdown;
305 if ( $groupExpiries[$group] ===
false ) {
310 if ( $groupExpiries[$group] && $groupExpiries[$group] <
wfTimestampNow() ) {
317 isset( $existingUGMs[$group] ) &&
318 ( $existingUGMs[$group]->getExpiry() ?:
'infinity' ) >
319 ( $groupExpiries[$group] ?:
'infinity' )
325 $removegroup[] = $group;
329 $this->
doSaveUserGroups( $user, $addgroup, $removegroup, $reason, [], $groupExpiries );
348 array $tags = [], array $groupExpiries = []
352 $groups = $user->getGroups();
353 $ugms = $user->getGroupMemberships();
355 $addable = array_merge( $changeable[
'add'],
$isself ? $changeable[
'add-self'] : [] );
356 $removable = array_merge( $changeable[
'remove'],
$isself ? $changeable[
'remove-self'] : [] );
358 $remove = array_unique(
359 array_intersect( (array)$remove, $removable, $groups ) );
360 $add = array_intersect( (array)$add, $addable );
365 $add = array_filter( $add,
366 function ( $group ) use ( $groups, $groupExpiries, $removable, $ugms ) {
367 if ( isset( $groupExpiries[$group] ) &&
368 !in_array( $group, $removable ) &&
369 isset( $ugms[$group] ) &&
370 ( $ugms[$group]->getExpiry() ?:
'infinity' ) >
371 ( $groupExpiries[$group] ?:
'infinity' )
375 return !in_array( $group, $groups ) || array_key_exists( $group, $groupExpiries );
380 $oldGroups = $groups;
381 $oldUGMs = $user->getGroupMemberships();
382 $newGroups = $oldGroups;
386 foreach ( $remove as $index => $group ) {
387 if ( !$user->removeGroup( $group ) ) {
388 unset( $remove[$index] );
391 $newGroups = array_diff( $newGroups, $remove );
394 foreach ( $add as $index => $group ) {
395 $expiry = $groupExpiries[$group] ??
null;
396 if ( !$user->addGroup( $group, $expiry ) ) {
397 unset( $add[$index] );
400 $newGroups = array_merge( $newGroups, $add );
402 $newGroups = array_unique( $newGroups );
403 $newUGMs = $user->getGroupMemberships();
406 $user->invalidateCache();
410 $reason, $oldUGMs, $newUGMs ] );
412 wfDebug(
'oldGroups: ' . print_r( $oldGroups,
true ) .
"\n" );
413 wfDebug(
'newGroups: ' . print_r( $newGroups,
true ) .
"\n" );
414 wfDebug(
'oldUGMs: ' . print_r( $oldUGMs,
true ) .
"\n" );
415 wfDebug(
'newUGMs: ' . print_r( $newUGMs,
true ) .
"\n" );
418 if ( $newGroups != $oldGroups || $newUGMs != $oldUGMs ) {
419 $this->
addLogEntry( $user, $oldGroups, $newGroups, $reason, $tags, $oldUGMs, $newUGMs );
422 return [ $add, $remove ];
436 return [
'expiry' => $ugm->getExpiry() ];
449 protected function addLogEntry( $user, array $oldGroups, array $newGroups, $reason,
450 array $tags, array $oldUGMs, array $newUGMs
454 $oldUGMs = array_map(
function ( $group ) use ( $oldUGMs ) {
455 return isset( $oldUGMs[$group] ) ?
459 $newUGMs = array_map(
function ( $group ) use ( $newUGMs ) {
460 return isset( $newUGMs[$group] ) ?
466 $logEntry->setPerformer( $this->
getUser() );
467 $logEntry->setTarget( $user->getUserPage() );
468 $logEntry->setComment( $reason );
469 $logEntry->setParameters( [
470 '4::oldgroups' => $oldGroups,
471 '5::newgroups' => $newGroups,
472 'oldmetadata' => $oldUGMs,
473 'newmetadata' => $newUGMs,
475 $logid = $logEntry->insert();
476 if ( count( $tags ) ) {
477 $logEntry->addTags( $tags );
479 $logEntry->publish( $logid );
489 $this->
getOutput()->addWikiTextAsInterface(
490 $status->getWikiText(
false,
false, $this->getLanguage() )
498 '@phan-var User $user';
500 $groups = $user->getGroups();
501 $groupMemberships = $user->getGroupMemberships();
518 public function fetchUser( $username, $writing =
true ) {
519 $parts = explode( $this->
getConfig()->
get(
'UserrightsInterwikiDelimiter' ), $username );
520 if ( count( $parts ) < 2 ) {
521 $name = trim( $username );
524 list( $name, $dbDomain ) = array_map(
'trim', $parts );
529 if ( $writing && !MediaWikiServices::getInstance()
531 ->userHasRight( $this->
getUser(),
'userrights-interwiki' )
541 if ( $name ===
'' ) {
545 if ( $name[0] ==
'#' ) {
548 $id = intval( substr( $name, 1 ) );
550 if ( $dbDomain ==
'' ) {
561 if ( $name ===
false ) {
567 if ( $dbDomain ==
'' ) {
573 if ( !$user || $user->isAnon() ) {
588 if ( empty( $ids ) ) {
589 return $this->
msg(
'rightsnone' )->inContentLanguage()->text();
591 return implode(
', ', $ids );
599 $this->
getOutput()->addModules(
'mediawiki.userSuggest' );
608 'id' =>
'mw-userrights-form1'
611 Html::hidden(
'title', $this->
getPageTitle()->getPrefixedText() ) .
614 $this->
msg(
'userrights-user-editname' )->text(),
618 str_replace(
'_',
' ', $this->mTarget ),
620 'class' =>
'mw-autocomplete-user',
623 $this->mFetchedUser ===
null ? [
'autofocus' =>
'' ] : []
627 $this->
msg(
'editusergroup' )->text()
629 Html::closeElement(
'fieldset' ) .
630 Html::closeElement(
'form' ) .
"\n"
644 $list = $membersList = $tempList = $tempMembersList = [];
645 foreach ( $groupMemberships as $ugm ) {
649 if ( $ugm->getExpiry() ) {
650 $tempList[] = $linkG;
651 $tempMembersList[] = $linkM;
654 $membersList[] = $linkM;
660 $autoMembersList = [];
661 if ( $user instanceof
User ) {
665 'html', $user->getName() );
670 $displayedList = $this->
msg(
'userrights-groupsmember-type' )
672 $language->commaList( array_merge( $tempList, $list ) ),
673 $language->commaList( array_merge( $tempMembersList, $membersList ) )
675 $displayedAutolist = $this->
msg(
'userrights-groupsmember-type' )
677 $language->commaList( $autoList ),
678 $language->commaList( $autoMembersList )
682 $count = count( $list ) + count( $tempList );
684 $grouplist = $this->
msg(
'userrights-groupsmember' )
685 ->numParams( $count )
686 ->params( $user->getName() )
688 $grouplist =
'<p>' . $grouplist .
' ' . $displayedList .
"</p>\n";
691 $count = count( $autoList );
693 $autogrouplistintro = $this->
msg(
'userrights-groupsmember-auto' )
694 ->numParams( $count )
695 ->params( $user->getName() )
697 $grouplist .=
'<p>' . $autogrouplistintro .
' ' . $displayedAutolist .
"</p>\n";
707 list( $groupCheckboxes, $canChangeAny ) =
715 'name' =>
'editGroup',
716 'id' =>
'mw-userrights-form2'
719 Html::hidden(
'user', $this->mTarget ) .
720 Html::hidden(
'wpEditToken', $this->
getUser()->getEditToken( $this->mTarget ) ) .
722 'conflictcheck-originalgroups',
723 implode(
',', $user->getGroups() )
730 $canChangeAny ?
'userrights-editusergroup' :
'userrights-viewusergroup',
735 $canChangeAny ?
'editinguser' :
'viewinguserrights'
737 ->rawParams( $userToolLinks )->parse()
739 if ( $canChangeAny ) {
741 $this->
msg(
'userrights-groups-help', $user->getName() )->parse() .
746 <td class='mw-label'>" .
747 Xml::label( $this->
msg(
'userrights-reason' )->text(),
'wpReason' ) .
749 <td class='mw-input'>" .
761 <td class='mw-submit'>" .
763 [
'name' =>
'saveusergroups' ] +
771 $this->
getOutput()->addHTML( $grouplist );
801 $expiryOptionsMsg = $this->
msg(
'userrights-expiry-options' )->inContentLanguage();
802 $expiryOptions = $expiryOptionsMsg->isDisabled() ?
804 explode(
',', $expiryOptionsMsg->text() );
808 $columns = [
'unchangeable' => [],
'changeable' => [] ];
810 foreach ( $allgroups as $group ) {
811 $set = isset( $usergroups[$group] );
815 $canOnlyLengthenExpiry = ( $set && $this->
canAdd( $group ) &&
816 !$this->
canRemove( $group ) && $usergroups[$group]->getExpiry() );
818 $disabledCheckbox = !(
819 ( $set && $this->
canRemove( $group ) ) ||
820 ( !$set && $this->
canAdd( $group ) ) );
822 $disabledExpiry = $disabledCheckbox && !$canOnlyLengthenExpiry;
824 $irreversible = !$disabledCheckbox && (
825 ( $set && !$this->
canAdd( $group ) ) ||
826 ( !$set && !$this->
canRemove( $group ) ) );
830 'disabled' => $disabledCheckbox,
831 'disabled-expiry' => $disabledExpiry,
832 'irreversible' => $irreversible
835 if ( $disabledCheckbox && $disabledExpiry ) {
836 $columns[
'unchangeable'][$group] = $checkbox;
838 $columns[
'changeable'][$group] = $checkbox;
843 $ret .=
Xml::openElement(
'table', [
'class' =>
'mw-userrights-groups' ] ) .
845 foreach ( $columns as $name => $column ) {
846 if ( $column === [] ) {
853 $this->
msg(
'userrights-' . $name .
'-col', count( $column ) )->text()
857 $ret .=
"</tr>\n<tr>\n";
858 foreach ( $columns as $column ) {
859 if ( $column === [] ) {
862 $ret .=
"\t<td style='vertical-align:top;'>\n";
863 foreach ( $column as $group => $checkbox ) {
864 $attr = [
'class' =>
'mw-userrights-groupcheckbox' ];
865 if ( $checkbox[
'disabled'] ) {
866 $attr[
'disabled'] =
'disabled';
870 if ( $checkbox[
'irreversible'] ) {
871 $text = $this->
msg(
'userrights-irreversible-marker', $member )->text();
872 } elseif ( $checkbox[
'disabled'] && !$checkbox[
'disabled-expiry'] ) {
873 $text = $this->
msg(
'userrights-no-shorten-expiry-marker', $member )->text();
878 "wpGroup-" . $group, $checkbox[
'set'], $attr );
884 $currentExpiry = isset( $usergroups[$group] ) ?
885 $usergroups[$group]->getExpiry() :
890 if ( $checkbox[
'set'] &&
891 ( $checkbox[
'irreversible'] || $checkbox[
'disabled-expiry'] )
893 if ( $currentExpiry ) {
894 $expiryFormatted = $uiLanguage->userTimeAndDate( $currentExpiry, $uiUser );
895 $expiryFormattedD = $uiLanguage->userDate( $currentExpiry, $uiUser );
896 $expiryFormattedT = $uiLanguage->userTime( $currentExpiry, $uiUser );
897 $expiryHtml = $this->
msg(
'userrights-expiry-current' )->params(
898 $expiryFormatted, $expiryFormattedD, $expiryFormattedT )->text();
900 $expiryHtml = $this->
msg(
'userrights-expiry-none' )->text();
904 $expiryHtml .= Html::hidden(
"wpExpiry-$group",
905 $currentExpiry ?
'existing' :
'infinite' );
906 $expiryHtml .=
"<br />\n";
909 $this->
msg(
'userrights-expiry' )->text() );
915 "mw-input-wpExpiry-$group",
916 $currentExpiry ?
'existing' :
'infinite'
918 if ( $checkbox[
'disabled-expiry'] ) {
919 $expiryFormOptions->setAttribute(
'disabled',
'disabled' );
922 if ( $currentExpiry ) {
923 $timestamp = $uiLanguage->userTimeAndDate( $currentExpiry, $uiUser );
924 $d = $uiLanguage->userDate( $currentExpiry, $uiUser );
925 $t = $uiLanguage->userTime( $currentExpiry, $uiUser );
926 $existingExpiryMessage = $this->
msg(
'userrights-expiry-existing',
927 $timestamp, $d,
$t );
928 $expiryFormOptions->addOption( $existingExpiryMessage->text(),
'existing' );
931 $expiryFormOptions->addOption(
932 $this->
msg(
'userrights-expiry-none' )->text(),
935 $expiryFormOptions->addOption(
936 $this->
msg(
'userrights-expiry-othertime' )->text(),
939 foreach ( $expiryOptions as $option ) {
940 if ( strpos( $option,
":" ) ===
false ) {
941 $displayText = $value = $option;
943 list( $displayText, $value ) = explode(
":", $option );
945 $expiryFormOptions->addOption( $displayText, htmlspecialchars( $value ) );
949 $expiryHtml .= $expiryFormOptions->getHTML() .
'<br />';
953 'id' =>
"mw-input-wpExpiry-$group-other",
954 'class' =>
'mw-userrights-expiryfield',
956 if ( $checkbox[
'disabled-expiry'] ) {
957 $attribs[
'disabled'] =
'disabled';
959 $expiryHtml .=
Xml::input(
"wpExpiry-$group-other", 30,
'', $attribs );
963 if ( $checkbox[
'set'] && $checkbox[
'disabled'] ) {
964 $expiryHtml .= Html::hidden(
"wpGroup-$group", 1 );
971 'id' =>
"mw-userrights-nested-wpGroup-$group",
972 'class' =>
'mw-userrights-nested',
974 $checkboxHtml .=
"\t\t\t" .
Xml::tags(
'div', $divAttribs, $expiryHtml ) .
"\n";
976 $ret .=
"\t\t" . ( ( $checkbox[
'disabled'] && $checkbox[
'disabled-expiry'] )
977 ?
Xml::tags(
'div', [
'class' =>
'mw-userrights-disabled' ], $checkboxHtml )
985 return [ $ret, (bool)$columns[
'changeable'] ];
997 $groups[
'remove'] ) || ( $this->isself && in_array( $group, $groups[
'remove-self'] )
1010 $groups[
'add'] ) || ( $this->isself && in_array( $group, $groups[
'add-self'] )
1025 return $this->
getUser()->changeableGroups();
1035 $rightsLogPage =
new LogPage(
'rights' );