Use new services in AbuseFilterRunner

The first one is UserGroupManager, used for the 'degroup' action. This
is a simple one-line replacement (repeated twice), and the current code
was already using this service under the hood.

The second one is BlockUser, which is not a one-line change (but still
quite simple). In particular, this allows us to avoid duplication with
core logic when constructing the log entry (this is now done by
BlockUser).

Bug: T248743
Change-Id: Ib7c1dc107a169b575f7021e64b6a8fee09529548
This commit is contained in:
Daimona Eaytoy 2020-10-11 23:35:13 +02:00
parent a7182acafd
commit 1efc324d97
3 changed files with 18 additions and 42 deletions

View file

@ -12,7 +12,7 @@
"license-name": "GPL-2.0-or-later",
"type": "antispam",
"requires": {
"MediaWiki": ">= 1.35.0"
"MediaWiki": ">= 1.36.0"
},
"AvailableRights": [
"abusefilter-modify",

View file

@ -164,7 +164,7 @@ class AFComputedVariable {
* @throws AFPException
*/
public function compute( AbuseFilterVariableHolder $vars ) {
// TODO: find a way to inject the User object from hook parameters.
// phpcs:ignore MediaWiki.Usage.DeprecatedGlobalVariables.Deprecated$wgUser
global $wgUser;
// Used for parsing wikitext from saved revisions and checking for

View file

@ -1,6 +1,5 @@
<?php
use MediaWiki\Block\DatabaseBlock;
use MediaWiki\Extension\AbuseFilter\AbuseFilterServices;
use MediaWiki\Extension\AbuseFilter\FilterProfiler;
use MediaWiki\Extension\AbuseFilter\Hooks\AbuseFilterHookRunner;
@ -773,13 +772,14 @@ class AbuseFilterRunner {
break;
case 'degroup':
if ( !$this->user->isAnon() ) {
$userGroupsManager = MediaWikiServices::getInstance()->getUserGroupManager();
// Pull the groups from the VariableHolder, so that they will always be computed.
// This allow us to pull the groups from the VariableHolder to undo the degroup
// via Special:AbuseFilter/revert.
$groups = $this->vars->getVar( 'user_groups', AbuseFilterVariableHolder::GET_LAX );
if ( $groups->type !== AFPData::DARRAY ) {
// Somehow, the variable wasn't set
$groups = $this->user->getEffectiveGroups();
$groups = $userGroupsManager->getUserEffectiveGroups( $this->user );
$this->vars->setVar( 'user_groups', $groups );
} else {
$groups = $groups->toNative();
@ -787,7 +787,7 @@ class AbuseFilterRunner {
$this->vars->setVar( 'user_groups', $groups );
foreach ( $groups as $group ) {
$this->user->removeGroup( $group );
$userGroupsManager->removeUserFromGroup( $this->user, $group );
}
$message = [
@ -931,49 +931,25 @@ class AbuseFilterRunner {
$isAutoBlock,
$preventEditOwnUserTalk
) {
$blockUserFactory = MediaWikiServices::getInstance()->getBlockUserFactory();
$filterUser = AbuseFilter::getFilterUser();
$reason = wfMessage(
'abusefilter-blockreason',
$ruleDesc, $ruleNumber
)->inContentLanguage()->text();
$block = new DatabaseBlock();
$block->setTarget( $target );
$block->setBlocker( $filterUser );
$block->setReason( $reason );
$block->isHardblock( false );
$block->isAutoblocking( $isAutoBlock );
$block->isCreateAccountBlocked( true );
$block->isUsertalkEditAllowed( !$preventEditOwnUserTalk );
$block->setExpiry( SpecialBlock::parseExpiryInput( $expiry ) );
$success = $block->insert();
if ( $success ) {
// Log it only if the block was successful
$logParams = [];
$logParams['5::duration'] = ( $block->getExpiry() === 'infinity' )
? 'indefinite'
: $expiry;
$flags = [ 'nocreate' ];
if ( !$block->isAutoblocking() && !IPUtils::isIPAddress( $target ) ) {
// Conditionally added same as SpecialBlock
$flags[] = 'noautoblock';
}
if ( $preventEditOwnUserTalk === true ) {
$flags[] = 'nousertalk';
}
$logParams['6::flags'] = implode( ',', $flags );
$logEntry = new ManualLogEntry( 'block', 'block' );
$logEntry->setTarget( Title::makeTitle( NS_USER, $target ) );
$logEntry->setComment( $reason );
$logEntry->setPerformer( $filterUser );
$logEntry->setParameters( $logParams );
$blockIds = array_merge( [ $success['id'] ], $success['autoIds'] );
$logEntry->setRelations( [ 'ipb_id' => $blockIds ] );
$logEntry->publish( $logEntry->insert() );
}
$blockUserFactory->newBlockUser(
$target,
$filterUser,
$expiry,
$reason,
[
'isHardBlock' => false,
'isAutoblocking' => $isAutoBlock,
'isCreateAccountBlocked' => true,
'isUserTalkEditBlocked' => $preventEditOwnUserTalk
]
)->placeBlockUnsafe();
}
/**