Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
GlobalRenameFactory
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 3
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 newGlobalRenameUser
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
20
 newGlobalRenameUserStatus
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
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\GlobalRename;
22
23use InvalidArgumentException;
24use MediaWiki\Extension\CentralAuth\CentralAuthDatabaseManager;
25use MediaWiki\Extension\CentralAuth\User\CentralAuthAntiSpoofManager;
26use MediaWiki\Extension\CentralAuth\User\CentralAuthUser;
27use MediaWiki\JobQueue\JobQueueGroupFactory;
28use MediaWiki\User\UserFactory;
29use MediaWiki\User\UserIdentity;
30use MediaWiki\User\UserRigorOptions;
31
32/**
33 * Constructs global renaming related command objects.
34 *
35 * @author Taavi Väänänen <hi@taavi.wtf>
36 */
37class GlobalRenameFactory {
38
39    private JobQueueGroupFactory $jobQueueGroupFactory;
40    private UserFactory $userFactory;
41
42    private CentralAuthAntiSpoofManager $caAntiSpoofManager;
43    private CentralAuthDatabaseManager $databaseManager;
44
45    /**
46     * @param JobQueueGroupFactory $jobQueueGroupFactory
47     * @param UserFactory $userFactory
48     * @param CentralAuthAntiSpoofManager $caAntiSpoofManager
49     * @param CentralAuthDatabaseManager $databaseManager
50     */
51    public function __construct(
52        JobQueueGroupFactory $jobQueueGroupFactory,
53        UserFactory $userFactory,
54        CentralAuthAntiSpoofManager $caAntiSpoofManager,
55        CentralAuthDatabaseManager $databaseManager
56    ) {
57        $this->jobQueueGroupFactory = $jobQueueGroupFactory;
58        $this->userFactory = $userFactory;
59        $this->caAntiSpoofManager = $caAntiSpoofManager;
60        $this->databaseManager = $databaseManager;
61    }
62
63    public function newGlobalRenameUser(
64        UserIdentity $performer,
65        CentralAuthUser $userToRename,
66        string $newName
67    ): GlobalRenameUser {
68        $userOld = $this->userFactory->newFromName( $userToRename->getName() );
69        if ( !$userOld ) {
70            throw new InvalidArgumentException( 'Name of the old user is not valid' );
71        }
72
73        // Avoid repeats of T343958. Some forms of creating a CentralAuthUser
74        // object do not canonicalize the username.
75        if ( $userOld->getName() !== $userToRename->getName() ) {
76            throw new InvalidArgumentException( 'Name of the global user is not in canonical form' );
77        }
78
79        $userNew = $this->userFactory->newFromName( $newName, UserRigorOptions::RIGOR_CREATABLE );
80        if ( !$userNew ) {
81            throw new InvalidArgumentException( 'Name old the new user is not creatable' );
82        }
83
84        return new GlobalRenameUser(
85            $performer,
86            $userOld,
87            $userToRename,
88            $userNew,
89            // TODO: this is only used for caching, maybe we can create a Factory service
90            // for CAUsers and related caching and inject that instead
91            CentralAuthUser::getInstance( $userNew ),
92            $this->newGlobalRenameUserStatus( $userNew->getName() ),
93            $this->jobQueueGroupFactory,
94            new GlobalRenameUserDatabaseUpdates( $this->databaseManager ),
95            new GlobalRenameUserLogger( $performer ),
96            $this->caAntiSpoofManager
97        );
98    }
99
100    public function newGlobalRenameUserStatus( string $username ): GlobalRenameUserStatus {
101        return new GlobalRenameUserStatus(
102            $this->databaseManager,
103            $username
104        );
105    }
106}