Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiLinkAccount.php
blob8a5ba651b5a369ab47d04790dcd68ca6cd57f377
1 <?php
2 /**
3 * Copyright © 2016 Wikimedia Foundation and contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 namespace MediaWiki\Api;
25 use MediaWiki\Auth\AuthenticationResponse;
26 use MediaWiki\Auth\AuthManager;
27 use MediaWiki\Utils\UrlUtils;
29 /**
30 * Link an account with AuthManager
32 * @ingroup API
34 class ApiLinkAccount extends ApiBase {
36 private AuthManager $authManager;
37 private UrlUtils $urlUtils;
39 public function __construct(
40 ApiMain $main,
41 string $action,
42 AuthManager $authManager,
43 UrlUtils $urlUtils
44 ) {
45 parent::__construct( $main, $action, 'link' );
46 $this->authManager = $authManager;
47 $this->urlUtils = $urlUtils;
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_LINK,
58 $this->needsToken(),
60 return $msgs;
63 public function execute() {
64 if ( !$this->getUser()->isNamed() ) {
65 $this->dieWithError( 'apierror-mustbeloggedin-linkaccounts', 'notloggedin' );
68 $params = $this->extractRequestParams();
70 $this->requireAtLeastOneParameter( $params, 'continue', 'returnurl' );
72 if ( $params['returnurl'] !== null ) {
73 $bits = $this->urlUtils->parse( $params['returnurl'] );
74 if ( !$bits || $bits['scheme'] === '' ) {
75 $encParamName = $this->encodeParamName( 'returnurl' );
76 $this->dieWithError(
77 [ 'apierror-badurl', $encParamName, wfEscapeWikiText( $params['returnurl'] ) ],
78 "badurl_{$encParamName}"
83 $helper = new ApiAuthManagerHelper( $this, $this->authManager );
85 // Check security-sensitive operation status
86 $helper->securitySensitiveOperation( 'LinkAccounts' );
88 // Make sure it's possible to link accounts
89 if ( !$this->authManager->canLinkAccounts() ) {
90 $this->getResult()->addValue( null, 'linkaccount', $helper->formatAuthenticationResponse(
91 AuthenticationResponse::newFail( $this->msg( 'userlogin-cannot-' . AuthManager::ACTION_LINK ) )
92 ) );
93 return;
96 // Perform the link step
97 if ( $params['continue'] ) {
98 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LINK_CONTINUE );
99 $res = $this->authManager->continueAccountLink( $reqs );
100 } else {
101 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LINK );
102 $res = $this->authManager->beginAccountLink( $this->getUser(), $reqs, $params['returnurl'] );
105 $this->getResult()->addValue( null, 'linkaccount',
106 $helper->formatAuthenticationResponse( $res ) );
109 public function isReadMode() {
110 return false;
113 public function isWriteMode() {
114 return true;
117 public function needsToken() {
118 return 'csrf';
121 public function getAllowedParams() {
122 return ApiAuthManagerHelper::getStandardParams( AuthManager::ACTION_LINK,
123 'requests', 'messageformat', 'mergerequestfields', 'returnurl', 'continue'
127 public function dynamicParameterDocumentation() {
128 return [ 'api-help-authmanagerhelper-additional-params', AuthManager::ACTION_LINK ];
131 protected function getExamplesMessages() {
132 return [
133 'action=linkaccount&provider=Example&linkreturnurl=http://example.org/&linktoken=123ABC'
134 => 'apihelp-linkaccount-example-link',
138 public function getHelpUrls() {
139 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Linkaccount';
143 /** @deprecated class alias since 1.43 */
144 class_alias( ApiLinkAccount::class, 'ApiLinkAccount' );