MediaWiki master
ApiLinkAccount.php
Go to the documentation of this file.
1<?php
25
31class ApiLinkAccount extends ApiBase {
32
33 private AuthManager $authManager;
34
40 public function __construct(
41 ApiMain $main,
42 $action,
43 AuthManager $authManager
44 ) {
45 parent::__construct( $main, $action, 'link' );
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_LINK,
57 $this->needsToken(),
58 ] );
59 return $msgs;
60 }
61
62 public function execute() {
63 if ( !$this->getUser()->isNamed() ) {
64 $this->dieWithError( 'apierror-mustbeloggedin-linkaccounts', 'notloggedin' );
65 }
66
68
69 $this->requireAtLeastOneParameter( $params, 'continue', 'returnurl' );
70
71 if ( $params['returnurl'] !== null ) {
72 $bits = wfParseUrl( $params['returnurl'] );
73 if ( !$bits || $bits['scheme'] === '' ) {
74 $encParamName = $this->encodeParamName( 'returnurl' );
75 $this->dieWithError(
76 [ 'apierror-badurl', $encParamName, wfEscapeWikiText( $params['returnurl'] ) ],
77 "badurl_{$encParamName}"
78 );
79 }
80 }
81
82 $helper = new ApiAuthManagerHelper( $this, $this->authManager );
83
84 // Check security-sensitive operation status
85 $helper->securitySensitiveOperation( 'LinkAccounts' );
86
87 // Make sure it's possible to link accounts
88 if ( !$this->authManager->canLinkAccounts() ) {
89 $this->getResult()->addValue( null, 'linkaccount', $helper->formatAuthenticationResponse(
90 AuthenticationResponse::newFail( $this->msg( 'userlogin-cannot-' . AuthManager::ACTION_LINK ) )
91 ) );
92 return;
93 }
94
95 // Perform the link step
96 if ( $params['continue'] ) {
97 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LINK_CONTINUE );
98 $res = $this->authManager->continueAccountLink( $reqs );
99 } else {
100 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LINK );
101 $res = $this->authManager->beginAccountLink( $this->getUser(), $reqs, $params['returnurl'] );
102 }
103
104 $this->getResult()->addValue( null, 'linkaccount',
105 $helper->formatAuthenticationResponse( $res ) );
106 }
107
108 public function isReadMode() {
109 return false;
110 }
111
112 public function isWriteMode() {
113 return true;
114 }
115
116 public function needsToken() {
117 return 'csrf';
118 }
119
120 public function getAllowedParams() {
121 return ApiAuthManagerHelper::getStandardParams( AuthManager::ACTION_LINK,
122 'requests', 'messageformat', 'mergerequestfields', 'returnurl', 'continue'
123 );
124 }
125
127 return [ 'api-help-authmanagerhelper-additional-params', AuthManager::ACTION_LINK ];
128 }
129
130 protected function getExamplesMessages() {
131 return [
132 'action=linkaccount&provider=Example&linkreturnurl=http://example.org/&linktoken=123ABC'
133 => 'apihelp-linkaccount-example-link',
134 ];
135 }
136
137 public function getHelpUrls() {
138 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Linkaccount';
139 }
140}
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.
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
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
Link an account with AuthManager.
isWriteMode()
Indicates whether this module requires write mode.
isReadMode()
Indicates whether this module requires read rights.
__construct(ApiMain $main, $action, AuthManager $authManager)
dynamicParameterDocumentation()
Indicate if the module supports dynamically-determined parameters that cannot be included in self::ge...
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.
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...
getExamplesMessages()
Returns usage examples for this module.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
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.
getContext()
Get the base IContextSource object.