Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
GlobalRenameHookHandler
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 4
210
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 onRenameUserWarning
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
56
 onRenameUserPreRename
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 onRenameUserComplete
0.00% covered (danger)
0.00%
0 / 8
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 ErrorPageError;
24use MediaWiki\Extension\CentralAuth\User\CentralAuthAntiSpoofManager;
25use MediaWiki\Extension\CentralAuth\User\CentralAuthUser;
26use MediaWiki\RenameUser\Hook\RenameUserCompleteHook;
27use MediaWiki\RenameUser\Hook\RenameUserPreRenameHook;
28use MediaWiki\RenameUser\Hook\RenameUserWarningHook;
29use MediaWiki\WikiMap\WikiMap;
30
31class GlobalRenameHookHandler implements
32    RenameUserCompleteHook,
33    RenameUserPreRenameHook,
34    RenameUserWarningHook
35{
36    private CentralAuthAntiSpoofManager $caAntiSpoofManager;
37
38    /**
39     * @param CentralAuthAntiSpoofManager $caAntiSpoofManager
40     */
41    public function __construct( CentralAuthAntiSpoofManager $caAntiSpoofManager ) {
42        $this->caAntiSpoofManager = $caAntiSpoofManager;
43    }
44
45    /**
46     * Warn bureaucrat about possible conflicts with unified accounts
47     * @param string $oldUsername
48     * @param string $newUsername
49     * @param array[] &$warnings
50     * @throws ErrorPageError
51     */
52    public function onRenameUserWarning( string $oldUsername, string $newUsername, array &$warnings ): void {
53        $oldCentral = CentralAuthUser::getPrimaryInstanceByName( $oldUsername );
54        if ( $oldCentral->exists() && $oldCentral->isAttached() ) {
55            $warnings[] = [ 'centralauth-renameuser-merged', $oldUsername, $newUsername ];
56        }
57        if ( $oldCentral->renameInProgress() ) {
58            $warnings[] = [ 'centralauth-renameuser-global-inprogress', $oldUsername ];
59        }
60
61        $newCentral = CentralAuthUser::getPrimaryInstanceByName( $newUsername );
62        if ( $newCentral->exists() && !$newCentral->isAttached() ) {
63            $warnings[] = [ 'centralauth-renameuser-reserved', $oldUsername, $newUsername ];
64        }
65
66        if ( $newCentral->renameInProgress() ) {
67            $warnings[] = [ 'centralauth-renameuser-global-inprogress', $newUsername ];
68            // Can potentially be renaming two accounts into the same name, so throw an error
69            throw new ErrorPageError(
70                'error', 'centralauth-renameuser-global-inprogress', [ $newUsername ]
71            );
72        }
73    }
74
75    /**
76     * @param int $uid
77     * @param string $old
78     * @param string $new
79     */
80    public function onRenameUserPreRename( int $uid, string $old, string $new ): void {
81        $oldCentral = CentralAuthUser::getPrimaryInstanceByName( $old );
82        // If we're doing a global rename, the account will not get unattached
83        // because the old account no longer exists
84        if ( $oldCentral->exists() && $oldCentral->isAttached() ) {
85            $oldCentral->adminUnattach( [ WikiMap::getCurrentWikiId() ] );
86        }
87    }
88
89    /**
90     * When renaming an account, update presence records and AntiSpoof system.
91     *
92     * @param int $uid
93     * @param string $old
94     * @param string $new
95     */
96    public function onRenameUserComplete( int $uid, string $old, string $new ): void {
97        $oldCentral = CentralAuthUser::getPrimaryInstanceByName( $old );
98        $newCentral = CentralAuthUser::getPrimaryInstanceByName( $new );
99
100        if ( $newCentral->exists() && $oldCentral->renameInProgressOn( WikiMap::getCurrentWikiId() ) ) {
101            // This is a global rename, just update the row.
102            $oldCentral->updateLocalName( WikiMap::getCurrentWikiId(), $new );
103        } else {
104            $oldCentral->removeLocalName( WikiMap::getCurrentWikiId() );
105            $newCentral->addLocalName( WikiMap::getCurrentWikiId() );
106        }
107
108        // Remove the old entry and add the new
109        $spoof = $this->caAntiSpoofManager->getSpoofUser( $new );
110        $spoof->update( $old );
111    }
112}