Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
CentralAuthUtilityService
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 3
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 autoCreateUser
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 scheduleCreationJobs
0.00% covered (danger)
0.00%
0 / 14
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;
22
23use MediaWiki\Auth\AuthManager;
24use MediaWiki\Config\Config;
25use MediaWiki\Context\RequestContext;
26use MediaWiki\Extension\CentralAuth\Config\CAMainConfigNames;
27use MediaWiki\Extension\CentralAuth\User\CentralAuthUser;
28use MediaWiki\JobQueue\JobFactory;
29use MediaWiki\JobQueue\JobQueueGroupFactory;
30use MediaWiki\Permissions\Authority;
31use MediaWiki\Title\TitleFactory;
32use MediaWiki\User\User;
33use MediaWiki\WikiMap\WikiMap;
34use Profiler;
35use RuntimeException;
36use StatusValue;
37
38/**
39 * Utility services that are useful in many parts of CentralAuth.
40 *
41 * @since 1.36
42 */
43class CentralAuthUtilityService {
44
45    private Config $config;
46    private AuthManager $authManager;
47    private TitleFactory $titleFactory;
48    private JobQueueGroupFactory $jobQueueGroupFactory;
49    private JobFactory $jobFactory;
50
51    public function __construct(
52        Config $config,
53        AuthManager $authManager,
54        TitleFactory $titleFactory,
55        JobQueueGroupFactory $jobQueueGroupFactory,
56        JobFactory $jobFactory
57    ) {
58        $this->config = $config;
59        $this->authManager = $authManager;
60        $this->titleFactory = $titleFactory;
61        $this->jobQueueGroupFactory = $jobQueueGroupFactory;
62        $this->jobFactory = $jobFactory;
63    }
64
65    /**
66     * Auto-create an account
67     *
68     * @param User $user User to auto-create
69     * @param bool $log Whether to generate a user creation log entry
70     * @param Authority|null $performer The user performing the creation
71     * @return StatusValue a status value
72     */
73    public function autoCreateUser( User $user, $log = true,
74        ?Authority $performer = null
75    ): StatusValue {
76        // Ignore warnings about primary database connections/writes...hard to avoid here
77
78        Profiler::instance()->getTransactionProfiler()->resetExpectations();
79
80        $source = CentralAuthPrimaryAuthenticationProvider::ID;
81        if ( !$this->authManager->getAuthenticationProvider( $source ) ) {
82            $source = AuthManager::AUTOCREATE_SOURCE_SESSION;
83        }
84        return $this->authManager->autoCreateUser( $user, $source, false, $log, $performer );
85    }
86
87    /**
88     * Sets up jobs to create and attach a local account for the given user on every wiki listed in
89     * $wgCentralAuthAutoCreateWikis.
90     * @param CentralAuthUser $centralUser
91     */
92    public function scheduleCreationJobs( CentralAuthUser $centralUser ) {
93        $name = $centralUser->getName();
94        $thisWiki = WikiMap::getCurrentWikiId();
95        $session = RequestContext::getMain()->exportSession();
96
97        $title = $this->titleFactory->makeTitleSafe( NS_USER, $name );
98
99        if ( !$title ) {
100            throw new RuntimeException( "Failed to create title for user page of $name" );
101        }
102
103        foreach ( $this->config->get( CAMainConfigNames::CentralAuthAutoCreateWikis ) as $wiki ) {
104            if ( $wiki === $thisWiki ) {
105                continue;
106            }
107
108            $job = $this->jobFactory->newJob(
109                'CentralAuthCreateLocalAccountJob',
110                [ 'name' => $name, 'from' => $thisWiki, 'session' => $session ]
111            );
112            $this->jobQueueGroupFactory->makeJobQueueGroup( $wiki )->lazyPush( $job );
113        }
114    }
115}