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.
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.
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
22 namespace MediaWiki\Api
;
24 use MediaWiki\User\TempUser\TempUserCreator
;
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
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.
37 class ApiAcquireTempUserName
extends ApiBase
{
39 private TempUserCreator
$tempUserCreator;
41 public function __construct(
44 TempUserCreator
$tempUserCreator
46 parent
::__construct( $main, $action );
47 $this->tempUserCreator
= $tempUserCreator;
50 public function execute() {
51 // Like TempUserCreator::shouldAutoCreate(), but without the action check
52 if ( !$this->tempUserCreator
->isEnabled() ) {
53 $this->dieWithError( 'apierror-tempuserdisabled', 'tempuserdisabled' );
55 if ( $this->getUser()->isRegistered() ) {
56 $this->dieWithError( 'apierror-alreadyregistered', 'alreadyregistered' );
58 $this->checkUserRightsAny( 'createaccount' );
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' );
68 $this->getResult()->addValue( null, $this->getModuleName(), $name );
71 public function isWriteMode() {
75 public function mustBePosted() {
80 /** @deprecated class alias since 1.43 */
81 class_alias( ApiAcquireTempUserName
::class, 'ApiAcquireTempUserName' );