Application.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
  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\TwoFactorBackupCodes\AppInfo;
  27. use Closure;
  28. use OCA\TwoFactorBackupCodes\Db\BackupCodeMapper;
  29. use OCA\TwoFactorBackupCodes\Event\CodesGenerated;
  30. use OCA\TwoFactorBackupCodes\Listener\ActivityPublisher;
  31. use OCA\TwoFactorBackupCodes\Listener\ClearNotifications;
  32. use OCA\TwoFactorBackupCodes\Listener\ProviderDisabled;
  33. use OCA\TwoFactorBackupCodes\Listener\ProviderEnabled;
  34. use OCA\TwoFactorBackupCodes\Listener\RegistryUpdater;
  35. use OCA\TwoFactorBackupCodes\Notifications\Notifier;
  36. use OCP\AppFramework\App;
  37. use OCP\AppFramework\Bootstrap\IBootContext;
  38. use OCP\AppFramework\Bootstrap\IBootstrap;
  39. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  40. use OCP\Authentication\TwoFactorAuth\IRegistry;
  41. use OCP\Notification\IManager;
  42. use OCP\Util;
  43. class Application extends App implements IBootstrap {
  44. public const APP_ID = 'twofactor_backupcodes';
  45. public function __construct() {
  46. parent::__construct(self::APP_ID);
  47. }
  48. public function register(IRegistrationContext $context): void {
  49. $this->registerHooksAndEvents($context);
  50. }
  51. public function boot(IBootContext $context): void {
  52. Util::connectHook('OC_User', 'post_deleteUser', $this, 'deleteUser');
  53. $context->injectFn(Closure::fromCallable([$this, 'registerNotification']));
  54. }
  55. /**
  56. * Register the hooks and events
  57. */
  58. public function registerHooksAndEvents(IRegistrationContext $context) {
  59. $context->registerEventListener(CodesGenerated::class, ActivityPublisher::class);
  60. $context->registerEventListener(CodesGenerated::class, RegistryUpdater::class);
  61. $context->registerEventListener(CodesGenerated::class, ClearNotifications::class);
  62. $context->registerEventListener(IRegistry::EVENT_PROVIDER_ENABLED, ProviderEnabled::class);
  63. $context->registerEventListener(IRegistry::EVENT_PROVIDER_DISABLED, ProviderDisabled::class);
  64. }
  65. private function registerNotification(IManager $manager) {
  66. $manager->registerNotifierService(Notifier::class);
  67. }
  68. public function deleteUser($params) {
  69. /** @var BackupCodeMapper $mapper */
  70. $mapper = $this->getContainer()->query(BackupCodeMapper::class);
  71. $mapper->deleteCodesByUserId($params['uid']);
  72. }
  73. }