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 // Make sure session is persisted
63 MediaWiki\Session\SessionManager
::getGlobalSession()->persist();
65 if ( $params['mailpassword'] && !$params['email'] ) {
66 $this->dieUsageMsg( 'noemail' );
69 if ( $params['language'] && !Language
::isSupportedLanguage( $params['language'] ) ) {
70 $this->dieUsage( 'Invalid language parameter', 'langinvalid' );
73 $context = new DerivativeContext( $this->getContext() );
74 $context->setRequest( new DerivativeRequest(
75 $this->getContext()->getRequest(),
78 'uselang' => $params['language'],
79 'wpName' => $params['name'],
80 'wpPassword' => $params['password'],
81 'wpRetype' => $params['password'],
82 'wpDomain' => $params['domain'],
83 'wpEmail' => $params['email'],
84 'wpRealName' => $params['realname'],
85 'wpCreateaccountToken' => $params['token'],
86 'wpCreateaccount' => $params['mailpassword'] ?
null : '1',
87 'wpCreateaccountMail' => $params['mailpassword'] ?
'1' : null
91 $loginForm = new LoginForm();
92 $loginForm->setContext( $context );
93 Hooks
::run( 'AddNewAccountApiForm', array( $this, $loginForm ) );
96 $status = $loginForm->addNewAccountInternal();
97 LoggerFactory
::getInstance( 'authmanager' )->info( 'Account creation attempt via API', array(
98 'event' => 'accountcreation',
102 if ( $status->isGood() ) {
104 $user = $status->getValue();
106 if ( $params['language'] ) {
107 $user->setOption( 'language', $params['language'] );
110 if ( $params['mailpassword'] ) {
111 // If mailpassword was set, disable the password and send an email.
112 $user->setPassword( null );
113 $status->merge( $loginForm->mailPasswordInternal(
116 'createaccount-title',
119 } elseif ( $this->getConfig()->get( 'EmailAuthentication' ) &&
120 Sanitizer
::validateEmail( $user->getEmail() )
122 // Send out an email authentication message if needed
123 $status->merge( $user->sendConfirmationMail() );
126 // Save settings (including confirmation token)
127 $user->saveSettings();
129 Hooks
::run( 'AddNewAccount', array( $user, $params['mailpassword'] ) );
131 if ( $params['mailpassword'] ) {
132 $logAction = 'byemail';
133 } elseif ( $this->getUser()->isLoggedIn() ) {
134 $logAction = 'create2';
136 $logAction = 'create';
138 $user->addNewUserLogEntry( $logAction, (string)$params['reason'] );
140 // Add username, id, and token to result.
141 $result['username'] = $user->getName();
142 $result['userid'] = $user->getId();
143 $result['token'] = $user->getToken();
146 $apiResult = $this->getResult();
148 if ( $status->hasMessage( 'sessionfailure' ) ||
$status->hasMessage( 'nocookiesfornew' ) ) {
149 // Token was incorrect, so add it to result, but don't throw an exception
150 // since not having the correct token is part of the normal
152 $result['token'] = LoginForm
::getCreateaccountToken();
153 $result['result'] = 'NeedToken';
154 } elseif ( !$status->isOK() ) {
155 // There was an error. Die now.
156 $this->dieStatus( $status );
157 } elseif ( !$status->isGood() ) {
158 // Status is not good, but OK. This means warnings.
159 $result['result'] = 'Warning';
161 // Add any warnings to the result
162 $warnings = $status->getErrorsByType( 'warning' );
164 foreach ( $warnings as &$warning ) {
165 ApiResult
::setIndexedTagName( $warning['params'], 'param' );
167 ApiResult
::setIndexedTagName( $warnings, 'warning' );
168 $result['warnings'] = $warnings;
171 // Everything was fine.
172 $result['result'] = 'Success';
175 // Give extensions a chance to modify the API result data
176 Hooks
::run( 'AddNewAccountApiResult', array( $this, $loginForm, &$result ) );
178 $apiResult->addValue( null, 'createaccount', $result );
181 public function mustBePosted() {
185 public function isReadMode() {
189 public function isWriteMode() {
193 public function getAllowedParams() {
196 ApiBase
::PARAM_TYPE
=> 'user',
197 ApiBase
::PARAM_REQUIRED
=> true
200 ApiBase
::PARAM_TYPE
=> 'password',
205 ApiBase
::PARAM_TYPE
=> 'string',
206 ApiBase
::PARAM_REQUIRED
=> $this->getConfig()->get( 'EmailConfirmToEdit' ),
209 'mailpassword' => array(
210 ApiBase
::PARAM_TYPE
=> 'boolean',
211 ApiBase
::PARAM_DFLT
=> false
218 protected function getExamplesMessages() {
220 'action=createaccount&name=testuser&password=test123'
221 => 'apihelp-createaccount-example-pass',
222 'action=createaccount&name=testmailuser&mailpassword=true&reason=MyReason'
223 => 'apihelp-createaccount-example-mail',
227 public function getHelpUrls() {
228 return 'https://www.mediawiki.org/wiki/API:Account_creation';