Notifier.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Jan-Christoph Borchardt <hey@jancborchardt.net>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\TwoFactorBackupCodes\Notifications;
  27. use OCP\IURLGenerator;
  28. use OCP\L10N\IFactory;
  29. use OCP\Notification\INotification;
  30. use OCP\Notification\INotifier;
  31. class Notifier implements INotifier {
  32. /** @var IFactory */
  33. private $factory;
  34. /** @var IURLGenerator */
  35. private $url;
  36. public function __construct(IFactory $factory, IURLGenerator $url) {
  37. $this->factory = $factory;
  38. $this->url = $url;
  39. }
  40. /**
  41. * Identifier of the notifier, only use [a-z0-9_]
  42. *
  43. * @return string
  44. * @since 17.0.0
  45. */
  46. public function getID(): string {
  47. return 'twofactor_backupcodes';
  48. }
  49. /**
  50. * Human readable name describing the notifier
  51. *
  52. * @return string
  53. * @since 17.0.0
  54. */
  55. public function getName(): string {
  56. return $this->factory->get('twofactor_backupcodes')->t('Second-factor backup codes');
  57. }
  58. public function prepare(INotification $notification, string $languageCode): INotification {
  59. if ($notification->getApp() !== 'twofactor_backupcodes') {
  60. // Not my app => throw
  61. throw new \InvalidArgumentException();
  62. }
  63. // Read the language from the notification
  64. $l = $this->factory->get('twofactor_backupcodes', $languageCode);
  65. switch ($notification->getSubject()) {
  66. case 'create_backupcodes':
  67. $notification->setParsedSubject(
  68. $l->t('Generate backup codes')
  69. )->setParsedMessage(
  70. $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.')
  71. );
  72. $notification->setLink($this->url->linkToRouteAbsolute('settings.PersonalSettings.index', ['section' => 'security']));
  73. $notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/password.svg')));
  74. return $notification;
  75. default:
  76. // Unknown subject => Unknown notification => throw
  77. throw new \InvalidArgumentException();
  78. }
  79. }
  80. }