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() ) {
51 $this->dieUsage( 'You cannot create a new account because you are blocked', 'blocked' );
54 $params = $this->extractRequestParams();
56 // Init session if necessary
57 if ( session_id() == '' ) {
61 if ( $params['mailpassword'] && !$params['email'] ) {
62 $this->dieUsageMsg( 'noemail' );
65 if ( $params['language'] && !Language
::isSupportedLanguage( $params['language'] ) ) {
66 $this->dieUsage( 'Invalid language parameter', 'langinvalid' );
69 $context = new DerivativeContext( $this->getContext() );
70 $context->setRequest( new DerivativeRequest(
71 $this->getContext()->getRequest(),
74 'uselang' => $params['language'],
75 'wpName' => $params['name'],
76 'wpPassword' => $params['password'],
77 'wpRetype' => $params['password'],
78 'wpDomain' => $params['domain'],
79 'wpEmail' => $params['email'],
80 'wpRealName' => $params['realname'],
81 'wpCreateaccountToken' => $params['token'],
82 'wpCreateaccount' => $params['mailpassword'] ?
null : '1',
83 'wpCreateaccountMail' => $params['mailpassword'] ?
'1' : null
87 $loginForm = new LoginForm();
88 $loginForm->setContext( $context );
89 Hooks
::run( 'AddNewAccountApiForm', array( $this, $loginForm ) );
92 $status = $loginForm->addNewaccountInternal();
94 if ( $status->isGood() ) {
96 $user = $status->getValue();
98 if ( $params['language'] ) {
99 $user->setOption( 'language', $params['language'] );
102 if ( $params['mailpassword'] ) {
103 // If mailpassword was set, disable the password and send an email.
104 $user->setPassword( null );
105 $status->merge( $loginForm->mailPasswordInternal(
108 'createaccount-title',
111 } elseif ( $this->getConfig()->get( 'EmailAuthentication' ) && Sanitizer
::validateEmail( $user->getEmail() ) ) {
112 // Send out an email authentication message if needed
113 $status->merge( $user->sendConfirmationMail() );
116 // Save settings (including confirmation token)
117 $user->saveSettings();
119 Hooks
::run( 'AddNewAccount', array( $user, $params['mailpassword'] ) );
121 if ( $params['mailpassword'] ) {
122 $logAction = 'byemail';
123 } elseif ( $this->getUser()->isLoggedIn() ) {
124 $logAction = 'create2';
126 $logAction = 'create';
128 $user->addNewUserLogEntry( $logAction, (string)$params['reason'] );
130 // Add username, id, and token to result.
131 $result['username'] = $user->getName();
132 $result['userid'] = $user->getId();
133 $result['token'] = $user->getToken();
136 $apiResult = $this->getResult();
138 if ( $status->hasMessage( 'sessionfailure' ) ||
$status->hasMessage( 'nocookiesfornew' ) ) {
139 // Token was incorrect, so add it to result, but don't throw an exception
140 // since not having the correct token is part of the normal
142 $result['token'] = LoginForm
::getCreateaccountToken();
143 $result['result'] = 'NeedToken';
144 } elseif ( !$status->isOK() ) {
145 // There was an error. Die now.
146 $this->dieStatus( $status );
147 } elseif ( !$status->isGood() ) {
148 // Status is not good, but OK. This means warnings.
149 $result['result'] = 'Warning';
151 // Add any warnings to the result
152 $warnings = $status->getErrorsByType( 'warning' );
154 foreach ( $warnings as &$warning ) {
155 $apiResult->setIndexedTagName( $warning['params'], 'param' );
157 $apiResult->setIndexedTagName( $warnings, 'warning' );
158 $result['warnings'] = $warnings;
161 // Everything was fine.
162 $result['result'] = 'Success';
165 // Give extensions a chance to modify the API result data
166 Hooks
::run( 'AddNewAccountApiResult', array( $this, $loginForm, &$result ) );
168 $apiResult->addValue( null, 'createaccount', $result );
171 public function mustBePosted() {
175 public function isReadMode() {
179 public function isWriteMode() {
183 public function getAllowedParams() {
186 ApiBase
::PARAM_TYPE
=> 'user',
187 ApiBase
::PARAM_REQUIRED
=> true
193 ApiBase
::PARAM_TYPE
=> 'string',
194 ApiBase
::PARAM_REQUIRED
=> $this->getConfig()->get( 'EmailConfirmToEdit' ),
197 'mailpassword' => array(
198 ApiBase
::PARAM_TYPE
=> 'boolean',
199 ApiBase
::PARAM_DFLT
=> false
206 protected function getExamplesMessages() {
208 'action=createaccount&name=testuser&password=test123'
209 => 'apihelp-createaccount-example-pass',
210 'action=createaccount&name=testmailuser&mailpassword=true&reason=MyReason'
211 => 'apihelp-createaccount-example-mail',
215 public function getHelpUrls() {
216 return 'https://www.mediawiki.org/wiki/API:Account_creation';