You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MountProvider.php 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  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 <pvince81@owncloud.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 OC\Cache\CappedMemoryCache;
  31. use OC\Files\View;
  32. use OCP\Files\Config\IMountProvider;
  33. use OCP\Files\Storage\IStorageFactory;
  34. use OCP\IConfig;
  35. use OCP\ILogger;
  36. use OCP\IUser;
  37. use OCP\Share\IManager;
  38. use OCP\Share\IShare;
  39. class MountProvider implements IMountProvider {
  40. /**
  41. * @var \OCP\IConfig
  42. */
  43. protected $config;
  44. /**
  45. * @var IManager
  46. */
  47. protected $shareManager;
  48. /**
  49. * @var ILogger
  50. */
  51. protected $logger;
  52. /**
  53. * @param \OCP\IConfig $config
  54. * @param IManager $shareManager
  55. * @param ILogger $logger
  56. */
  57. public function __construct(IConfig $config, IManager $shareManager, ILogger $logger) {
  58. $this->config = $config;
  59. $this->shareManager = $shareManager;
  60. $this->logger = $logger;
  61. }
  62. /**
  63. * Get all mountpoints applicable for the user and check for shares where we need to update the etags
  64. *
  65. * @param \OCP\IUser $user
  66. * @param \OCP\Files\Storage\IStorageFactory $loader
  67. * @return \OCP\Files\Mount\IMountPoint[]
  68. */
  69. public function getMountsForUser(IUser $user, IStorageFactory $loader) {
  70. $shares = $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_USER, null, -1);
  71. $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_GROUP, null, -1));
  72. $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_CIRCLE, null, -1));
  73. $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_ROOM, null, -1));
  74. // filter out excluded shares and group shares that includes self
  75. $shares = array_filter($shares, function (\OCP\Share\IShare $share) use ($user) {
  76. return $share->getPermissions() > 0 && $share->getShareOwner() !== $user->getUID();
  77. });
  78. $superShares = $this->buildSuperShares($shares, $user);
  79. $mounts = [];
  80. $view = new View('/' . $user->getUID() . '/files');
  81. $ownerViews = [];
  82. $sharingDisabledForUser = $this->shareManager->sharingDisabledForUser($user->getUID());
  83. $foldersExistCache = new CappedMemoryCache();
  84. foreach ($superShares as $share) {
  85. try {
  86. /** @var \OCP\Share\IShare $parentShare */
  87. $parentShare = $share[0];
  88. if ($parentShare->getStatus() !== IShare::STATUS_ACCEPTED &&
  89. ($parentShare->getShareType() === IShare::TYPE_GROUP ||
  90. $parentShare->getShareType() === IShare::TYPE_USERGROUP ||
  91. $parentShare->getShareType() === IShare::TYPE_USER)) {
  92. continue;
  93. }
  94. $owner = $parentShare->getShareOwner();
  95. if (!isset($ownerViews[$owner])) {
  96. $ownerViews[$owner] = new View('/' . $parentShare->getShareOwner() . '/files');
  97. }
  98. $mount = new SharedMount(
  99. '\OCA\Files_Sharing\SharedStorage',
  100. $mounts,
  101. [
  102. 'user' => $user->getUID(),
  103. // parent share
  104. 'superShare' => $parentShare,
  105. // children/component of the superShare
  106. 'groupedShares' => $share[1],
  107. 'ownerView' => $ownerViews[$owner],
  108. 'sharingDisabledForUser' => $sharingDisabledForUser
  109. ],
  110. $loader,
  111. $view,
  112. $foldersExistCache
  113. );
  114. $mounts[$mount->getMountPoint()] = $mount;
  115. } catch (\Exception $e) {
  116. $this->logger->logException($e);
  117. $this->logger->error('Error while trying to create shared mount');
  118. }
  119. }
  120. // array_filter removes the null values from the array
  121. return array_values(array_filter($mounts));
  122. }
  123. /**
  124. * Groups shares by path (nodeId) and target path
  125. *
  126. * @param \OCP\Share\IShare[] $shares
  127. * @return \OCP\Share\IShare[][] array of grouped shares, each element in the
  128. * array is a group which itself is an array of shares
  129. */
  130. private function groupShares(array $shares) {
  131. $tmp = [];
  132. foreach ($shares as $share) {
  133. if (!isset($tmp[$share->getNodeId()])) {
  134. $tmp[$share->getNodeId()] = [];
  135. }
  136. $tmp[$share->getNodeId()][] = $share;
  137. }
  138. $result = [];
  139. // sort by stime, the super share will be based on the least recent share
  140. foreach ($tmp as &$tmp2) {
  141. @usort($tmp2, function ($a, $b) {
  142. $aTime = $a->getShareTime()->getTimestamp();
  143. $bTime = $b->getShareTime()->getTimestamp();
  144. if ($aTime === $bTime) {
  145. return $a->getId() < $b->getId() ? -1 : 1;
  146. }
  147. return $aTime < $bTime ? -1 : 1;
  148. });
  149. $result[] = $tmp2;
  150. }
  151. return array_values($result);
  152. }
  153. /**
  154. * Build super shares (virtual share) by grouping them by node id and target,
  155. * then for each group compute the super share and return it along with the matching
  156. * grouped shares. The most permissive permissions are used based on the permissions
  157. * of all shares within the group.
  158. *
  159. * @param \OCP\Share\IShare[] $allShares
  160. * @param \OCP\IUser $user user
  161. * @return array Tuple of [superShare, groupedShares]
  162. */
  163. private function buildSuperShares(array $allShares, \OCP\IUser $user) {
  164. $result = [];
  165. $groupedShares = $this->groupShares($allShares);
  166. /** @var \OCP\Share\IShare[] $shares */
  167. foreach ($groupedShares as $shares) {
  168. if (count($shares) === 0) {
  169. continue;
  170. }
  171. $superShare = $this->shareManager->newShare();
  172. // compute super share based on first entry of the group
  173. $superShare->setId($shares[0]->getId())
  174. ->setShareOwner($shares[0]->getShareOwner())
  175. ->setNodeId($shares[0]->getNodeId())
  176. ->setShareType($shares[0]->getShareType())
  177. ->setTarget($shares[0]->getTarget());
  178. // use most permissive permissions
  179. $permissions = 0;
  180. $status = IShare::STATUS_PENDING;
  181. foreach ($shares as $share) {
  182. $permissions |= $share->getPermissions();
  183. $status = max($status, $share->getStatus());
  184. if ($share->getTarget() !== $superShare->getTarget()) {
  185. // adjust target, for database consistency
  186. $share->setTarget($superShare->getTarget());
  187. try {
  188. $this->shareManager->moveShare($share, $user->getUID());
  189. } catch (\InvalidArgumentException $e) {
  190. // ignore as it is not important and we don't want to
  191. // block FS setup
  192. // the subsequent code anyway only uses the target of the
  193. // super share
  194. // such issue can usually happen when dealing with
  195. // null groups which usually appear with group backend
  196. // caching inconsistencies
  197. $this->logger->debug(
  198. 'Could not adjust share target for share ' . $share->getId() . ' to make it consistent: ' . $e->getMessage(),
  199. ['app' => 'files_sharing']
  200. );
  201. }
  202. }
  203. if (!is_null($share->getNodeCacheEntry())) {
  204. $superShare->setNodeCacheEntry($share->getNodeCacheEntry());
  205. }
  206. }
  207. $superShare->setPermissions($permissions)
  208. ->setStatus($status);
  209. $result[] = [$superShare, $shares];
  210. }
  211. return $result;
  212. }
  213. }