Application.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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\Files_Sharing\AppInfo;
  8. use OC\Group\DisplayNameCache as GroupDisplayNameCache;
  9. use OC\Share\Share;
  10. use OC\User\DisplayNameCache;
  11. use OCA\Files\Event\LoadAdditionalScriptsEvent;
  12. use OCA\Files\Event\LoadSidebar;
  13. use OCA\Files_Sharing\Capabilities;
  14. use OCA\Files_Sharing\External\Manager;
  15. use OCA\Files_Sharing\External\MountProvider as ExternalMountProvider;
  16. use OCA\Files_Sharing\Helper;
  17. use OCA\Files_Sharing\Listener\BeforeDirectFileDownloadListener;
  18. use OCA\Files_Sharing\Listener\BeforeZipCreatedListener;
  19. use OCA\Files_Sharing\Listener\LoadAdditionalListener;
  20. use OCA\Files_Sharing\Listener\LoadPublicFileRequestAuthListener;
  21. use OCA\Files_Sharing\Listener\LoadSidebarListener;
  22. use OCA\Files_Sharing\Listener\ShareInteractionListener;
  23. use OCA\Files_Sharing\Listener\UserAddedToGroupListener;
  24. use OCA\Files_Sharing\Listener\UserShareAcceptanceListener;
  25. use OCA\Files_Sharing\Middleware\OCSShareAPIMiddleware;
  26. use OCA\Files_Sharing\Middleware\ShareInfoMiddleware;
  27. use OCA\Files_Sharing\Middleware\SharingCheckMiddleware;
  28. use OCA\Files_Sharing\MountProvider;
  29. use OCA\Files_Sharing\Notification\Listener;
  30. use OCA\Files_Sharing\Notification\Notifier;
  31. use OCA\Files_Sharing\ShareBackend\File;
  32. use OCA\Files_Sharing\ShareBackend\Folder;
  33. use OCP\AppFramework\App;
  34. use OCP\AppFramework\Bootstrap\IBootContext;
  35. use OCP\AppFramework\Bootstrap\IBootstrap;
  36. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  37. use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
  38. use OCP\Collaboration\Resources\LoadAdditionalScriptsEvent as ResourcesLoadAdditionalScriptsEvent;
  39. use OCP\EventDispatcher\IEventDispatcher;
  40. use OCP\Federation\ICloudIdManager;
  41. use OCP\Files\Config\IMountProviderCollection;
  42. use OCP\Files\Events\BeforeDirectFileDownloadEvent;
  43. use OCP\Files\Events\BeforeZipCreatedEvent;
  44. use OCP\Group\Events\GroupChangedEvent;
  45. use OCP\Group\Events\GroupDeletedEvent;
  46. use OCP\Group\Events\UserAddedEvent;
  47. use OCP\IDBConnection;
  48. use OCP\IGroup;
  49. use OCP\Share\Events\ShareCreatedEvent;
  50. use OCP\User\Events\UserChangedEvent;
  51. use OCP\User\Events\UserDeletedEvent;
  52. use OCP\Util;
  53. use Psr\Container\ContainerInterface;
  54. use Symfony\Component\EventDispatcher\GenericEvent as OldGenericEvent;
  55. class Application extends App implements IBootstrap {
  56. public const APP_ID = 'files_sharing';
  57. public function __construct(array $urlParams = []) {
  58. parent::__construct(self::APP_ID, $urlParams);
  59. }
  60. public function register(IRegistrationContext $context): void {
  61. $context->registerService(ExternalMountProvider::class, function (ContainerInterface $c) {
  62. return new ExternalMountProvider(
  63. $c->get(IDBConnection::class),
  64. function () use ($c) {
  65. return $c->get(Manager::class);
  66. },
  67. $c->get(ICloudIdManager::class)
  68. );
  69. });
  70. /**
  71. * Middleware
  72. */
  73. $context->registerMiddleWare(SharingCheckMiddleware::class);
  74. $context->registerMiddleWare(OCSShareAPIMiddleware::class);
  75. $context->registerMiddleWare(ShareInfoMiddleware::class);
  76. $context->registerCapability(Capabilities::class);
  77. $context->registerNotifierService(Notifier::class);
  78. $context->registerEventListener(UserChangedEvent::class, DisplayNameCache::class);
  79. $context->registerEventListener(UserDeletedEvent::class, DisplayNameCache::class);
  80. $context->registerEventListener(GroupChangedEvent::class, GroupDisplayNameCache::class);
  81. $context->registerEventListener(GroupDeletedEvent::class, GroupDisplayNameCache::class);
  82. // Sidebar and files scripts
  83. $context->registerEventListener(LoadAdditionalScriptsEvent::class, LoadAdditionalListener::class);
  84. $context->registerEventListener(LoadSidebar::class, LoadSidebarListener::class);
  85. $context->registerEventListener(ShareCreatedEvent::class, ShareInteractionListener::class);
  86. $context->registerEventListener(ShareCreatedEvent::class, UserShareAcceptanceListener::class);
  87. $context->registerEventListener(UserAddedEvent::class, UserAddedToGroupListener::class);
  88. // Handle download events for view only checks
  89. $context->registerEventListener(BeforeZipCreatedEvent::class, BeforeZipCreatedListener::class);
  90. $context->registerEventListener(BeforeDirectFileDownloadEvent::class, BeforeDirectFileDownloadListener::class);
  91. // File request auth
  92. $context->registerEventListener(BeforeTemplateRenderedEvent::class, LoadPublicFileRequestAuthListener::class);
  93. }
  94. public function boot(IBootContext $context): void {
  95. $context->injectFn([$this, 'registerMountProviders']);
  96. $context->injectFn([$this, 'registerEventsScripts']);
  97. Helper::registerHooks();
  98. Share::registerBackend('file', File::class);
  99. Share::registerBackend('folder', Folder::class, 'file');
  100. }
  101. public function registerMountProviders(IMountProviderCollection $mountProviderCollection, MountProvider $mountProvider, ExternalMountProvider $externalMountProvider): void {
  102. $mountProviderCollection->registerProvider($mountProvider);
  103. $mountProviderCollection->registerProvider($externalMountProvider);
  104. }
  105. public function registerEventsScripts(IEventDispatcher $dispatcher): void {
  106. $dispatcher->addListener(ResourcesLoadAdditionalScriptsEvent::class, function () {
  107. \OCP\Util::addScript('files_sharing', 'collaboration');
  108. });
  109. $dispatcher->addListener(\OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent::class, function () {
  110. /**
  111. * Always add main sharing script
  112. */
  113. Util::addScript(self::APP_ID, 'main');
  114. });
  115. // notifications api to accept incoming user shares
  116. $dispatcher->addListener(ShareCreatedEvent::class, function (ShareCreatedEvent $event) {
  117. /** @var Listener $listener */
  118. $listener = $this->getContainer()->query(Listener::class);
  119. $listener->shareNotification($event);
  120. });
  121. $dispatcher->addListener(IGroup::class . '::postAddUser', function ($event) {
  122. if (!$event instanceof OldGenericEvent) {
  123. return;
  124. }
  125. /** @var Listener $listener */
  126. $listener = $this->getContainer()->query(Listener::class);
  127. $listener->userAddedToGroup($event);
  128. });
  129. }
  130. }