Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 40 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Manager | |
0.00% |
0 / 40 |
|
0.00% |
0 / 5 |
90 | |
0.00% |
0 / 1 |
| isEnabled | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| notifyDisabled | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| notifyEnabled | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
| notifyRecoveryTokensRemaining | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
| notifyRecoveryTokensGeneratedForUser | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Copyright (C) 2022 Kunal Mehta <legoktm@debian.org> |
| 4 | * |
| 5 | * @license GPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | namespace MediaWiki\Extension\OATHAuth\Notifications; |
| 9 | |
| 10 | use MediaWiki\Extension\Notifications\Model\Event; |
| 11 | use MediaWiki\Extension\OATHAuth\OATHUser; |
| 12 | use MediaWiki\Notification\RecipientSet; |
| 13 | use MediaWiki\Registration\ExtensionRegistry; |
| 14 | use MediaWiki\SpecialPage\SpecialPage; |
| 15 | use MediaWiki\User\UserIdentity; |
| 16 | |
| 17 | /** |
| 18 | * Manages logic for configuring and sending out notifications with Echo |
| 19 | */ |
| 20 | class Manager { |
| 21 | |
| 22 | /** |
| 23 | * Whether Echo is installed and can be used |
| 24 | */ |
| 25 | private static function isEnabled(): bool { |
| 26 | return ExtensionRegistry::getInstance()->isLoaded( 'Echo' ); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Send a notification that 2FA has been disabled |
| 31 | * |
| 32 | * @param OATHUser $oUser |
| 33 | * @param bool $self Whether they disabled it themselves |
| 34 | */ |
| 35 | public static function notifyDisabled( OATHUser $oUser, bool $self ): void { |
| 36 | if ( !self::isEnabled() ) { |
| 37 | return; |
| 38 | } |
| 39 | Event::create( [ |
| 40 | // message used: notification-header-oathauth-disable |
| 41 | 'type' => 'oathauth-disable', |
| 42 | 'title' => SpecialPage::getTitleFor( 'Preferences' ), |
| 43 | 'agent' => $oUser->getUser(), |
| 44 | 'extra' => [ |
| 45 | 'self' => $self, |
| 46 | 'activeDevices' => count( $oUser->getNonSpecialKeys() ), |
| 47 | ] |
| 48 | ] ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Send a notification that 2FA has been enabled |
| 53 | */ |
| 54 | public static function notifyEnabled( OATHUser $oUser ): void { |
| 55 | if ( !self::isEnabled() ) { |
| 56 | return; |
| 57 | } |
| 58 | Event::create( [ |
| 59 | // message used: notification-header-oathauth-enable |
| 60 | 'type' => 'oathauth-enable', |
| 61 | 'title' => SpecialPage::getTitleFor( 'Preferences' ), |
| 62 | 'agent' => $oUser->getUser(), |
| 63 | 'extra' => [ |
| 64 | 'activeDevices' => count( $oUser->getNonSpecialKeys() ), |
| 65 | ], |
| 66 | ] ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Send a notification that the user has $tokenCount recovery tokens left |
| 71 | */ |
| 72 | public static function notifyRecoveryTokensRemaining( |
| 73 | OATHUser $oUser, int $tokenCount, int $generatedCount |
| 74 | ): void { |
| 75 | if ( !self::isEnabled() ) { |
| 76 | return; |
| 77 | } |
| 78 | Event::create( [ |
| 79 | // message used: notification-header-oathauth-recoverycodes-count |
| 80 | 'type' => 'oathauth-recoverycodes-count', |
| 81 | 'agent' => $oUser->getUser(), |
| 82 | 'extra' => [ |
| 83 | 'codeCount' => $tokenCount, |
| 84 | 'generatedCount' => $generatedCount, |
| 85 | ], |
| 86 | ] ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Sends a notification that the user had additional recovery tokens generated for them |
| 91 | */ |
| 92 | public static function notifyRecoveryTokensGeneratedForUser( |
| 93 | UserIdentity $targetUser, |
| 94 | int $tokenCount |
| 95 | ): void { |
| 96 | if ( !self::isEnabled() ) { |
| 97 | return; |
| 98 | } |
| 99 | Event::create( [ |
| 100 | // message used: notification-header-oathauth-recoverycodes-generated-for-user |
| 101 | 'type' => 'oathauth-recoverycodes-generated-for-user', |
| 102 | 'extra' => [ |
| 103 | 'codeCount' => $tokenCount |
| 104 | ], |
| 105 | ], new RecipientSet( $targetUser ) ); |
| 106 | } |
| 107 | } |