1
0

Application.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 OCA\Encryption\Crypto\Crypt;
  9. use OCA\Encryption\Crypto\DecryptAll;
  10. use OCA\Encryption\Crypto\EncryptAll;
  11. use OCA\Encryption\Crypto\Encryption;
  12. use OCA\Encryption\HookManager;
  13. use OCA\Encryption\Hooks\UserHooks;
  14. use OCA\Encryption\KeyManager;
  15. use OCA\Encryption\Recovery;
  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\IConfig;
  25. use Psr\Log\LoggerInterface;
  26. class Application extends App implements IBootstrap {
  27. public const APP_ID = 'encryption';
  28. public function __construct(array $urlParams = []) {
  29. parent::__construct(self::APP_ID, $urlParams);
  30. }
  31. public function register(IRegistrationContext $context): void {
  32. }
  33. public function boot(IBootContext $context): void {
  34. \OCP\Util::addScript(self::APP_ID, 'encryption');
  35. $context->injectFn(function (IManager $encryptionManager) use ($context): void {
  36. if (!($encryptionManager instanceof \OC\Encryption\Manager)) {
  37. return;
  38. }
  39. if (!$encryptionManager->isReady()) {
  40. return;
  41. }
  42. $context->injectFn($this->registerEncryptionModule(...));
  43. $context->injectFn($this->registerHooks(...));
  44. $context->injectFn($this->setUp(...));
  45. });
  46. }
  47. public function setUp(IManager $encryptionManager) {
  48. if ($encryptionManager->isEnabled()) {
  49. /** @var Setup $setup */
  50. $setup = $this->getContainer()->query(Setup::class);
  51. $setup->setupSystem();
  52. }
  53. }
  54. /**
  55. * register hooks
  56. */
  57. public function registerHooks(IConfig $config) {
  58. if (!$config->getSystemValueBool('maintenance')) {
  59. $container = $this->getContainer();
  60. $server = $container->getServer();
  61. // Register our hooks and fire them.
  62. $hookManager = new HookManager();
  63. $hookManager->registerHook([
  64. new UserHooks($container->query(KeyManager::class),
  65. $server->getUserManager(),
  66. $server->get(LoggerInterface::class),
  67. $container->query(Setup::class),
  68. $server->getUserSession(),
  69. $container->query(Util::class),
  70. $container->query(Session::class),
  71. $container->query(Crypt::class),
  72. $container->query(Recovery::class))
  73. ]);
  74. $hookManager->fireHooks();
  75. } else {
  76. // Logout user if we are in maintenance to force re-login
  77. $this->getContainer()->getServer()->getUserSession()->logout();
  78. }
  79. }
  80. public function registerEncryptionModule(IManager $encryptionManager) {
  81. $container = $this->getContainer();
  82. $encryptionManager->registerEncryptionModule(
  83. Encryption::ID,
  84. Encryption::DISPLAY_NAME,
  85. function () use ($container) {
  86. return new Encryption(
  87. $container->query(Crypt::class),
  88. $container->query(KeyManager::class),
  89. $container->query(Util::class),
  90. $container->query(Session::class),
  91. $container->query(EncryptAll::class),
  92. $container->query(DecryptAll::class),
  93. $container->getServer()->get(LoggerInterface::class),
  94. $container->getServer()->getL10N($container->getAppName())
  95. );
  96. });
  97. }
  98. }