MountProvider.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 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. $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_DECK, null, -1));
  75. // filter out excluded shares and group shares that includes self
  76. $shares = array_filter($shares, function (\OCP\Share\IShare $share) use ($user) {
  77. return $share->getPermissions() > 0 && $share->getShareOwner() !== $user->getUID();
  78. });
  79. $superShares = $this->buildSuperShares($shares, $user);
  80. $mounts = [];
  81. $view = new View('/' . $user->getUID() . '/files');
  82. $ownerViews = [];
  83. $sharingDisabledForUser = $this->shareManager->sharingDisabledForUser($user->getUID());
  84. $foldersExistCache = new CappedMemoryCache();
  85. foreach ($superShares as $share) {
  86. try {
  87. /** @var \OCP\Share\IShare $parentShare */
  88. $parentShare = $share[0];
  89. if ($parentShare->getStatus() !== IShare::STATUS_ACCEPTED &&
  90. ($parentShare->getShareType() === IShare::TYPE_GROUP ||
  91. $parentShare->getShareType() === IShare::TYPE_USERGROUP ||
  92. $parentShare->getShareType() === IShare::TYPE_USER)) {
  93. continue;
  94. }
  95. $owner = $parentShare->getShareOwner();
  96. if (!isset($ownerViews[$owner])) {
  97. $ownerViews[$owner] = new View('/' . $parentShare->getShareOwner() . '/files');
  98. }
  99. $mount = new SharedMount(
  100. '\OCA\Files_Sharing\SharedStorage',
  101. $mounts,
  102. [
  103. 'user' => $user->getUID(),
  104. // parent share
  105. 'superShare' => $parentShare,
  106. // children/component of the superShare
  107. 'groupedShares' => $share[1],
  108. 'ownerView' => $ownerViews[$owner],
  109. 'sharingDisabledForUser' => $sharingDisabledForUser
  110. ],
  111. $loader,
  112. $view,
  113. $foldersExistCache
  114. );
  115. $mounts[$mount->getMountPoint()] = $mount;
  116. } catch (\Exception $e) {
  117. $this->logger->logException($e);
  118. $this->logger->error('Error while trying to create shared mount');
  119. }
  120. }
  121. // array_filter removes the null values from the array
  122. return array_values(array_filter($mounts));
  123. }
  124. /**
  125. * Groups shares by path (nodeId) and target path
  126. *
  127. * @param \OCP\Share\IShare[] $shares
  128. * @return \OCP\Share\IShare[][] array of grouped shares, each element in the
  129. * array is a group which itself is an array of shares
  130. */
  131. private function groupShares(array $shares) {
  132. $tmp = [];
  133. foreach ($shares as $share) {
  134. if (!isset($tmp[$share->getNodeId()])) {
  135. $tmp[$share->getNodeId()] = [];
  136. }
  137. $tmp[$share->getNodeId()][] = $share;
  138. }
  139. $result = [];
  140. // sort by stime, the super share will be based on the least recent share
  141. foreach ($tmp as &$tmp2) {
  142. @usort($tmp2, function ($a, $b) {
  143. $aTime = $a->getShareTime()->getTimestamp();
  144. $bTime = $b->getShareTime()->getTimestamp();
  145. if ($aTime === $bTime) {
  146. return $a->getId() < $b->getId() ? -1 : 1;
  147. }
  148. return $aTime < $bTime ? -1 : 1;
  149. });
  150. $result[] = $tmp2;
  151. }
  152. return array_values($result);
  153. }
  154. /**
  155. * Build super shares (virtual share) by grouping them by node id and target,
  156. * then for each group compute the super share and return it along with the matching
  157. * grouped shares. The most permissive permissions are used based on the permissions
  158. * of all shares within the group.
  159. *
  160. * @param \OCP\Share\IShare[] $allShares
  161. * @param \OCP\IUser $user user
  162. * @return array Tuple of [superShare, groupedShares]
  163. */
  164. private function buildSuperShares(array $allShares, \OCP\IUser $user) {
  165. $result = [];
  166. $groupedShares = $this->groupShares($allShares);
  167. /** @var \OCP\Share\IShare[] $shares */
  168. foreach ($groupedShares as $shares) {
  169. if (count($shares) === 0) {
  170. continue;
  171. }
  172. $superShare = $this->shareManager->newShare();
  173. // compute super share based on first entry of the group
  174. $superShare->setId($shares[0]->getId())
  175. ->setShareOwner($shares[0]->getShareOwner())
  176. ->setNodeId($shares[0]->getNodeId())
  177. ->setShareType($shares[0]->getShareType())
  178. ->setTarget($shares[0]->getTarget());
  179. // use most permissive permissions
  180. $permissions = 0;
  181. $status = IShare::STATUS_PENDING;
  182. foreach ($shares as $share) {
  183. $permissions |= $share->getPermissions();
  184. $status = max($status, $share->getStatus());
  185. if ($share->getTarget() !== $superShare->getTarget()) {
  186. // adjust target, for database consistency
  187. $share->setTarget($superShare->getTarget());
  188. try {
  189. $this->shareManager->moveShare($share, $user->getUID());
  190. } catch (\InvalidArgumentException $e) {
  191. // ignore as it is not important and we don't want to
  192. // block FS setup
  193. // the subsequent code anyway only uses the target of the
  194. // super share
  195. // such issue can usually happen when dealing with
  196. // null groups which usually appear with group backend
  197. // caching inconsistencies
  198. $this->logger->debug(
  199. 'Could not adjust share target for share ' . $share->getId() . ' to make it consistent: ' . $e->getMessage(),
  200. ['app' => 'files_sharing']
  201. );
  202. }
  203. }
  204. if (!is_null($share->getNodeCacheEntry())) {
  205. $superShare->setNodeCacheEntry($share->getNodeCacheEntry());
  206. }
  207. }
  208. $superShare->setPermissions($permissions)
  209. ->setStatus($status);
  210. $result[] = [$superShare, $shares];
  211. }
  212. return $result;
  213. }
  214. }