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 $params = $this->extractRequestParams();
36 // Init session if necessary
37 if ( session_id() == '' ) {
41 if( $params['mailpassword'] && !$params['email'] ) {
42 $this->dieUsageMsg( 'noemail' );
45 $context = new DerivativeContext( $this->getContext() );
46 $context->setRequest( new DerivativeRequest(
47 $this->getContext()->getRequest(),
50 'uselang' => $params['language'],
51 'wpName' => $params['name'],
52 'wpPassword' => $params['password'],
53 'wpRetype' => $params['password'],
54 'wpDomain' => $params['domain'],
55 'wpEmail' => $params['email'],
56 'wpRealName' => $params['realname'],
57 'wpCreateaccountToken' => $params['token'],
58 'wpCreateaccount' => $params['mailpassword'] ?
null : '1',
59 'wpCreateaccountMail' => $params['mailpassword'] ?
'1' : null
63 $loginForm = new LoginForm();
64 $loginForm->setContext( $context );
67 $status = $loginForm->addNewaccountInternal();
69 if( $status->isGood() ) {
71 $user = $status->getValue();
73 // If we showed up language selection links, and one was in use, be
74 // smart (and sensible) and save that language as the user's preference
75 global $wgLoginLanguageSelector, $wgEmailAuthentication;
76 if( $wgLoginLanguageSelector && $params['language'] ) {
77 $user->setOption( 'language', $params['language'] );
80 if( $params['mailpassword'] ) {
81 // If mailpassword was set, disable the password and send an email.
82 $user->setPassword( null );
83 $status->merge( $loginForm->mailPasswordInternal( $user, false, 'createaccount-title', 'createaccount-text' ) );
84 } elseif( $wgEmailAuthentication && Sanitizer
::validateEmail( $user->getEmail() ) ) {
85 // Send out an email authentication message if needed
86 $status->merge( $user->sendConfirmationMail() );
89 // Save settings (including confirmation token)
90 $user->saveSettings();
92 wfRunHooks( 'AddNewAccount', array( $user, $params['mailpassword'] ) );
94 if ( $params['mailpassword'] ) {
95 $logAction = 'byemail';
96 } elseif ( $this->getUser()->isLoggedIn() ) {
97 $logAction = 'create2';
99 $logAction = 'create';
101 $user->addNewUserLogEntry( $logAction, (string)$params['reason'] );
103 // Add username, id, and token to result.
104 $result['username'] = $user->getName();
105 $result['userid'] = $user->getId();
106 $result['token'] = $user->getToken();
109 $apiResult = $this->getResult();
111 if( $status->hasMessage( 'sessionfailure' ) ) {
112 // Token was incorrect, so add it to result, but don't throw an exception.
113 $result['token'] = LoginForm
::getCreateaccountToken();
114 $result['result'] = 'needtoken';
115 } elseif( !$status->isOK() ) {
116 // There was an error. Die now.
117 // Cannot use dieUsageMsg() directly because extensions
118 // might return custom error messages.
119 $errors = $status->getErrorsArray();
120 if( $errors[0] instanceof Message
) {
124 $code = array_shift( $errors[0] );
125 $desc = wfMessage( $code, $errors[0] );
127 $this->dieUsage( $desc, $code );
128 } elseif( !$status->isGood() ) {
129 // Status is not good, but OK. This means warnings.
130 $result['result'] = 'warning';
132 // Add any warnings to the result
133 $warnings = $status->getErrorsByType( 'warning' );
135 foreach( $warnings as &$warning ) {
136 $apiResult->setIndexedTagName( $warning['params'], 'param' );
138 $apiResult->setIndexedTagName( $warnings, 'warning' );
139 $result['warnings'] = $warnings;
142 // Everything was fine.
143 $result['result'] = 'success';
146 $apiResult->addValue( null, 'createaccount', $result );
149 public function getDescription() {
150 return 'Create a new user account.';
153 public function mustBePosted() {
157 public function isReadMode() {
161 public function isWriteMode() {
165 public function getAllowedParams() {
166 global $wgEmailConfirmToEdit;
169 ApiBase
::PARAM_TYPE
=> 'user',
170 ApiBase
::PARAM_REQUIRED
=> true
176 ApiBase
::PARAM_TYPE
=> 'string',
177 ApiBase
::PARAM_REQUIRED
=> $wgEmailConfirmToEdit
180 'mailpassword' => array(
181 ApiBase
::PARAM_TYPE
=> 'boolean',
182 ApiBase
::PARAM_DFLT
=> false
189 public function getParamDescription() {
190 $p = $this->getModulePrefix();
192 'name' => 'Username',
193 'password' => "Password (ignored if {$p}mailpassword is set)",
194 'domain' => 'Domain for external authentication (optional)',
195 'token' => 'Account creation token obtained in first request',
196 'email' => 'Email address of user (optional)',
197 'realname' => 'Real name of user (optional)',
198 'mailpassword' => 'If set to any value, a random password will be emailed to the user',
199 'reason' => 'Optional reason for creating the account to be put in the logs',
200 'language' => 'Language code to set as default for the user (optional, defaults to content language)'
204 public function getResultProperties() {
206 'createaccount' => array(
208 ApiBase
::PROP_TYPE
=> array(
215 ApiBase
::PROP_TYPE
=> 'string',
216 ApiBase
::PROP_NULLABLE
=> true
219 ApiBase
::PROP_TYPE
=> 'int',
220 ApiBase
::PROP_NULLABLE
=> true
223 ApiBase
::PROP_TYPE
=> 'string',
224 ApiBase
::PROP_NULLABLE
=> true
230 public function getPossibleErrors() {
231 $localErrors = array(
234 'sorbs_create_account_reason',
237 'password-name-match',
238 'password-login-forbidden',
240 'invalidemailaddress',
244 $errors = parent
::getPossibleErrors();
245 // All local errors are from LoginForm, which means they're actually message keys.
246 foreach( $localErrors as $error ) {
247 $errors[] = array( 'code' => $error, 'info' => wfMessage( $error )->parse() );
250 // 'passwordtooshort' has parameters. :(
251 global $wgMinimalPasswordLength;
253 'code' => 'passwordtooshort',
254 'info' => wfMessage( 'passwordtooshort', $wgMinimalPasswordLength )->parse()
259 public function getExamples() {
261 'api.php?action=createaccount&name=testuser&password=test123',
262 'api.php?action=createaccount&name=testmailuser&mailpassword=true&reason=MyReason',
266 public function getHelpUrls() {
267 return 'https://www.mediawiki.org/wiki/API:Account_creation';