MountProvider.php 7.5 KB

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