MountProvider.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Sharing;
  8. use OC\Files\View;
  9. use OCA\Files_Sharing\Event\ShareMountedEvent;
  10. use OCP\Cache\CappedMemoryCache;
  11. use OCP\EventDispatcher\IEventDispatcher;
  12. use OCP\Files\Config\IMountProvider;
  13. use OCP\Files\Mount\IMountPoint;
  14. use OCP\Files\Storage\IStorageFactory;
  15. use OCP\ICacheFactory;
  16. use OCP\IConfig;
  17. use OCP\IUser;
  18. use OCP\Share\IManager;
  19. use OCP\Share\IShare;
  20. use Psr\Log\LoggerInterface;
  21. class MountProvider implements IMountProvider {
  22. /**
  23. * @param IConfig $config
  24. * @param IManager $shareManager
  25. * @param LoggerInterface $logger
  26. */
  27. public function __construct(
  28. protected IConfig $config,
  29. protected IManager $shareManager,
  30. protected LoggerInterface $logger,
  31. protected IEventDispatcher $eventDispatcher,
  32. protected ICacheFactory $cacheFactory,
  33. ) {
  34. }
  35. /**
  36. * Get all mountpoints applicable for the user and check for shares where we need to update the etags
  37. *
  38. * @param IUser $user
  39. * @param IStorageFactory $loader
  40. * @return IMountPoint[]
  41. */
  42. public function getMountsForUser(IUser $user, IStorageFactory $loader) {
  43. $shares = $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_USER, null, -1);
  44. $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_GROUP, null, -1));
  45. $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_CIRCLE, null, -1));
  46. $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_ROOM, null, -1));
  47. $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_DECK, null, -1));
  48. $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_SCIENCEMESH, null, -1));
  49. // filter out excluded shares and group shares that includes self
  50. $shares = array_filter($shares, function (IShare $share) use ($user) {
  51. return $share->getPermissions() > 0 && $share->getShareOwner() !== $user->getUID();
  52. });
  53. $superShares = $this->buildSuperShares($shares, $user);
  54. $mounts = [];
  55. $view = new View('/' . $user->getUID() . '/files');
  56. $ownerViews = [];
  57. $sharingDisabledForUser = $this->shareManager->sharingDisabledForUser($user->getUID());
  58. /** @var CappedMemoryCache<bool> $folderExistCache */
  59. $foldersExistCache = new CappedMemoryCache();
  60. foreach ($superShares as $share) {
  61. try {
  62. /** @var IShare $parentShare */
  63. $parentShare = $share[0];
  64. if ($parentShare->getStatus() !== IShare::STATUS_ACCEPTED &&
  65. ($parentShare->getShareType() === IShare::TYPE_GROUP ||
  66. $parentShare->getShareType() === IShare::TYPE_USERGROUP ||
  67. $parentShare->getShareType() === IShare::TYPE_USER)) {
  68. continue;
  69. }
  70. $owner = $parentShare->getShareOwner();
  71. if (!isset($ownerViews[$owner])) {
  72. $ownerViews[$owner] = new View('/' . $parentShare->getShareOwner() . '/files');
  73. }
  74. $mount = new SharedMount(
  75. '\OCA\Files_Sharing\SharedStorage',
  76. $mounts,
  77. [
  78. 'user' => $user->getUID(),
  79. // parent share
  80. 'superShare' => $parentShare,
  81. // children/component of the superShare
  82. 'groupedShares' => $share[1],
  83. 'ownerView' => $ownerViews[$owner],
  84. 'sharingDisabledForUser' => $sharingDisabledForUser
  85. ],
  86. $loader,
  87. $view,
  88. $foldersExistCache,
  89. $this->eventDispatcher,
  90. $user,
  91. $this->cacheFactory->createLocal('share-valid-mountpoint')
  92. );
  93. $event = new ShareMountedEvent($mount);
  94. $this->eventDispatcher->dispatchTyped($event);
  95. $mounts[$mount->getMountPoint()] = $mount;
  96. foreach ($event->getAdditionalMounts() as $additionalMount) {
  97. $mounts[$additionalMount->getMountPoint()] = $additionalMount;
  98. }
  99. } catch (\Exception $e) {
  100. $this->logger->error(
  101. 'Error while trying to create shared mount',
  102. [
  103. 'app' => 'files_sharing',
  104. 'exception' => $e,
  105. ],
  106. );
  107. }
  108. }
  109. // array_filter removes the null values from the array
  110. return array_values(array_filter($mounts));
  111. }
  112. /**
  113. * Groups shares by path (nodeId) and target path
  114. *
  115. * @param IShare[] $shares
  116. * @return IShare[][] array of grouped shares, each element in the
  117. * array is a group which itself is an array of shares
  118. */
  119. private function groupShares(array $shares) {
  120. $tmp = [];
  121. foreach ($shares as $share) {
  122. if (!isset($tmp[$share->getNodeId()])) {
  123. $tmp[$share->getNodeId()] = [];
  124. }
  125. $tmp[$share->getNodeId()][] = $share;
  126. }
  127. $result = [];
  128. // sort by stime, the super share will be based on the least recent share
  129. foreach ($tmp as &$tmp2) {
  130. @usort($tmp2, function ($a, $b) {
  131. $aTime = $a->getShareTime()->getTimestamp();
  132. $bTime = $b->getShareTime()->getTimestamp();
  133. if ($aTime === $bTime) {
  134. return $a->getId() < $b->getId() ? -1 : 1;
  135. }
  136. return $aTime < $bTime ? -1 : 1;
  137. });
  138. $result[] = $tmp2;
  139. }
  140. return array_values($result);
  141. }
  142. /**
  143. * Build super shares (virtual share) by grouping them by node id and target,
  144. * then for each group compute the super share and return it along with the matching
  145. * grouped shares. The most permissive permissions are used based on the permissions
  146. * of all shares within the group.
  147. *
  148. * @param IShare[] $allShares
  149. * @param IUser $user user
  150. * @return array Tuple of [superShare, groupedShares]
  151. */
  152. private function buildSuperShares(array $allShares, IUser $user) {
  153. $result = [];
  154. $groupedShares = $this->groupShares($allShares);
  155. /** @var IShare[] $shares */
  156. foreach ($groupedShares as $shares) {
  157. if (count($shares) === 0) {
  158. continue;
  159. }
  160. $superShare = $this->shareManager->newShare();
  161. // compute super share based on first entry of the group
  162. $superShare->setId($shares[0]->getId())
  163. ->setShareOwner($shares[0]->getShareOwner())
  164. ->setNodeId($shares[0]->getNodeId())
  165. ->setShareType($shares[0]->getShareType())
  166. ->setTarget($shares[0]->getTarget());
  167. // Gather notes from all the shares.
  168. // Since these are readly available here, storing them
  169. // enables the DAV FilesPlugin to avoid executing many
  170. // DB queries to retrieve the same information.
  171. $allNotes = implode("\n", array_map(function ($sh) {
  172. return $sh->getNote();
  173. }, $shares));
  174. $superShare->setNote($allNotes);
  175. // use most permissive permissions
  176. // this covers the case where there are multiple shares for the same
  177. // file e.g. from different groups and different permissions
  178. $superPermissions = 0;
  179. $superAttributes = $this->shareManager->newShare()->newAttributes();
  180. $status = IShare::STATUS_PENDING;
  181. foreach ($shares as $share) {
  182. $superPermissions |= $share->getPermissions();
  183. $status = max($status, $share->getStatus());
  184. // update permissions
  185. $superPermissions |= $share->getPermissions();
  186. // update share permission attributes
  187. $attributes = $share->getAttributes();
  188. if ($attributes !== null) {
  189. foreach ($attributes->toArray() as $attribute) {
  190. if ($superAttributes->getAttribute($attribute['scope'], $attribute['key']) === true) {
  191. // if super share attribute is already enabled, it is most permissive
  192. continue;
  193. }
  194. // update supershare attributes with subshare attribute
  195. $superAttributes->setAttribute($attribute['scope'], $attribute['key'], $attribute['value']);
  196. }
  197. }
  198. // adjust target, for database consistency if needed
  199. if ($share->getTarget() !== $superShare->getTarget()) {
  200. $share->setTarget($superShare->getTarget());
  201. try {
  202. $this->shareManager->moveShare($share, $user->getUID());
  203. } catch (\InvalidArgumentException $e) {
  204. // ignore as it is not important and we don't want to
  205. // block FS setup
  206. // the subsequent code anyway only uses the target of the
  207. // super share
  208. // such issue can usually happen when dealing with
  209. // null groups which usually appear with group backend
  210. // caching inconsistencies
  211. $this->logger->debug(
  212. 'Could not adjust share target for share ' . $share->getId() . ' to make it consistent: ' . $e->getMessage(),
  213. ['app' => 'files_sharing']
  214. );
  215. }
  216. }
  217. if (!is_null($share->getNodeCacheEntry())) {
  218. $superShare->setNodeCacheEntry($share->getNodeCacheEntry());
  219. }
  220. }
  221. $superShare->setPermissions($superPermissions);
  222. $superShare->setStatus($status);
  223. $superShare->setAttributes($superAttributes);
  224. $result[] = [$superShare, $shares];
  225. }
  226. return $result;
  227. }
  228. }