2018-09-29 14:56:23 -04:00
< ? php
2019-12-03 13:57:53 -05:00
2018-09-29 14:56:23 -04:00
declare ( strict_types = 1 );
2019-12-03 13:57:53 -05:00
2018-09-29 14:56:23 -04:00
/**
2024-05-30 14:13:41 -04:00
* SPDX - FileCopyrightText : 2018 Nextcloud GmbH and Nextcloud contributors
* SPDX - License - Identifier : AGPL - 3.0 - or - later
2018-09-29 14:56:23 -04:00
*/
namespace OCA\TwoFactorBackupCodes\Notifications ;
2019-02-11 08:29:44 -05:00
use OCP\IURLGenerator ;
2018-09-29 14:56:23 -04:00
use OCP\L10N\IFactory ;
use OCP\Notification\INotification ;
use OCP\Notification\INotifier ;
2024-06-25 05:20:48 -04:00
use OCP\Notification\UnknownNotificationException ;
2018-09-29 14:56:23 -04:00
class Notifier implements INotifier {
/** @var IFactory */
private $factory ;
2019-02-11 08:29:44 -05:00
/** @var IURLGenerator */
private $url ;
public function __construct ( IFactory $factory , IURLGenerator $url ) {
2018-09-29 14:56:23 -04:00
$this -> factory = $factory ;
2019-02-11 08:29:44 -05:00
$this -> url = $url ;
2018-09-29 14:56:23 -04:00
}
2019-04-12 07:44:23 -04:00
/**
* Identifier of the notifier , only use [ a - z0 - 9_ ]
*
* @ return string
* @ since 17.0 . 0
*/
public function getID () : string {
return 'twofactor_backupcodes' ;
}
/**
* Human readable name describing the notifier
*
* @ return string
* @ since 17.0 . 0
*/
public function getName () : string {
return $this -> factory -> get ( 'twofactor_backupcodes' ) -> t ( 'Second-factor backup codes' );
}
public function prepare ( INotification $notification , string $languageCode ) : INotification {
2018-09-29 14:56:23 -04:00
if ( $notification -> getApp () !== 'twofactor_backupcodes' ) {
// Not my app => throw
2024-06-25 05:20:48 -04:00
throw new UnknownNotificationException ();
2018-09-29 14:56:23 -04:00
}
// Read the language from the notification
$l = $this -> factory -> get ( 'twofactor_backupcodes' , $languageCode );
switch ( $notification -> getSubject ()) {
case 'create_backupcodes' :
$notification -> setParsedSubject (
$l -> t ( 'Generate backup codes' )
) -> setParsedMessage (
2019-07-16 04:23:12 -04:00
$l -> t ( 'You enabled two-factor authentication but did not generate backup codes yet. They are needed to restore access to your account in case you lose your second factor.' )
2018-09-29 14:56:23 -04:00
);
2019-02-11 08:29:44 -05:00
$notification -> setLink ( $this -> url -> linkToRouteAbsolute ( 'settings.PersonalSettings.index' , [ 'section' => 'security' ]));
2019-07-16 04:23:34 -04:00
$notification -> setIcon ( $this -> url -> getAbsoluteURL ( $this -> url -> imagePath ( 'core' , 'actions/password.svg' )));
2018-09-29 14:56:23 -04:00
return $notification ;
default :
// Unknown subject => Unknown notification => throw
2024-06-25 05:20:48 -04:00
throw new UnknownNotificationException ();
2018-09-29 14:56:23 -04:00
}
}
}