2020-10-25 15:30:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\AbuseFilter;
|
|
|
|
|
2022-03-04 18:04:08 +00:00
|
|
|
use MediaWiki\Permissions\Authority;
|
2023-12-10 19:03:19 +00:00
|
|
|
use MediaWiki\User\User;
|
2020-10-25 15:30:43 +00:00
|
|
|
use MediaWiki\User\UserGroupManager;
|
|
|
|
use MediaWiki\User\UserIdentity;
|
2024-01-31 17:50:53 +00:00
|
|
|
use MediaWiki\User\UserNameUtils;
|
2020-10-25 15:30:43 +00:00
|
|
|
use MessageLocalizer;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
|
|
|
class FilterUser {
|
|
|
|
public const SERVICE_NAME = 'AbuseFilterFilterUser';
|
|
|
|
|
2024-01-31 17:50:53 +00:00
|
|
|
private MessageLocalizer $messageLocalizer;
|
|
|
|
private UserGroupManager $userGroupManager;
|
|
|
|
private UserNameUtils $userNameUtils;
|
|
|
|
private LoggerInterface $logger;
|
2020-10-25 15:30:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param MessageLocalizer $messageLocalizer
|
|
|
|
* @param UserGroupManager $userGroupManager
|
2024-01-31 17:50:53 +00:00
|
|
|
* @param UserNameUtils $userNameUtils
|
2020-10-25 15:30:43 +00:00
|
|
|
* @param LoggerInterface $logger
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
MessageLocalizer $messageLocalizer,
|
|
|
|
UserGroupManager $userGroupManager,
|
2024-01-31 17:50:53 +00:00
|
|
|
UserNameUtils $userNameUtils,
|
2020-10-25 15:30:43 +00:00
|
|
|
LoggerInterface $logger
|
|
|
|
) {
|
|
|
|
$this->messageLocalizer = $messageLocalizer;
|
|
|
|
$this->userGroupManager = $userGroupManager;
|
2024-01-31 17:50:53 +00:00
|
|
|
$this->userNameUtils = $userNameUtils;
|
2020-10-25 15:30:43 +00:00
|
|
|
$this->logger = $logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-03-04 18:04:08 +00:00
|
|
|
* @return Authority
|
|
|
|
*/
|
|
|
|
public function getAuthority(): Authority {
|
|
|
|
return $this->getUser();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-25 15:30:43 +00:00
|
|
|
* @return UserIdentity
|
|
|
|
*/
|
2022-03-04 18:04:08 +00:00
|
|
|
public function getUserIdentity(): UserIdentity {
|
|
|
|
return $this->getUser();
|
|
|
|
}
|
|
|
|
|
2024-01-31 17:50:53 +00:00
|
|
|
/**
|
|
|
|
* Compares the given $user to see if they are the same as the FilterUser.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isSameUserAs( UserIdentity $user ): bool {
|
|
|
|
// Checking the usernames are equal is enough, as this is what is done by
|
|
|
|
// User::equals and UserIdentityValue::equals.
|
|
|
|
return $user->getName() === $this->getFilterUserName();
|
|
|
|
}
|
|
|
|
|
2022-03-04 18:04:08 +00:00
|
|
|
/**
|
|
|
|
* @todo Stop using the User class when User::newSystemUser is refactored.
|
|
|
|
* @return User
|
|
|
|
*/
|
|
|
|
private function getUser(): User {
|
2024-01-31 17:50:53 +00:00
|
|
|
$user = User::newSystemUser( $this->getFilterUserName(), [ 'steal' => true ] );
|
|
|
|
'@phan-var User $user';
|
|
|
|
|
|
|
|
// Promote user to 'sysop' so it doesn't look
|
|
|
|
// like an unprivileged account is blocking users
|
|
|
|
if ( !in_array( 'sysop', $this->userGroupManager->getUserGroups( $user ) ) ) {
|
|
|
|
$this->userGroupManager->addUserToGroup( $user, 'sysop' );
|
|
|
|
}
|
2020-10-25 15:30:43 +00:00
|
|
|
|
2024-01-31 17:50:53 +00:00
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the username for the FilterUser.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getFilterUserName(): string {
|
|
|
|
$username = $this->messageLocalizer->msg( 'abusefilter-blocker' )->inContentLanguage()->text();
|
|
|
|
if ( !$this->userNameUtils->getCanonical( $username ) ) {
|
2020-10-25 15:30:43 +00:00
|
|
|
// User name is invalid. Don't throw because this is a system message, easy
|
|
|
|
// to change and make wrong either by mistake or intentionally to break the site.
|
|
|
|
$this->logger->warning(
|
|
|
|
'The AbuseFilter user\'s name is invalid. Please change it in ' .
|
|
|
|
'MediaWiki:abusefilter-blocker'
|
|
|
|
);
|
|
|
|
// Use the default name to avoid breaking other stuff. This should have no harm,
|
|
|
|
// aside from blocks temporarily attributed to another user.
|
2021-06-06 11:58:31 +00:00
|
|
|
// Don't use the database in case the English onwiki message is broken, T284364
|
2024-01-31 17:50:53 +00:00
|
|
|
$username = $this->messageLocalizer->msg( 'abusefilter-blocker' )
|
2021-06-06 11:58:31 +00:00
|
|
|
->inLanguage( 'en' )
|
|
|
|
->useDatabase( false )
|
|
|
|
->text();
|
2020-10-25 15:30:43 +00:00
|
|
|
}
|
2024-01-31 17:50:53 +00:00
|
|
|
return $username;
|
2020-10-25 15:30:43 +00:00
|
|
|
}
|
|
|
|
}
|