Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
43.24% covered (danger)
43.24%
16 / 37
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
BlockHookHandler
43.24% covered (danger)
43.24%
16 / 37
33.33% covered (danger)
33.33%
1 / 3
56.14
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 onGetUserBlock
60.00% covered (warning)
60.00%
15 / 25
0.00% covered (danger)
0.00%
0 / 1
14.18
 onOtherBlockLogLink
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
30
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\Block\AbstractBlock;
24use MediaWiki\Block\CompositeBlock;
25use MediaWiki\Block\Hook\GetUserBlockHook;
26use MediaWiki\Block\SystemBlock;
27use MediaWiki\Extension\CentralAuth\User\CentralAuthUser;
28use MediaWiki\Hook\OtherBlockLogLinkHook;
29use MediaWiki\Html\Html;
30use MediaWiki\Message\Message;
31use MediaWiki\User\User;
32use MediaWiki\User\UserNameUtils;
33use MediaWiki\WikiMap\WikiMap;
34use Wikimedia\IPUtils;
35
36/**
37 * @author Taavi Väänänen <hi@taavi.wtf>
38 */
39class BlockHookHandler implements
40    GetUserBlockHook,
41    OtherBlockLogLinkHook
42{
43    private UserNameUtils $userNameUtils;
44
45    public function __construct( UserNameUtils $userNameUtils ) {
46        $this->userNameUtils = $userNameUtils;
47    }
48
49    /**
50     * Make sure a user is hidden if their global account is hidden.
51     * If a user's global account is hidden (suppressed):
52     * - if locally blocked and hidden, do nothing
53     * - if not blocked, add a system block with a suppression
54     * - if blocked but not hidden, make a new composite block
55     *   containing the existing blocks plus a system block with a
56     *   suppression
57     *
58     * @param User $user
59     * @param string|null $ip
60     * @param AbstractBlock|null &$block
61     * @return bool
62     */
63    public function onGetUserBlock( $user, $ip, &$block ) {
64        if ( $block && $block->getHideName() ) {
65            return false;
66        }
67        if ( !$this->userNameUtils->isValid( $user->getName() ) ) {
68            // Only valid usernames can be handled (and hidden) by CentralAuth.
69            return true;
70        }
71
72        $centralUser = CentralAuthUser::getInstance( $user );
73        if ( $centralUser->exists()
74            && ( $centralUser->isAttached() || !$user->isRegistered() )
75            && $centralUser->getHiddenLevelInt() === CentralAuthUser::HIDDEN_LEVEL_SUPPRESSED
76        ) {
77            $hideUserBlock = new SystemBlock( [
78                'address' => $user,
79                'hideName' => true,
80                'systemBlock' => 'hideuser',
81            ] );
82
83            if ( $block === null ) {
84                $block = $hideUserBlock;
85                return false;
86            }
87
88            $blocks = $block->toArray();
89
90            $blocks[] = $hideUserBlock;
91            $block = new CompositeBlock( [
92                'address' => $ip,
93                'reason' => new Message( 'blockedtext-composite-reason' ),
94                'originalBlocks' => $blocks,
95            ] );
96
97            return false;
98        }
99
100        return true;
101    }
102
103    /**
104     * Creates a link to the global lock log
105     * @param array &$otherBlockLink Message with a link to the global block log
106     * @param string $user The username to be checked
107     * @return bool true
108     */
109    public function onOtherBlockLogLink( &$otherBlockLink, $user ) {
110        if (
111            IPUtils::isIPAddress( $user )
112            || !$this->userNameUtils->isValid( $user )
113        ) {
114            // Only usernames can be locked.
115            return true;
116        }
117
118        $caUser = CentralAuthUser::getInstanceByName( $user );
119        if ( $caUser->isLocked() && in_array( WikiMap::getCurrentWikiId(), $caUser->listAttached() ) ) {
120            $otherBlockLink[] = Html::rawElement(
121                'span',
122                [ 'class' => 'mw-centralauth-lock-loglink plainlinks' ],
123                wfMessage( 'centralauth-block-already-locked', $user )->parse()
124            );
125        }
126        return true;
127    }
128}