MediaWiki master
ApiAMCreateAccount.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Api;
10
14
21
22 public function __construct(
23 ApiMain $main,
24 string $action,
25 private readonly AuthManager $authManager,
26 private readonly UrlUtils $urlUtils,
27 ) {
28 parent::__construct( $main, $action, 'create' );
29 }
30
32 public function getFinalDescription() {
33 // A bit of a hack to append 'api-help-authmanager-general-usage'
34 $msgs = parent::getFinalDescription();
35 $msgs[] = $this->msg( 'api-help-authmanager-general-usage',
36 $this->getModulePrefix(),
37 $this->getModuleName(),
38 $this->getModulePath(),
39 AuthManager::ACTION_CREATE,
40 $this->needsToken(),
41 );
42 return $msgs;
43 }
44
45 public function execute() {
46 $params = $this->extractRequestParams();
47 $performer = $this->getUser();
48
49 $this->requireAtLeastOneParameter( $params, 'continue', 'returnurl' );
50
51 if ( $params['returnurl'] !== null ) {
52 $bits = $this->urlUtils->parse( $params['returnurl'] );
53 if ( !$bits || $bits['scheme'] === '' ) {
54 $encParamName = $this->encodeParamName( 'returnurl' );
55 $this->dieWithError(
56 [ 'apierror-badurl', $encParamName, wfEscapeWikiText( $params['returnurl'] ) ],
57 "badurl_{$encParamName}"
58 );
59 }
60 }
61
62 $helper = new ApiAuthManagerHelper( $this, $this->authManager );
63
64 // Make sure it's possible to create accounts
65 if ( !$this->authManager->canCreateAccounts() ) {
66 $res = AuthenticationResponse::newFail( $this->msg( 'userlogin-cannot-' . AuthManager::ACTION_CREATE ) );
67 $this->getResult()->addValue( null, 'createaccount',
68 $helper->formatAuthenticationResponse( $res ) );
69 $helper->logAuthenticationResult( 'accountcreation', $performer, $res );
70 return;
71 }
72
73 // Perform the create step
74 if ( $params['continue'] ) {
75 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_CREATE_CONTINUE );
76 $res = $this->authManager->continueAccountCreation( $reqs );
77 } else {
78 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_CREATE );
79 if ( $params['preservestate'] ) {
80 $req = $helper->getPreservedRequest();
81 if ( $req ) {
82 $reqs[] = $req;
83 }
84 }
85 $res = $this->authManager->beginAccountCreation(
86 $this->getAuthority(),
87 $reqs,
88 $params['returnurl']
89 );
90 }
91
92 $this->getResult()->addValue( null, 'createaccount',
93 $helper->formatAuthenticationResponse( $res ) );
94 $helper->logAuthenticationResult( 'accountcreation', $performer, $res );
95 }
96
98 public function isReadMode() {
99 return false;
100 }
101
103 public function isWriteMode() {
104 return true;
105 }
106
108 public function needsToken() {
109 return 'createaccount';
110 }
111
113 public function getAllowedParams() {
114 $ret = ApiAuthManagerHelper::getStandardParams( AuthManager::ACTION_CREATE,
115 'requests', 'messageformat', 'mergerequestfields', 'preservestate', 'returnurl', 'continue'
116 );
117 $ret['preservestate'][ApiBase::PARAM_HELP_MSG_APPEND][] =
118 'apihelp-createaccount-param-preservestate';
119 return $ret;
120 }
121
124 return [ 'api-help-authmanagerhelper-additional-params', AuthManager::ACTION_CREATE ];
125 }
126
128 protected function getExamplesMessages() {
129 return [
130 'action=createaccount&username=Example&password=ExamplePassword&retype=ExamplePassword'
131 . '&createreturnurl=http://example.org/&createtoken=123ABC'
132 => 'apihelp-createaccount-example-create',
133 ];
134 }
135
137 public function getHelpUrls() {
138 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Account_creation';
139 }
140}
141
143class_alias( ApiAMCreateAccount::class, 'ApiAMCreateAccount' );
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Create an account with AuthManager.
isReadMode()
Indicates whether this module requires read rights.to override bool
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
isWriteMode()
Indicates whether this module requires write access to the wiki.API modules must override this method...
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.Modules are strongly encouraged to us...
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.1.25, returning boolean false is deprecated...
getFinalDescription()
Get the final module description, after hooks have had a chance to tweak it as needed....
getExamplesMessages()
Returns usage examples for this module.Return value has query strings as keys, with values being eith...
__construct(ApiMain $main, string $action, private readonly AuthManager $authManager, private readonly UrlUtils $urlUtils,)
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:60
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1522
getModulePrefix()
Get parameter prefix (usually two letters or an empty string).
Definition ApiBase.php:566
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:557
requireAtLeastOneParameter( $params,... $required)
Die if 0 of a certain set of parameters is set and not false.
Definition ApiBase.php:1039
getModulePath()
Get the path to this module.
Definition ApiBase.php:636
getResult()
Get the result object.
Definition ApiBase.php:696
const PARAM_HELP_MSG_APPEND
((string|array|Message)[]) Specify additional i18n messages to append to the normal message for this ...
Definition ApiBase.php:174
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition ApiBase.php:815
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:837
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:66
AuthManager is the authentication system in MediaWiki and serves entry point for authentication.
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