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