Application.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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\Event\LoadAdditionalScriptsEvent;
  35. use OCA\Files\Event\LoadSidebar;
  36. use OCA\Files_Sharing\Capabilities;
  37. use OCA\Files_Sharing\External\Manager;
  38. use OCA\Files_Sharing\External\MountProvider as ExternalMountProvider;
  39. use OCA\Files_Sharing\Helper;
  40. use OCA\Files_Sharing\Listener\LoadAdditionalListener;
  41. use OCA\Files_Sharing\Listener\LoadSidebarListener;
  42. use OCA\Files_Sharing\Listener\ShareInteractionListener;
  43. use OCA\Files_Sharing\Listener\UserAddedToGroupListener;
  44. use OCA\Files_Sharing\Listener\UserShareAcceptanceListener;
  45. use OCA\Files_Sharing\Middleware\OCSShareAPIMiddleware;
  46. use OCA\Files_Sharing\Middleware\ShareInfoMiddleware;
  47. use OCA\Files_Sharing\Middleware\SharingCheckMiddleware;
  48. use OCA\Files_Sharing\MountProvider;
  49. use OCA\Files_Sharing\Notification\Listener;
  50. use OCA\Files_Sharing\Notification\Notifier;
  51. use OCA\Files_Sharing\ShareBackend\File;
  52. use OCA\Files_Sharing\ShareBackend\Folder;
  53. use OCA\Files_Sharing\ViewOnly;
  54. use OCP\AppFramework\App;
  55. use OCP\AppFramework\Bootstrap\IBootContext;
  56. use OCP\AppFramework\Bootstrap\IBootstrap;
  57. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  58. use OCP\Collaboration\Resources\LoadAdditionalScriptsEvent as ResourcesLoadAdditionalScriptsEvent;
  59. use OCP\EventDispatcher\IEventDispatcher;
  60. use OCP\Federation\ICloudIdManager;
  61. use OCP\Files\Config\IMountProviderCollection;
  62. use OCP\Files\Events\BeforeDirectFileDownloadEvent;
  63. use OCP\Files\Events\BeforeZipCreatedEvent;
  64. use OCP\Files\IRootFolder;
  65. use OCP\Group\Events\GroupChangedEvent;
  66. use OCP\Group\Events\GroupDeletedEvent;
  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\User\Events\UserDeletedEvent;
  74. use OCP\Util;
  75. use Psr\Container\ContainerInterface;
  76. use Symfony\Component\EventDispatcher\GenericEvent as OldGenericEvent;
  77. class Application extends App implements IBootstrap {
  78. public const APP_ID = 'files_sharing';
  79. public function __construct(array $urlParams = []) {
  80. parent::__construct(self::APP_ID, $urlParams);
  81. }
  82. public function register(IRegistrationContext $context): void {
  83. $context->registerService(ExternalMountProvider::class, function (ContainerInterface $c) {
  84. return new ExternalMountProvider(
  85. $c->get(IDBConnection::class),
  86. function () use ($c) {
  87. return $c->get(Manager::class);
  88. },
  89. $c->get(ICloudIdManager::class)
  90. );
  91. });
  92. /**
  93. * Middleware
  94. */
  95. $context->registerMiddleWare(SharingCheckMiddleware::class);
  96. $context->registerMiddleWare(OCSShareAPIMiddleware::class);
  97. $context->registerMiddleWare(ShareInfoMiddleware::class);
  98. $context->registerCapability(Capabilities::class);
  99. $context->registerNotifierService(Notifier::class);
  100. $context->registerEventListener(UserChangedEvent::class, DisplayNameCache::class);
  101. $context->registerEventListener(UserDeletedEvent::class, DisplayNameCache::class);
  102. $context->registerEventListener(GroupChangedEvent::class, GroupDisplayNameCache::class);
  103. $context->registerEventListener(GroupDeletedEvent::class, GroupDisplayNameCache::class);
  104. }
  105. public function boot(IBootContext $context): void {
  106. $context->injectFn([$this, 'registerMountProviders']);
  107. $context->injectFn([$this, 'registerEventsScripts']);
  108. $context->injectFn([$this, 'registerDownloadEvents']);
  109. Helper::registerHooks();
  110. Share::registerBackend('file', File::class);
  111. Share::registerBackend('folder', Folder::class, 'file');
  112. }
  113. public function registerMountProviders(IMountProviderCollection $mountProviderCollection, MountProvider $mountProvider, ExternalMountProvider $externalMountProvider): void {
  114. $mountProviderCollection->registerProvider($mountProvider);
  115. $mountProviderCollection->registerProvider($externalMountProvider);
  116. }
  117. public function registerEventsScripts(IEventDispatcher $dispatcher): void {
  118. // sidebar and files scripts
  119. $dispatcher->addServiceListener(LoadAdditionalScriptsEvent::class, LoadAdditionalListener::class);
  120. $dispatcher->addServiceListener(LoadSidebar::class, LoadSidebarListener::class);
  121. $dispatcher->addServiceListener(ShareCreatedEvent::class, ShareInteractionListener::class);
  122. $dispatcher->addServiceListener(ShareCreatedEvent::class, UserShareAcceptanceListener::class);
  123. $dispatcher->addServiceListener(UserAddedEvent::class, UserAddedToGroupListener::class);
  124. $dispatcher->addListener(ResourcesLoadAdditionalScriptsEvent::class, function () {
  125. \OCP\Util::addScript('files_sharing', 'collaboration');
  126. });
  127. $dispatcher->addListener(\OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent::class, function () {
  128. /**
  129. * Always add main sharing script
  130. */
  131. Util::addScript(self::APP_ID, 'main');
  132. });
  133. // notifications api to accept incoming user shares
  134. $dispatcher->addListener(ShareCreatedEvent::class, function (ShareCreatedEvent $event) {
  135. /** @var Listener $listener */
  136. $listener = $this->getContainer()->query(Listener::class);
  137. $listener->shareNotification($event);
  138. });
  139. $dispatcher->addListener(IGroup::class . '::postAddUser', function ($event) {
  140. if (!$event instanceof OldGenericEvent) {
  141. return;
  142. }
  143. /** @var Listener $listener */
  144. $listener = $this->getContainer()->query(Listener::class);
  145. $listener->userAddedToGroup($event);
  146. });
  147. }
  148. public function registerDownloadEvents(
  149. IEventDispatcher $dispatcher,
  150. IUserSession $userSession,
  151. IRootFolder $rootFolder
  152. ): void {
  153. $dispatcher->addListener(
  154. BeforeDirectFileDownloadEvent::class,
  155. function (BeforeDirectFileDownloadEvent $event) use ($userSession, $rootFolder): void {
  156. $pathsToCheck = [$event->getPath()];
  157. // Check only for user/group shares. Don't restrict e.g. share links
  158. $user = $userSession->getUser();
  159. if ($user) {
  160. $viewOnlyHandler = new ViewOnly(
  161. $rootFolder->getUserFolder($user->getUID())
  162. );
  163. if (!$viewOnlyHandler->check($pathsToCheck)) {
  164. $event->setSuccessful(false);
  165. $event->setErrorMessage('Access to this resource or one of its sub-items has been denied.');
  166. }
  167. }
  168. }
  169. );
  170. $dispatcher->addListener(
  171. BeforeZipCreatedEvent::class,
  172. function (BeforeZipCreatedEvent $event) use ($userSession, $rootFolder): void {
  173. $dir = $event->getDirectory();
  174. $files = $event->getFiles();
  175. $pathsToCheck = [];
  176. foreach ($files as $file) {
  177. $pathsToCheck[] = $dir . '/' . $file;
  178. }
  179. // Check only for user/group shares. Don't restrict e.g. share links
  180. $user = $userSession->getUser();
  181. if ($user) {
  182. $viewOnlyHandler = new ViewOnly(
  183. $rootFolder->getUserFolder($user->getUID())
  184. );
  185. if (!$viewOnlyHandler->check($pathsToCheck)) {
  186. $event->setErrorMessage('Access to this resource or one of its sub-items has been denied.');
  187. $event->setSuccessful(false);
  188. } else {
  189. $event->setSuccessful(true);
  190. }
  191. } else {
  192. $event->setSuccessful(true);
  193. }
  194. }
  195. );
  196. }
  197. }