Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbuseFilterHookHandler
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 5
306
0.00% covered (danger)
0.00%
0 / 1
 onAbuseFilterAlterVariables
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
12
 onAbuseFilter_computeVariable
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
90
 onAbuseFilter_generateUserVars
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 onAbuseFilter_builder
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 onAbuseFilterShouldFilterAction
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\Extension\CentralAuth\Hooks\Handlers;
22
23use MediaWiki\Extension\AbuseFilter\Hooks\AbuseFilterAlterVariablesHook;
24use MediaWiki\Extension\AbuseFilter\Hooks\AbuseFilterBuilderHook;
25use MediaWiki\Extension\AbuseFilter\Hooks\AbuseFilterComputeVariableHook;
26use MediaWiki\Extension\AbuseFilter\Hooks\AbuseFilterGenerateUserVarsHook;
27use MediaWiki\Extension\AbuseFilter\Hooks\AbuseFilterShouldFilterActionHook;
28use MediaWiki\Extension\AbuseFilter\Variables\VariableHolder;
29use MediaWiki\Extension\CentralAuth\GlobalRename\LocalRenameJob\LocalPageMoveJob;
30use MediaWiki\Extension\CentralAuth\User\CentralAuthUser;
31use MediaWiki\Title\Title;
32use MediaWiki\User\User;
33use RecentChange;
34
35class AbuseFilterHookHandler implements
36    AbuseFilterAlterVariablesHook,
37    AbuseFilterBuilderHook,
38    AbuseFilterComputeVariableHook,
39    AbuseFilterGenerateUserVarsHook,
40    AbuseFilterShouldFilterActionHook
41{
42
43    /**
44     * Load our global_account_groups and global_account_editcount variables
45     * during (auto)createaccount actions.
46     *
47     * @param VariableHolder &$vars
48     * @param Title $title Title object target of the action
49     * @param User $user User object performer of the action
50     */
51    public function onAbuseFilterAlterVariables(
52        VariableHolder &$vars,
53        Title $title,
54        User $user
55    ) {
56        $action = $vars->getComputedVariable( 'action' )->toString();
57        if ( $action === 'createaccount' ) {
58            $vars->setVar( 'global_account_groups', [] );
59            $vars->setVar( 'global_account_editcount', 0 );
60        } elseif ( $action === 'autocreateaccount' ) {
61            $accountname = $vars->getComputedVariable( 'accountname' )->toString();
62            $vars->setLazyLoadVar(
63                'global_account_groups',
64                'global-user-groups',
65                [ 'user' => $accountname, 'new' => true ]
66            );
67            $vars->setLazyLoadVar(
68                'global_account_editcount',
69                'global-user-editcount',
70                [ 'user' => $accountname, 'new' => true ]
71            );
72        }
73    }
74
75    /**
76     * Computes the global_user_groups and global_user_editcount variables
77     * @param string $method
78     * @param VariableHolder $vars
79     * @param array $parameters
80     * @param ?string &$result
81     * @return bool
82     */
83    public function onAbuseFilter_computeVariable(
84        string $method,
85        VariableHolder $vars,
86        array $parameters,
87        ?string &$result
88    ) {
89        if ( $method == 'global-user-groups' ) {
90            $user = CentralAuthUser::getInstanceByName( $parameters['user'] );
91            if ( !$user->exists() ) {
92                $result = [];
93            } elseif ( !( $parameters['new'] ?? false ) && !$user->isAttached() ) {
94                $result = [];
95            } else {
96                $result = $user->getGlobalGroups();
97            }
98            return false;
99        } elseif ( $method == 'global-user-editcount' ) {
100            $user = CentralAuthUser::getInstanceByName( $parameters['user'] );
101            if ( !$user->exists() ) {
102                $result = 0;
103            } elseif ( !( $parameters['new'] ?? false ) && !$user->isAttached() ) {
104                $result = 0;
105            } else {
106                $result = $user->getGlobalEditCount();
107            }
108            return false;
109        } else {
110            return true;
111        }
112    }
113
114    /**
115     * Load our global_user_groups and global_user_editcount variables
116     * @param VariableHolder $vars
117     * @param User $user
118     * @param ?RecentChange $rc
119     * @return bool
120     */
121    public function onAbuseFilter_generateUserVars(
122        VariableHolder $vars,
123        User $user,
124        ?RecentChange $rc
125    ) {
126        $vars->setLazyLoadVar( 'global_user_groups', 'global-user-groups', [ 'user' => $user->getName() ] );
127        $vars->setLazyLoadVar( 'global_user_editcount', 'global-user-editcount', [ 'user' => $user->getName() ] );
128        return true;
129    }
130
131    /**
132     * Tell AbuseFilter about our global_user_groups and global_user_editcount variables
133     * @param array &$realValues
134     * @return bool
135     */
136    public function onAbuseFilter_builder( array &$realValues ) {
137        // Uses: 'abusefilter-edit-builder-vars-global-user-groups'
138        $realValues['vars']['global_user_groups'] = 'global-user-groups';
139        // Uses: 'abusefilter-edit-builder-vars-global-user-editcount'
140        $realValues['vars']['global_user_editcount'] = 'global-user-editcount';
141        // Uses: 'abusefilter-edit-builder-vars-global-account-groups'
142        $realValues['vars']['global_account_groups'] = 'global-account-groups';
143        // Uses: 'abusefilter-edit-builder-vars-global-account-editcount'
144        $realValues['vars']['global_account_editcount'] = 'global-account-editcount';
145        return true;
146    }
147
148    /**
149     * Avoid filtering page moves during global rename
150     *
151     * @param VariableHolder $vars
152     * @param Title $title
153     * @param User $user
154     * @param array &$skipReasons
155     * @return bool
156     */
157    public function onAbuseFilterShouldFilterAction(
158        VariableHolder $vars,
159        Title $title,
160        User $user,
161        array &$skipReasons
162    ) {
163        $action = $vars->getComputedVariable( 'action' )->toString();
164        if ( $action === 'move' && LocalPageMoveJob::$moveInProgress === true ) {
165            $skipReasons[] = "CentralAuth: $user is moving $title for global rename";
166            // Don't allow reusing this flag
167            LocalPageMoveJob::$moveInProgress = false;
168            return false;
169        }
170        return true;
171    }
172}