SecurityProvider.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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@owncloud.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Settings\Activity;
  26. use InvalidArgumentException;
  27. use OCP\Activity\IEvent;
  28. use OCP\Activity\IManager;
  29. use OCP\Activity\IProvider;
  30. use OCP\IURLGenerator;
  31. use OCP\L10N\IFactory as L10nFactory;
  32. class SecurityProvider implements IProvider {
  33. /** @var L10nFactory */
  34. private $l10n;
  35. /** @var IURLGenerator */
  36. private $urlGenerator;
  37. /** @var IManager */
  38. private $activityManager;
  39. public function __construct(L10nFactory $l10n, IURLGenerator $urlGenerator, IManager $activityManager) {
  40. $this->urlGenerator = $urlGenerator;
  41. $this->l10n = $l10n;
  42. $this->activityManager = $activityManager;
  43. }
  44. public function parse($language, IEvent $event, IEvent $previousEvent = null) {
  45. if ($event->getType() !== 'security') {
  46. throw new InvalidArgumentException();
  47. }
  48. $l = $this->l10n->get('settings', $language);
  49. switch ($event->getSubject()) {
  50. case 'twofactor_success':
  51. $params = $event->getSubjectParameters();
  52. $event->setParsedSubject($l->t('You successfully logged in using two-factor authentication (%1$s)', [
  53. $params['provider'],
  54. ]));
  55. if ($this->activityManager->getRequirePNG()) {
  56. $event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/password.png')));
  57. } else {
  58. $event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/password.svg')));
  59. }
  60. break;
  61. case 'twofactor_failed':
  62. $params = $event->getSubjectParameters();
  63. $event->setParsedSubject($l->t('A login attempt using two-factor authentication failed (%1$s)', [
  64. $params['provider'],
  65. ]));
  66. if ($this->activityManager->getRequirePNG()) {
  67. $event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/password.png')));
  68. } else {
  69. $event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/password.svg')));
  70. }
  71. break;
  72. case 'remote_wipe_start':
  73. $params = $event->getSubjectParameters();
  74. $event->setParsedSubject($l->t('Remote wipe was started on %1$s', [
  75. $params['name'],
  76. ]));
  77. if ($this->activityManager->getRequirePNG()) {
  78. $event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/delete.png')));
  79. } else {
  80. $event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/delete.svg')));
  81. }
  82. break;
  83. case 'remote_wipe_finish':
  84. $params = $event->getSubjectParameters();
  85. $event->setParsedSubject($l->t('Remote wipe has finished on %1$s', [
  86. $params['name'],
  87. ]));
  88. if ($this->activityManager->getRequirePNG()) {
  89. $event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/delete.png')));
  90. } else {
  91. $event->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/delete.svg')));
  92. }
  93. break;
  94. default:
  95. throw new InvalidArgumentException();
  96. }
  97. return $event;
  98. }
  99. }