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