Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.89% covered (warning)
88.89%
16 / 18
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbuseFilterPreAuthenticationProvider
88.89% covered (warning)
88.89%
16 / 18
50.00% covered (danger)
50.00%
2 / 4
6.05
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 testForAccountCreation
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 testUserForCreation
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 testUser
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
2.00
1<?php
2
3namespace MediaWiki\Extension\AbuseFilter;
4
5use IBufferingStatsdDataFactory;
6use MediaWiki\Auth\AbstractPreAuthenticationProvider;
7use MediaWiki\Auth\AuthenticationRequest;
8use MediaWiki\Extension\AbuseFilter\VariableGenerator\VariableGeneratorFactory;
9use MediaWiki\SpecialPage\SpecialPage;
10use MediaWiki\User\User;
11use MediaWiki\User\UserFactory;
12use StatusValue;
13
14/**
15 * AuthenticationProvider used to filter account creations. This runs after normal preauth providers
16 * to keep the log cleaner.
17 */
18class 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}