Application.php 5.7 KB

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