1
0

MountProvider.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Maxence Lange <maxence@nextcloud.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Vincent Petry <pvince81@owncloud.com>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\Files_Sharing;
  28. use OC\Cache\CappedMemoryCache;
  29. use OC\Files\View;
  30. use OCP\Files\Config\IMountProvider;
  31. use OCP\Files\Storage\IStorageFactory;
  32. use OCP\IConfig;
  33. use OCP\ILogger;
  34. use OCP\IUser;
  35. use OCP\Share\IManager;
  36. class MountProvider implements IMountProvider {
  37. /**
  38. * @var \OCP\IConfig
  39. */
  40. protected $config;
  41. /**
  42. * @var IManager
  43. */
  44. protected $shareManager;
  45. /**
  46. * @var ILogger
  47. */
  48. protected $logger;
  49. /**
  50. * @param \OCP\IConfig $config
  51. * @param IManager $shareManager
  52. * @param ILogger $logger
  53. */
  54. public function __construct(IConfig $config, IManager $shareManager, ILogger $logger) {
  55. $this->config = $config;
  56. $this->shareManager = $shareManager;
  57. $this->logger = $logger;
  58. }
  59. /**
  60. * Get all mountpoints applicable for the user and check for shares where we need to update the etags
  61. *
  62. * @param \OCP\IUser $user
  63. * @param \OCP\Files\Storage\IStorageFactory $storageFactory
  64. * @return \OCP\Files\Mount\IMountPoint[]
  65. */
  66. public function getMountsForUser(IUser $user, IStorageFactory $storageFactory) {
  67. $shares = $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_USER, null, -1);
  68. $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_GROUP, null, -1));
  69. $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_CIRCLE, null, -1));
  70. $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_ROOM, null, -1));
  71. // filter out excluded shares and group shares that includes self
  72. $shares = array_filter($shares, function (\OCP\Share\IShare $share) use ($user) {
  73. return $share->getPermissions() > 0 && $share->getShareOwner() !== $user->getUID();
  74. });
  75. $superShares = $this->buildSuperShares($shares, $user);
  76. $mounts = [];
  77. $view = new View('/' . $user->getUID() . '/files');
  78. $ownerViews = [];
  79. $sharingDisabledForUser = $this->shareManager->sharingDisabledForUser($user->getUID());
  80. $foldersExistCache = new CappedMemoryCache();
  81. foreach ($superShares as $share) {
  82. try {
  83. /** @var \OCP\Share\IShare $parentShare */
  84. $parentShare = $share[0];
  85. $owner = $parentShare->getShareOwner();
  86. if (!isset($ownerViews[$owner])) {
  87. $ownerViews[$owner] = new View('/' . $parentShare->getShareOwner() . '/files');
  88. }
  89. $mount = new SharedMount(
  90. '\OCA\Files_Sharing\SharedStorage',
  91. $mounts,
  92. [
  93. 'user' => $user->getUID(),
  94. // parent share
  95. 'superShare' => $parentShare,
  96. // children/component of the superShare
  97. 'groupedShares' => $share[1],
  98. 'ownerView' => $ownerViews[$owner],
  99. 'sharingDisabledForUser' => $sharingDisabledForUser
  100. ],
  101. $storageFactory,
  102. $view,
  103. $foldersExistCache
  104. );
  105. $mounts[$mount->getMountPoint()] = $mount;
  106. } catch (\Exception $e) {
  107. $this->logger->logException($e);
  108. $this->logger->error('Error while trying to create shared mount');
  109. }
  110. }
  111. // array_filter removes the null values from the array
  112. return array_values(array_filter($mounts));
  113. }
  114. /**
  115. * Groups shares by path (nodeId) and target path
  116. *
  117. * @param \OCP\Share\IShare[] $shares
  118. * @return \OCP\Share\IShare[][] array of grouped shares, each element in the
  119. * array is a group which itself is an array of shares
  120. */
  121. private function groupShares(array $shares) {
  122. $tmp = [];
  123. foreach ($shares as $share) {
  124. if (!isset($tmp[$share->getNodeId()])) {
  125. $tmp[$share->getNodeId()] = [];
  126. }
  127. $tmp[$share->getNodeId()][] = $share;
  128. }
  129. $result = [];
  130. // sort by stime, the super share will be based on the least recent share
  131. foreach ($tmp as &$tmp2) {
  132. @usort($tmp2, function($a, $b) {
  133. if ($a->getShareTime() <= $b->getShareTime()) {
  134. return -1;
  135. }
  136. return 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 \OCP\Share\IShare[] $allShares
  149. * @param \OCP\IUser $user user
  150. * @return array Tuple of [superShare, groupedShares]
  151. */
  152. private function buildSuperShares(array $allShares, \OCP\IUser $user) {
  153. $result = [];
  154. $groupedShares = $this->groupShares($allShares);
  155. /** @var \OCP\Share\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. ->setTarget($shares[0]->getTarget());
  166. // use most permissive permissions
  167. $permissions = 0;
  168. foreach ($shares as $share) {
  169. $permissions |= $share->getPermissions();
  170. if ($share->getTarget() !== $superShare->getTarget()) {
  171. // adjust target, for database consistency
  172. $share->setTarget($superShare->getTarget());
  173. try {
  174. $this->shareManager->moveShare($share, $user->getUID());
  175. } catch (\InvalidArgumentException $e) {
  176. // ignore as it is not important and we don't want to
  177. // block FS setup
  178. // the subsequent code anyway only uses the target of the
  179. // super share
  180. // such issue can usually happen when dealing with
  181. // null groups which usually appear with group backend
  182. // caching inconsistencies
  183. $this->logger->debug(
  184. 'Could not adjust share target for share ' . $share->getId() . ' to make it consistent: ' . $e->getMessage(),
  185. ['app' => 'files_sharing']
  186. );
  187. }
  188. }
  189. if (!is_null($share->getNodeCacheEntry())) {
  190. $superShare->setNodeCacheEntry($share->getNodeCacheEntry());
  191. }
  192. }
  193. $superShare->setPermissions($permissions);
  194. $result[] = [$superShare, $shares];
  195. }
  196. return $result;
  197. }
  198. }