SecurityProvider.php 2.9 KB

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