MountProvider.php 9.6 KB

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