1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
|
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\Specials;
use LogEventsList;
use LogPage;
use ManualLogEntry;
use MediaWiki\CommentStore\CommentStore;
use MediaWiki\Html\Html;
use MediaWiki\HTMLForm\Field\HTMLUserTextField;
use MediaWiki\HTMLForm\HTMLForm;
use MediaWiki\Linker\Linker;
use MediaWiki\MainConfigNames;
use MediaWiki\MediaWikiServices;
use MediaWiki\Output\OutputPage;
use MediaWiki\SpecialPage\SpecialPage;
use MediaWiki\Status\Status;
use MediaWiki\Title\Title;
use MediaWiki\User\ActorStoreFactory;
use MediaWiki\User\TempUser\TempUserConfig;
use MediaWiki\User\UserFactory;
use MediaWiki\User\UserGroupManager;
use MediaWiki\User\UserGroupManagerFactory;
use MediaWiki\User\UserGroupMembership;
use MediaWiki\User\UserIdentity;
use MediaWiki\User\UserNamePrefixSearch;
use MediaWiki\User\UserNameUtils;
use MediaWiki\Watchlist\WatchlistManager;
use MediaWiki\WikiMap\WikiMap;
use MediaWiki\Xml\Xml;
use MediaWiki\Xml\XmlSelect;
use PermissionsError;
use UserBlockedError;
use Wikimedia\Rdbms\IDBAccessObject;
/**
* Special page to allow managing user group membership
*
* @ingroup SpecialPage
*/
class SpecialUserRights extends SpecialPage {
/**
* The target of the local right-adjuster's interest. Can be gotten from
* either a GET parameter or a subpage-style parameter, so have a member
* variable for it.
* @var null|string
*/
protected $mTarget;
/**
* @var null|UserIdentity The user object of the target username or null.
*/
protected $mFetchedUser = null;
/** @var bool */
protected $isself = false;
private UserGroupManagerFactory $userGroupManagerFactory;
/** @var UserGroupManager|null The UserGroupManager of the target username or null */
private $userGroupManager = null;
private UserNameUtils $userNameUtils;
private UserNamePrefixSearch $userNamePrefixSearch;
private UserFactory $userFactory;
private ActorStoreFactory $actorStoreFactory;
private WatchlistManager $watchlistManager;
private TempUserConfig $tempUserConfig;
/**
* @param UserGroupManagerFactory|null $userGroupManagerFactory
* @param UserNameUtils|null $userNameUtils
* @param UserNamePrefixSearch|null $userNamePrefixSearch
* @param UserFactory|null $userFactory
* @param ActorStoreFactory|null $actorStoreFactory
* @param WatchlistManager|null $watchlistManager
* @param TempUserConfig|null $tempUserConfig
*/
public function __construct(
?UserGroupManagerFactory $userGroupManagerFactory = null,
?UserNameUtils $userNameUtils = null,
?UserNamePrefixSearch $userNamePrefixSearch = null,
?UserFactory $userFactory = null,
?ActorStoreFactory $actorStoreFactory = null,
?WatchlistManager $watchlistManager = null,
?TempUserConfig $tempUserConfig = null
) {
parent::__construct( 'Userrights' );
$services = MediaWikiServices::getInstance();
// This class is extended and therefore falls back to global state - T263207
$this->userNameUtils = $userNameUtils ?? $services->getUserNameUtils();
$this->userNamePrefixSearch = $userNamePrefixSearch ?? $services->getUserNamePrefixSearch();
$this->userFactory = $userFactory ?? $services->getUserFactory();
$this->userGroupManagerFactory = $userGroupManagerFactory ?? $services->getUserGroupManagerFactory();
$this->actorStoreFactory = $actorStoreFactory ?? $services->getActorStoreFactory();
$this->watchlistManager = $watchlistManager ?? $services->getWatchlistManager();
$this->tempUserConfig = $tempUserConfig ?? $services->getTempUserConfig();
}
public function doesWrites() {
return true;
}
/**
* Check whether the current user (from context) can change the target user's rights.
*
* This function can be used without submitting the special page
*
* @param UserIdentity $targetUser User whose rights are being changed
* @param bool $checkIfSelf If false, assume that the current user can add/remove groups defined
* in $wgGroupsAddToSelf / $wgGroupsRemoveFromSelf, without checking if it's the same as target
* user
* @return bool
*/
public function userCanChangeRights( UserIdentity $targetUser, $checkIfSelf = true ) {
if (
!$targetUser->isRegistered() ||
$this->userNameUtils->isTemp( $targetUser->getName() )
) {
return false;
}
$userGroupManager = $this->userGroupManagerFactory
->getUserGroupManager( $targetUser->getWikiId() );
$available = $userGroupManager->getGroupsChangeableBy( $this->getAuthority() );
if ( $available['add'] || $available['remove'] ) {
// can change some rights for any user
return true;
}
$isself = $this->getUser()->equals( $targetUser );
if ( ( $available['add-self'] || $available['remove-self'] )
&& ( $isself || !$checkIfSelf )
) {
// can change some rights for self
return true;
}
return false;
}
/**
* Manage forms to be shown according to posted data.
* Depending on the submit button used, call a form or a save function.
*
* @param string|null $par String if any subpage provided, else null
* @throws UserBlockedError|PermissionsError
*/
public function execute( $par ) {
$user = $this->getUser();
$request = $this->getRequest();
$session = $request->getSession();
$out = $this->getOutput();
$out->addModules( [ 'mediawiki.special.userrights' ] );
$this->mTarget = $par ?? $request->getVal( 'user' );
if ( $this->mTarget === null ) {
$fetchedStatus = Status::newFatal( 'nouserspecified' );
} else {
$this->mTarget = trim( $this->mTarget );
if ( $this->userNameUtils->getCanonical( $this->mTarget ) === $user->getName() ) {
$this->isself = true;
}
$fetchedStatus = $this->fetchUser( $this->mTarget, true );
}
if ( $fetchedStatus->isOK() ) {
$this->mFetchedUser = $fetchedUser = $fetchedStatus->value;
// Phan false positive on Status object - T323205
'@phan-var UserIdentity $fetchedUser';
$wikiId = $fetchedUser->getWikiId();
if ( $wikiId === UserIdentity::LOCAL ) {
// Set the 'relevant user' in the skin, so it displays links like Contributions,
// User logs, UserRights, etc.
$this->getSkin()->setRelevantUser( $this->mFetchedUser );
}
$this->userGroupManager = $this->userGroupManagerFactory
->getUserGroupManager( $wikiId );
}
// show a successbox, if the user rights was saved successfully
if (
$session->get( 'specialUserrightsSaveSuccess' ) &&
$this->mFetchedUser !== null
) {
// Remove session data for the success message
$session->remove( 'specialUserrightsSaveSuccess' );
$out->addModuleStyles( 'mediawiki.notification.convertmessagebox.styles' );
$out->addHTML(
Html::successBox(
Html::element(
'p',
[],
$this->msg( 'savedrights', $this->getDisplayUsername( $this->mFetchedUser ) )->text()
),
'mw-notify-success'
)
);
}
$this->setHeaders();
$this->outputHeader();
$out->addModuleStyles( 'mediawiki.special' );
$this->addHelpLink( 'Help:Assigning permissions' );
$this->switchForm();
if (
$request->wasPosted() &&
$request->getCheck( 'saveusergroups' ) &&
$this->mTarget !== null &&
$user->matchEditToken( $request->getVal( 'wpEditToken' ), $this->mTarget )
) {
/*
* If the user is blocked and they only have "partial" access
* (e.g. they don't have the userrights permission), then don't
* allow them to change any user rights.
*/
if ( !$this->getAuthority()->isAllowed( 'userrights' ) ) {
$block = $user->getBlock();
if ( $block && $block->isSitewide() ) {
throw new UserBlockedError(
$block,
$user,
$this->getLanguage(),
$request->getIP()
);
}
}
$this->checkReadOnly();
// save settings
if ( !$fetchedStatus->isOK() ) {
$this->getOutput()->addWikiTextAsInterface(
$fetchedStatus->getWikiText( false, false, $this->getLanguage() )
);
return;
}
$targetUser = $this->mFetchedUser;
$conflictCheck = $request->getVal( 'conflictcheck-originalgroups' );
$conflictCheck = ( $conflictCheck === '' ) ? [] : explode( ',', $conflictCheck );
$userGroups = $this->userGroupManager->getUserGroups( $targetUser, IDBAccessObject::READ_LATEST );
if ( $userGroups !== $conflictCheck ) {
$out->addHTML( Html::errorBox(
$this->msg( 'userrights-conflict' )->parse()
) );
} else {
$status = $this->saveUserGroups(
$request->getText( 'user-reason' ),
$targetUser
);
if ( $status->isOK() ) {
// Set session data for the success message
$session->set( 'specialUserrightsSaveSuccess', 1 );
$out->redirect( $this->getSuccessURL() );
return;
} else {
// Print an error message and redisplay the form
$out->wrapWikiTextAsInterface(
'error', $status->getWikiText( false, false, $this->getLanguage() )
);
}
}
}
// show some more forms
if ( $this->mTarget !== null ) {
$this->editUserGroupsForm( $this->mTarget );
}
}
private function getSuccessURL() {
return $this->getPageTitle( $this->mTarget )->getFullURL();
}
/**
* Returns true if this user rights form can set and change user group expiries.
* Subclasses may wish to override this to return false.
*
* @return bool
*/
public function canProcessExpiries() {
return true;
}
/**
* Converts a user group membership expiry string into a timestamp. Words like
* 'existing' or 'other' should have been filtered out before calling this
* function.
*
* @param string $expiry
* @return string|null|false A string containing a valid timestamp, or null
* if the expiry is infinite, or false if the timestamp is not valid
*/
public static function expiryToTimestamp( $expiry ) {
if ( wfIsInfinity( $expiry ) ) {
return null;
}
$unix = strtotime( $expiry );
if ( !$unix || $unix === -1 ) {
return false;
}
// @todo FIXME: Non-qualified absolute times are not in users specified timezone
// and there isn't notice about it in the ui (see ProtectionForm::getExpiry)
return wfTimestamp( TS_MW, $unix );
}
/**
* Save user groups changes in the database.
* Data comes from the editUserGroupsForm() form function
*
* @param string $reason Reason for group change
* @param UserIdentity $user
* @return Status
*/
protected function saveUserGroups( string $reason, UserIdentity $user ) {
if ( $this->userNameUtils->isTemp( $user->getName() ) ) {
return Status::newFatal( 'userrights-no-tempuser' );
}
// Prevent cross-wiki assignment of groups to temporary accounts on wikis where the feature is not known.
if (
$user->getWikiId() !== UserIdentity::LOCAL &&
!$this->tempUserConfig->isKnown() &&
$this->tempUserConfig->isReservedName( $user->getName() )
) {
return Status::newFatal( 'userrights-cross-wiki-assignment-for-reserved-name' );
}
$allgroups = $this->userGroupManager->listAllGroups();
$addgroup = [];
$groupExpiries = []; // associative array of (group name => expiry)
$removegroup = [];
$existingUGMs = $this->userGroupManager->getUserGroupMemberships( $user );
// This could possibly create a highly unlikely race condition if permissions are changed between
// when the form is loaded and when the form is saved. Ignoring it for the moment.
foreach ( $allgroups as $group ) {
// We'll tell it to remove all unchecked groups, and add all checked groups.
// Later on, this gets filtered for what can actually be removed
if ( $this->getRequest()->getCheck( "wpGroup-$group" ) ) {
$addgroup[] = $group;
if ( $this->canProcessExpiries() ) {
// read the expiry information from the request
$expiryDropdown = $this->getRequest()->getVal( "wpExpiry-$group" );
if ( $expiryDropdown === 'existing' ) {
continue;
}
if ( $expiryDropdown === 'other' ) {
$expiryValue = $this->getRequest()->getVal( "wpExpiry-$group-other" );
} else {
$expiryValue = $expiryDropdown;
}
// validate the expiry
$groupExpiries[$group] = self::expiryToTimestamp( $expiryValue );
if ( $groupExpiries[$group] === false ) {
return Status::newFatal( 'userrights-invalid-expiry', $group );
}
// not allowed to have things expiring in the past
if ( $groupExpiries[$group] && $groupExpiries[$group] < wfTimestampNow() ) {
return Status::newFatal( 'userrights-expiry-in-past', $group );
}
// if the user can only add this group (not remove it), the expiry time
// cannot be brought forward (T156784)
if ( !$this->canRemove( $group ) &&
isset( $existingUGMs[$group] ) &&
( $existingUGMs[$group]->getExpiry() ?: 'infinity' ) >
( $groupExpiries[$group] ?: 'infinity' )
) {
return Status::newFatal( 'userrights-cannot-shorten-expiry', $group );
}
}
} else {
$removegroup[] = $group;
}
}
$this->doSaveUserGroups( $user, $addgroup, $removegroup, $reason, [], $groupExpiries );
if ( $user->getWikiId() === UserIdentity::LOCAL && $this->getRequest()->getCheck( 'wpWatch' ) ) {
$this->watchlistManager->addWatchIgnoringRights(
$this->getUser(),
Title::makeTitle( NS_USER, $user->getName() )
);
}
return Status::newGood();
}
/**
* Save user groups changes in the database. This function does not throw errors;
* instead, it ignores groups that the performer does not have permission to set.
*
* This function can be used without submitting the special page
*
* @param UserIdentity $user
* @param string[] $add Array of groups to add
* @param string[] $remove Array of groups to remove
* @param string $reason Reason for group change
* @param string[] $tags Array of change tags to add to the log entry
* @param array<string,?string> $groupExpiries Associative array of (group name => expiry),
* containing only those groups that are to have new expiry values set
* @return array Tuple of added, then removed groups
*/
public function doSaveUserGroups( $user, array $add, array $remove, string $reason = '',
array $tags = [], array $groupExpiries = []
) {
// Validate input set...
$isself = $user->getName() == $this->getUser()->getName();
if ( $this->userGroupManager !== null ) {
// Used after form submit
$userGroupManager = $this->userGroupManager;
} else {
// Used as backend-function
$userGroupManager = $this->userGroupManagerFactory
->getUserGroupManager( $user->getWikiId() );
}
$groups = $userGroupManager->getUserGroups( $user );
$ugms = $userGroupManager->getUserGroupMemberships( $user );
$changeable = $userGroupManager->getGroupsChangeableBy( $this->getAuthority() );
$addable = array_merge( $changeable['add'], $isself ? $changeable['add-self'] : [] );
$removable = array_merge( $changeable['remove'], $isself ? $changeable['remove-self'] : [] );
$remove = array_unique( array_intersect( $remove, $removable, $groups ) );
$add = array_intersect( $add, $addable );
// add only groups that are not already present or that need their expiry updated,
// UNLESS the user can only add this group (not remove it) and the expiry time
// is being brought forward (T156784)
$add = array_filter( $add,
static function ( $group ) use ( $groups, $groupExpiries, $removable, $ugms ) {
if ( isset( $groupExpiries[$group] ) &&
!in_array( $group, $removable ) &&
isset( $ugms[$group] ) &&
( $ugms[$group]->getExpiry() ?: 'infinity' ) >
( $groupExpiries[$group] ?: 'infinity' )
) {
return false;
}
return !in_array( $group, $groups ) || array_key_exists( $group, $groupExpiries );
} );
if ( $user->getWikiId() === UserIdentity::LOCAL ) {
// For compatibility local changes are provided as User object to the hook
$hookUser = $this->userFactory->newFromUserIdentity( $user );
} else {
// Interwiki changes are provided as UserIdentity since 1.41, was UserRightsProxy before
$hookUser = $user;
}
$this->getHookRunner()->onChangeUserGroups( $this->getUser(), $hookUser, $add, $remove );
$oldGroups = $groups;
$oldUGMs = $userGroupManager->getUserGroupMemberships( $user );
$newGroups = $oldGroups;
// Remove groups, then add new ones/update expiries of existing ones
if ( $remove ) {
foreach ( $remove as $index => $group ) {
if ( !$userGroupManager->removeUserFromGroup( $user, $group ) ) {
unset( $remove[$index] );
}
}
$newGroups = array_diff( $newGroups, $remove );
}
if ( $add ) {
foreach ( $add as $index => $group ) {
$expiry = $groupExpiries[$group] ?? null;
if ( !$userGroupManager->addUserToGroup( $user, $group, $expiry, true ) ) {
unset( $add[$index] );
}
}
$newGroups = array_merge( $newGroups, $add );
}
$newGroups = array_unique( $newGroups );
$newUGMs = $userGroupManager->getUserGroupMemberships( $user );
// Ensure that caches are cleared
$this->userFactory->invalidateCache( $user );
// update groups in external authentication database
$this->getHookRunner()->onUserGroupsChanged( $hookUser, $add, $remove,
$this->getUser(), $reason, $oldUGMs, $newUGMs );
wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) );
wfDebug( 'newGroups: ' . print_r( $newGroups, true ) );
wfDebug( 'oldUGMs: ' . print_r( $oldUGMs, true ) );
wfDebug( 'newUGMs: ' . print_r( $newUGMs, true ) );
// Only add a log entry if something actually changed
if ( $newGroups != $oldGroups || $newUGMs != $oldUGMs ) {
$this->addLogEntry( $user, $oldGroups, $newGroups, $reason, $tags, $oldUGMs, $newUGMs );
}
return [ $add, $remove ];
}
/**
* Serialise a UserGroupMembership object for storage in the log_params section
* of the logging table. Only keeps essential data, removing redundant fields.
*
* @param UserGroupMembership|null $ugm May be null if things get borked
* @return array|null
*/
protected static function serialiseUgmForLog( $ugm ) {
if ( !$ugm instanceof UserGroupMembership ) {
return null;
}
return [ 'expiry' => $ugm->getExpiry() ];
}
/**
* Add a rights log entry for an action.
* @param UserIdentity $user
* @param array $oldGroups
* @param array $newGroups
* @param string $reason
* @param string[] $tags Change tags for the log entry
* @param array $oldUGMs Associative array of (group name => UserGroupMembership)
* @param array $newUGMs Associative array of (group name => UserGroupMembership)
*/
protected function addLogEntry( $user, array $oldGroups, array $newGroups, string $reason,
array $tags, array $oldUGMs, array $newUGMs
) {
// make sure $oldUGMs and $newUGMs are in the same order, and serialise
// each UGM object to a simplified array
$oldUGMs = array_map( static function ( $group ) use ( $oldUGMs ) {
return isset( $oldUGMs[$group] ) ?
self::serialiseUgmForLog( $oldUGMs[$group] ) :
null;
}, $oldGroups );
$newUGMs = array_map( static function ( $group ) use ( $newUGMs ) {
return isset( $newUGMs[$group] ) ?
self::serialiseUgmForLog( $newUGMs[$group] ) :
null;
}, $newGroups );
$logEntry = new ManualLogEntry( 'rights', 'rights' );
$logEntry->setPerformer( $this->getUser() );
$logEntry->setTarget( Title::makeTitle( NS_USER, $this->getDisplayUsername( $user ) ) );
$logEntry->setComment( $reason );
$logEntry->setParameters( [
'4::oldgroups' => $oldGroups,
'5::newgroups' => $newGroups,
'oldmetadata' => $oldUGMs,
'newmetadata' => $newUGMs,
] );
$logid = $logEntry->insert();
if ( count( $tags ) ) {
$logEntry->addTags( $tags );
}
$logEntry->publish( $logid );
}
/**
* Edit user groups membership
* @param string $username Name of the user.
*/
private function editUserGroupsForm( $username ) {
$status = $this->fetchUser( $username, true );
if ( !$status->isOK() ) {
$this->getOutput()->addWikiTextAsInterface(
$status->getWikiText( false, false, $this->getLanguage() )
);
return;
}
/** @var UserIdentity $user */
$user = $status->value;
'@phan-var UserIdentity $user';
$groups = $this->userGroupManager->getUserGroups( $user );
$groupMemberships = $this->userGroupManager->getUserGroupMemberships( $user );
$this->showEditUserGroupsForm( $user, $groups, $groupMemberships );
// This isn't really ideal logging behavior, but let's not hide the
// interwiki logs if we're using them as is.
$this->showLogFragment( $user, $this->getOutput() );
}
/**
* Normalize the input username, which may be local or remote, and
* return a user identity object, use it on other services for manipulating rights
*
* Side effects: error output for invalid access
* @param string $username
* @param bool $writing
* @return Status
*/
public function fetchUser( $username, $writing = true ) {
$parts = explode( $this->getConfig()->get( MainConfigNames::UserrightsInterwikiDelimiter ),
$username );
if ( count( $parts ) < 2 ) {
$name = trim( $username );
$wikiId = UserIdentity::LOCAL;
} else {
[ $name, $wikiId ] = array_map( 'trim', $parts );
if ( WikiMap::isCurrentWikiId( $wikiId ) ) {
$wikiId = UserIdentity::LOCAL;
} else {
if ( $writing &&
!$this->getAuthority()->isAllowed( 'userrights-interwiki' )
) {
return Status::newFatal( 'userrights-no-interwiki' );
}
$localDatabases = $this->getConfig()->get( MainConfigNames::LocalDatabases );
if ( !in_array( $wikiId, $localDatabases ) ) {
return Status::newFatal( 'userrights-nodatabase', $wikiId );
}
}
}
if ( $name === '' ) {
return Status::newFatal( 'nouserspecified' );
}
$userIdentityLookup = $this->actorStoreFactory->getUserIdentityLookup( $wikiId );
if ( $name[0] == '#' ) {
// Numeric ID can be specified...
$id = intval( substr( $name, 1 ) );
$user = $userIdentityLookup->getUserIdentityByUserId( $id );
if ( !$user ) {
// Different error message for compatibility
return Status::newFatal( 'noname' );
}
$name = $user->getName();
} else {
$name = $this->userNameUtils->getCanonical( $name );
if ( $name === false ) {
// invalid name
return Status::newFatal( 'nosuchusershort', $username );
}
$user = $userIdentityLookup->getUserIdentityByName( $name );
}
if ( $this->userNameUtils->isTemp( $name ) ) {
return Status::newFatal( 'userrights-no-group' );
}
if ( !$user || !$user->isRegistered() ) {
return Status::newFatal( 'nosuchusershort', $username );
}
// Prevent cross-wiki assignment of groups to temporary accounts on wikis where the feature is not known.
// We have to check this here, as ApiUserrights uses this to validate before assigning user rights.
if (
$user->getWikiId() !== UserIdentity::LOCAL &&
!$this->tempUserConfig->isKnown() &&
$this->tempUserConfig->isReservedName( $name )
) {
return Status::newFatal( 'userrights-no-group' );
}
if ( $user->getWikiId() === UserIdentity::LOCAL &&
$this->userFactory->newFromUserIdentity( $user )->isHidden() &&
!$this->getAuthority()->isAllowed( 'hideuser' )
) {
// Cannot see hidden users, pretend they don't exist
return Status::newFatal( 'nosuchusershort', $username );
}
return Status::newGood( $user );
}
/**
* @since 1.15
*
* @param array $ids
*
* @return string
*/
public function makeGroupNameList( $ids ) {
if ( !$ids ) {
return $this->msg( 'rightsnone' )->inContentLanguage()->text();
} else {
return implode( ', ', $ids );
}
}
/**
* Display a HTMLUserTextField form to allow searching for a named user only
*/
protected function switchForm() {
$formDescriptor = [
'user' => [
'class' => HTMLUserTextField::class,
'label-message' => 'userrights-user-editname',
'name' => 'user',
'ipallowed' => true,
'iprange' => true,
'excludetemp' => true, // Do not show temp users: T341684
'autofocus' => $this->mFetchedUser === null,
'default' => $this->mTarget,
]
];
$htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() );
$htmlForm
->setMethod( 'GET' )
->setAction( wfScript() )
->setName( 'uluser' )
->setTitle( SpecialPage::getTitleFor( 'Userrights' ) )
->setWrapperLegendMsg( 'userrights-lookup-user' )
->setId( 'mw-userrights-form1' )
->setSubmitTextMsg( 'editusergroup' )
->prepareForm()
->displayForm( true );
}
/**
* Show the form to edit group memberships.
*
* @param UserIdentity $user
* @param string[] $groups Array of groups the user is in. Not used by this implementation
* anymore, but kept for backward compatibility with subclasses
* @param UserGroupMembership[] $groupMemberships Associative array of (group name => UserGroupMembership
* object) containing the groups the user is in
*/
protected function showEditUserGroupsForm( $user, $groups, $groupMemberships ) {
$list = $membersList = $tempList = $tempMembersList = [];
foreach ( $groupMemberships as $ugm ) {
$linkG = UserGroupMembership::getLinkHTML( $ugm, $this->getContext() );
$linkM = UserGroupMembership::getLinkHTML( $ugm, $this->getContext(), $user->getName() );
if ( $ugm->getExpiry() ) {
$tempList[] = $linkG;
$tempMembersList[] = $linkM;
} else {
$list[] = $linkG;
$membersList[] = $linkM;
}
}
$autoList = [];
$autoMembersList = [];
if ( $user->getWikiId() === UserIdentity::LOCAL ) {
// Listing autopromote groups works only on the local wiki
foreach ( $this->userGroupManager->getUserAutopromoteGroups( $user ) as $group ) {
$autoList[] = UserGroupMembership::getLinkHTML( $group, $this->getContext() );
$autoMembersList[] = UserGroupMembership::getLinkHTML( $group, $this->getContext(), $user->getName() );
}
}
$language = $this->getLanguage();
$displayedList = $this->msg( 'userrights-groupsmember-type' )
->rawParams(
$language->commaList( array_merge( $tempList, $list ) ),
$language->commaList( array_merge( $tempMembersList, $membersList ) )
)->escaped();
$displayedAutolist = $this->msg( 'userrights-groupsmember-type' )
->rawParams(
$language->commaList( $autoList ),
$language->commaList( $autoMembersList )
)->escaped();
$grouplist = '';
$count = count( $list ) + count( $tempList );
if ( $count > 0 ) {
$grouplist = $this->msg( 'userrights-groupsmember' )
->numParams( $count )
->params( $user->getName() )
->parse();
$grouplist = '<p>' . $grouplist . ' ' . $displayedList . "</p>\n";
}
$count = count( $autoList );
if ( $count > 0 ) {
$autogrouplistintro = $this->msg( 'userrights-groupsmember-auto' )
->numParams( $count )
->params( $user->getName() )
->parse();
$grouplist .= '<p>' . $autogrouplistintro . ' ' . $displayedAutolist . "</p>\n";
}
$systemUser = $user->getWikiId() === UserIdentity::LOCAL
&& $this->userFactory->newFromUserIdentity( $user )->isSystemUser();
if ( $systemUser ) {
$systemusernote = $this->msg( 'userrights-systemuser' )
->params( $user->getName() )
->parse();
$grouplist .= '<p>' . $systemusernote . "</p>\n";
}
// Only add an email link if the user is not a system user
$flags = $systemUser ? 0 : Linker::TOOL_LINKS_EMAIL;
$userToolLinks = Linker::userToolLinks(
$user->getId( $user->getWikiId() ),
$this->getDisplayUsername( $user ),
false, /* default for redContribsWhenNoEdits */
$flags
);
[ $groupCheckboxes, $canChangeAny ] =
$this->groupCheckboxes( $groupMemberships, $user );
$this->getOutput()->addHTML(
Html::openElement(
'form',
[
'method' => 'post',
'action' => $this->getPageTitle()->getLocalURL(),
'name' => 'editGroup',
'id' => 'mw-userrights-form2'
]
) .
Html::hidden( 'user', $this->mTarget ) .
Html::hidden( 'wpEditToken', $this->getUser()->getEditToken( $this->mTarget ) ) .
Html::hidden(
'conflictcheck-originalgroups',
implode( ',', $this->userGroupManager->getUserGroups( $user ) )
) . // Conflict detection
Html::openElement( 'fieldset' ) .
Html::element(
'legend',
[],
$this->msg(
$canChangeAny ? 'userrights-editusergroup' : 'userrights-viewusergroup',
$user->getName()
)->text()
) .
$this->msg(
$canChangeAny ? 'editinguser' : 'viewinguserrights'
)->params( wfEscapeWikiText( $this->getDisplayUsername( $user ) ) )
->rawParams( $userToolLinks )->parse()
);
if ( $canChangeAny ) {
$this->getOutput()->addHTML(
$this->msg( 'userrights-groups-help', $user->getName() )->parse() .
$grouplist .
$groupCheckboxes .
Html::openElement( 'table', [ 'id' => 'mw-userrights-table-outer' ] ) .
"<tr>
<td class='mw-label'>" .
Html::label( $this->msg( 'userrights-reason' )->text(), 'wpReason' ) .
"</td>
<td class='mw-input'>" .
Xml::input( 'user-reason', 60, $this->getRequest()->getVal( 'user-reason' ) ?? false, [
'id' => 'wpReason',
// HTML maxlength uses "UTF-16 code units", which means that characters outside BMP
// (e.g. emojis) count for two each. This limit is overridden in JS to instead count
// Unicode codepoints.
'maxlength' => CommentStore::COMMENT_CHARACTER_LIMIT,
] ) .
"</td>
</tr>
<tr>
<td></td>
<td class='mw-submit'>" .
Html::submitButton( $this->msg( 'saveusergroups', $user->getName() )->text(),
[ 'name' => 'saveusergroups' ] +
Linker::tooltipAndAccesskeyAttribs( 'userrights-set' )
) .
"</td>
</tr>
<tr>
<td></td>
<td class='mw-input'>" .
Html::check( 'wpWatch', false, [ 'id' => 'wpWatch' ] ) .
' ' . Html::label( $this->msg( 'userrights-watchuser' )->text(), 'wpWatch' ) .
"</td>
</tr>" .
Xml::closeElement( 'table' ) . "\n"
);
} else {
$this->getOutput()->addHTML( $grouplist );
}
$this->getOutput()->addHTML(
Xml::closeElement( 'fieldset' ) .
Xml::closeElement( 'form' ) . "\n"
);
}
/**
* Adds a table with checkboxes where you can select what groups to add/remove
*
* @param UserGroupMembership[] $usergroups Associative array of (group name as string =>
* UserGroupMembership object) for groups the user belongs to
* @param UserIdentity $user
* @return array Array with 2 elements: the XHTML table element with checkxboes, and
* whether any groups are changeable
*/
private function groupCheckboxes( $usergroups, UserIdentity $user ) {
$allgroups = $this->userGroupManager->listAllGroups();
$ret = '';
// Get the list of preset expiry times from the system message
$expiryOptionsMsg = $this->msg( 'userrights-expiry-options' )->inContentLanguage();
$expiryOptions = $expiryOptionsMsg->isDisabled()
? []
: XmlSelect::parseOptionsMessage( $expiryOptionsMsg->text() );
// Put all column info into an associative array so that extensions can
// more easily manage it.
$columns = [ 'unchangeable' => [], 'changeable' => [] ];
foreach ( $allgroups as $group ) {
$set = isset( $usergroups[$group] );
// Users who can add the group, but not remove it, can only lengthen
// expiries, not shorten them. So they should only see the expiry
// dropdown if the group currently has a finite expiry
$canOnlyLengthenExpiry = ( $set && $this->canAdd( $group ) &&
!$this->canRemove( $group ) && $usergroups[$group]->getExpiry() );
// Should the checkbox be disabled?
$disabledCheckbox = !(
( $set && $this->canRemove( $group ) ) ||
( !$set && $this->canAdd( $group ) ) );
// Should the expiry elements be disabled?
$disabledExpiry = $disabledCheckbox && !$canOnlyLengthenExpiry;
// Do we need to point out that this action is irreversible?
$irreversible = !$disabledCheckbox && (
( $set && !$this->canAdd( $group ) ) ||
( !$set && !$this->canRemove( $group ) ) );
$checkbox = [
'set' => $set,
'disabled' => $disabledCheckbox,
'disabled-expiry' => $disabledExpiry,
'irreversible' => $irreversible
];
if ( $disabledCheckbox && $disabledExpiry ) {
$columns['unchangeable'][$group] = $checkbox;
} else {
$columns['changeable'][$group] = $checkbox;
}
}
// Build the HTML table
$ret .= Xml::openElement( 'table', [ 'class' => 'mw-userrights-groups' ] ) .
"<tr>\n";
foreach ( $columns as $name => $column ) {
if ( $column === [] ) {
continue;
}
// Messages: userrights-changeable-col, userrights-unchangeable-col
$ret .= Xml::element(
'th',
null,
$this->msg( 'userrights-' . $name . '-col', count( $column ) )->text()
);
}
$ret .= "</tr>\n<tr>\n";
$uiLanguage = $this->getLanguage();
$userName = $user->getName();
foreach ( $columns as $column ) {
if ( $column === [] ) {
continue;
}
$ret .= "\t<td style='vertical-align:top;'>\n";
foreach ( $column as $group => $checkbox ) {
$member = $uiLanguage->getGroupMemberName( $group, $userName );
if ( $checkbox['irreversible'] ) {
$text = $this->msg( 'userrights-irreversible-marker', $member )->text();
} elseif ( $checkbox['disabled'] && !$checkbox['disabled-expiry'] ) {
$text = $this->msg( 'userrights-no-shorten-expiry-marker', $member )->text();
} else {
$text = $member;
}
$checkboxHtml = Html::element( 'input', [
'type' => 'checkbox', 'name' => "wpGroup-$group", 'value' => '1',
'id' => "wpGroup-$group", 'checked' => $checkbox['set'],
'class' => 'mw-userrights-groupcheckbox',
'disabled' => $checkbox['disabled'],
] ) . ' ' . Html::label( $text, "wpGroup-$group" );
if ( $this->canProcessExpiries() ) {
$uiUser = $this->getUser();
$currentExpiry = isset( $usergroups[$group] ) ?
$usergroups[$group]->getExpiry() :
null;
// If the user can't modify the expiry, print the current expiry below
// it in plain text. Otherwise provide UI to set/change the expiry
if ( $checkbox['set'] &&
( $checkbox['irreversible'] || $checkbox['disabled-expiry'] )
) {
if ( $currentExpiry ) {
$expiryFormatted = $uiLanguage->userTimeAndDate( $currentExpiry, $uiUser );
$expiryFormattedD = $uiLanguage->userDate( $currentExpiry, $uiUser );
$expiryFormattedT = $uiLanguage->userTime( $currentExpiry, $uiUser );
$expiryHtml = Xml::element( 'span', null,
$this->msg( 'userrights-expiry-current' )->params(
$expiryFormatted, $expiryFormattedD, $expiryFormattedT )->text() );
} else {
$expiryHtml = Xml::element( 'span', null,
$this->msg( 'userrights-expiry-none' )->text() );
}
// T171345: Add a hidden form element so that other groups can still be manipulated,
// otherwise saving errors out with an invalid expiry time for this group.
$expiryHtml .= Html::hidden( "wpExpiry-$group",
$currentExpiry ? 'existing' : 'infinite' );
$expiryHtml .= "<br />\n";
} else {
$expiryHtml = Html::element( 'span', [],
$this->msg( 'userrights-expiry' )->text() );
$expiryHtml .= Html::openElement( 'span' );
// add a form element to set the expiry date
$expiryFormOptions = new XmlSelect(
"wpExpiry-$group",
"mw-input-wpExpiry-$group", // forward compatibility with HTMLForm
$currentExpiry ? 'existing' : 'infinite'
);
if ( $checkbox['disabled-expiry'] ) {
$expiryFormOptions->setAttribute( 'disabled', 'disabled' );
}
if ( $currentExpiry ) {
$timestamp = $uiLanguage->userTimeAndDate( $currentExpiry, $uiUser );
$d = $uiLanguage->userDate( $currentExpiry, $uiUser );
$t = $uiLanguage->userTime( $currentExpiry, $uiUser );
$existingExpiryMessage = $this->msg( 'userrights-expiry-existing',
$timestamp, $d, $t );
$expiryFormOptions->addOption( $existingExpiryMessage->text(), 'existing' );
}
$expiryFormOptions->addOption(
$this->msg( 'userrights-expiry-none' )->text(),
'infinite'
);
$expiryFormOptions->addOption(
$this->msg( 'userrights-expiry-othertime' )->text(),
'other'
);
$expiryFormOptions->addOptions( $expiryOptions );
// Add expiry dropdown
$expiryHtml .= $expiryFormOptions->getHTML() . '<br />';
// Add custom expiry field
$expiryHtml .= Html::element( 'input', [
'name' => "wpExpiry-$group-other", 'size' => 30, 'value' => '',
'id' => "mw-input-wpExpiry-$group-other",
'class' => 'mw-userrights-expiryfield',
'disabled' => $checkbox['disabled-expiry'],
] );
// If the user group is set but the checkbox is disabled, mimic a
// checked checkbox in the form submission
if ( $checkbox['set'] && $checkbox['disabled'] ) {
$expiryHtml .= Html::hidden( "wpGroup-$group", 1 );
}
$expiryHtml .= Html::closeElement( 'span' );
}
$divAttribs = [
'id' => "mw-userrights-nested-wpGroup-$group",
'class' => 'mw-userrights-nested',
];
$checkboxHtml .= "\t\t\t" . Html::rawElement( 'div', $divAttribs, $expiryHtml ) . "\n";
}
$ret .= "\t\t" . ( ( $checkbox['disabled'] && $checkbox['disabled-expiry'] )
? Html::rawElement( 'div', [ 'class' => 'mw-userrights-disabled' ], $checkboxHtml )
: Html::rawElement( 'div', [], $checkboxHtml )
) . "\n";
}
$ret .= "\t</td>\n";
}
$ret .= Html::closeElement( 'tr' ) . Html::closeElement( 'table' );
return [ $ret, (bool)$columns['changeable'] ];
}
/**
* @param string $group The name of the group to check
* @return bool Can we remove the group?
*/
private function canRemove( $group ) {
$groups = $this->changeableGroups();
return in_array(
$group,
$groups['remove'] ) || ( $this->isself && in_array( $group, $groups['remove-self'] )
);
}
/**
* @param string $group The name of the group to check
* @return bool Can we add the group?
*/
private function canAdd( $group ) {
$groups = $this->changeableGroups();
return in_array(
$group,
$groups['add'] ) || ( $this->isself && in_array( $group, $groups['add-self'] )
);
}
/**
* @return array [
* 'add' => [ addablegroups ],
* 'remove' => [ removablegroups ],
* 'add-self' => [ addablegroups to self ],
* 'remove-self' => [ removable groups from self ]
* ]
* @phan-return array{add:list<string>,remove:list<string>,add-self:list<string>,remove-self:list<string>}
*/
protected function changeableGroups() {
return $this->userGroupManager->getGroupsChangeableBy( $this->getContext()->getAuthority() );
}
/**
* Get a display user name. This includes the {@}domain part for interwiki users.
* Use UserIdentity::getName for {{GENDER:}} in messages and
* use the "display user name" for visible user names in logs or messages
*
* @param UserIdentity $user
* @return string
*/
private function getDisplayUsername( UserIdentity $user ) {
$userName = $user->getName();
if ( $user->getWikiId() !== UserIdentity::LOCAL ) {
$userName .= $this->getConfig()->get( MainConfigNames::UserrightsInterwikiDelimiter )
. $user->getWikiId();
}
return $userName;
}
/**
* Show a rights log fragment for the specified user
*
* @param UserIdentity $user User to show log for
* @param OutputPage $output OutputPage to use
*/
protected function showLogFragment( $user, $output ) {
$rightsLogPage = new LogPage( 'rights' );
$output->addHTML( Html::element( 'h2', [], $rightsLogPage->getName()->text() ) );
LogEventsList::showLogExtract( $output, 'rights',
Title::makeTitle( NS_USER, $this->getDisplayUsername( $user ) ) );
}
/**
* Return an array of subpages beginning with $search that this special page will accept.
*
* @param string $search Prefix to search for
* @param int $limit Maximum number of results to return (usually 10)
* @param int $offset Number of results to skip (usually 0)
* @return string[] Matching subpages
*/
public function prefixSearchSubpages( $search, $limit, $offset ) {
$search = $this->userNameUtils->getCanonical( $search );
if ( !$search ) {
// No prefix suggestion for invalid user
return [];
}
// Autocomplete subpage as user list - public to allow caching
return $this->userNamePrefixSearch
->search( UserNamePrefixSearch::AUDIENCE_PUBLIC, $search, $limit, $offset );
}
protected function getGroupName() {
return 'users';
}
}
/**
* Retain the old class name for backwards compatibility.
* @deprecated since 1.40
*/
class_alias( SpecialUserRights::class, 'UserrightsPage' );
|