Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.33% covered (warning)
83.33%
25 / 30
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CentralAuthForcedLocalCreationService
83.33% covered (warning)
83.33%
25 / 30
0.00% covered (danger)
0.00%
0 / 2
10.46
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 attemptAutoCreateLocalUserFromName
89.29% covered (warning)
89.29%
25 / 28
0.00% covered (danger)
0.00%
0 / 1
9.10
1<?php
2/**
3 * @section LICENSE
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 * @file
20 */
21
22namespace MediaWiki\Extension\CentralAuth\User;
23
24use ManualLogEntry;
25use MediaWiki\Deferred\SiteStatsUpdate;
26use MediaWiki\Extension\CentralAuth\CentralAuthUtilityService;
27use MediaWiki\Permissions\Authority;
28use MediaWiki\Status\Status;
29use MediaWiki\User\UserFactory;
30
31/**
32 * Service for forcing a local account to be created.
33 *
34 * @since 1.36
35 * @author Taavi "Majavah" Väänänen
36 */
37class CentralAuthForcedLocalCreationService {
38    /** @var UserFactory */
39    private $userFactory;
40
41    /** @var CentralAuthUtilityService */
42    private $utilityService;
43
44    /**
45     * @param UserFactory $userFactory
46     * @param CentralAuthUtilityService $utilityService
47     */
48    public function __construct(
49        UserFactory $userFactory,
50        CentralAuthUtilityService $utilityService
51    ) {
52        $this->userFactory = $userFactory;
53        $this->utilityService = $utilityService;
54    }
55
56    /**
57     * Attempt to create a local user for the specified username.
58     * @param string $username
59     * @param Authority|null $performer
60     * @param string|null $reason
61     * @return Status
62     */
63    public function attemptAutoCreateLocalUserFromName(
64        string $username,
65        Authority $performer = null,
66        $reason = null
67    ): Status {
68        $user = $this->userFactory->newFromName( $username );
69
70        if ( !$user ) {
71            // invalid username
72            return Status::newFatal( 'centralauth-createlocal-no-global-account' );
73        }
74
75        if ( $user->getId() ) {
76            return Status::newFatal( 'centralauth-createlocal-already-exists' );
77        }
78
79        $centralUser = CentralAuthUser::getInstance( $user );
80
81        if ( !$centralUser->exists() ) {
82            return Status::newFatal( 'centralauth-createlocal-no-global-account' );
83        }
84
85        if ( $centralUser->isSuppressed() ) {
86            $canSuppress = $performer && $performer->isAllowed( 'centralauth-suppress' );
87
88            return Status::newFatal( $canSuppress
89                ? 'centralauth-createlocal-suppressed'
90                : 'centralauth-createlocal-no-global-account' );
91        }
92
93        $status = $this->utilityService->autoCreateUser( $user, false, $performer );
94        if ( !$status->isGood() ) {
95            return Status::wrap( $status );
96        }
97
98        // Add log entry
99        if ( $performer ) {
100            $logEntry = new ManualLogEntry( 'newusers', 'forcecreatelocal' );
101            $logEntry->setPerformer( $performer->getUser() );
102            $logEntry->setTarget( $user->getUserPage() );
103            $logEntry->setComment( $reason );
104            $logEntry->setParameters( [
105                '4::userid' => $user->getId(),
106            ] );
107
108            $logId = $logEntry->insert();
109            $logEntry->publish( $logId );
110        }
111
112        // Update user count
113        SiteStatsUpdate::factory( [ 'users' => 1 ] )->doUpdate();
114
115        return Status::newGood();
116    }
117}