Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.33% covered (success)
93.33%
14 / 15
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiAcquireTempUserName
93.33% covered (success)
93.33%
14 / 15
75.00% covered (warning)
75.00%
3 / 4
7.01
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
4
 isWriteMode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 mustBePosted
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
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
22use MediaWiki\User\TempUser\TempUserCreator;
23
24/**
25 * Acquire a temporary user username and stash it in the current session, if temp account creation
26 * is enabled and the current user is logged out. If a name has already been stashed, returns the
27 * same name.
28 *
29 * If the user later performs an action that results in temp account creation, the stashed username
30 * will be used for their account. It may also be used in previews. However, the account is not
31 * created yet, and the name is not visible to other users.
32 *
33 * @ingroup API
34 */
35class ApiAcquireTempUserName extends ApiBase {
36
37    private TempUserCreator $tempUserCreator;
38
39    public function __construct(
40        ApiMain $main,
41        string $action,
42        TempUserCreator $tempUserCreator
43    ) {
44        parent::__construct( $main, $action );
45        $this->tempUserCreator = $tempUserCreator;
46    }
47
48    public function execute() {
49        // Like TempUserCreator::shouldAutoCreate(), but without the action check
50        if ( !$this->tempUserCreator->isEnabled() ) {
51            $this->dieWithError( 'apierror-tempuserdisabled', 'tempuserdisabled' );
52        }
53        if ( $this->getUser()->isRegistered() ) {
54            $this->dieWithError( 'apierror-alreadyregistered', 'alreadyregistered' );
55        }
56        $this->checkUserRightsAny( 'createaccount' );
57
58        // Checks passed, acquire the name
59        $session = $this->getRequest()->getSession();
60        $name = $this->tempUserCreator->acquireAndStashName( $session );
61        if ( $name === null ) {
62            $this->dieWithError( 'apierror-tempuseracquirefailed', 'tempuseracquirefailed' );
63        }
64
65        $session->persist();
66        $this->getResult()->addValue( null, $this->getModuleName(), $name );
67    }
68
69    public function isWriteMode() {
70        return true;
71    }
72
73    public function mustBePosted() {
74        return true;
75    }
76}