MediaWiki master
ApiAMCreateAccount.php
Go to the documentation of this file.
1<?php
25
32
33 private AuthManager $authManager;
34
40 public function __construct(
41 ApiMain $main,
42 $action,
43 AuthManager $authManager
44 ) {
45 parent::__construct( $main, $action, 'create' );
46 $this->authManager = $authManager;
47 }
48
49 public function getFinalDescription() {
50 // A bit of a hack to append 'api-help-authmanager-general-usage'
51 $msgs = parent::getFinalDescription();
52 $msgs[] = ApiBase::makeMessage( 'api-help-authmanager-general-usage', $this->getContext(), [
53 $this->getModulePrefix(),
54 $this->getModuleName(),
55 $this->getModulePath(),
56 AuthManager::ACTION_CREATE,
57 $this->needsToken(),
58 ] );
59 return $msgs;
60 }
61
62 public function execute() {
64
65 $this->requireAtLeastOneParameter( $params, 'continue', 'returnurl' );
66
67 if ( $params['returnurl'] !== null ) {
68 $bits = wfParseUrl( $params['returnurl'] );
69 if ( !$bits || $bits['scheme'] === '' ) {
70 $encParamName = $this->encodeParamName( 'returnurl' );
71 $this->dieWithError(
72 [ 'apierror-badurl', $encParamName, wfEscapeWikiText( $params['returnurl'] ) ],
73 "badurl_{$encParamName}"
74 );
75 }
76 }
77
78 $helper = new ApiAuthManagerHelper( $this, $this->authManager );
79
80 // Make sure it's possible to create accounts
81 if ( !$this->authManager->canCreateAccounts() ) {
82 $res = AuthenticationResponse::newFail( $this->msg( 'userlogin-cannot-' . AuthManager::ACTION_CREATE ) );
83 $this->getResult()->addValue( null, 'createaccount',
84 $helper->formatAuthenticationResponse( $res ) );
85 $helper->logAuthenticationResult( 'accountcreation', $res );
86 return;
87 }
88
89 // Perform the create step
90 if ( $params['continue'] ) {
91 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_CREATE_CONTINUE );
92 $res = $this->authManager->continueAccountCreation( $reqs );
93 } else {
94 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_CREATE );
95 if ( $params['preservestate'] ) {
96 $req = $helper->getPreservedRequest();
97 if ( $req ) {
98 $reqs[] = $req;
99 }
100 }
101 $res = $this->authManager->beginAccountCreation(
102 $this->getAuthority(),
103 $reqs,
104 $params['returnurl']
105 );
106 }
107
108 $this->getResult()->addValue( null, 'createaccount',
109 $helper->formatAuthenticationResponse( $res ) );
110 $helper->logAuthenticationResult( 'accountcreation', $res );
111 }
112
113 public function isReadMode() {
114 return false;
115 }
116
117 public function isWriteMode() {
118 return true;
119 }
120
121 public function needsToken() {
122 return 'createaccount';
123 }
124
125 public function getAllowedParams() {
126 $ret = ApiAuthManagerHelper::getStandardParams( AuthManager::ACTION_CREATE,
127 'requests', 'messageformat', 'mergerequestfields', 'preservestate', 'returnurl', 'continue'
128 );
129 $ret['preservestate'][ApiBase::PARAM_HELP_MSG_APPEND][] =
130 'apihelp-createaccount-param-preservestate';
131 return $ret;
132 }
133
135 return [ 'api-help-authmanagerhelper-additional-params', AuthManager::ACTION_CREATE ];
136 }
137
138 protected function getExamplesMessages() {
139 return [
140 'action=createaccount&username=Example&password=ExamplePassword&retype=ExamplePassword'
141 . '&createreturnurl=http://example.org/&createtoken=123ABC'
142 => 'apihelp-createaccount-example-create',
143 ];
144 }
145
146 public function getHelpUrls() {
147 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Account_creation';
148 }
149}
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
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.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
isReadMode()
Indicates whether this module requires read rights.
dynamicParameterDocumentation()
Indicate if the module supports dynamically-determined parameters that cannot be included in self::ge...
getFinalDescription()
Get the final module description, after hooks have had a chance to tweak it as needed.
getHelpUrls()
Return links to more detailed help pages about the module.
__construct(ApiMain $main, $action, AuthManager $authManager)
needsToken()
Returns the token type this module requires in order to execute.
getExamplesMessages()
Returns usage examples for this module.
isWriteMode()
Indicates whether this module requires write mode.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
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:64
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1533
getModulePrefix()
Get parameter prefix (usually two letters or an empty string).
Definition ApiBase.php:541
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition ApiBase.php:789
static makeMessage( $msg, IContextSource $context, array $params=null)
Create a Message from a string or array.
Definition ApiBase.php:1285
const PARAM_HELP_MSG_APPEND
((string|array|Message)[]) Specify additional i18n messages to append to the normal message for this ...
Definition ApiBase.php:178
requireAtLeastOneParameter( $params,... $required)
Die if 0 of a certain set of parameters is set and not false.
Definition ApiBase.php:1010
getResult()
Get the result object.
Definition ApiBase.php:671
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:811
getModulePath()
Get the path to this module.
Definition ApiBase.php:611
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:532
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:65
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()
getContext()
Get the base IContextSource object.