Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
PreferencesHandler
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 3
110
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
 onGetPreferences
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 onSaveUserOptions
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2
3namespace MediaWiki\Extension\AbuseFilter\Hooks\Handlers;
4
5use MediaWiki\Extension\AbuseFilter\AbuseLoggerFactory;
6use MediaWiki\Permissions\PermissionManager;
7use MediaWiki\Preferences\Hook\GetPreferencesHook;
8use MediaWiki\User\Options\Hook\SaveUserOptionsHook;
9use MediaWiki\User\UserIdentity;
10
11class PreferencesHandler implements GetPreferencesHook, SaveUserOptionsHook {
12    private PermissionManager $permissionManager;
13
14    private AbuseLoggerFactory $abuseLoggerFactory;
15
16    public function __construct(
17        PermissionManager $permissionManager,
18        AbuseLoggerFactory $abuseLoggerFactory
19    ) {
20        $this->permissionManager = $permissionManager;
21        $this->abuseLoggerFactory = $abuseLoggerFactory;
22    }
23
24    /** @inheritDoc */
25    public function onGetPreferences( $user, &$preferences ): void {
26        if ( !$this->permissionManager->userHasRight( $user, 'abusefilter-access-protected-vars' ) ) {
27            return;
28        }
29
30        $preferences['abusefilter-protected-vars-view-agreement'] = [
31            'type' => 'toggle',
32            'label-message' => 'abusefilter-preference-protected-vars-view-agreement',
33            'section' => 'personal/abusefilter',
34            'noglobal' => true,
35        ];
36    }
37
38    /**
39     * @param UserIdentity $user
40     * @param array &$modifiedOptions
41     * @param array $originalOptions
42     */
43    public function onSaveUserOptions( UserIdentity $user, array &$modifiedOptions, array $originalOptions ) {
44        $wasEnabled = !empty( $originalOptions['abusefilter-protected-vars-view-agreement'] );
45        $wasDisabled = !$wasEnabled;
46
47        $willEnable = !empty( $modifiedOptions['abusefilter-protected-vars-view-agreement'] );
48        $willDisable = isset( $modifiedOptions['abusefilter-protected-vars-view-agreement'] ) &&
49            !$modifiedOptions['abusefilter-protected-vars-view-agreement'];
50
51        if (
52            ( $wasEnabled && $willDisable ) ||
53            ( $wasDisabled && $willEnable )
54        ) {
55            $logger = $this->abuseLoggerFactory->getProtectedVarsAccessLogger();
56            if ( $willEnable ) {
57                $logger->logAccessEnabled( $user );
58            } else {
59                $logger->logAccessDisabled( $user );
60            }
61        }
62    }
63}