MountProvider.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 OCP\Files\Config\IMountProvider;
  29. use OCP\Files\Storage\IStorageFactory;
  30. use OCP\IConfig;
  31. use OCP\ILogger;
  32. use OCP\IUser;
  33. use OCP\Share\IManager;
  34. class MountProvider implements IMountProvider {
  35. /**
  36. * @var \OCP\IConfig
  37. */
  38. protected $config;
  39. /**
  40. * @var IManager
  41. */
  42. protected $shareManager;
  43. /**
  44. * @var ILogger
  45. */
  46. protected $logger;
  47. /**
  48. * @param \OCP\IConfig $config
  49. * @param IManager $shareManager
  50. * @param ILogger $logger
  51. */
  52. public function __construct(IConfig $config, IManager $shareManager, ILogger $logger) {
  53. $this->config = $config;
  54. $this->shareManager = $shareManager;
  55. $this->logger = $logger;
  56. }
  57. /**
  58. * Get all mountpoints applicable for the user and check for shares where we need to update the etags
  59. *
  60. * @param \OCP\IUser $user
  61. * @param \OCP\Files\Storage\IStorageFactory $storageFactory
  62. * @return \OCP\Files\Mount\IMountPoint[]
  63. */
  64. public function getMountsForUser(IUser $user, IStorageFactory $storageFactory) {
  65. $shares = $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_USER, null, -1);
  66. $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_GROUP, null, -1));
  67. $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_CIRCLE, null, -1));
  68. // filter out excluded shares and group shares that includes self
  69. $shares = array_filter($shares, function (\OCP\Share\IShare $share) use ($user) {
  70. return $share->getPermissions() > 0 && $share->getShareOwner() !== $user->getUID();
  71. });
  72. $superShares = $this->buildSuperShares($shares, $user);
  73. $mounts = [];
  74. foreach ($superShares as $share) {
  75. try {
  76. $mounts[] = new SharedMount(
  77. '\OCA\Files_Sharing\SharedStorage',
  78. $mounts,
  79. [
  80. 'user' => $user->getUID(),
  81. // parent share
  82. 'superShare' => $share[0],
  83. // children/component of the superShare
  84. 'groupedShares' => $share[1],
  85. ],
  86. $storageFactory
  87. );
  88. } catch (\Exception $e) {
  89. $this->logger->logException($e);
  90. $this->logger->error('Error while trying to create shared mount');
  91. }
  92. }
  93. // array_filter removes the null values from the array
  94. return array_filter($mounts);
  95. }
  96. /**
  97. * Groups shares by path (nodeId) and target path
  98. *
  99. * @param \OCP\Share\IShare[] $shares
  100. * @return \OCP\Share\IShare[][] array of grouped shares, each element in the
  101. * array is a group which itself is an array of shares
  102. */
  103. private function groupShares(array $shares) {
  104. $tmp = [];
  105. foreach ($shares as $share) {
  106. if (!isset($tmp[$share->getNodeId()])) {
  107. $tmp[$share->getNodeId()] = [];
  108. }
  109. $tmp[$share->getNodeId()][] = $share;
  110. }
  111. $result = [];
  112. // sort by stime, the super share will be based on the least recent share
  113. foreach ($tmp as &$tmp2) {
  114. @usort($tmp2, function($a, $b) {
  115. if ($a->getShareTime() <= $b->getShareTime()) {
  116. return -1;
  117. }
  118. return 1;
  119. });
  120. $result[] = $tmp2;
  121. }
  122. return array_values($result);
  123. }
  124. /**
  125. * Build super shares (virtual share) by grouping them by node id and target,
  126. * then for each group compute the super share and return it along with the matching
  127. * grouped shares. The most permissive permissions are used based on the permissions
  128. * of all shares within the group.
  129. *
  130. * @param \OCP\Share\IShare[] $allShares
  131. * @param \OCP\IUser $user user
  132. * @return array Tuple of [superShare, groupedShares]
  133. */
  134. private function buildSuperShares(array $allShares, \OCP\IUser $user) {
  135. $result = [];
  136. $groupedShares = $this->groupShares($allShares);
  137. /** @var \OCP\Share\IShare[] $shares */
  138. foreach ($groupedShares as $shares) {
  139. if (count($shares) === 0) {
  140. continue;
  141. }
  142. $superShare = $this->shareManager->newShare();
  143. // compute super share based on first entry of the group
  144. $superShare->setId($shares[0]->getId())
  145. ->setShareOwner($shares[0]->getShareOwner())
  146. ->setNodeId($shares[0]->getNodeId())
  147. ->setTarget($shares[0]->getTarget());
  148. // use most permissive permissions
  149. $permissions = 0;
  150. foreach ($shares as $share) {
  151. $permissions |= $share->getPermissions();
  152. if ($share->getTarget() !== $superShare->getTarget()) {
  153. // adjust target, for database consistency
  154. $share->setTarget($superShare->getTarget());
  155. try {
  156. $this->shareManager->moveShare($share, $user->getUID());
  157. } catch (\InvalidArgumentException $e) {
  158. // ignore as it is not important and we don't want to
  159. // block FS setup
  160. // the subsequent code anyway only uses the target of the
  161. // super share
  162. // such issue can usually happen when dealing with
  163. // null groups which usually appear with group backend
  164. // caching inconsistencies
  165. $this->logger->debug(
  166. 'Could not adjust share target for share ' . $share->getId() . ' to make it consistent: ' . $e->getMessage(),
  167. ['app' => 'files_sharing']
  168. );
  169. }
  170. }
  171. if (!is_null($share->getNodeCacheEntry())) {
  172. $superShare->setNodeCacheEntry($share->getNodeCacheEntry());
  173. }
  174. }
  175. $superShare->setPermissions($permissions);
  176. $result[] = [$superShare, $shares];
  177. }
  178. return $result;
  179. }
  180. }