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
23 namespace MediaWiki\Api
;
25 use MediaWiki\Auth\AuthenticationResponse
;
26 use MediaWiki\Auth\AuthManager
;
27 use MediaWiki\Utils\UrlUtils
;
30 * Create an account with AuthManager
34 class ApiAMCreateAccount
extends ApiBase
{
36 private AuthManager
$authManager;
37 private UrlUtils
$urlUtils;
39 public function __construct(
42 AuthManager
$authManager,
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
,
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' );
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 );
91 // Perform the create step
92 if ( $params['continue'] ) {
93 $reqs = $helper->loadAuthenticationRequests( AuthManager
::ACTION_CREATE_CONTINUE
);
94 $res = $this->authManager
->continueAccountCreation( $reqs );
96 $reqs = $helper->loadAuthenticationRequests( AuthManager
::ACTION_CREATE
);
97 if ( $params['preservestate'] ) {
98 $req = $helper->getPreservedRequest();
103 $res = $this->authManager
->beginAccountCreation(
104 $this->getAuthority(),
110 $this->getResult()->addValue( null, 'createaccount',
111 $helper->formatAuthenticationResponse( $res ) );
112 $helper->logAuthenticationResult( 'accountcreation', $performer, $res );
115 public function isReadMode() {
119 public function isWriteMode() {
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';
136 public function dynamicParameterDocumentation() {
137 return [ 'api-help-authmanagerhelper-additional-params', AuthManager
::ACTION_CREATE
];
140 protected function getExamplesMessages() {
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' );