Application.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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\GroupDeletedEvent;
  68. use OCP\Group\Events\UserAddedEvent;
  69. use OCP\IDBConnection;
  70. use OCP\IGroup;
  71. use OCP\IUserSession;
  72. use OCP\Share\Events\ShareCreatedEvent;
  73. use OCP\User\Events\UserChangedEvent;
  74. use OCP\User\Events\UserDeletedEvent;
  75. use OCP\Util;
  76. use Psr\Container\ContainerInterface;
  77. use Symfony\Component\EventDispatcher\GenericEvent as OldGenericEvent;
  78. class Application extends App implements IBootstrap {
  79. public const APP_ID = 'files_sharing';
  80. public function __construct(array $urlParams = []) {
  81. parent::__construct(self::APP_ID, $urlParams);
  82. }
  83. public function register(IRegistrationContext $context): void {
  84. $context->registerService(ExternalMountProvider::class, function (ContainerInterface $c) {
  85. return new ExternalMountProvider(
  86. $c->get(IDBConnection::class),
  87. function () use ($c) {
  88. return $c->get(Manager::class);
  89. },
  90. $c->get(ICloudIdManager::class)
  91. );
  92. });
  93. /**
  94. * Middleware
  95. */
  96. $context->registerMiddleWare(SharingCheckMiddleware::class);
  97. $context->registerMiddleWare(OCSShareAPIMiddleware::class);
  98. $context->registerMiddleWare(ShareInfoMiddleware::class);
  99. $context->registerCapability(Capabilities::class);
  100. $context->registerNotifierService(Notifier::class);
  101. $context->registerEventListener(UserChangedEvent::class, DisplayNameCache::class);
  102. $context->registerEventListener(UserDeletedEvent::class, DisplayNameCache::class);
  103. $context->registerEventListener(GroupChangedEvent::class, GroupDisplayNameCache::class);
  104. $context->registerEventListener(GroupDeletedEvent::class, GroupDisplayNameCache::class);
  105. }
  106. public function boot(IBootContext $context): void {
  107. $context->injectFn([$this, 'registerMountProviders']);
  108. $context->injectFn([$this, 'registerEventsScripts']);
  109. $context->injectFn([$this, 'registerDownloadEvents']);
  110. Helper::registerHooks();
  111. Share::registerBackend('file', File::class);
  112. Share::registerBackend('folder', Folder::class, 'file');
  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. $dispatcher->addListener(\OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent::class, function () {
  129. /**
  130. * Always add main sharing script
  131. */
  132. Util::addScript(self::APP_ID, 'main');
  133. });
  134. // notifications api to accept incoming user shares
  135. $dispatcher->addListener(ShareCreatedEvent::class, function (ShareCreatedEvent $event) {
  136. /** @var Listener $listener */
  137. $listener = $this->getContainer()->query(Listener::class);
  138. $listener->shareNotification($event);
  139. });
  140. $dispatcher->addListener(IGroup::class . '::postAddUser', function ($event) {
  141. if (!$event instanceof OldGenericEvent) {
  142. return;
  143. }
  144. /** @var Listener $listener */
  145. $listener = $this->getContainer()->query(Listener::class);
  146. $listener->userAddedToGroup($event);
  147. });
  148. }
  149. public function registerDownloadEvents(
  150. IEventDispatcher $dispatcher,
  151. IUserSession $userSession,
  152. IRootFolder $rootFolder
  153. ): void {
  154. $dispatcher->addListener(
  155. BeforeDirectFileDownloadEvent::class,
  156. function (BeforeDirectFileDownloadEvent $event) use ($userSession, $rootFolder): void {
  157. $pathsToCheck = [$event->getPath()];
  158. // Check only for user/group shares. Don't restrict e.g. share links
  159. $user = $userSession->getUser();
  160. if ($user) {
  161. $viewOnlyHandler = new ViewOnly(
  162. $rootFolder->getUserFolder($user->getUID())
  163. );
  164. if (!$viewOnlyHandler->check($pathsToCheck)) {
  165. $event->setSuccessful(false);
  166. $event->setErrorMessage('Access to this resource or one of its sub-items has been denied.');
  167. }
  168. }
  169. }
  170. );
  171. $dispatcher->addListener(
  172. BeforeZipCreatedEvent::class,
  173. function (BeforeZipCreatedEvent $event) use ($userSession, $rootFolder): void {
  174. $dir = $event->getDirectory();
  175. $files = $event->getFiles();
  176. $pathsToCheck = [];
  177. foreach ($files as $file) {
  178. $pathsToCheck[] = $dir . '/' . $file;
  179. }
  180. // Check only for user/group shares. Don't restrict e.g. share links
  181. $user = $userSession->getUser();
  182. if ($user) {
  183. $viewOnlyHandler = new ViewOnly(
  184. $rootFolder->getUserFolder($user->getUID())
  185. );
  186. if (!$viewOnlyHandler->check($pathsToCheck)) {
  187. $event->setErrorMessage('Access to this resource or one of its sub-items has been denied.');
  188. $event->setSuccessful(false);
  189. } else {
  190. $event->setSuccessful(true);
  191. }
  192. } else {
  193. $event->setSuccessful(true);
  194. }
  195. }
  196. );
  197. }
  198. }