Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
88.89% |
16 / 18 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
AbuseFilterPreAuthenticationProvider | |
88.89% |
16 / 18 |
|
50.00% |
2 / 4 |
6.05 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
testForAccountCreation | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
testUserForCreation | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
testUser | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
2.00 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\AbuseFilter; |
4 | |
5 | use IBufferingStatsdDataFactory; |
6 | use MediaWiki\Auth\AbstractPreAuthenticationProvider; |
7 | use MediaWiki\Auth\AuthenticationRequest; |
8 | use MediaWiki\Extension\AbuseFilter\VariableGenerator\VariableGeneratorFactory; |
9 | use MediaWiki\User\UserFactory; |
10 | use SpecialPage; |
11 | use StatusValue; |
12 | use User; |
13 | |
14 | /** |
15 | * AuthenticationProvider used to filter account creations. This runs after normal preauth providers |
16 | * to keep the log cleaner. |
17 | */ |
18 | class AbuseFilterPreAuthenticationProvider extends AbstractPreAuthenticationProvider { |
19 | /** @var VariableGeneratorFactory */ |
20 | private $variableGeneratorFactory; |
21 | /** @var FilterRunnerFactory */ |
22 | private $filterRunnerFactory; |
23 | /** @var IBufferingStatsdDataFactory */ |
24 | private $statsd; |
25 | /** @var UserFactory */ |
26 | private $userFactory; |
27 | |
28 | /** |
29 | * @param VariableGeneratorFactory $variableGeneratorFactory |
30 | * @param FilterRunnerFactory $filterRunnerFactory |
31 | * @param IBufferingStatsdDataFactory $statsd |
32 | * @param UserFactory $userFactory |
33 | */ |
34 | public function __construct( |
35 | VariableGeneratorFactory $variableGeneratorFactory, |
36 | FilterRunnerFactory $filterRunnerFactory, |
37 | IBufferingStatsdDataFactory $statsd, |
38 | UserFactory $userFactory |
39 | ) { |
40 | $this->variableGeneratorFactory = $variableGeneratorFactory; |
41 | $this->filterRunnerFactory = $filterRunnerFactory; |
42 | $this->statsd = $statsd; |
43 | $this->userFactory = $userFactory; |
44 | } |
45 | |
46 | /** |
47 | * @param User $user |
48 | * @param User $creator |
49 | * @param AuthenticationRequest[] $reqs |
50 | * @return StatusValue |
51 | */ |
52 | public function testForAccountCreation( $user, $creator, array $reqs ): StatusValue { |
53 | return $this->testUser( $user, $creator, false ); |
54 | } |
55 | |
56 | /** |
57 | * @param User $user |
58 | * @param bool|string $autocreate |
59 | * @param array $options |
60 | * @return StatusValue |
61 | */ |
62 | public function testUserForCreation( $user, $autocreate, array $options = [] ): StatusValue { |
63 | // if this is not an autocreation, testForAccountCreation already handled it |
64 | if ( $autocreate ) { |
65 | // Make sure to use an anon as the creator, see T272244 |
66 | return $this->testUser( $user, $this->userFactory->newAnonymous(), true ); |
67 | } |
68 | return StatusValue::newGood(); |
69 | } |
70 | |
71 | /** |
72 | * @param User $user The user being created or autocreated |
73 | * @param User $creator The user who caused $user to be created (can be anonymous) |
74 | * @param bool $autocreate Is this an autocreation? |
75 | * @return StatusValue |
76 | */ |
77 | private function testUser( $user, $creator, $autocreate ): StatusValue { |
78 | $startTime = microtime( true ); |
79 | if ( $user->getName() === wfMessage( 'abusefilter-blocker' )->inContentLanguage()->text() ) { |
80 | return StatusValue::newFatal( 'abusefilter-accountreserved' ); |
81 | } |
82 | |
83 | $title = SpecialPage::getTitleFor( 'Userlogin' ); |
84 | $builder = $this->variableGeneratorFactory->newRunGenerator( $creator, $title ); |
85 | $vars = $builder->getAccountCreationVars( $user, $autocreate ); |
86 | |
87 | // pass creator in explicitly to prevent recording the current user on autocreation - T135360 |
88 | $runner = $this->filterRunnerFactory->newRunner( $creator, $title, $vars, 'default' ); |
89 | $status = $runner->run(); |
90 | |
91 | $this->statsd->timing( 'timing.createaccountAbuseFilter', microtime( true ) - $startTime ); |
92 | |
93 | return $status->getStatusValue(); |
94 | } |
95 | } |