Application.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Encryption\AppInfo;
  8. use OC\Core\Events\BeforePasswordResetEvent;
  9. use OC\Core\Events\PasswordResetEvent;
  10. use OCA\Encryption\Crypto\Crypt;
  11. use OCA\Encryption\Crypto\DecryptAll;
  12. use OCA\Encryption\Crypto\EncryptAll;
  13. use OCA\Encryption\Crypto\Encryption;
  14. use OCA\Encryption\KeyManager;
  15. use OCA\Encryption\Listeners\UserEventsListener;
  16. use OCA\Encryption\Session;
  17. use OCA\Encryption\Users\Setup;
  18. use OCA\Encryption\Util;
  19. use OCP\AppFramework\App;
  20. use OCP\AppFramework\Bootstrap\IBootContext;
  21. use OCP\AppFramework\Bootstrap\IBootstrap;
  22. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  23. use OCP\Encryption\IManager;
  24. use OCP\EventDispatcher\IEventDispatcher;
  25. use OCP\IConfig;
  26. use OCP\IL10N;
  27. use OCP\IUserSession;
  28. use OCP\User\Events\BeforePasswordUpdatedEvent;
  29. use OCP\User\Events\PasswordUpdatedEvent;
  30. use OCP\User\Events\UserCreatedEvent;
  31. use OCP\User\Events\UserDeletedEvent;
  32. use Psr\Log\LoggerInterface;
  33. class Application extends App implements IBootstrap {
  34. public const APP_ID = 'encryption';
  35. public function __construct(array $urlParams = []) {
  36. parent::__construct(self::APP_ID, $urlParams);
  37. }
  38. public function register(IRegistrationContext $context): void {
  39. }
  40. public function boot(IBootContext $context): void {
  41. \OCP\Util::addScript(self::APP_ID, 'encryption');
  42. $context->injectFn(function (IManager $encryptionManager) use ($context): void {
  43. if (!($encryptionManager instanceof \OC\Encryption\Manager)) {
  44. return;
  45. }
  46. if (!$encryptionManager->isReady()) {
  47. return;
  48. }
  49. $context->injectFn($this->registerEncryptionModule(...));
  50. $context->injectFn($this->registerEventListeners(...));
  51. $context->injectFn($this->setUp(...));
  52. });
  53. }
  54. public function setUp(IManager $encryptionManager) {
  55. if ($encryptionManager->isEnabled()) {
  56. /** @var Setup $setup */
  57. $setup = $this->getContainer()->get(Setup::class);
  58. $setup->setupSystem();
  59. }
  60. }
  61. public function registerEventListeners(IConfig $config, IEventDispatcher $eventDispatcher, IManager $encryptionManager): void {
  62. if (!$encryptionManager->isEnabled()) {
  63. return;
  64. }
  65. if ($config->getSystemValueBool('maintenance')) {
  66. // Logout user if we are in maintenance to force re-login
  67. $this->getContainer()->get(IUserSession::class)->logout();
  68. return;
  69. }
  70. // No maintenance so register all events
  71. $eventDispatcher->addServiceListener(UserCreatedEvent::class, UserEventsListener::class);
  72. $eventDispatcher->addServiceListener(UserDeletedEvent::class, UserEventsListener::class);
  73. $eventDispatcher->addServiceListener(BeforePasswordUpdatedEvent::class, UserEventsListener::class);
  74. $eventDispatcher->addServiceListener(PasswordUpdatedEvent::class, UserEventsListener::class);
  75. $eventDispatcher->addServiceListener(BeforePasswordResetEvent::class, UserEventsListener::class);
  76. $eventDispatcher->addServiceListener(PasswordResetEvent::class, UserEventsListener::class);
  77. }
  78. public function registerEncryptionModule(IManager $encryptionManager) {
  79. $container = $this->getContainer();
  80. $encryptionManager->registerEncryptionModule(
  81. Encryption::ID,
  82. Encryption::DISPLAY_NAME,
  83. function () use ($container) {
  84. return new Encryption(
  85. $container->get(Crypt::class),
  86. $container->get(KeyManager::class),
  87. $container->get(Util::class),
  88. $container->get(Session::class),
  89. $container->get(EncryptAll::class),
  90. $container->get(DecryptAll::class),
  91. $container->get(LoggerInterface::class),
  92. $container->get(IL10N::class),
  93. );
  94. });
  95. }
  96. }