Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
UserLogoutHookHandler
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 3
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 onUserLogout
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 onUserLogoutComplete
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\Extension\CentralAuth\Hooks\Handlers;
22
23use MediaWiki\Config\Config;
24use MediaWiki\Deferred\DeferredUpdates;
25use MediaWiki\Extension\CentralAuth\CentralAuthHooks;
26use MediaWiki\Extension\CentralAuth\User\CentralAuthUser;
27use MediaWiki\Hook\UserLogoutCompleteHook;
28use MediaWiki\User\Hook\UserLogoutHook;
29use MediaWiki\User\User;
30use MediaWiki\WikiMap\WikiMap;
31use RequestContext;
32
33class UserLogoutHookHandler implements
34    UserLogoutCompleteHook,
35    UserLogoutHook
36{
37    /** @var Config */
38    private $config;
39
40    /**
41     * @param Config $config
42     */
43    public function __construct( Config $config ) {
44        $this->config = $config;
45    }
46
47    /**
48     * @param User $user
49     * @return bool
50     */
51    public function onUserLogout( $user ) {
52        if ( !$this->config->get( 'CentralAuthCookies' ) ) {
53            // Use local sessions only.
54            return true;
55        }
56
57        $username = $user->getName();
58        DeferredUpdates::addCallableUpdate( static function () use ( $username ) {
59            $centralUser = CentralAuthUser::getPrimaryInstanceByName( $username );
60            if ( $centralUser->exists() ) {
61                $centralUser->resetAuthToken();
62            }
63        } );
64
65        return true;
66    }
67
68    /**
69     * @param User $user
70     * @param string &$inject_html
71     * @param string $oldName Unused
72     * @return bool
73     */
74    public function onUserLogoutComplete( $user, &$inject_html, $oldName ) {
75        if ( !$this->config->get( 'CentralAuthCookies' ) ) {
76            return true;
77        }
78
79        $wikis = CentralAuthHooks::getAutoLoginWikis();
80        $loginWiki = $this->config->get( 'CentralAuthLoginWiki' );
81        if ( $loginWiki && $loginWiki !== WikiMap::getCurrentWikiId() ) {
82            $wikis[$loginWiki] = $loginWiki;
83        }
84
85        $csp = RequestContext::getMain()->getOutput()->getCSP();
86        // No other domains
87        if ( !$wikis ) {
88            $inject_html = wfMessage( 'centralauth-logout-no-others' )->escaped();
89        } else {
90            $inject_html = '<div class="centralauth-logout-box"><p>' .
91                wfMessage( 'centralauth-logout-progress' )
92                    ->params( $user->getName() )
93                    ->numParams( count( $wikis ) )
94                    ->escaped() . "</p>\n<p>";
95            foreach ( $wikis as $wikiID ) {
96                $params = [
97                    'type' => 'icon',
98                ];
99                if ( CentralAuthHooks::isMobileDomain() ) {
100                    $params['mobile'] = 1;
101                }
102                $inject_html .= CentralAuthHooks::getAuthIconHtml(
103                    $wikiID, 'Special:CentralAutoLogin/deleteCookies', $params, $csp
104                );
105            }
106            $inject_html .= "</p></div>\n";
107        }
108
109        return true;
110    }
111}