Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / api / ApiCreateAccount.php
bloba7ba48a102d723cb0c40cf1ce1195c0a9bcbab4f
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() {
32 // If we're in JSON callback mode, no tokens can be obtained
33 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
34 $this->dieUsage( 'Cannot create account when using a callback', 'aborted' );
37 // $loginForm->addNewaccountInternal will throw exceptions
38 // if wiki is read only (already handled by api), user is blocked or does not have rights.
39 // Use userCan in order to hit GlobalBlock checks (according to Special:userlogin)
40 $loginTitle = SpecialPage::getTitleFor( 'Userlogin' );
41 if ( !$loginTitle->userCan( 'createaccount', $this->getUser() ) ) {
42 $this->dieUsage(
43 'You do not have the right to create a new account',
44 'permdenied-createaccount'
47 if ( $this->getUser()->isBlockedFromCreateAccount() ) {
48 $this->dieUsage( 'You cannot create a new account because you are blocked', 'blocked' );
51 $params = $this->extractRequestParams();
53 // Init session if necessary
54 if ( session_id() == '' ) {
55 wfSetupSession();
58 if ( $params['mailpassword'] && !$params['email'] ) {
59 $this->dieUsageMsg( 'noemail' );
62 if ( $params['language'] && !Language::isSupportedLanguage( $params['language'] ) ) {
63 $this->dieUsage( 'Invalid language parameter', 'langinvalid' );
66 $context = new DerivativeContext( $this->getContext() );
67 $context->setRequest( new DerivativeRequest(
68 $this->getContext()->getRequest(),
69 array(
70 'type' => 'signup',
71 'uselang' => $params['language'],
72 'wpName' => $params['name'],
73 'wpPassword' => $params['password'],
74 'wpRetype' => $params['password'],
75 'wpDomain' => $params['domain'],
76 'wpEmail' => $params['email'],
77 'wpRealName' => $params['realname'],
78 'wpCreateaccountToken' => $params['token'],
79 'wpCreateaccount' => $params['mailpassword'] ? null : '1',
80 'wpCreateaccountMail' => $params['mailpassword'] ? '1' : null
82 ) );
84 $loginForm = new LoginForm();
85 $loginForm->setContext( $context );
86 Hooks::run( 'AddNewAccountApiForm', array( $this, $loginForm ) );
87 $loginForm->load();
89 $status = $loginForm->addNewaccountInternal();
90 $result = array();
91 if ( $status->isGood() ) {
92 // Success!
93 $user = $status->getValue();
95 if ( $params['language'] ) {
96 $user->setOption( 'language', $params['language'] );
99 if ( $params['mailpassword'] ) {
100 // If mailpassword was set, disable the password and send an email.
101 $user->setPassword( null );
102 $status->merge( $loginForm->mailPasswordInternal(
103 $user,
104 false,
105 'createaccount-title',
106 'createaccount-text'
107 ) );
108 } elseif ( $this->getConfig()->get( 'EmailAuthentication' ) && Sanitizer::validateEmail( $user->getEmail() ) ) {
109 // Send out an email authentication message if needed
110 $status->merge( $user->sendConfirmationMail() );
113 // Save settings (including confirmation token)
114 $user->saveSettings();
116 Hooks::run( 'AddNewAccount', array( $user, $params['mailpassword'] ) );
118 if ( $params['mailpassword'] ) {
119 $logAction = 'byemail';
120 } elseif ( $this->getUser()->isLoggedIn() ) {
121 $logAction = 'create2';
122 } else {
123 $logAction = 'create';
125 $user->addNewUserLogEntry( $logAction, (string)$params['reason'] );
127 // Add username, id, and token to result.
128 $result['username'] = $user->getName();
129 $result['userid'] = $user->getId();
130 $result['token'] = $user->getToken();
133 $apiResult = $this->getResult();
135 if ( $status->hasMessage( 'sessionfailure' ) || $status->hasMessage( 'nocookiesfornew' ) ) {
136 // Token was incorrect, so add it to result, but don't throw an exception
137 // since not having the correct token is part of the normal
138 // flow of events.
139 $result['token'] = LoginForm::getCreateaccountToken();
140 $result['result'] = 'NeedToken';
141 } elseif ( !$status->isOK() ) {
142 // There was an error. Die now.
143 $this->dieStatus( $status );
144 } elseif ( !$status->isGood() ) {
145 // Status is not good, but OK. This means warnings.
146 $result['result'] = 'Warning';
148 // Add any warnings to the result
149 $warnings = $status->getErrorsByType( 'warning' );
150 if ( $warnings ) {
151 foreach ( $warnings as &$warning ) {
152 $apiResult->setIndexedTagName( $warning['params'], 'param' );
154 $apiResult->setIndexedTagName( $warnings, 'warning' );
155 $result['warnings'] = $warnings;
157 } else {
158 // Everything was fine.
159 $result['result'] = 'Success';
162 // Give extensions a chance to modify the API result data
163 Hooks::run( 'AddNewAccountApiResult', array( $this, $loginForm, &$result ) );
165 $apiResult->addValue( null, 'createaccount', $result );
168 public function mustBePosted() {
169 return true;
172 public function isReadMode() {
173 return false;
176 public function isWriteMode() {
177 return true;
180 public function getAllowedParams() {
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 => $this->getConfig()->get( 'EmailConfirmToEdit' ),
193 'realname' => null,
194 'mailpassword' => array(
195 ApiBase::PARAM_TYPE => 'boolean',
196 ApiBase::PARAM_DFLT => false
198 'reason' => null,
199 'language' => null
203 protected function getExamplesMessages() {
204 return array(
205 'action=createaccount&name=testuser&password=test123'
206 => 'apihelp-createaccount-example-pass',
207 'action=createaccount&name=testmailuser&mailpassword=true&reason=MyReason'
208 => 'apihelp-createaccount-example-mail',
212 public function getHelpUrls() {
213 return 'https://www.mediawiki.org/wiki/API:Account_creation';