Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
UserCreationHookHandler
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 2
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 onLocalUserCreated
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
20
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\Auth\Hook\LocalUserCreatedHook;
24use MediaWiki\Extension\CentralAuth\CentralAuthDatabaseManager;
25use MediaWiki\Extension\CentralAuth\CentralAuthUtilityService;
26use MediaWiki\Extension\CentralAuth\User\CentralAuthAntiSpoofManager;
27use MediaWiki\Extension\CentralAuth\User\CentralAuthUser;
28use MediaWiki\User\User;
29use MediaWiki\WikiMap\WikiMap;
30
31class UserCreationHookHandler implements
32    LocalUserCreatedHook
33{
34    private CentralAuthAntiSpoofManager $caAntiSpoofManager;
35
36    /** @var CentralAuthDatabaseManager */
37    private $databaseManager;
38
39    /** @var CentralAuthUtilityService */
40    private $utilityService;
41
42    /**
43     * @param CentralAuthAntiSpoofManager $caAntiSpoofManager
44     * @param CentralAuthDatabaseManager $databaseManager
45     * @param CentralAuthUtilityService $utilityService
46     */
47    public function __construct(
48        CentralAuthAntiSpoofManager $caAntiSpoofManager,
49        CentralAuthDatabaseManager $databaseManager,
50        CentralAuthUtilityService $utilityService
51    ) {
52        $this->caAntiSpoofManager = $caAntiSpoofManager;
53        $this->databaseManager = $databaseManager;
54        $this->utilityService = $utilityService;
55    }
56
57    /**
58     * Populate localuser table and update AntiSpoof system
59     *
60     * @param User $user
61     * @param bool $autocreated
62     * @return void
63     */
64    public function onLocalUserCreated( $user, $autocreated ) {
65        $centralUser = CentralAuthUser::getPrimaryInstance( $user );
66
67        // If some other AuthManager PrimaryAuthenticationProvider is creating
68        // the user, we should still create a central user for them so
69        // CentralAuthIdLookup can have an ID for this new user right away.
70        if ( !$centralUser->exists() && !$centralUser->listUnattached() ) {
71            if ( $centralUser->register( null, $user->getEmail() ) ) {
72                $centralUser->attach( WikiMap::getCurrentWikiId(), 'new' );
73                $this->databaseManager->getCentralPrimaryDB()->onTransactionCommitOrIdle(
74                    function () use ( $centralUser ) {
75                        $this->utilityService->scheduleCreationJobs( $centralUser );
76                    },
77                    __METHOD__
78                );
79            }
80        }
81
82        $centralUser->addLocalName( WikiMap::getCurrentWikiId() );
83
84        // Record the username's thing-bob after a user account is created
85        $spoof = $this->caAntiSpoofManager->getSpoofUser( $user->getName() );
86        $spoof->record();
87    }
88}