MountProvider.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. // 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. $foldersExistCache = new CappedMemoryCache();
  80. foreach ($superShares as $share) {
  81. try {
  82. /** @var \OCP\Share\IShare $parentShare */
  83. $parentShare = $share[0];
  84. $owner = $parentShare->getShareOwner();
  85. if (!isset($ownerViews[$owner])) {
  86. $ownerViews[$owner] = new View('/' . $parentShare->getShareOwner() . '/files');
  87. }
  88. $mount = new SharedMount(
  89. '\OCA\Files_Sharing\SharedStorage',
  90. $mounts,
  91. [
  92. 'user' => $user->getUID(),
  93. // parent share
  94. 'superShare' => $parentShare,
  95. // children/component of the superShare
  96. 'groupedShares' => $share[1],
  97. 'ownerView' => $ownerViews[$owner],
  98. 'sharingDisabledForUser' => $sharingDisabledForUser
  99. ],
  100. $storageFactory,
  101. $view,
  102. $foldersExistCache
  103. );
  104. $mounts[$mount->getMountPoint()] = $mount;
  105. } catch (\Exception $e) {
  106. $this->logger->logException($e);
  107. $this->logger->error('Error while trying to create shared mount');
  108. }
  109. }
  110. // array_filter removes the null values from the array
  111. return array_values(array_filter($mounts));
  112. }
  113. /**
  114. * Groups shares by path (nodeId) and target path
  115. *
  116. * @param \OCP\Share\IShare[] $shares
  117. * @return \OCP\Share\IShare[][] array of grouped shares, each element in the
  118. * array is a group which itself is an array of shares
  119. */
  120. private function groupShares(array $shares) {
  121. $tmp = [];
  122. foreach ($shares as $share) {
  123. if (!isset($tmp[$share->getNodeId()])) {
  124. $tmp[$share->getNodeId()] = [];
  125. }
  126. $tmp[$share->getNodeId()][] = $share;
  127. }
  128. $result = [];
  129. // sort by stime, the super share will be based on the least recent share
  130. foreach ($tmp as &$tmp2) {
  131. @usort($tmp2, function($a, $b) {
  132. if ($a->getShareTime() <= $b->getShareTime()) {
  133. return -1;
  134. }
  135. return 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. ->setTarget($shares[0]->getTarget());
  165. // use most permissive permissions
  166. $permissions = 0;
  167. foreach ($shares as $share) {
  168. $permissions |= $share->getPermissions();
  169. if ($share->getTarget() !== $superShare->getTarget()) {
  170. // adjust target, for database consistency
  171. $share->setTarget($superShare->getTarget());
  172. try {
  173. $this->shareManager->moveShare($share, $user->getUID());
  174. } catch (\InvalidArgumentException $e) {
  175. // ignore as it is not important and we don't want to
  176. // block FS setup
  177. // the subsequent code anyway only uses the target of the
  178. // super share
  179. // such issue can usually happen when dealing with
  180. // null groups which usually appear with group backend
  181. // caching inconsistencies
  182. $this->logger->debug(
  183. 'Could not adjust share target for share ' . $share->getId() . ' to make it consistent: ' . $e->getMessage(),
  184. ['app' => 'files_sharing']
  185. );
  186. }
  187. }
  188. if (!is_null($share->getNodeCacheEntry())) {
  189. $superShare->setNodeCacheEntry($share->getNodeCacheEntry());
  190. }
  191. }
  192. $superShare->setPermissions($permissions);
  193. $result[] = [$superShare, $shares];
  194. }
  195. return $result;
  196. }
  197. }