Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Manager
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 3
30
0.00% covered (danger)
0.00%
0 / 1
 isEnabled
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 notifyDisabled
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
 notifyEnabled
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Copyright (C) 2022 Kunal Mehta <legoktm@debian.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 */
20
21namespace MediaWiki\Extension\OATHAuth\Notifications;
22
23use ExtensionRegistry;
24use MediaWiki\Extension\Notifications\Model\Event;
25use MediaWiki\Extension\OATHAuth\OATHUser;
26use MediaWiki\SpecialPage\SpecialPage;
27
28/**
29 * Manages logic for configuring and sending out notifications with Echo
30 */
31class Manager {
32
33    /**
34     * Whether Echo is installed and can be used
35     *
36     * @return bool
37     */
38    private static function isEnabled(): bool {
39        return ExtensionRegistry::getInstance()->isLoaded( 'Echo' );
40    }
41
42    /**
43     * Send a notification that 2FA has been disabled
44     *
45     * @param OATHUser $oUser
46     * @param bool $self Whether they disabled it themselves
47     */
48    public static function notifyDisabled( OATHUser $oUser, bool $self ) {
49        if ( !self::isEnabled() ) {
50            return;
51        }
52        Event::create( [
53            // message used: notification-header-oathauth-disable
54            'type' => 'oathauth-disable',
55            'title' => SpecialPage::getTitleFor( 'Preferences' ),
56            'agent' => $oUser->getUser(),
57            'extra' => [
58                'self' => $self,
59                'activeDevices' => count( $oUser->getKeys() ),
60            ]
61        ] );
62    }
63
64    /**
65     * Send a notification that 2FA has been enabled
66     *
67     * @param OATHUser $oUser
68     */
69    public static function notifyEnabled( OATHUser $oUser ) {
70        if ( !self::isEnabled() ) {
71            return;
72        }
73        Event::create( [
74            // message used: notification-header-oathauth-enable
75            'type' => 'oathauth-enable',
76            'title' => SpecialPage::getTitleFor( 'Preferences' ),
77            'agent' => $oUser->getUser(),
78            'extra' => [
79                'activeDevices' => count( $oUser->getKeys() ),
80            ],
81        ] );
82    }
83}