Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
87.50% |
14 / 16 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ApiAcquireTempUserName | |
93.33% |
14 / 15 |
|
75.00% |
3 / 4 |
7.01 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
4 | |||
| isWriteMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| mustBePosted | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @license GPL-2.0-or-later |
| 5 | * @file |
| 6 | */ |
| 7 | |
| 8 | namespace MediaWiki\Api; |
| 9 | |
| 10 | use 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 | */ |
| 23 | class ApiAcquireTempUserName extends ApiBase { |
| 24 | |
| 25 | private TempUserCreator $tempUserCreator; |
| 26 | |
| 27 | public function __construct( |
| 28 | ApiMain $main, |
| 29 | string $action, |
| 30 | TempUserCreator $tempUserCreator |
| 31 | ) { |
| 32 | parent::__construct( $main, $action ); |
| 33 | $this->tempUserCreator = $tempUserCreator; |
| 34 | } |
| 35 | |
| 36 | public function execute() { |
| 37 | // Like TempUserCreator::shouldAutoCreate(), but without the action check |
| 38 | if ( !$this->tempUserCreator->isEnabled() ) { |
| 39 | $this->dieWithError( 'apierror-tempuserdisabled', 'tempuserdisabled' ); |
| 40 | } |
| 41 | if ( $this->getUser()->isRegistered() ) { |
| 42 | $this->dieWithError( 'apierror-alreadyregistered', 'alreadyregistered' ); |
| 43 | } |
| 44 | $this->checkUserRightsAny( 'createaccount' ); |
| 45 | |
| 46 | // Checks passed, acquire the name |
| 47 | $session = $this->getRequest()->getSession(); |
| 48 | $name = $this->tempUserCreator->acquireAndStashName( $session ); |
| 49 | if ( $name === null ) { |
| 50 | $this->dieWithError( 'apierror-tempuseracquirefailed', 'tempuseracquirefailed' ); |
| 51 | } |
| 52 | |
| 53 | $session->persist(); |
| 54 | $this->getResult()->addValue( null, $this->getModuleName(), $name ); |
| 55 | } |
| 56 | |
| 57 | /** @inheritDoc */ |
| 58 | public function isWriteMode() { |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | /** @inheritDoc */ |
| 63 | public function mustBePosted() { |
| 64 | return true; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** @deprecated class alias since 1.43 */ |
| 69 | class_alias( ApiAcquireTempUserName::class, 'ApiAcquireTempUserName' ); |