Notifier.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. public function __construct(
  15. private IFactory $factory,
  16. private IURLGenerator $url,
  17. ) {
  18. }
  19. /**
  20. * Identifier of the notifier, only use [a-z0-9_]
  21. *
  22. * @return string
  23. * @since 17.0.0
  24. */
  25. public function getID(): string {
  26. return 'twofactor_backupcodes';
  27. }
  28. /**
  29. * Human readable name describing the notifier
  30. *
  31. * @return string
  32. * @since 17.0.0
  33. */
  34. public function getName(): string {
  35. return $this->factory->get('twofactor_backupcodes')->t('Second-factor backup codes');
  36. }
  37. public function prepare(INotification $notification, string $languageCode): INotification {
  38. if ($notification->getApp() !== 'twofactor_backupcodes') {
  39. // Not my app => throw
  40. throw new UnknownNotificationException();
  41. }
  42. // Read the language from the notification
  43. $l = $this->factory->get('twofactor_backupcodes', $languageCode);
  44. switch ($notification->getSubject()) {
  45. case 'create_backupcodes':
  46. $notification->setParsedSubject(
  47. $l->t('Generate backup codes')
  48. )->setParsedMessage(
  49. $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.')
  50. );
  51. $notification->setLink($this->url->linkToRouteAbsolute('settings.PersonalSettings.index', ['section' => 'security']));
  52. $notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/password.svg')));
  53. return $notification;
  54. default:
  55. // Unknown subject => Unknown notification => throw
  56. throw new UnknownNotificationException();
  57. }
  58. }
  59. }