Debian Codegraph @

includes/specials/SpecialLinkAccounts.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Specials;
4
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;
14
15 /**
16 * Link/unlink external accounts to the current user.
17 *
18 * To interact with this page, account providers need to register themselves with AuthManager.
19 *
20 * @ingroup SpecialPage
21 * @ingroup Auth
22 */
23 class SpecialLinkAccounts extends AuthManagerSpecialPage {
24 /** @inheritDoc */
25 protected static $allowedActions = [
26 AuthManager::ACTION_LINK, AuthManager::ACTION_LINK_CONTINUE,
27 ];
28
29 /**
30 * @param AuthManager $authManager
31 */
32 public function __construct( AuthManager $authManager ) {
33 parent::__construct( 'LinkAccounts' );
34 $this->setAuthManager( $authManager );
35 }
36
37 /** @inheritDoc */
38 public function getRestriction(): string {
39 return 'editmyprivateinfo';
40 }
41
42 protected function getGroupName() {
43 return 'login';
44 }
45
46 public function isListed() {
47 return $this->getAuthManager()->canLinkAccounts();
48 }
49
50 protected function getRequestBlacklist() {
51 return $this->getConfig()->get( MainConfigNames::ChangeCredentialsBlacklist );
52 }
53
54 /**
55 * @param null|string $subPage
56 * @throws ErrorPageError
57 */
58 public function execute( $subPage ) {
59 $this->setHeaders();
60 $this->loadAuth( $subPage );
61
62 if ( !$this->isActionAllowed( $this->authAction ) ) {
63 if ( $this->authAction === AuthManager::ACTION_LINK ) {
64 // looks like no linking provider is installed or willing to take this user
65 $titleMessage = $this->msg( 'cannotlink-no-provider-title' );
66 $errorMessage = $this->msg( 'cannotlink-no-provider' );
67 throw new ErrorPageError( $titleMessage, $errorMessage );
68 } else {
69 // user probably back-button-navigated into an auth session that no longer exists
70 // FIXME would be nice to show a message
71 $this->getOutput()->redirect( $this->getPageTitle()->getFullURL( '', false,
72 PROTO_HTTPS ) );
73 return;
74 }
75 }
76
77 $this->outputHeader();
78
79 $status = $this->trySubmit();
80
81 if ( $status === false || !$status->isOK() ) {
82 $this->displayForm( $status );
83 return;
84 }
85
86 $response = $status->getValue();
87
88 switch ( $response->status ) {
89 case AuthenticationResponse::PASS:
90 $this->success();
91 break;
92 case AuthenticationResponse::FAIL:
93 $this->loadAuth( '', AuthManager::ACTION_LINK, true );
94 $this->displayForm( StatusValue::newFatal( $response->message ) );
95 break;
96 case AuthenticationResponse::REDIRECT:
97 $this->getOutput()->redirect( $response->redirectTarget );
98 break;
99 case AuthenticationResponse::UI:
100 $this->authAction = AuthManager::ACTION_LINK_CONTINUE;
101 $this->authRequests = $response->neededRequests;
102 $this->displayForm( StatusValue::newFatal( $response->message ) );
103 break;
104 default:
105 throw new LogicException( 'invalid AuthenticationResponse' );
106 }
107 }
108
109 protected function getDefaultAction( $subPage ) {
110 return AuthManager::ACTION_LINK;
111 }
112
113 /**
114 * @param AuthenticationRequest[] $requests
115 * @param string $action AuthManager action name, should be ACTION_LINK or ACTION_LINK_CONTINUE
116 * @return HTMLForm
117 */
118 protected function getAuthForm( array $requests, $action ) {
119 $form = parent::getAuthForm( $requests, $action );
120 $form->setSubmitTextMsg( 'linkaccounts-submit' );
121 return $form;
122 }
123
124 /**
125 * Show a success message.
126 */
127 protected function success() {
128 $this->loadAuth( '', AuthManager::ACTION_LINK, true );
129 $this->displayForm( StatusValue::newFatal( $this->msg( 'linkaccounts-success-text' ) ) );
130 }
131 }
132
133 /** @deprecated class alias since 1.41 */
134 class_alias( SpecialLinkAccounts::class, 'SpecialLinkAccounts' );