Merge "Use UserGroupManager when reverting degroup action"

This commit is contained in:
jenkins-bot 2020-10-25 09:24:15 +00:00 committed by Gerrit Code Review
commit 8fe9902af3
2 changed files with 16 additions and 21 deletions

View file

@ -809,6 +809,7 @@ class AbuseFilterRunner {
break;
}
// TODO Core should provide a logging method
$logEntry = new ManualLogEntry( 'rights', 'rights' );
$logEntry->setPerformer( AbuseFilter::getFilterUser() );
$logEntry->setTarget( $this->user->getUserPage() );

View file

@ -2,6 +2,7 @@
use MediaWiki\Block\DatabaseBlock;
use MediaWiki\Extension\AbuseFilter\AbuseFilterServices;
use MediaWiki\MediaWikiServices;
class AbuseFilterViewRevert extends AbuseFilterView {
/** @var int */
@ -315,32 +316,25 @@ class AbuseFilterViewRevert extends AbuseFilterView {
return AbuseFilterServices::getBlockAutopromoteStore()
->unblockAutopromote( $target, $this->getUser(), $msg );
case 'degroup':
$userGroupsManager = MediaWikiServices::getInstance()->getUserGroupManager();
// Pull the user's groups from the vars.
$oldGroups = $result['vars']->getVar( 'user_groups' )->toNative();
$oldGroups = array_diff( $oldGroups, User::getImplicitGroups() );
$removedGroups = $result['vars']->getVar( 'user_groups' )->toNative();
$removedGroups = array_diff( $removedGroups, $userGroupsManager->listAllImplicitGroups() );
$user = User::newFromId( $result['userid'] );
$currentGroups = $userGroupsManager->getUserGroups( $user );
$rows = [];
foreach ( $oldGroups as $group ) {
$rows[] = [
'ug_user' => $result['userid'],
'ug_group' => $group
];
$done = false;
foreach ( $removedGroups as $group ) {
// TODO An addUserToGroups method with bulk updates would be nice
$done = $userGroupsManager->addUserToGroup( $user, $group ) || $done;
}
// Cheat a little bit. User::addGroup repeatedly is too slow.
$user = User::newFromId( $result['userid'] );
$currentGroups = $user->getGroups();
$newGroups = array_merge( $oldGroups, $currentGroups );
// Don't do anything if there are no groups to add.
if ( !count( array_diff( $newGroups, $currentGroups ) ) ) {
// Don't log if no groups were added.
if ( !$done ) {
return false;
}
$dbw = wfGetDB( DB_MASTER );
$dbw->insert( 'user_groups', $rows, __METHOD__, [ 'IGNORE' ] );
$user->invalidateCache();
// TODO Core should provide a logging method
$logEntry = new ManualLogEntry( 'rights', 'rights' );
$logEntry->setTarget( $user->getUserPage() );
$logEntry->setPerformer( $this->getUser() );
@ -353,13 +347,13 @@ class AbuseFilterViewRevert extends AbuseFilterView {
);
$logEntry->setParameters( [
'4::oldgroups' => $currentGroups,
'5::newgroups' => $newGroups
'5::newgroups' => $userGroupsManager->getUserGroups( $user )
] );
$logEntry->publish( $logEntry->insert() );
return true;
}
throw new MWException( 'Invalid action' . $action );
throw new MWException( "Invalid action $action" );
}
}