1
0

MountProvider.php 8.5 KB

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