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 8.1KB

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