Application.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  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 OCA\TwoFactorBackupCodes\AppInfo;
  25. use OCA\TwoFactorBackupCodes\Db\BackupCodeMapper;
  26. use OCA\TwoFactorBackupCodes\Event\CodesGenerated;
  27. use OCA\TwoFactorBackupCodes\Listener\ActivityPublisher;
  28. use OCA\TwoFactorBackupCodes\Listener\ClearNotifications;
  29. use OCA\TwoFactorBackupCodes\Listener\ProviderDisabled;
  30. use OCA\TwoFactorBackupCodes\Listener\ProviderEnabled;
  31. use OCA\TwoFactorBackupCodes\Listener\RegistryUpdater;
  32. use OCA\TwoFactorBackupCodes\Notifications\Notifier;
  33. use OCP\AppFramework\App;
  34. use OCP\Authentication\TwoFactorAuth\IRegistry;
  35. use OCP\EventDispatcher\IEventDispatcher;
  36. use OCP\IL10N;
  37. use OCP\Notification\IManager;
  38. use OCP\Util;
  39. class Application extends App {
  40. public function __construct() {
  41. parent::__construct('twofactor_backupcodes');
  42. }
  43. /**
  44. * Register the different app parts
  45. */
  46. public function register() {
  47. $this->registerHooksAndEvents();
  48. $this->registerNotification();
  49. }
  50. /**
  51. * Register the hooks and events
  52. */
  53. public function registerHooksAndEvents() {
  54. Util::connectHook('OC_User', 'post_deleteUser', $this, 'deleteUser');
  55. $container = $this->getContainer();
  56. /** @var IEventDispatcher $eventDispatcher */
  57. $eventDispatcher = $container->query(IEventDispatcher::class);
  58. $eventDispatcher->addServiceListener(CodesGenerated::class, ActivityPublisher::class);
  59. $eventDispatcher->addServiceListener(CodesGenerated::class, RegistryUpdater::class);
  60. $eventDispatcher->addServiceListener(CodesGenerated::class, ClearNotifications::class);
  61. $eventDispatcher->addServiceListener(IRegistry::EVENT_PROVIDER_ENABLED, ProviderEnabled::class);
  62. $eventDispatcher->addServiceListener(IRegistry::EVENT_PROVIDER_DISABLED, ProviderDisabled::class);
  63. }
  64. public function registerNotification() {
  65. $container = $this->getContainer();
  66. /** @var IManager $manager */
  67. $manager = $container->query(IManager::class);
  68. $manager->registerNotifier(
  69. function() use ($container) {
  70. return $container->query(Notifier::class);
  71. },
  72. function () use ($container) {
  73. $l = $container->query(IL10N::class);
  74. return ['id' => 'twofactor_backupcodes', 'name' => $l->t('Second-factor backup codes')];
  75. }
  76. );
  77. }
  78. public function deleteUser($params) {
  79. /** @var BackupCodeMapper $mapper */
  80. $mapper = $this->getContainer()->query(BackupCodeMapper::class);
  81. $mapper->deleteCodesByUserId($params['uid']);
  82. }
  83. }