[JsonCodec] Use wikimedia/json-codec to implement JsonCodec
[mediawiki.git] / includes / specials / SpecialLinkAccounts.php
blobcd8a9847fedf6290aeb44b8f9201b86cd8b2628a
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 /**
30 * @param AuthManager $authManager
32 public function __construct( AuthManager $authManager ) {
33 parent::__construct( 'LinkAccounts' );
34 $this->setAuthManager( $authManager );
37 protected function getGroupName() {
38 return 'login';
41 public function isListed() {
42 return $this->getAuthManager()->canLinkAccounts();
45 protected function getRequestBlacklist() {
46 return $this->getConfig()->get( MainConfigNames::ChangeCredentialsBlacklist );
49 /**
50 * @param null|string $subPage
51 * @throws ErrorPageError
53 public function execute( $subPage ) {
54 $this->setHeaders();
55 $this->loadAuth( $subPage );
57 if ( !$this->isActionAllowed( $this->authAction ) ) {
58 if ( $this->authAction === AuthManager::ACTION_LINK ) {
59 // looks like no linking provider is installed or willing to take this user
60 $titleMessage = $this->msg( 'cannotlink-no-provider-title' );
61 $errorMessage = $this->msg( 'cannotlink-no-provider' );
62 throw new ErrorPageError( $titleMessage, $errorMessage );
63 } else {
64 // user probably back-button-navigated into an auth session that no longer exists
65 // FIXME would be nice to show a message
66 $this->getOutput()->redirect( $this->getPageTitle()->getFullURL( '', false,
67 PROTO_HTTPS ) );
68 return;
72 $this->outputHeader();
74 $status = $this->trySubmit();
76 if ( $status === false || !$status->isOK() ) {
77 $this->displayForm( $status );
78 return;
81 $response = $status->getValue();
83 switch ( $response->status ) {
84 case AuthenticationResponse::PASS:
85 $this->success();
86 break;
87 case AuthenticationResponse::FAIL:
88 $this->loadAuth( '', AuthManager::ACTION_LINK, true );
89 $this->displayForm( StatusValue::newFatal( $response->message ) );
90 break;
91 case AuthenticationResponse::REDIRECT:
92 $this->getOutput()->redirect( $response->redirectTarget );
93 break;
94 case AuthenticationResponse::UI:
95 $this->authAction = AuthManager::ACTION_LINK_CONTINUE;
96 $this->authRequests = $response->neededRequests;
97 $this->displayForm( StatusValue::newFatal( $response->message ) );
98 break;
99 default:
100 throw new LogicException( 'invalid AuthenticationResponse' );
104 protected function getDefaultAction( $subPage ) {
105 return AuthManager::ACTION_LINK;
109 * @param AuthenticationRequest[] $requests
110 * @param string $action AuthManager action name, should be ACTION_LINK or ACTION_LINK_CONTINUE
111 * @return HTMLForm
113 protected function getAuthForm( array $requests, $action ) {
114 $form = parent::getAuthForm( $requests, $action );
115 $form->setSubmitTextMsg( 'linkaccounts-submit' );
116 return $form;
120 * Show a success message.
122 protected function success() {
123 $this->loadAuth( '', AuthManager::ACTION_LINK, true );
124 $this->displayForm( StatusValue::newFatal( $this->msg( 'linkaccounts-success-text' ) ) );
128 /** @deprecated class alias since 1.41 */
129 class_alias( SpecialLinkAccounts::class, 'SpecialLinkAccounts' );