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