Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Manager
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 4
56
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
 notifyRecoveryTokensRemaining
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 MediaWiki\Extension\Notifications\Model\Event;
24use MediaWiki\Extension\OATHAuth\OATHUser;
25use MediaWiki\Registration\ExtensionRegistry;
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
84    /**
85     * Send a notification that the user has $tokenCount recovery tokens left
86     *
87     * @param OATHUser $oUser
88     * @param int $tokenCount
89     * @param int $generatedCount
90     */
91    public static function notifyRecoveryTokensRemaining( OATHUser $oUser, int $tokenCount, int $generatedCount ) {
92        if ( !self::isEnabled() ) {
93            return;
94        }
95        Event::create( [
96            // message used: notification-header-oathauth-recoverycodes-count
97            'type' => 'oathauth-recoverycodes-count',
98            'agent' => $oUser->getUser(),
99            'extra' => [
100                'codeCount' => $tokenCount,
101                'generatedCount' => $generatedCount,
102            ],
103        ] );
104    }
105}