Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiAMCreateAccount.php
blobe4fee7e501ffbb90ec89de5dfec4126733de0d0e
1 <?php
2 /**
3 * Copyright © 2016 Wikimedia Foundation and contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 namespace MediaWiki\Api;
25 use MediaWiki\Auth\AuthenticationResponse;
26 use MediaWiki\Auth\AuthManager;
27 use MediaWiki\Utils\UrlUtils;
29 /**
30 * Create an account with AuthManager
32 * @ingroup API
34 class ApiAMCreateAccount extends ApiBase {
36 private AuthManager $authManager;
37 private UrlUtils $urlUtils;
39 public function __construct(
40 ApiMain $main,
41 string $action,
42 AuthManager $authManager,
43 UrlUtils $urlUtils
44 ) {
45 parent::__construct( $main, $action, 'create' );
46 $this->authManager = $authManager;
47 $this->urlUtils = $urlUtils;
50 public function getFinalDescription() {
51 // A bit of a hack to append 'api-help-authmanager-general-usage'
52 $msgs = parent::getFinalDescription();
53 $msgs[] = $this->msg( 'api-help-authmanager-general-usage',
54 $this->getModulePrefix(),
55 $this->getModuleName(),
56 $this->getModulePath(),
57 AuthManager::ACTION_CREATE,
58 $this->needsToken(),
60 return $msgs;
63 public function execute() {
64 $params = $this->extractRequestParams();
65 $performer = $this->getUser();
67 $this->requireAtLeastOneParameter( $params, 'continue', 'returnurl' );
69 if ( $params['returnurl'] !== null ) {
70 $bits = $this->urlUtils->parse( $params['returnurl'] );
71 if ( !$bits || $bits['scheme'] === '' ) {
72 $encParamName = $this->encodeParamName( 'returnurl' );
73 $this->dieWithError(
74 [ 'apierror-badurl', $encParamName, wfEscapeWikiText( $params['returnurl'] ) ],
75 "badurl_{$encParamName}"
80 $helper = new ApiAuthManagerHelper( $this, $this->authManager );
82 // Make sure it's possible to create accounts
83 if ( !$this->authManager->canCreateAccounts() ) {
84 $res = AuthenticationResponse::newFail( $this->msg( 'userlogin-cannot-' . AuthManager::ACTION_CREATE ) );
85 $this->getResult()->addValue( null, 'createaccount',
86 $helper->formatAuthenticationResponse( $res ) );
87 $helper->logAuthenticationResult( 'accountcreation', $performer, $res );
88 return;
91 // Perform the create step
92 if ( $params['continue'] ) {
93 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_CREATE_CONTINUE );
94 $res = $this->authManager->continueAccountCreation( $reqs );
95 } else {
96 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_CREATE );
97 if ( $params['preservestate'] ) {
98 $req = $helper->getPreservedRequest();
99 if ( $req ) {
100 $reqs[] = $req;
103 $res = $this->authManager->beginAccountCreation(
104 $this->getAuthority(),
105 $reqs,
106 $params['returnurl']
110 $this->getResult()->addValue( null, 'createaccount',
111 $helper->formatAuthenticationResponse( $res ) );
112 $helper->logAuthenticationResult( 'accountcreation', $performer, $res );
115 public function isReadMode() {
116 return false;
119 public function isWriteMode() {
120 return true;
123 public function needsToken() {
124 return 'createaccount';
127 public function getAllowedParams() {
128 $ret = ApiAuthManagerHelper::getStandardParams( AuthManager::ACTION_CREATE,
129 'requests', 'messageformat', 'mergerequestfields', 'preservestate', 'returnurl', 'continue'
131 $ret['preservestate'][ApiBase::PARAM_HELP_MSG_APPEND][] =
132 'apihelp-createaccount-param-preservestate';
133 return $ret;
136 public function dynamicParameterDocumentation() {
137 return [ 'api-help-authmanagerhelper-additional-params', AuthManager::ACTION_CREATE ];
140 protected function getExamplesMessages() {
141 return [
142 'action=createaccount&username=Example&password=ExamplePassword&retype=ExamplePassword'
143 . '&createreturnurl=http://example.org/&createtoken=123ABC'
144 => 'apihelp-createaccount-example-create',
148 public function getHelpUrls() {
149 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Account_creation';
153 /** @deprecated class alias since 1.43 */
154 class_alias( ApiAMCreateAccount::class, 'ApiAMCreateAccount' );