Fixed spacing in db/debug/diff/externalstore/objectcache folder
[mediawiki.git] / includes / api / ApiCreateAccount.php
blob59ff3247921384b39f1337b1a931a0a7283ba39c
1 <?php
2 /**
3 * Created on August 7, 2012
5 * Copyright © 2012 Tyler Romeo <tylerromeo@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
22 * @file
25 /**
26 * Unit to authenticate account registration attempts to the current wiki.
28 * @ingroup API
30 class ApiCreateAccount extends ApiBase {
31 public function execute() {
33 // $loginForm->addNewaccountInternal will throw exceptions
34 // if wiki is read only (already handled by api), user is blocked or does not have rights.
35 // Use userCan in order to hit GlobalBlock checks (according to Special:userlogin)
36 $loginTitle = SpecialPage::getTitleFor( 'Userlogin' );
37 if ( !$loginTitle->userCan( 'createaccount', $this->getUser() ) ) {
38 $this->dieUsage( 'You do not have the right to create a new account', 'permdenied-createaccount' );
40 if ( $this->getUser()->isBlockedFromCreateAccount() ) {
41 $this->dieUsage( 'You cannot create a new account because you are blocked', 'blocked' );
44 $params = $this->extractRequestParams();
46 // Init session if necessary
47 if ( session_id() == '' ) {
48 wfSetupSession();
51 if ( $params['mailpassword'] && !$params['email'] ) {
52 $this->dieUsageMsg( 'noemail' );
55 if ( $params['language'] && !Language::isSupportedLanguage( $params['language'] ) ) {
56 $this->dieUsage( 'Invalid language parameter', 'langinvalid' );
59 $context = new DerivativeContext( $this->getContext() );
60 $context->setRequest( new DerivativeRequest(
61 $this->getContext()->getRequest(),
62 array(
63 'type' => 'signup',
64 'uselang' => $params['language'],
65 'wpName' => $params['name'],
66 'wpPassword' => $params['password'],
67 'wpRetype' => $params['password'],
68 'wpDomain' => $params['domain'],
69 'wpEmail' => $params['email'],
70 'wpRealName' => $params['realname'],
71 'wpCreateaccountToken' => $params['token'],
72 'wpCreateaccount' => $params['mailpassword'] ? null : '1',
73 'wpCreateaccountMail' => $params['mailpassword'] ? '1' : null
75 ) );
77 $loginForm = new LoginForm();
78 $loginForm->setContext( $context );
79 $loginForm->load();
81 $status = $loginForm->addNewaccountInternal();
82 $result = array();
83 if ( $status->isGood() ) {
84 // Success!
85 global $wgEmailAuthentication;
86 $user = $status->getValue();
88 if ( $params['language'] ) {
89 $user->setOption( 'language', $params['language'] );
92 if ( $params['mailpassword'] ) {
93 // If mailpassword was set, disable the password and send an email.
94 $user->setPassword( null );
95 $status->merge( $loginForm->mailPasswordInternal( $user, false, 'createaccount-title', 'createaccount-text' ) );
96 } elseif ( $wgEmailAuthentication && Sanitizer::validateEmail( $user->getEmail() ) ) {
97 // Send out an email authentication message if needed
98 $status->merge( $user->sendConfirmationMail() );
101 // Save settings (including confirmation token)
102 $user->saveSettings();
104 wfRunHooks( 'AddNewAccount', array( $user, $params['mailpassword'] ) );
106 if ( $params['mailpassword'] ) {
107 $logAction = 'byemail';
108 } elseif ( $this->getUser()->isLoggedIn() ) {
109 $logAction = 'create2';
110 } else {
111 $logAction = 'create';
113 $user->addNewUserLogEntry( $logAction, (string)$params['reason'] );
115 // Add username, id, and token to result.
116 $result['username'] = $user->getName();
117 $result['userid'] = $user->getId();
118 $result['token'] = $user->getToken();
121 $apiResult = $this->getResult();
123 if ( $status->hasMessage( 'sessionfailure' ) || $status->hasMessage( 'nocookiesfornew' ) ) {
124 // Token was incorrect, so add it to result, but don't throw an exception
125 // since not having the correct token is part of the normal
126 // flow of events.
127 $result['token'] = LoginForm::getCreateaccountToken();
128 $result['result'] = 'needtoken';
129 } elseif ( !$status->isOK() ) {
130 // There was an error. Die now.
131 // Cannot use dieUsageMsg() directly because extensions
132 // might return custom error messages.
133 $errors = $status->getErrorsArray();
134 if ( $errors[0] instanceof Message ) {
135 $code = 'aborted';
136 $desc = $errors[0];
137 } else {
138 $code = array_shift( $errors[0] );
139 $desc = wfMessage( $code, $errors[0] );
141 $this->dieUsage( $desc, $code );
142 } elseif ( !$status->isGood() ) {
143 // Status is not good, but OK. This means warnings.
144 $result['result'] = 'warning';
146 // Add any warnings to the result
147 $warnings = $status->getErrorsByType( 'warning' );
148 if ( $warnings ) {
149 foreach ( $warnings as &$warning ) {
150 $apiResult->setIndexedTagName( $warning['params'], 'param' );
152 $apiResult->setIndexedTagName( $warnings, 'warning' );
153 $result['warnings'] = $warnings;
155 } else {
156 // Everything was fine.
157 $result['result'] = 'success';
160 $apiResult->addValue( null, 'createaccount', $result );
163 public function getDescription() {
164 return 'Create a new user account.';
167 public function mustBePosted() {
168 return true;
171 public function isReadMode() {
172 return false;
175 public function isWriteMode() {
176 return true;
179 public function getAllowedParams() {
180 global $wgEmailConfirmToEdit;
181 return array(
182 'name' => array(
183 ApiBase::PARAM_TYPE => 'user',
184 ApiBase::PARAM_REQUIRED => true
186 'password' => null,
187 'domain' => null,
188 'token' => null,
189 'email' => array(
190 ApiBase::PARAM_TYPE => 'string',
191 ApiBase::PARAM_REQUIRED => $wgEmailConfirmToEdit
193 'realname' => null,
194 'mailpassword' => array(
195 ApiBase::PARAM_TYPE => 'boolean',
196 ApiBase::PARAM_DFLT => false
198 'reason' => null,
199 'language' => null
203 public function getParamDescription() {
204 $p = $this->getModulePrefix();
205 return array(
206 'name' => 'Username',
207 'password' => "Password (ignored if {$p}mailpassword is set)",
208 'domain' => 'Domain for external authentication (optional)',
209 'token' => 'Account creation token obtained in first request',
210 'email' => 'Email address of user (optional)',
211 'realname' => 'Real name of user (optional)',
212 'mailpassword' => 'If set to any value, a random password will be emailed to the user',
213 'reason' => 'Optional reason for creating the account to be put in the logs',
214 'language' => 'Language code to set as default for the user (optional, defaults to content language)'
218 public function getResultProperties() {
219 return array(
220 'createaccount' => array(
221 'result' => array(
222 ApiBase::PROP_TYPE => array(
223 'success',
224 'warning',
225 'needtoken'
228 'username' => array(
229 ApiBase::PROP_TYPE => 'string',
230 ApiBase::PROP_NULLABLE => true
232 'userid' => array(
233 ApiBase::PROP_TYPE => 'int',
234 ApiBase::PROP_NULLABLE => true
236 'token' => array(
237 ApiBase::PROP_TYPE => 'string',
238 ApiBase::PROP_NULLABLE => true
244 public function getPossibleErrors() {
245 // Note the following errors aren't possible and don't need to be listed:
246 // sessionfailure, nocookiesfornew, badretype
247 $localErrors = array(
248 'wrongpassword', // Actually caused by wrong domain field. Riddle me that...
249 'sorbs_create_account_reason',
250 'noname',
251 'userexists',
252 'password-name-match', // from User::getPasswordValidity
253 'password-login-forbidden', // from User::getPasswordValidity
254 'noemailtitle',
255 'invalidemailaddress',
256 'externaldberror',
257 'acct_creation_throttle_hit',
260 $errors = parent::getPossibleErrors();
261 // All local errors are from LoginForm, which means they're actually message keys.
262 foreach ( $localErrors as $error ) {
263 $errors[] = array( 'code' => $error, 'info' => wfMessage( $error )->parse() );
266 $errors[] = array(
267 'code' => 'permdenied-createaccount',
268 'info' => 'You do not have the right to create a new account'
270 $errors[] = array(
271 'code' => 'blocked',
272 'info' => 'You cannot create a new account because you are blocked'
274 $errors[] = array(
275 'code' => 'aborted',
276 'info' => 'Account creation aborted by hook (info may vary)'
278 $errors[] = array(
279 'code' => 'langinvalid',
280 'info' => 'Invalid language parameter'
283 // 'passwordtooshort' has parameters. :(
284 global $wgMinimalPasswordLength;
285 $errors[] = array(
286 'code' => 'passwordtooshort',
287 'info' => wfMessage( 'passwordtooshort', $wgMinimalPasswordLength )->parse()
289 return $errors;
292 public function getExamples() {
293 return array(
294 'api.php?action=createaccount&name=testuser&password=test123',
295 'api.php?action=createaccount&name=testmailuser&mailpassword=true&reason=MyReason',
299 public function getHelpUrls() {
300 return 'https://www.mediawiki.org/wiki/API:Account_creation';