Application.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author John Molakvoæ <skjnldsv@protonmail.com>
  9. * @author Julius Härtl <jus@bitgrid.net>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OCA\Files_Sharing\AppInfo;
  31. use OC\Group\DisplayNameCache as GroupDisplayNameCache;
  32. use OC\Share\Share;
  33. use OC\User\DisplayNameCache;
  34. use OCA\Files_Sharing\Capabilities;
  35. use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent;
  36. use OCA\Files_Sharing\External\Manager;
  37. use OCA\Files_Sharing\External\MountProvider as ExternalMountProvider;
  38. use OCA\Files_Sharing\Helper;
  39. use OCA\Files_Sharing\Listener\LoadAdditionalListener;
  40. use OCA\Files_Sharing\Listener\LoadSidebarListener;
  41. use OCA\Files_Sharing\Listener\ShareInteractionListener;
  42. use OCA\Files_Sharing\Listener\UserAddedToGroupListener;
  43. use OCA\Files_Sharing\Listener\UserShareAcceptanceListener;
  44. use OCA\Files_Sharing\Middleware\OCSShareAPIMiddleware;
  45. use OCA\Files_Sharing\Middleware\ShareInfoMiddleware;
  46. use OCA\Files_Sharing\Middleware\SharingCheckMiddleware;
  47. use OCA\Files_Sharing\MountProvider;
  48. use OCA\Files_Sharing\Notification\Listener;
  49. use OCA\Files_Sharing\Notification\Notifier;
  50. use OCA\Files\Event\LoadAdditionalScriptsEvent;
  51. use OCA\Files\Event\LoadSidebar;
  52. use OCA\Files_Sharing\ShareBackend\File;
  53. use OCA\Files_Sharing\ShareBackend\Folder;
  54. use OCA\Files_Sharing\ViewOnly;
  55. use OCP\AppFramework\App;
  56. use OCP\AppFramework\Bootstrap\IBootContext;
  57. use OCP\AppFramework\Bootstrap\IBootstrap;
  58. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  59. use OCP\Collaboration\Resources\LoadAdditionalScriptsEvent as ResourcesLoadAdditionalScriptsEvent;
  60. use OCP\EventDispatcher\IEventDispatcher;
  61. use OCP\Federation\ICloudIdManager;
  62. use OCP\Files\Config\IMountProviderCollection;
  63. use OCP\Files\Events\BeforeDirectFileDownloadEvent;
  64. use OCP\Files\Events\BeforeZipCreatedEvent;
  65. use OCP\Files\IRootFolder;
  66. use OCP\Group\Events\GroupChangedEvent;
  67. use OCP\Group\Events\UserAddedEvent;
  68. use OCP\IDBConnection;
  69. use OCP\IGroup;
  70. use OCP\IUserSession;
  71. use OCP\Share\Events\ShareCreatedEvent;
  72. use OCP\User\Events\UserChangedEvent;
  73. use OCP\Util;
  74. use Psr\Container\ContainerInterface;
  75. use Symfony\Component\EventDispatcher\GenericEvent as OldGenericEvent;
  76. class Application extends App implements IBootstrap {
  77. public const APP_ID = 'files_sharing';
  78. public function __construct(array $urlParams = []) {
  79. parent::__construct(self::APP_ID, $urlParams);
  80. }
  81. public function register(IRegistrationContext $context): void {
  82. $context->registerService(ExternalMountProvider::class, function (ContainerInterface $c) {
  83. return new ExternalMountProvider(
  84. $c->get(IDBConnection::class),
  85. function () use ($c) {
  86. return $c->get(Manager::class);
  87. },
  88. $c->get(ICloudIdManager::class)
  89. );
  90. });
  91. /**
  92. * Middleware
  93. */
  94. $context->registerMiddleWare(SharingCheckMiddleware::class);
  95. $context->registerMiddleWare(OCSShareAPIMiddleware::class);
  96. $context->registerMiddleWare(ShareInfoMiddleware::class);
  97. $context->registerCapability(Capabilities::class);
  98. $context->registerNotifierService(Notifier::class);
  99. $context->registerEventListener(UserChangedEvent::class, DisplayNameCache::class);
  100. $context->registerEventListener(GroupChangedEvent::class, GroupDisplayNameCache::class);
  101. }
  102. public function boot(IBootContext $context): void {
  103. $context->injectFn([$this, 'registerMountProviders']);
  104. $context->injectFn([$this, 'registerEventsScripts']);
  105. $context->injectFn([$this, 'registerDownloadEvents']);
  106. Helper::registerHooks();
  107. Share::registerBackend('file', File::class);
  108. Share::registerBackend('folder', Folder::class, 'file');
  109. /**
  110. * Always add main sharing script
  111. */
  112. Util::addScript(self::APP_ID, 'main');
  113. }
  114. public function registerMountProviders(IMountProviderCollection $mountProviderCollection, MountProvider $mountProvider, ExternalMountProvider $externalMountProvider): void {
  115. $mountProviderCollection->registerProvider($mountProvider);
  116. $mountProviderCollection->registerProvider($externalMountProvider);
  117. }
  118. public function registerEventsScripts(IEventDispatcher $dispatcher): void {
  119. // sidebar and files scripts
  120. $dispatcher->addServiceListener(LoadAdditionalScriptsEvent::class, LoadAdditionalListener::class);
  121. $dispatcher->addServiceListener(LoadSidebar::class, LoadSidebarListener::class);
  122. $dispatcher->addServiceListener(ShareCreatedEvent::class, ShareInteractionListener::class);
  123. $dispatcher->addServiceListener(ShareCreatedEvent::class, UserShareAcceptanceListener::class);
  124. $dispatcher->addServiceListener(UserAddedEvent::class, UserAddedToGroupListener::class);
  125. $dispatcher->addListener(ResourcesLoadAdditionalScriptsEvent::class, function () {
  126. \OCP\Util::addScript('files_sharing', 'collaboration');
  127. });
  128. // notifications api to accept incoming user shares
  129. $dispatcher->addListener(ShareCreatedEvent::class, function (ShareCreatedEvent $event) {
  130. /** @var Listener $listener */
  131. $listener = $this->getContainer()->query(Listener::class);
  132. $listener->shareNotification($event);
  133. });
  134. $dispatcher->addListener(IGroup::class . '::postAddUser', function ($event) {
  135. if (!$event instanceof OldGenericEvent) {
  136. return;
  137. }
  138. /** @var Listener $listener */
  139. $listener = $this->getContainer()->query(Listener::class);
  140. $listener->userAddedToGroup($event);
  141. });
  142. }
  143. public function registerDownloadEvents(
  144. IEventDispatcher $dispatcher,
  145. IUserSession $userSession,
  146. IRootFolder $rootFolder
  147. ): void {
  148. $dispatcher->addListener(
  149. BeforeDirectFileDownloadEvent::class,
  150. function (BeforeDirectFileDownloadEvent $event) use ($userSession, $rootFolder): void {
  151. $pathsToCheck = [$event->getPath()];
  152. // Check only for user/group shares. Don't restrict e.g. share links
  153. $user = $userSession->getUser();
  154. if ($user) {
  155. $viewOnlyHandler = new ViewOnly(
  156. $rootFolder->getUserFolder($user->getUID())
  157. );
  158. if (!$viewOnlyHandler->check($pathsToCheck)) {
  159. $event->setSuccessful(false);
  160. $event->setErrorMessage('Access to this resource or one of its sub-items has been denied.');
  161. }
  162. }
  163. }
  164. );
  165. $dispatcher->addListener(
  166. BeforeZipCreatedEvent::class,
  167. function (BeforeZipCreatedEvent $event) use ($userSession, $rootFolder): void {
  168. $dir = $event->getDirectory();
  169. $files = $event->getFiles();
  170. $pathsToCheck = [];
  171. foreach ($files as $file) {
  172. $pathsToCheck[] = $dir . '/' . $file;
  173. }
  174. // Check only for user/group shares. Don't restrict e.g. share links
  175. $user = $userSession->getUser();
  176. if ($user) {
  177. $viewOnlyHandler = new ViewOnly(
  178. $rootFolder->getUserFolder($user->getUID())
  179. );
  180. if (!$viewOnlyHandler->check($pathsToCheck)) {
  181. $event->setErrorMessage('Access to this resource or one of its sub-items has been denied.');
  182. $event->setSuccessful(false);
  183. } else {
  184. $event->setSuccessful(true);
  185. }
  186. } else {
  187. $event->setSuccessful(true);
  188. }
  189. }
  190. );
  191. }
  192. }