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
24 use MediaWiki\Logger\LoggerFactory
;
27 * Unit to authenticate account registration attempts to the current wiki.
31 class ApiCreateAccount
extends ApiBase
{
32 public function execute() {
33 // If we're in a mode that breaks the same-origin policy, no tokens can
35 if ( $this->lacksSameOriginSecurity() ) {
37 'Cannot create account when the same-origin policy is not applied', 'aborted'
41 // $loginForm->addNewaccountInternal will throw exceptions
42 // if wiki is read only (already handled by api), user is blocked or does not have rights.
43 // Use userCan in order to hit GlobalBlock checks (according to Special:userlogin)
44 $loginTitle = SpecialPage
::getTitleFor( 'Userlogin' );
45 if ( !$loginTitle->userCan( 'createaccount', $this->getUser() ) ) {
47 'You do not have the right to create a new account',
48 'permdenied-createaccount'
51 if ( $this->getUser()->isBlockedFromCreateAccount() ) {
53 'You cannot create a new account because you are blocked',
56 array( 'blockinfo' => ApiQueryUserInfo
::getBlockInfo( $this->getUser()->getBlock() ) )
60 $params = $this->extractRequestParams();
62 // Init session if necessary
63 if ( session_id() == '' ) {
67 if ( $params['mailpassword'] && !$params['email'] ) {
68 $this->dieUsageMsg( 'noemail' );
71 if ( $params['language'] && !Language
::isSupportedLanguage( $params['language'] ) ) {
72 $this->dieUsage( 'Invalid language parameter', 'langinvalid' );
75 $context = new DerivativeContext( $this->getContext() );
76 $context->setRequest( new DerivativeRequest(
77 $this->getContext()->getRequest(),
80 'uselang' => $params['language'],
81 'wpName' => $params['name'],
82 'wpPassword' => $params['password'],
83 'wpRetype' => $params['password'],
84 'wpDomain' => $params['domain'],
85 'wpEmail' => $params['email'],
86 'wpRealName' => $params['realname'],
87 'wpCreateaccountToken' => $params['token'],
88 'wpCreateaccount' => $params['mailpassword'] ?
null : '1',
89 'wpCreateaccountMail' => $params['mailpassword'] ?
'1' : null
93 $loginForm = new LoginForm();
94 $loginForm->setContext( $context );
95 Hooks
::run( 'AddNewAccountApiForm', array( $this, $loginForm ) );
98 $status = $loginForm->addNewAccountInternal();
99 LoggerFactory
::getInstance( 'authmanager' )->info( 'Account creation attempt via API', array(
100 'event' => 'accountcreation',
104 if ( $status->isGood() ) {
106 $user = $status->getValue();
108 if ( $params['language'] ) {
109 $user->setOption( 'language', $params['language'] );
112 if ( $params['mailpassword'] ) {
113 // If mailpassword was set, disable the password and send an email.
114 $user->setPassword( null );
115 $status->merge( $loginForm->mailPasswordInternal(
118 'createaccount-title',
121 } elseif ( $this->getConfig()->get( 'EmailAuthentication' ) &&
122 Sanitizer
::validateEmail( $user->getEmail() )
124 // Send out an email authentication message if needed
125 $status->merge( $user->sendConfirmationMail() );
128 // Save settings (including confirmation token)
129 $user->saveSettings();
131 Hooks
::run( 'AddNewAccount', array( $user, $params['mailpassword'] ) );
133 if ( $params['mailpassword'] ) {
134 $logAction = 'byemail';
135 } elseif ( $this->getUser()->isLoggedIn() ) {
136 $logAction = 'create2';
138 $logAction = 'create';
140 $user->addNewUserLogEntry( $logAction, (string)$params['reason'] );
142 // Add username, id, and token to result.
143 $result['username'] = $user->getName();
144 $result['userid'] = $user->getId();
145 $result['token'] = $user->getToken();
148 $apiResult = $this->getResult();
150 if ( $status->hasMessage( 'sessionfailure' ) ||
$status->hasMessage( 'nocookiesfornew' ) ) {
151 // Token was incorrect, so add it to result, but don't throw an exception
152 // since not having the correct token is part of the normal
154 $result['token'] = LoginForm
::getCreateaccountToken();
155 $result['result'] = 'NeedToken';
156 } elseif ( !$status->isOK() ) {
157 // There was an error. Die now.
158 $this->dieStatus( $status );
159 } elseif ( !$status->isGood() ) {
160 // Status is not good, but OK. This means warnings.
161 $result['result'] = 'Warning';
163 // Add any warnings to the result
164 $warnings = $status->getErrorsByType( 'warning' );
166 foreach ( $warnings as &$warning ) {
167 ApiResult
::setIndexedTagName( $warning['params'], 'param' );
169 ApiResult
::setIndexedTagName( $warnings, 'warning' );
170 $result['warnings'] = $warnings;
173 // Everything was fine.
174 $result['result'] = 'Success';
177 // Give extensions a chance to modify the API result data
178 Hooks
::run( 'AddNewAccountApiResult', array( $this, $loginForm, &$result ) );
180 $apiResult->addValue( null, 'createaccount', $result );
183 public function mustBePosted() {
187 public function isReadMode() {
191 public function isWriteMode() {
195 public function getAllowedParams() {
198 ApiBase
::PARAM_TYPE
=> 'user',
199 ApiBase
::PARAM_REQUIRED
=> true
202 ApiBase
::PARAM_TYPE
=> 'password',
207 ApiBase
::PARAM_TYPE
=> 'string',
208 ApiBase
::PARAM_REQUIRED
=> $this->getConfig()->get( 'EmailConfirmToEdit' ),
211 'mailpassword' => array(
212 ApiBase
::PARAM_TYPE
=> 'boolean',
213 ApiBase
::PARAM_DFLT
=> false
220 protected function getExamplesMessages() {
222 'action=createaccount&name=testuser&password=test123'
223 => 'apihelp-createaccount-example-pass',
224 'action=createaccount&name=testmailuser&mailpassword=true&reason=MyReason'
225 => 'apihelp-createaccount-example-mail',
229 public function getHelpUrls() {
230 return 'https://www.mediawiki.org/wiki/API:Account_creation';