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