Notifier.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\TwoFactorBackupCodes\Notifications;
  8. use OCP\IURLGenerator;
  9. use OCP\L10N\IFactory;
  10. use OCP\Notification\INotification;
  11. use OCP\Notification\INotifier;
  12. use OCP\Notification\UnknownNotificationException;
  13. class Notifier implements INotifier {
  14. /** @var IFactory */
  15. private $factory;
  16. /** @var IURLGenerator */
  17. private $url;
  18. public function __construct(IFactory $factory, IURLGenerator $url) {
  19. $this->factory = $factory;
  20. $this->url = $url;
  21. }
  22. /**
  23. * Identifier of the notifier, only use [a-z0-9_]
  24. *
  25. * @return string
  26. * @since 17.0.0
  27. */
  28. public function getID(): string {
  29. return 'twofactor_backupcodes';
  30. }
  31. /**
  32. * Human readable name describing the notifier
  33. *
  34. * @return string
  35. * @since 17.0.0
  36. */
  37. public function getName(): string {
  38. return $this->factory->get('twofactor_backupcodes')->t('Second-factor backup codes');
  39. }
  40. public function prepare(INotification $notification, string $languageCode): INotification {
  41. if ($notification->getApp() !== 'twofactor_backupcodes') {
  42. // Not my app => throw
  43. throw new UnknownNotificationException();
  44. }
  45. // Read the language from the notification
  46. $l = $this->factory->get('twofactor_backupcodes', $languageCode);
  47. switch ($notification->getSubject()) {
  48. case 'create_backupcodes':
  49. $notification->setParsedSubject(
  50. $l->t('Generate backup codes')
  51. )->setParsedMessage(
  52. $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.')
  53. );
  54. $notification->setLink($this->url->linkToRouteAbsolute('settings.PersonalSettings.index', ['section' => 'security']));
  55. $notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/password.svg')));
  56. return $notification;
  57. default:
  58. // Unknown subject => Unknown notification => throw
  59. throw new UnknownNotificationException();
  60. }
  61. }
  62. }