1
0

MountProvider.php 9.4 KB

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