SecurityProvider.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Settings\Activity;
  8. use OCP\Activity\Exceptions\UnknownActivityException;
  9. use OCP\Activity\IEvent;
  10. use OCP\Activity\IManager;
  11. use OCP\Activity\IProvider;
  12. use OCP\IURLGenerator;
  13. use OCP\L10N\IFactory as L10nFactory;
  14. class SecurityProvider implements IProvider {
  15. /** @var L10nFactory */
  16. private $l10n;
  17. /** @var IURLGenerator */
  18. private $urlGenerator;
  19. /** @var IManager */
  20. private $activityManager;
  21. public function __construct(L10nFactory $l10n, IURLGenerator $urlGenerator, IManager $activityManager) {
  22. $this->urlGenerator = $urlGenerator;
  23. $this->l10n = $l10n;
  24. $this->activityManager = $activityManager;
  25. }
  26. public function parse($language, IEvent $event, ?IEvent $previousEvent = null) {
  27. if ($event->getType() !== 'security') {
  28. throw new UnknownActivityException();
  29. }
  30. $l = $this->l10n->get('settings', $language);
  31. switch ($event->getSubject()) {
  32. case 'twofactor_success':
  33. $params = $event->getSubjectParameters();
  34. $event->setParsedSubject($l->t('You successfully logged in using two-factor authentication (%1$s)', [
  35. $params['provider'],
  36. ]));
  37. if ($this->activityManager->getRequirePNG()) {
  38. $event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/password.png')));
  39. } else {
  40. $event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/password.svg')));
  41. }
  42. break;
  43. case 'twofactor_failed':
  44. $params = $event->getSubjectParameters();
  45. $event->setParsedSubject($l->t('A login attempt using two-factor authentication failed (%1$s)', [
  46. $params['provider'],
  47. ]));
  48. if ($this->activityManager->getRequirePNG()) {
  49. $event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/password.png')));
  50. } else {
  51. $event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/password.svg')));
  52. }
  53. break;
  54. case 'remote_wipe_start':
  55. $params = $event->getSubjectParameters();
  56. $event->setParsedSubject($l->t('Remote wipe was started on %1$s', [
  57. $params['name'],
  58. ]));
  59. if ($this->activityManager->getRequirePNG()) {
  60. $event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/delete.png')));
  61. } else {
  62. $event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/delete.svg')));
  63. }
  64. break;
  65. case 'remote_wipe_finish':
  66. $params = $event->getSubjectParameters();
  67. $event->setParsedSubject($l->t('Remote wipe has finished on %1$s', [
  68. $params['name'],
  69. ]));
  70. if ($this->activityManager->getRequirePNG()) {
  71. $event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/delete.png')));
  72. } else {
  73. $event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/delete.svg')));
  74. }
  75. break;
  76. default:
  77. throw new UnknownActivityException();
  78. }
  79. return $event;
  80. }
  81. }