SecurityProvider.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  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\Settings\Activity;
  27. use InvalidArgumentException;
  28. use OCP\Activity\IEvent;
  29. use OCP\Activity\IManager;
  30. use OCP\Activity\IProvider;
  31. use OCP\IURLGenerator;
  32. use OCP\L10N\IFactory as L10nFactory;
  33. class SecurityProvider implements IProvider {
  34. /** @var L10nFactory */
  35. private $l10n;
  36. /** @var IURLGenerator */
  37. private $urlGenerator;
  38. /** @var IManager */
  39. private $activityManager;
  40. public function __construct(L10nFactory $l10n, IURLGenerator $urlGenerator, IManager $activityManager) {
  41. $this->urlGenerator = $urlGenerator;
  42. $this->l10n = $l10n;
  43. $this->activityManager = $activityManager;
  44. }
  45. public function parse($language, IEvent $event, ?IEvent $previousEvent = null) {
  46. if ($event->getType() !== 'security') {
  47. throw new InvalidArgumentException();
  48. }
  49. $l = $this->l10n->get('settings', $language);
  50. switch ($event->getSubject()) {
  51. case 'twofactor_success':
  52. $params = $event->getSubjectParameters();
  53. $event->setParsedSubject($l->t('You successfully logged in using two-factor authentication (%1$s)', [
  54. $params['provider'],
  55. ]));
  56. if ($this->activityManager->getRequirePNG()) {
  57. $event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/password.png')));
  58. } else {
  59. $event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/password.svg')));
  60. }
  61. break;
  62. case 'twofactor_failed':
  63. $params = $event->getSubjectParameters();
  64. $event->setParsedSubject($l->t('A login attempt using two-factor authentication failed (%1$s)', [
  65. $params['provider'],
  66. ]));
  67. if ($this->activityManager->getRequirePNG()) {
  68. $event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/password.png')));
  69. } else {
  70. $event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/password.svg')));
  71. }
  72. break;
  73. case 'remote_wipe_start':
  74. $params = $event->getSubjectParameters();
  75. $event->setParsedSubject($l->t('Remote wipe was started on %1$s', [
  76. $params['name'],
  77. ]));
  78. if ($this->activityManager->getRequirePNG()) {
  79. $event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/delete.png')));
  80. } else {
  81. $event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/delete.svg')));
  82. }
  83. break;
  84. case 'remote_wipe_finish':
  85. $params = $event->getSubjectParameters();
  86. $event->setParsedSubject($l->t('Remote wipe has finished on %1$s', [
  87. $params['name'],
  88. ]));
  89. if ($this->activityManager->getRequirePNG()) {
  90. $event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/delete.png')));
  91. } else {
  92. $event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/delete.svg')));
  93. }
  94. break;
  95. default:
  96. throw new InvalidArgumentException();
  97. }
  98. return $event;
  99. }
  100. }