Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialLinkAccounts
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 9
342
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getRestriction
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getGroupName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isListed
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRequestBlacklist
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
110
 getDefaultAction
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAuthForm
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 success
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Specials;
4
5use LogicException;
6use MediaWiki\Auth\AuthenticationRequest;
7use MediaWiki\Auth\AuthenticationResponse;
8use MediaWiki\Auth\AuthManager;
9use MediaWiki\Exception\ErrorPageError;
10use MediaWiki\HTMLForm\HTMLForm;
11use MediaWiki\MainConfigNames;
12use MediaWiki\SpecialPage\AuthManagerSpecialPage;
13use 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 */
23class SpecialLinkAccounts extends AuthManagerSpecialPage {
24    /** @inheritDoc */
25    protected static $allowedActions = [
26        AuthManager::ACTION_LINK, AuthManager::ACTION_LINK_CONTINUE,
27    ];
28
29    public function __construct( AuthManager $authManager ) {
30        parent::__construct( 'LinkAccounts' );
31        $this->setAuthManager( $authManager );
32    }
33
34    /** @inheritDoc */
35    public function getRestriction(): string {
36        return 'editmyprivateinfo';
37    }
38
39    /** @inheritDoc */
40    protected function getGroupName() {
41        return 'login';
42    }
43
44    /** @inheritDoc */
45    public function isListed() {
46        return $this->getAuthManager()->canLinkAccounts();
47    }
48
49    /** @inheritDoc */
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    /** @inheritDoc */
110    protected function getDefaultAction( $subPage ) {
111        return AuthManager::ACTION_LINK;
112    }
113
114    /**
115     * @param AuthenticationRequest[] $requests
116     * @param string $action AuthManager action name, should be ACTION_LINK or ACTION_LINK_CONTINUE
117     * @return HTMLForm
118     */
119    protected function getAuthForm( array $requests, $action ) {
120        $form = parent::getAuthForm( $requests, $action );
121        $form->setSubmitTextMsg( 'linkaccounts-submit' );
122        return $form;
123    }
124
125    /**
126     * Show a success message.
127     */
128    protected function success() {
129        $this->loadAuth( '', AuthManager::ACTION_LINK, true );
130        $this->displayForm( StatusValue::newFatal( 'linkaccounts-success-text' ) );
131    }
132}
133
134// @codeCoverageIgnoreStart
135/** @deprecated class alias since 1.41 */
136class_alias( SpecialLinkAccounts::class, 'SpecialLinkAccounts' );
137// @codeCoverageIgnoreEnd