MediaWiki master
ApiAMCreateAccount.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Api;
10
14
21
22 private AuthManager $authManager;
23 private UrlUtils $urlUtils;
24
25 public function __construct(
26 ApiMain $main,
27 string $action,
28 AuthManager $authManager,
29 UrlUtils $urlUtils
30 ) {
31 parent::__construct( $main, $action, 'create' );
32 $this->authManager = $authManager;
33 $this->urlUtils = $urlUtils;
34 }
35
37 public function getFinalDescription() {
38 // A bit of a hack to append 'api-help-authmanager-general-usage'
39 $msgs = parent::getFinalDescription();
40 $msgs[] = $this->msg( 'api-help-authmanager-general-usage',
41 $this->getModulePrefix(),
42 $this->getModuleName(),
43 $this->getModulePath(),
44 AuthManager::ACTION_CREATE,
45 $this->needsToken(),
46 );
47 return $msgs;
48 }
49
50 public function execute() {
51 $params = $this->extractRequestParams();
52 $performer = $this->getUser();
53
54 $this->requireAtLeastOneParameter( $params, 'continue', 'returnurl' );
55
56 if ( $params['returnurl'] !== null ) {
57 $bits = $this->urlUtils->parse( $params['returnurl'] );
58 if ( !$bits || $bits['scheme'] === '' ) {
59 $encParamName = $this->encodeParamName( 'returnurl' );
60 $this->dieWithError(
61 [ 'apierror-badurl', $encParamName, wfEscapeWikiText( $params['returnurl'] ) ],
62 "badurl_{$encParamName}"
63 );
64 }
65 }
66
67 $helper = new ApiAuthManagerHelper( $this, $this->authManager );
68
69 // Make sure it's possible to create accounts
70 if ( !$this->authManager->canCreateAccounts() ) {
71 $res = AuthenticationResponse::newFail( $this->msg( 'userlogin-cannot-' . AuthManager::ACTION_CREATE ) );
72 $this->getResult()->addValue( null, 'createaccount',
73 $helper->formatAuthenticationResponse( $res ) );
74 $helper->logAuthenticationResult( 'accountcreation', $performer, $res );
75 return;
76 }
77
78 // Perform the create step
79 if ( $params['continue'] ) {
80 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_CREATE_CONTINUE );
81 $res = $this->authManager->continueAccountCreation( $reqs );
82 } else {
83 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_CREATE );
84 if ( $params['preservestate'] ) {
85 $req = $helper->getPreservedRequest();
86 if ( $req ) {
87 $reqs[] = $req;
88 }
89 }
90 $res = $this->authManager->beginAccountCreation(
91 $this->getAuthority(),
92 $reqs,
93 $params['returnurl']
94 );
95 }
96
97 $this->getResult()->addValue( null, 'createaccount',
98 $helper->formatAuthenticationResponse( $res ) );
99 $helper->logAuthenticationResult( 'accountcreation', $performer, $res );
100 }
101
103 public function isReadMode() {
104 return false;
105 }
106
108 public function isWriteMode() {
109 return true;
110 }
111
113 public function needsToken() {
114 return 'createaccount';
115 }
116
118 public function getAllowedParams() {
119 $ret = ApiAuthManagerHelper::getStandardParams( AuthManager::ACTION_CREATE,
120 'requests', 'messageformat', 'mergerequestfields', 'preservestate', 'returnurl', 'continue'
121 );
122 $ret['preservestate'][ApiBase::PARAM_HELP_MSG_APPEND][] =
123 'apihelp-createaccount-param-preservestate';
124 return $ret;
125 }
126
129 return [ 'api-help-authmanagerhelper-additional-params', AuthManager::ACTION_CREATE ];
130 }
131
133 protected function getExamplesMessages() {
134 return [
135 'action=createaccount&username=Example&password=ExamplePassword&retype=ExamplePassword'
136 . '&createreturnurl=http://example.org/&createtoken=123ABC'
137 => 'apihelp-createaccount-example-create',
138 ];
139 }
140
142 public function getHelpUrls() {
143 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Account_creation';
144 }
145}
146
148class_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...
__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.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...
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:61
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1511
getModulePrefix()
Get parameter prefix (usually two letters or an empty string).
Definition ApiBase.php:552
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:543
requireAtLeastOneParameter( $params,... $required)
Die if 0 of a certain set of parameters is set and not false.
Definition ApiBase.php:1025
getModulePath()
Get the path to this module.
Definition ApiBase.php:622
getResult()
Get the result object.
Definition ApiBase.php:682
const PARAM_HELP_MSG_APPEND
((string|array|Message)[]) Specify additional i18n messages to append to the normal message for this ...
Definition ApiBase.php:175
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition ApiBase.php:801
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:823
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