SecurityProvider.php 2.8 KB

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