Debian Codegraph @

includes/api/ApiAMCreateAccount.php

raw symbols SCIP
1 <?php
2 /**
3 * Copyright © 2016 Wikimedia Foundation and contributors
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 namespace MediaWiki\Api;
24
25 use MediaWiki\Auth\AuthenticationResponse;
26 use MediaWiki\Auth\AuthManager;
27 use MediaWiki\Utils\UrlUtils;
28
29 /**
30 * Create an account with AuthManager
31 *
32 * @ingroup API
33 */
34 class ApiAMCreateAccount extends ApiBase {
35
36 private AuthManager $authManager;
37 private UrlUtils $urlUtils;
38
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;
48 }
49
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(),
59 );
60 return $msgs;
61 }
62
63 public function execute() {
64 $params = $this->extractRequestParams();
65
66 $this->requireAtLeastOneParameter( $params, 'continue', 'returnurl' );
67
68 if ( $params['returnurl'] !== null ) {
69 $bits = $this->urlUtils->parse( $params['returnurl'] );
70 if ( !$bits || $bits['scheme'] === '' ) {
71 $encParamName = $this->encodeParamName( 'returnurl' );
72 $this->dieWithError(
73 [ 'apierror-badurl', $encParamName, wfEscapeWikiText( $params['returnurl'] ) ],
74 "badurl_{$encParamName}"
75 );
76 }
77 }
78
79 $helper = new ApiAuthManagerHelper( $this, $this->authManager );
80
81 // Make sure it's possible to create accounts
82 if ( !$this->authManager->canCreateAccounts() ) {
83 $res = AuthenticationResponse::newFail( $this->msg( 'userlogin-cannot-' . AuthManager::ACTION_CREATE ) );
84 $this->getResult()->addValue( null, 'createaccount',
85 $helper->formatAuthenticationResponse( $res ) );
86 $helper->logAuthenticationResult( 'accountcreation', $res );
87 return;
88 }
89
90 // Perform the create step
91 if ( $params['continue'] ) {
92 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_CREATE_CONTINUE );
93 $res = $this->authManager->continueAccountCreation( $reqs );
94 } else {
95 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_CREATE );
96 if ( $params['preservestate'] ) {
97 $req = $helper->getPreservedRequest();
98 if ( $req ) {
99 $reqs[] = $req;
100 }
101 }
102 $res = $this->authManager->beginAccountCreation(
103 $this->getAuthority(),
104 $reqs,
105 $params['returnurl']
106 );
107 }
108
109 $this->getResult()->addValue( null, 'createaccount',
110 $helper->formatAuthenticationResponse( $res ) );
111 $helper->logAuthenticationResult( 'accountcreation', $res );
112 }
113
114 public function isReadMode() {
115 return false;
116 }
117
118 public function isWriteMode() {
119 return true;
120 }
121
122 public function needsToken() {
123 return 'createaccount';
124 }
125
126 public function getAllowedParams() {
127 $ret = ApiAuthManagerHelper::getStandardParams( AuthManager::ACTION_CREATE,
128 'requests', 'messageformat', 'mergerequestfields', 'preservestate', 'returnurl', 'continue'
129 );
130 $ret['preservestate'][ApiBase::PARAM_HELP_MSG_APPEND][] =
131 'apihelp-createaccount-param-preservestate';
132 return $ret;
133 }
134
135 public function dynamicParameterDocumentation() {
136 return [ 'api-help-authmanagerhelper-additional-params', AuthManager::ACTION_CREATE ];
137 }
138
139 protected function getExamplesMessages() {
140 return [
141 'action=createaccount&username=Example&password=ExamplePassword&retype=ExamplePassword'
142 . '&createreturnurl=http://example.org/&createtoken=123ABC'
143 => 'apihelp-createaccount-example-create',
144 ];
145 }
146
147 public function getHelpUrls() {
148 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Account_creation';
149 }
150 }
151
152 /** @deprecated class alias since 1.43 */
153 class_alias( ApiAMCreateAccount::class, 'ApiAMCreateAccount' );