You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Application.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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\LegacyBeforeTemplateRenderedListener;
  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\Event\LoadAdditionalScriptsEvent;
  52. use OCA\Files\Event\LoadSidebar;
  53. use OCP\Files\Event\BeforeDirectGetEvent;
  54. use OCA\Files_Sharing\ShareBackend\File;
  55. use OCA\Files_Sharing\ShareBackend\Folder;
  56. use OCA\Files_Sharing\ViewOnly;
  57. use OCP\AppFramework\App;
  58. use OCP\AppFramework\Bootstrap\IBootContext;
  59. use OCP\AppFramework\Bootstrap\IBootstrap;
  60. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  61. use OCP\Collaboration\Resources\LoadAdditionalScriptsEvent as ResourcesLoadAdditionalScriptsEvent;
  62. use OCP\EventDispatcher\IEventDispatcher;
  63. use OCP\EventDispatcher\GenericEvent;
  64. use OCP\Federation\ICloudIdManager;
  65. use OCP\Files\Config\IMountProviderCollection;
  66. use OCP\Files\Events\BeforeDirectFileDownloadEvent;
  67. use OCP\Files\Events\BeforeZipCreatedEvent;
  68. use OCP\Files\IRootFolder;
  69. use OCP\Group\Events\GroupChangedEvent;
  70. use OCP\Group\Events\UserAddedEvent;
  71. use OCP\IDBConnection;
  72. use OCP\IGroup;
  73. use OCP\IUserSession;
  74. use OCP\L10N\IFactory;
  75. use OCP\Share\Events\ShareCreatedEvent;
  76. use OCP\Share\IManager;
  77. use OCP\User\Events\UserChangedEvent;
  78. use OCP\Util;
  79. use Psr\Container\ContainerInterface;
  80. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  81. use Symfony\Component\EventDispatcher\GenericEvent as OldGenericEvent;
  82. class Application extends App implements IBootstrap {
  83. public const APP_ID = 'files_sharing';
  84. public function __construct(array $urlParams = []) {
  85. parent::__construct(self::APP_ID, $urlParams);
  86. }
  87. public function register(IRegistrationContext $context): void {
  88. $context->registerService(ExternalMountProvider::class, function (ContainerInterface $c) {
  89. return new ExternalMountProvider(
  90. $c->get(IDBConnection::class),
  91. function () use ($c) {
  92. return $c->get(Manager::class);
  93. },
  94. $c->get(ICloudIdManager::class)
  95. );
  96. });
  97. /**
  98. * Middleware
  99. */
  100. $context->registerMiddleWare(SharingCheckMiddleware::class);
  101. $context->registerMiddleWare(OCSShareAPIMiddleware::class);
  102. $context->registerMiddleWare(ShareInfoMiddleware::class);
  103. $context->registerCapability(Capabilities::class);
  104. $context->registerNotifierService(Notifier::class);
  105. $context->registerEventListener(UserChangedEvent::class, DisplayNameCache::class);
  106. $context->registerEventListener(GroupChangedEvent::class, GroupDisplayNameCache::class);
  107. }
  108. public function boot(IBootContext $context): void {
  109. $context->injectFn([$this, 'registerMountProviders']);
  110. $context->injectFn([$this, 'registerEventsScripts']);
  111. $context->injectFn([$this, 'registerDownloadEvents']);
  112. $context->injectFn([$this, 'setupSharingMenus']);
  113. Helper::registerHooks();
  114. Share::registerBackend('file', File::class);
  115. Share::registerBackend('folder', Folder::class, 'file');
  116. /**
  117. * Always add main sharing script
  118. */
  119. Util::addScript(self::APP_ID, 'main');
  120. }
  121. public function registerMountProviders(IMountProviderCollection $mountProviderCollection, MountProvider $mountProvider, ExternalMountProvider $externalMountProvider): void {
  122. $mountProviderCollection->registerProvider($mountProvider);
  123. $mountProviderCollection->registerProvider($externalMountProvider);
  124. }
  125. public function registerEventsScripts(IEventDispatcher $dispatcher, EventDispatcherInterface $oldDispatcher): void {
  126. // sidebar and files scripts
  127. $dispatcher->addServiceListener(LoadAdditionalScriptsEvent::class, LoadAdditionalListener::class);
  128. $dispatcher->addServiceListener(BeforeTemplateRenderedEvent::class, LegacyBeforeTemplateRenderedListener::class);
  129. $dispatcher->addServiceListener(LoadSidebar::class, LoadSidebarListener::class);
  130. $dispatcher->addServiceListener(ShareCreatedEvent::class, ShareInteractionListener::class);
  131. $dispatcher->addServiceListener(ShareCreatedEvent::class, UserShareAcceptanceListener::class);
  132. $dispatcher->addServiceListener(UserAddedEvent::class, UserAddedToGroupListener::class);
  133. $dispatcher->addListener(ResourcesLoadAdditionalScriptsEvent::class, function () {
  134. \OCP\Util::addScript('files_sharing', 'collaboration');
  135. });
  136. // notifications api to accept incoming user shares
  137. $oldDispatcher->addListener('OCP\Share::postShare', function (OldGenericEvent $event) {
  138. /** @var Listener $listener */
  139. $listener = $this->getContainer()->query(Listener::class);
  140. $listener->shareNotification($event);
  141. });
  142. $oldDispatcher->addListener(IGroup::class . '::postAddUser', function (OldGenericEvent $event) {
  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. public function setupSharingMenus(IManager $shareManager, IFactory $l10nFactory, IUserSession $userSession): void {
  198. if (!$shareManager->shareApiEnabled() || !class_exists('\OCA\Files\App')) {
  199. return;
  200. }
  201. $navigationManager = \OCA\Files\App::getNavigationManager();
  202. // show_Quick_Access stored as string
  203. $navigationManager->add(function () use ($shareManager, $l10nFactory, $userSession) {
  204. $l = $l10nFactory->get('files_sharing');
  205. $user = $userSession->getUser();
  206. $userId = $user ? $user->getUID() : null;
  207. $sharingSublistArray = [];
  208. if ($shareManager->sharingDisabledForUser($userId) === false) {
  209. $sharingSublistArray[] = [
  210. 'id' => 'sharingout',
  211. 'appname' => 'files_sharing',
  212. 'script' => 'list.php',
  213. 'order' => 16,
  214. 'name' => $l->t('Shared with others'),
  215. ];
  216. }
  217. $sharingSublistArray[] = [
  218. 'id' => 'sharingin',
  219. 'appname' => 'files_sharing',
  220. 'script' => 'list.php',
  221. 'order' => 15,
  222. 'name' => $l->t('Shared with you'),
  223. ];
  224. if ($shareManager->sharingDisabledForUser($userId) === false) {
  225. // Check if sharing by link is enabled
  226. if ($shareManager->shareApiAllowLinks()) {
  227. $sharingSublistArray[] = [
  228. 'id' => 'sharinglinks',
  229. 'appname' => 'files_sharing',
  230. 'script' => 'list.php',
  231. 'order' => 17,
  232. 'name' => $l->t('Shared by link'),
  233. ];
  234. }
  235. }
  236. $sharingSublistArray[] = [
  237. 'id' => 'deletedshares',
  238. 'appname' => 'files_sharing',
  239. 'script' => 'list.php',
  240. 'order' => 19,
  241. 'name' => $l->t('Deleted shares'),
  242. ];
  243. $sharingSublistArray[] = [
  244. 'id' => 'pendingshares',
  245. 'appname' => 'files_sharing',
  246. 'script' => 'list.php',
  247. 'order' => 19,
  248. 'name' => $l->t('Pending shares'),
  249. ];
  250. return [
  251. 'id' => 'shareoverview',
  252. 'appname' => 'files_sharing',
  253. 'script' => 'list.php',
  254. 'order' => 18,
  255. 'name' => $l->t('Shares'),
  256. 'classes' => 'collapsible',
  257. 'sublist' => $sharingSublistArray,
  258. ];
  259. });
  260. }
  261. }