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
26 * Unit to authenticate account registration attempts to the current wiki.
30 class ApiCreateAccount
extends ApiBase
{
31 public function execute() {
32 // If we're in a mode that breaks the same-origin policy, no tokens can
34 if ( $this->lacksSameOriginSecurity() ) {
36 'Cannot create account when the same-origin policy is not applied', 'aborted'
40 // $loginForm->addNewaccountInternal will throw exceptions
41 // if wiki is read only (already handled by api), user is blocked or does not have rights.
42 // Use userCan in order to hit GlobalBlock checks (according to Special:userlogin)
43 $loginTitle = SpecialPage
::getTitleFor( 'Userlogin' );
44 if ( !$loginTitle->userCan( 'createaccount', $this->getUser() ) ) {
46 'You do not have the right to create a new account',
47 'permdenied-createaccount'
50 if ( $this->getUser()->isBlockedFromCreateAccount() ) {
52 'You cannot create a new account because you are blocked',
55 array( 'blockinfo' => ApiQueryUserInfo
::getBlockInfo( $this->getUser()->getBlock() ) )
59 $params = $this->extractRequestParams();
61 // Init session if necessary
62 if ( session_id() == '' ) {
66 if ( $params['mailpassword'] && !$params['email'] ) {
67 $this->dieUsageMsg( 'noemail' );
70 if ( $params['language'] && !Language
::isSupportedLanguage( $params['language'] ) ) {
71 $this->dieUsage( 'Invalid language parameter', 'langinvalid' );
74 $context = new DerivativeContext( $this->getContext() );
75 $context->setRequest( new DerivativeRequest(
76 $this->getContext()->getRequest(),
79 'uselang' => $params['language'],
80 'wpName' => $params['name'],
81 'wpPassword' => $params['password'],
82 'wpRetype' => $params['password'],
83 'wpDomain' => $params['domain'],
84 'wpEmail' => $params['email'],
85 'wpRealName' => $params['realname'],
86 'wpCreateaccountToken' => $params['token'],
87 'wpCreateaccount' => $params['mailpassword'] ?
null : '1',
88 'wpCreateaccountMail' => $params['mailpassword'] ?
'1' : null
92 $loginForm = new LoginForm();
93 $loginForm->setContext( $context );
94 Hooks
::run( 'AddNewAccountApiForm', array( $this, $loginForm ) );
97 $status = $loginForm->addNewaccountInternal();
99 if ( $status->isGood() ) {
101 $user = $status->getValue();
103 if ( $params['language'] ) {
104 $user->setOption( 'language', $params['language'] );
107 if ( $params['mailpassword'] ) {
108 // If mailpassword was set, disable the password and send an email.
109 $user->setPassword( null );
110 $status->merge( $loginForm->mailPasswordInternal(
113 'createaccount-title',
116 } elseif ( $this->getConfig()->get( 'EmailAuthentication' ) && Sanitizer
::validateEmail( $user->getEmail() ) ) {
117 // Send out an email authentication message if needed
118 $status->merge( $user->sendConfirmationMail() );
121 // Save settings (including confirmation token)
122 $user->saveSettings();
124 Hooks
::run( 'AddNewAccount', array( $user, $params['mailpassword'] ) );
126 if ( $params['mailpassword'] ) {
127 $logAction = 'byemail';
128 } elseif ( $this->getUser()->isLoggedIn() ) {
129 $logAction = 'create2';
131 $logAction = 'create';
133 $user->addNewUserLogEntry( $logAction, (string)$params['reason'] );
135 // Add username, id, and token to result.
136 $result['username'] = $user->getName();
137 $result['userid'] = $user->getId();
138 $result['token'] = $user->getToken();
141 $apiResult = $this->getResult();
143 if ( $status->hasMessage( 'sessionfailure' ) ||
$status->hasMessage( 'nocookiesfornew' ) ) {
144 // Token was incorrect, so add it to result, but don't throw an exception
145 // since not having the correct token is part of the normal
147 $result['token'] = LoginForm
::getCreateaccountToken();
148 $result['result'] = 'NeedToken';
149 } elseif ( !$status->isOK() ) {
150 // There was an error. Die now.
151 $this->dieStatus( $status );
152 } elseif ( !$status->isGood() ) {
153 // Status is not good, but OK. This means warnings.
154 $result['result'] = 'Warning';
156 // Add any warnings to the result
157 $warnings = $status->getErrorsByType( 'warning' );
159 foreach ( $warnings as &$warning ) {
160 ApiResult
::setIndexedTagName( $warning['params'], 'param' );
162 ApiResult
::setIndexedTagName( $warnings, 'warning' );
163 $result['warnings'] = $warnings;
166 // Everything was fine.
167 $result['result'] = 'Success';
170 // Give extensions a chance to modify the API result data
171 Hooks
::run( 'AddNewAccountApiResult', array( $this, $loginForm, &$result ) );
173 $apiResult->addValue( null, 'createaccount', $result );
176 public function mustBePosted() {
180 public function isReadMode() {
184 public function isWriteMode() {
188 public function getAllowedParams() {
191 ApiBase
::PARAM_TYPE
=> 'user',
192 ApiBase
::PARAM_REQUIRED
=> true
195 ApiBase
::PARAM_TYPE
=> 'password',
200 ApiBase
::PARAM_TYPE
=> 'string',
201 ApiBase
::PARAM_REQUIRED
=> $this->getConfig()->get( 'EmailConfirmToEdit' ),
204 'mailpassword' => array(
205 ApiBase
::PARAM_TYPE
=> 'boolean',
206 ApiBase
::PARAM_DFLT
=> false
213 protected function getExamplesMessages() {
215 'action=createaccount&name=testuser&password=test123'
216 => 'apihelp-createaccount-example-pass',
217 'action=createaccount&name=testmailuser&mailpassword=true&reason=MyReason'
218 => 'apihelp-createaccount-example-mail',
222 public function getHelpUrls() {
223 return 'https://www.mediawiki.org/wiki/API:Account_creation';