Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
CheckUserHandler
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
4 / 4
10
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 onCheckUserInsertChangesRow
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 onCheckUserInsertLogEventRow
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 onCheckUserInsertPrivateEventRow
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace MediaWiki\Extension\AbuseFilter\Hooks\Handlers;
4
5use MediaWiki\CheckUser\Hook\CheckUserInsertChangesRowHook;
6use MediaWiki\CheckUser\Hook\CheckUserInsertLogEventRowHook;
7use MediaWiki\CheckUser\Hook\CheckUserInsertPrivateEventRowHook;
8use MediaWiki\Extension\AbuseFilter\FilterUser;
9use MediaWiki\User\UserIdentity;
10use MediaWiki\User\UserIdentityUtils;
11use RecentChange;
12
13class CheckUserHandler implements
14    CheckUserInsertChangesRowHook,
15    CheckUserInsertPrivateEventRowHook,
16    CheckUserInsertLogEventRowHook
17{
18
19    /** @var FilterUser */
20    private $filterUser;
21
22    /** @var UserIdentityUtils */
23    private $userIdentityUtils;
24
25    /**
26     * @param FilterUser $filterUser
27     * @param UserIdentityUtils $userIdentityUtils
28     */
29    public function __construct(
30        FilterUser $filterUser,
31        UserIdentityUtils $userIdentityUtils
32    ) {
33        $this->filterUser = $filterUser;
34        $this->userIdentityUtils = $userIdentityUtils;
35    }
36
37    /**
38     * Any edits by the filter user should always be marked as by the software
39     * using IP 127.0.0.1, no XFF and no UA.
40     *
41     * @inheritDoc
42     */
43    public function onCheckUserInsertChangesRow(
44        string &$ip, &$xff, array &$row, UserIdentity $user, ?RecentChange $rc
45    ) {
46        if (
47            $this->userIdentityUtils->isNamed( $user ) &&
48            $this->filterUser->isSameUserAs( $user )
49        ) {
50            $ip = '127.0.0.1';
51            $xff = false;
52            $row['cuc_agent'] = '';
53        }
54    }
55
56    /**
57     * Any log actions by the filter user should always be marked as by the software
58     * using IP 127.0.0.1, no XFF and no UA.
59     *
60     * @inheritDoc
61     */
62    public function onCheckUserInsertLogEventRow(
63        string &$ip, &$xff, array &$row, UserIdentity $user, int $id, ?RecentChange $rc
64    ) {
65        if (
66            $this->userIdentityUtils->isNamed( $user ) &&
67            $this->filterUser->isSameUserAs( $user )
68        ) {
69            $ip = '127.0.0.1';
70            $xff = false;
71            $row['cule_agent'] = '';
72        }
73    }
74
75    /**
76     * Any log actions by the filter user should always be marked as by the software
77     * using IP 127.0.0.1, no XFF and no UA.
78     *
79     * @inheritDoc
80     */
81    public function onCheckUserInsertPrivateEventRow(
82        string &$ip, &$xff, array &$row, UserIdentity $user, ?RecentChange $rc
83    ) {
84        if (
85            $this->userIdentityUtils->isNamed( $user ) &&
86            $this->filterUser->isSameUserAs( $user )
87        ) {
88            $ip = '127.0.0.1';
89            $xff = false;
90            $row['cupe_agent'] = '';
91        }
92    }
93}