Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / includes / api / ApiLinkAccount.php
blob9a21e7620cf556b938fb8f43f7d7071f3e5251c3
1 <?php
2 /**
3 * Copyright © 2016 Brad Jorsch <bjorsch@wikimedia.org>
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 use MediaWiki\Auth\AuthManager;
24 use MediaWiki\Auth\AuthenticationResponse;
26 /**
27 * Link an account with AuthManager
29 * @ingroup API
31 class ApiLinkAccount extends ApiBase {
33 public function __construct( ApiMain $main, $action ) {
34 parent::__construct( $main, $action, 'link' );
37 public function getFinalDescription() {
38 // A bit of a hack to append 'api-help-authmanager-general-usage'
39 $msgs = parent::getFinalDescription();
40 $msgs[] = ApiBase::makeMessage( 'api-help-authmanager-general-usage', $this->getContext(), [
41 $this->getModulePrefix(),
42 $this->getModuleName(),
43 $this->getModulePath(),
44 AuthManager::ACTION_LINK,
45 self::needsToken(),
46 ] );
47 return $msgs;
50 public function execute() {
51 if ( !$this->getUser()->isLoggedIn() ) {
52 $this->dieWithError( 'apierror-mustbeloggedin-linkaccounts', 'notloggedin' );
55 $params = $this->extractRequestParams();
57 $this->requireAtLeastOneParameter( $params, 'continue', 'returnurl' );
59 if ( $params['returnurl'] !== null ) {
60 $bits = wfParseUrl( $params['returnurl'] );
61 if ( !$bits || $bits['scheme'] === '' ) {
62 $encParamName = $this->encodeParamName( 'returnurl' );
63 $this->dieWithError(
64 [ 'apierror-badurl', $encParamName, wfEscapeWikiText( $params['returnurl'] ) ],
65 "badurl_{$encParamName}"
70 $helper = new ApiAuthManagerHelper( $this );
71 $manager = AuthManager::singleton();
73 // Check security-sensitive operation status
74 $helper->securitySensitiveOperation( 'LinkAccounts' );
76 // Make sure it's possible to link accounts
77 if ( !$manager->canLinkAccounts() ) {
78 $this->getResult()->addValue( null, 'linkaccount', $helper->formatAuthenticationResponse(
79 AuthenticationResponse::newFail( $this->msg( 'userlogin-cannot-' . AuthManager::ACTION_LINK ) )
80 ) );
81 return;
84 // Perform the link step
85 if ( $params['continue'] ) {
86 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LINK_CONTINUE );
87 $res = $manager->continueAccountLink( $reqs );
88 } else {
89 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LINK );
90 $res = $manager->beginAccountLink( $this->getUser(), $reqs, $params['returnurl'] );
93 $this->getResult()->addValue( null, 'linkaccount',
94 $helper->formatAuthenticationResponse( $res ) );
97 public function isReadMode() {
98 return false;
101 public function isWriteMode() {
102 return true;
105 public function needsToken() {
106 return 'csrf';
109 public function getAllowedParams() {
110 return ApiAuthManagerHelper::getStandardParams( AuthManager::ACTION_LINK,
111 'requests', 'messageformat', 'mergerequestfields', 'returnurl', 'continue'
115 public function dynamicParameterDocumentation() {
116 return [ 'api-help-authmanagerhelper-additional-params', AuthManager::ACTION_LINK ];
119 protected function getExamplesMessages() {
120 return [
121 'action=linkaccount&provider=Example&linkreturnurl=http://example.org/&linktoken=123ABC'
122 => 'apihelp-linkaccount-example-link',
126 public function getHelpUrls() {
127 return 'https://www.mediawiki.org/wiki/API:Linkaccount';