1
0

MountProvider.php 9.8 KB

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