1
0

Application.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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\Share\Share;
  32. use OC\User\DisplayNameCache;
  33. use OCA\Files_Sharing\Capabilities;
  34. use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent;
  35. use OCA\Files_Sharing\External\Manager;
  36. use OCA\Files_Sharing\External\MountProvider as ExternalMountProvider;
  37. use OCA\Files_Sharing\Helper;
  38. use OCA\Files_Sharing\Listener\LegacyBeforeTemplateRenderedListener;
  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 OCP\Files\Event\BeforeDirectGetEvent;
  53. use OCA\Files_Sharing\ShareBackend\File;
  54. use OCA\Files_Sharing\ShareBackend\Folder;
  55. use OCA\Files_Sharing\ViewOnly;
  56. use OCP\AppFramework\App;
  57. use OCP\AppFramework\Bootstrap\IBootContext;
  58. use OCP\AppFramework\Bootstrap\IBootstrap;
  59. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  60. use OCP\Collaboration\Resources\LoadAdditionalScriptsEvent as ResourcesLoadAdditionalScriptsEvent;
  61. use OCP\EventDispatcher\IEventDispatcher;
  62. use OCP\EventDispatcher\GenericEvent;
  63. use OCP\Federation\ICloudIdManager;
  64. use OCP\Files\Config\IMountProviderCollection;
  65. use OCP\Files\Events\BeforeDirectFileDownloadEvent;
  66. use OCP\Files\Events\BeforeZipCreatedEvent;
  67. use OCP\Files\IRootFolder;
  68. use OCP\Group\Events\UserAddedEvent;
  69. use OCP\IDBConnection;
  70. use OCP\IGroup;
  71. use OCP\IUserSession;
  72. use OCP\L10N\IFactory;
  73. use OCP\Share\Events\ShareCreatedEvent;
  74. use OCP\Share\IManager;
  75. use OCP\User\Events\UserChangedEvent;
  76. use OCP\Util;
  77. use Psr\Container\ContainerInterface;
  78. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  79. use Symfony\Component\EventDispatcher\GenericEvent as OldGenericEvent;
  80. class Application extends App implements IBootstrap {
  81. public const APP_ID = 'files_sharing';
  82. public function __construct(array $urlParams = []) {
  83. parent::__construct(self::APP_ID, $urlParams);
  84. }
  85. public function register(IRegistrationContext $context): void {
  86. $context->registerService(ExternalMountProvider::class, function (ContainerInterface $c) {
  87. return new ExternalMountProvider(
  88. $c->get(IDBConnection::class),
  89. function () use ($c) {
  90. return $c->get(Manager::class);
  91. },
  92. $c->get(ICloudIdManager::class)
  93. );
  94. });
  95. /**
  96. * Middleware
  97. */
  98. $context->registerMiddleWare(SharingCheckMiddleware::class);
  99. $context->registerMiddleWare(OCSShareAPIMiddleware::class);
  100. $context->registerMiddleWare(ShareInfoMiddleware::class);
  101. $context->registerCapability(Capabilities::class);
  102. $context->registerNotifierService(Notifier::class);
  103. $context->registerEventListener(UserChangedEvent::class, DisplayNameCache::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. $context->injectFn([$this, 'setupSharingMenus']);
  110. Helper::registerHooks();
  111. Share::registerBackend('file', File::class);
  112. Share::registerBackend('folder', Folder::class, 'file');
  113. /**
  114. * Always add main sharing script
  115. */
  116. Util::addScript(self::APP_ID, 'main');
  117. }
  118. public function registerMountProviders(IMountProviderCollection $mountProviderCollection, MountProvider $mountProvider, ExternalMountProvider $externalMountProvider): void {
  119. $mountProviderCollection->registerProvider($mountProvider);
  120. $mountProviderCollection->registerProvider($externalMountProvider);
  121. }
  122. public function registerEventsScripts(IEventDispatcher $dispatcher, EventDispatcherInterface $oldDispatcher): void {
  123. // sidebar and files scripts
  124. $dispatcher->addServiceListener(LoadAdditionalScriptsEvent::class, LoadAdditionalListener::class);
  125. $dispatcher->addServiceListener(BeforeTemplateRenderedEvent::class, LegacyBeforeTemplateRenderedListener::class);
  126. $dispatcher->addServiceListener(LoadSidebar::class, LoadSidebarListener::class);
  127. $dispatcher->addServiceListener(ShareCreatedEvent::class, ShareInteractionListener::class);
  128. $dispatcher->addServiceListener(ShareCreatedEvent::class, UserShareAcceptanceListener::class);
  129. $dispatcher->addServiceListener(UserAddedEvent::class, UserAddedToGroupListener::class);
  130. $dispatcher->addListener(ResourcesLoadAdditionalScriptsEvent::class, function () {
  131. \OCP\Util::addScript('files_sharing', 'collaboration');
  132. });
  133. // notifications api to accept incoming user shares
  134. $oldDispatcher->addListener('OCP\Share::postShare', function (OldGenericEvent $event) {
  135. /** @var Listener $listener */
  136. $listener = $this->getContainer()->query(Listener::class);
  137. $listener->shareNotification($event);
  138. });
  139. $oldDispatcher->addListener(IGroup::class . '::postAddUser', function (OldGenericEvent $event) {
  140. /** @var Listener $listener */
  141. $listener = $this->getContainer()->query(Listener::class);
  142. $listener->userAddedToGroup($event);
  143. });
  144. }
  145. public function registerDownloadEvents(
  146. IEventDispatcher $dispatcher,
  147. IUserSession $userSession,
  148. IRootFolder $rootFolder
  149. ): void {
  150. $dispatcher->addListener(
  151. BeforeDirectFileDownloadEvent::class,
  152. function (BeforeDirectFileDownloadEvent $event) use ($userSession, $rootFolder): void {
  153. $pathsToCheck = [$event->getPath()];
  154. // Check only for user/group shares. Don't restrict e.g. share links
  155. $user = $userSession->getUser();
  156. if ($user) {
  157. $viewOnlyHandler = new ViewOnly(
  158. $rootFolder->getUserFolder($user->getUID())
  159. );
  160. if (!$viewOnlyHandler->check($pathsToCheck)) {
  161. $event->setSuccessful(false);
  162. $event->setErrorMessage('Access to this resource or one of its sub-items has been denied.');
  163. }
  164. }
  165. }
  166. );
  167. $dispatcher->addListener(
  168. BeforeZipCreatedEvent::class,
  169. function (BeforeZipCreatedEvent $event) use ($userSession, $rootFolder): void {
  170. $dir = $event->getDirectory();
  171. $files = $event->getFiles();
  172. $pathsToCheck = [];
  173. foreach ($files as $file) {
  174. $pathsToCheck[] = $dir . '/' . $file;
  175. }
  176. // Check only for user/group shares. Don't restrict e.g. share links
  177. $user = $userSession->getUser();
  178. if ($user) {
  179. $viewOnlyHandler = new ViewOnly(
  180. $rootFolder->getUserFolder($user->getUID())
  181. );
  182. if (!$viewOnlyHandler->check($pathsToCheck)) {
  183. $event->setErrorMessage('Access to this resource or one of its sub-items has been denied.');
  184. $event->setSuccessful(false);
  185. } else {
  186. $event->setSuccessful(true);
  187. }
  188. } else {
  189. $event->setSuccessful(true);
  190. }
  191. }
  192. );
  193. }
  194. public function setupSharingMenus(IManager $shareManager, IFactory $l10nFactory, IUserSession $userSession): void {
  195. if (!$shareManager->shareApiEnabled() || !class_exists('\OCA\Files\App')) {
  196. return;
  197. }
  198. $navigationManager = \OCA\Files\App::getNavigationManager();
  199. // show_Quick_Access stored as string
  200. $navigationManager->add(function () use ($shareManager, $l10nFactory, $userSession) {
  201. $l = $l10nFactory->get('files_sharing');
  202. $user = $userSession->getUser();
  203. $userId = $user ? $user->getUID() : null;
  204. $sharingSublistArray = [];
  205. if ($shareManager->sharingDisabledForUser($userId) === false) {
  206. $sharingSublistArray[] = [
  207. 'id' => 'sharingout',
  208. 'appname' => 'files_sharing',
  209. 'script' => 'list.php',
  210. 'order' => 16,
  211. 'name' => $l->t('Shared with others'),
  212. ];
  213. }
  214. $sharingSublistArray[] = [
  215. 'id' => 'sharingin',
  216. 'appname' => 'files_sharing',
  217. 'script' => 'list.php',
  218. 'order' => 15,
  219. 'name' => $l->t('Shared with you'),
  220. ];
  221. if ($shareManager->sharingDisabledForUser($userId) === false) {
  222. // Check if sharing by link is enabled
  223. if ($shareManager->shareApiAllowLinks()) {
  224. $sharingSublistArray[] = [
  225. 'id' => 'sharinglinks',
  226. 'appname' => 'files_sharing',
  227. 'script' => 'list.php',
  228. 'order' => 17,
  229. 'name' => $l->t('Shared by link'),
  230. ];
  231. }
  232. }
  233. $sharingSublistArray[] = [
  234. 'id' => 'deletedshares',
  235. 'appname' => 'files_sharing',
  236. 'script' => 'list.php',
  237. 'order' => 19,
  238. 'name' => $l->t('Deleted shares'),
  239. ];
  240. $sharingSublistArray[] = [
  241. 'id' => 'pendingshares',
  242. 'appname' => 'files_sharing',
  243. 'script' => 'list.php',
  244. 'order' => 19,
  245. 'name' => $l->t('Pending shares'),
  246. ];
  247. return [
  248. 'id' => 'shareoverview',
  249. 'appname' => 'files_sharing',
  250. 'script' => 'list.php',
  251. 'order' => 18,
  252. 'name' => $l->t('Shares'),
  253. 'classes' => 'collapsible',
  254. 'sublist' => $sharingSublistArray,
  255. 'expandedState' => 'show_sharing_menu'
  256. ];
  257. });
  258. }
  259. }