2ce532b920c958af0f3283cd6fadf56900bd46f2
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 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() ) ) {
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() == '' ) {
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(),
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
84 $loginForm = new LoginForm();
85 $loginForm->setContext( $context );
86 wfRunHooks( 'AddNewAccountApiForm', array( $this, $loginForm ) );
89 $status = $loginForm->addNewaccountInternal();
91 if ( $status->isGood() ) {
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(
105 'createaccount-title',
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 wfRunHooks( 'AddNewAccount', array( $user, $params['mailpassword'] ) );
118 if ( $params['mailpassword'] ) {
119 $logAction = 'byemail';
120 } elseif ( $this->getUser()->isLoggedIn() ) {
121 $logAction = 'create2';
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
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' );
151 foreach ( $warnings as &$warning ) {
152 $apiResult->setIndexedTagName( $warning['params'], 'param' );
154 $apiResult->setIndexedTagName( $warnings, 'warning' );
155 $result['warnings'] = $warnings;
158 // Everything was fine.
159 $result['result'] = 'Success';
162 // Give extensions a chance to modify the API result data
163 wfRunHooks( 'AddNewAccountApiResult', array( $this, $loginForm, &$result ) );
165 $apiResult->addValue( null, 'createaccount', $result );
168 public function getDescription() {
169 return 'Create a new user account.';
172 public function mustBePosted() {
176 public function isReadMode() {
180 public function isWriteMode() {
184 public function getAllowedParams() {
187 ApiBase
::PARAM_TYPE
=> 'user',
188 ApiBase
::PARAM_REQUIRED
=> true
194 ApiBase
::PARAM_TYPE
=> 'string',
195 ApiBase
::PARAM_REQUIRED
=> $this->getConfig()->get( 'EmailConfirmToEdit' ),
198 'mailpassword' => array(
199 ApiBase
::PARAM_TYPE
=> 'boolean',
200 ApiBase
::PARAM_DFLT
=> false
207 public function getParamDescription() {
208 $p = $this->getModulePrefix();
211 'name' => 'Username',
212 'password' => "Password (ignored if {$p}mailpassword is set)",
213 'domain' => 'Domain for external authentication (optional)',
214 'token' => 'Account creation token obtained in first request',
215 'email' => 'Email address of user (optional)',
216 'realname' => 'Real name of user (optional)',
217 'mailpassword' => 'If set to any value, a random password will be emailed to the user',
218 'reason' => 'Optional reason for creating the account to be put in the logs',
220 => 'Language code to set as default for the user (optional, defaults to content language)'
224 public function getExamples() {
226 'api.php?action=createaccount&name=testuser&password=test123',
227 'api.php?action=createaccount&name=testmailuser&mailpassword=true&reason=MyReason',
231 public function getHelpUrls() {
232 return 'https://www.mediawiki.org/wiki/API:Account_creation';