Merge "rest: Return a 400 for invalid render IDs"
[mediawiki.git] / includes / specials / SpecialLinkAccounts.php
blobe4722a735fd9bf96f4ca9121cd09ec739b367b00
1 <?php
3 namespace MediaWiki\Specials;
5 use ErrorPageError;
6 use LogicException;
7 use MediaWiki\Auth\AuthenticationRequest;
8 use MediaWiki\Auth\AuthenticationResponse;
9 use MediaWiki\Auth\AuthManager;
10 use MediaWiki\HTMLForm\HTMLForm;
11 use MediaWiki\MainConfigNames;
12 use MediaWiki\SpecialPage\AuthManagerSpecialPage;
13 use StatusValue;
15 /**
16 * Link/unlink external accounts to the current user.
18 * To interact with this page, account providers need to register themselves with AuthManager.
20 * @ingroup SpecialPage
21 * @ingroup Auth
23 class SpecialLinkAccounts extends AuthManagerSpecialPage {
24 /** @inheritDoc */
25 protected static $allowedActions = [
26 AuthManager::ACTION_LINK, AuthManager::ACTION_LINK_CONTINUE,
29 public function __construct( AuthManager $authManager ) {
30 parent::__construct( 'LinkAccounts' );
31 $this->setAuthManager( $authManager );
34 protected function getGroupName() {
35 return 'login';
38 public function isListed() {
39 return $this->getAuthManager()->canLinkAccounts();
42 protected function getRequestBlacklist() {
43 return $this->getConfig()->get( MainConfigNames::ChangeCredentialsBlacklist );
46 /**
47 * @param null|string $subPage
48 * @throws ErrorPageError
50 public function execute( $subPage ) {
51 $this->setHeaders();
52 $this->loadAuth( $subPage );
54 if ( !$this->isActionAllowed( $this->authAction ) ) {
55 if ( $this->authAction === AuthManager::ACTION_LINK ) {
56 // looks like no linking provider is installed or willing to take this user
57 $titleMessage = $this->msg( 'cannotlink-no-provider-title' );
58 $errorMessage = $this->msg( 'cannotlink-no-provider' );
59 throw new ErrorPageError( $titleMessage, $errorMessage );
60 } else {
61 // user probably back-button-navigated into an auth session that no longer exists
62 // FIXME would be nice to show a message
63 $this->getOutput()->redirect( $this->getPageTitle()->getFullURL( '', false,
64 PROTO_HTTPS ) );
65 return;
69 $this->outputHeader();
71 $status = $this->trySubmit();
73 if ( $status === false || !$status->isOK() ) {
74 $this->displayForm( $status );
75 return;
78 $response = $status->getValue();
80 switch ( $response->status ) {
81 case AuthenticationResponse::PASS:
82 $this->success();
83 break;
84 case AuthenticationResponse::FAIL:
85 $this->loadAuth( '', AuthManager::ACTION_LINK, true );
86 $this->displayForm( StatusValue::newFatal( $response->message ) );
87 break;
88 case AuthenticationResponse::REDIRECT:
89 $this->getOutput()->redirect( $response->redirectTarget );
90 break;
91 case AuthenticationResponse::UI:
92 $this->authAction = AuthManager::ACTION_LINK_CONTINUE;
93 $this->authRequests = $response->neededRequests;
94 $this->displayForm( StatusValue::newFatal( $response->message ) );
95 break;
96 default:
97 throw new LogicException( 'invalid AuthenticationResponse' );
101 protected function getDefaultAction( $subPage ) {
102 return AuthManager::ACTION_LINK;
106 * @param AuthenticationRequest[] $requests
107 * @param string $action AuthManager action name, should be ACTION_LINK or ACTION_LINK_CONTINUE
108 * @return HTMLForm
110 protected function getAuthForm( array $requests, $action ) {
111 $form = parent::getAuthForm( $requests, $action );
112 $form->setSubmitTextMsg( 'linkaccounts-submit' );
113 return $form;
117 * Show a success message.
119 protected function success() {
120 $this->loadAuth( '', AuthManager::ACTION_LINK, true );
121 $this->displayForm( StatusValue::newFatal( $this->msg( 'linkaccounts-success-text' ) ) );
125 /** @deprecated class alias since 1.41 */
126 class_alias( SpecialLinkAccounts::class, 'SpecialLinkAccounts' );