MediaWiki master
ApiAMCreateAccount.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Api;
24
28
35
36 private AuthManager $authManager;
37 private UrlUtils $urlUtils;
38
39 public function __construct(
40 ApiMain $main,
41 string $action,
42 AuthManager $authManager,
43 UrlUtils $urlUtils
44 ) {
45 parent::__construct( $main, $action, 'create' );
46 $this->authManager = $authManager;
47 $this->urlUtils = $urlUtils;
48 }
49
50 public function getFinalDescription() {
51 // A bit of a hack to append 'api-help-authmanager-general-usage'
52 $msgs = parent::getFinalDescription();
53 $msgs[] = $this->msg( 'api-help-authmanager-general-usage',
54 $this->getModulePrefix(),
55 $this->getModuleName(),
56 $this->getModulePath(),
57 AuthManager::ACTION_CREATE,
58 $this->needsToken(),
59 );
60 return $msgs;
61 }
62
63 public function execute() {
65 $performer = $this->getUser();
66
67 $this->requireAtLeastOneParameter( $params, 'continue', 'returnurl' );
68
69 if ( $params['returnurl'] !== null ) {
70 $bits = $this->urlUtils->parse( $params['returnurl'] );
71 if ( !$bits || $bits['scheme'] === '' ) {
72 $encParamName = $this->encodeParamName( 'returnurl' );
73 $this->dieWithError(
74 [ 'apierror-badurl', $encParamName, wfEscapeWikiText( $params['returnurl'] ) ],
75 "badurl_{$encParamName}"
76 );
77 }
78 }
79
80 $helper = new ApiAuthManagerHelper( $this, $this->authManager );
81
82 // Make sure it's possible to create accounts
83 if ( !$this->authManager->canCreateAccounts() ) {
84 $res = AuthenticationResponse::newFail( $this->msg( 'userlogin-cannot-' . AuthManager::ACTION_CREATE ) );
85 $this->getResult()->addValue( null, 'createaccount',
86 $helper->formatAuthenticationResponse( $res ) );
87 $helper->logAuthenticationResult( 'accountcreation', $performer, $res );
88 return;
89 }
90
91 // Perform the create step
92 if ( $params['continue'] ) {
93 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_CREATE_CONTINUE );
94 $res = $this->authManager->continueAccountCreation( $reqs );
95 } else {
96 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_CREATE );
97 if ( $params['preservestate'] ) {
98 $req = $helper->getPreservedRequest();
99 if ( $req ) {
100 $reqs[] = $req;
101 }
102 }
103 $res = $this->authManager->beginAccountCreation(
104 $this->getAuthority(),
105 $reqs,
106 $params['returnurl']
107 );
108 }
109
110 $this->getResult()->addValue( null, 'createaccount',
111 $helper->formatAuthenticationResponse( $res ) );
112 $helper->logAuthenticationResult( 'accountcreation', $performer, $res );
113 }
114
115 public function isReadMode() {
116 return false;
117 }
118
119 public function isWriteMode() {
120 return true;
121 }
122
123 public function needsToken() {
124 return 'createaccount';
125 }
126
127 public function getAllowedParams() {
128 $ret = ApiAuthManagerHelper::getStandardParams( AuthManager::ACTION_CREATE,
129 'requests', 'messageformat', 'mergerequestfields', 'preservestate', 'returnurl', 'continue'
130 );
131 $ret['preservestate'][ApiBase::PARAM_HELP_MSG_APPEND][] =
132 'apihelp-createaccount-param-preservestate';
133 return $ret;
134 }
135
137 return [ 'api-help-authmanagerhelper-additional-params', AuthManager::ACTION_CREATE ];
138 }
139
140 protected function getExamplesMessages() {
141 return [
142 'action=createaccount&username=Example&password=ExamplePassword&retype=ExamplePassword'
143 . '&createreturnurl=http://example.org/&createtoken=123ABC'
144 => 'apihelp-createaccount-example-create',
145 ];
146 }
147
148 public function getHelpUrls() {
149 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Account_creation';
150 }
151}
152
154class_alias( ApiAMCreateAccount::class, 'ApiAMCreateAccount' );
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
array $params
The job parameters.
Create an account with AuthManager.
isReadMode()
Indicates whether this module requires read rights.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
isWriteMode()
Indicates whether this module requires write access to the wiki.
__construct(ApiMain $main, string $action, AuthManager $authManager, UrlUtils $urlUtils)
dynamicParameterDocumentation()
Indicate if the module supports dynamically-determined parameters that cannot be included in self::ge...
needsToken()
Returns the token type this module requires in order to execute.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getHelpUrls()
Return links to more detailed help pages about the module.
getFinalDescription()
Get the final module description, after hooks have had a chance to tweak it as needed.
getExamplesMessages()
Returns usage examples for this module.
Helper class for AuthManager-using API modules.
static getStandardParams( $action,... $wantedParams)
Fetch the standard parameters this helper recognizes.
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:76
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1531
getModulePrefix()
Get parameter prefix (usually two letters or an empty string).
Definition ApiBase.php:580
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:571
requireAtLeastOneParameter( $params,... $required)
Die if 0 of a certain set of parameters is set and not false.
Definition ApiBase.php:1050
getModulePath()
Get the path to this module.
Definition ApiBase.php:650
getResult()
Get the result object.
Definition ApiBase.php:710
const PARAM_HELP_MSG_APPEND
((string|array|Message)[]) Specify additional i18n messages to append to the normal message for this ...
Definition ApiBase.php:192
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition ApiBase.php:829
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:851
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:78
This serves as the entry point to the authentication system.
This is a value object to hold authentication response data.
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
A service to expand, parse, and otherwise manipulate URLs.
Definition UrlUtils.php:16