Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
GlobalRenameUserValidator
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 2
56
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
 validate
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3namespace MediaWiki\Extension\CentralAuth\GlobalRename;
4
5use MediaWiki\Extension\CentralAuth\User\CentralAuthUser;
6use MediaWiki\Status\Status;
7use MediaWiki\User\UserIdentity;
8use MediaWiki\User\UserNameUtils;
9
10/**
11 * Verify whether a potential global rename is valid
12 *
13 * @license GPL-2.0-or-later
14 * @author Marius Hoch < hoo@online.de >
15 */
16class GlobalRenameUserValidator {
17
18    private UserNameUtils $userNameUtils;
19
20    public function __construct( UserNameUtils $userNameUtils ) {
21        $this->userNameUtils = $userNameUtils;
22    }
23
24    /**
25     * Check that we can perform the rename
26     *
27     * @param UserIdentity $oldUser
28     * @param UserIdentity $newUser
29     *
30     * @return Status
31     */
32    public function validate( UserIdentity $oldUser, UserIdentity $newUser ) {
33        $status = new Status();
34
35        if ( !$this->userNameUtils->isCreatable( $newUser->getName() ) ) {
36            $status->fatal( 'centralauth-rename-badusername' );
37        }
38
39        $caOldUser = CentralAuthUser::getInstance( $oldUser );
40        if ( !$caOldUser->exists() ) {
41            $status->fatal( 'centralauth-rename-doesnotexist' );
42        }
43        $caNewUser = CentralAuthUser::getInstance( $newUser );
44        if ( $caNewUser->exists() ) {
45            $status->fatal( 'centralauth-rename-alreadyexists' );
46        }
47
48        $unattached = $caNewUser->listUnattached();
49        if ( $unattached ) {
50            $status->fatal( 'centralauth-rename-unattached-intheway' );
51        }
52
53        // Check we're not currently renaming the user
54        $renameState = $caOldUser->renameInProgress();
55        if ( $renameState ) {
56            $status->fatal( 'centralauth-rename-alreadyinprogress', $renameState[1] );
57        }
58
59        return $status;
60    }
61}