|
7 | 7 | */ |
8 | 8 | namespace OCA\Files_Sharing; |
9 | 9 |
|
| 10 | +use Exception; |
10 | 11 | use InvalidArgumentException; |
11 | 12 | use OC\Files\View; |
12 | 13 | use OCA\Files_Sharing\Event\ShareMountedEvent; |
|
23 | 24 | use OCP\Share\IManager; |
24 | 25 | use OCP\Share\IShare; |
25 | 26 | use Psr\Log\LoggerInterface; |
| 27 | +use function count; |
26 | 28 |
|
27 | 29 | class MountProvider implements IMountProvider { |
28 | 30 | /** |
@@ -58,85 +60,10 @@ public function getMountsForUser(IUser $user, IStorageFactory $loader) { |
58 | 60 | $this->shareManager->getSharedWith($userId, IShare::TYPE_SCIENCEMESH, null, -1), |
59 | 61 | ); |
60 | 62 |
|
61 | | - // filter out shares owned or shared by the user and ones for which |
62 | | - // the user has no permissions |
63 | | - $shares = array_filter($shares, function (IShare $share) use ($userId) { |
64 | | - return $share->getPermissions() > 0 && $share->getShareOwner() !== $userId && $share->getSharedBy() !== $userId; |
65 | | - }); |
66 | | - |
| 63 | + $shares = $this->filterShares($shares, $userId); |
67 | 64 | $superShares = $this->buildSuperShares($shares, $user); |
68 | 65 |
|
69 | | - $allMounts = $this->mountManager->getAll(); |
70 | | - $mounts = []; |
71 | | - $view = new View('/' . $userId . '/files'); |
72 | | - $ownerViews = []; |
73 | | - $sharingDisabledForUser = $this->shareManager->sharingDisabledForUser($userId); |
74 | | - /** @var CappedMemoryCache<bool> $folderExistCache */ |
75 | | - $foldersExistCache = new CappedMemoryCache(); |
76 | | - |
77 | | - $validShareCache = $this->cacheFactory->createLocal('share-valid-mountpoint-max'); |
78 | | - $maxValidatedShare = $validShareCache->get($userId) ?? 0; |
79 | | - $newMaxValidatedShare = $maxValidatedShare; |
80 | | - |
81 | | - foreach ($superShares as $share) { |
82 | | - [$parentShare, $groupedShares] = $share; |
83 | | - try { |
84 | | - if ($parentShare->getStatus() !== IShare::STATUS_ACCEPTED |
85 | | - && ($parentShare->getShareType() === IShare::TYPE_GROUP |
86 | | - || $parentShare->getShareType() === IShare::TYPE_USERGROUP |
87 | | - || $parentShare->getShareType() === IShare::TYPE_USER)) { |
88 | | - continue; |
89 | | - } |
90 | | - |
91 | | - $owner = $parentShare->getShareOwner(); |
92 | | - if (!isset($ownerViews[$owner])) { |
93 | | - $ownerViews[$owner] = new View('/' . $owner . '/files'); |
94 | | - } |
95 | | - $shareId = (int)$parentShare->getId(); |
96 | | - $mount = new SharedMount( |
97 | | - '\OCA\Files_Sharing\SharedStorage', |
98 | | - $allMounts, |
99 | | - [ |
100 | | - 'user' => $userId, |
101 | | - // parent share |
102 | | - 'superShare' => $parentShare, |
103 | | - // children/component of the superShare |
104 | | - 'groupedShares' => $groupedShares, |
105 | | - 'ownerView' => $ownerViews[$owner], |
106 | | - 'sharingDisabledForUser' => $sharingDisabledForUser |
107 | | - ], |
108 | | - $loader, |
109 | | - $view, |
110 | | - $foldersExistCache, |
111 | | - $this->eventDispatcher, |
112 | | - $user, |
113 | | - ($shareId <= $maxValidatedShare), |
114 | | - ); |
115 | | - |
116 | | - $newMaxValidatedShare = max($shareId, $newMaxValidatedShare); |
117 | | - |
118 | | - $event = new ShareMountedEvent($mount); |
119 | | - $this->eventDispatcher->dispatchTyped($event); |
120 | | - |
121 | | - $mounts[$mount->getMountPoint()] = $allMounts[$mount->getMountPoint()] = $mount; |
122 | | - foreach ($event->getAdditionalMounts() as $additionalMount) { |
123 | | - $allMounts[$additionalMount->getMountPoint()] = $mounts[$additionalMount->getMountPoint()] = $additionalMount; |
124 | | - } |
125 | | - } catch (\Exception $e) { |
126 | | - $this->logger->error( |
127 | | - 'Error while trying to create shared mount', |
128 | | - [ |
129 | | - 'app' => 'files_sharing', |
130 | | - 'exception' => $e, |
131 | | - ], |
132 | | - ); |
133 | | - } |
134 | | - } |
135 | | - |
136 | | - $validShareCache->set($userId, $newMaxValidatedShare, 24 * 60 * 60); |
137 | | - |
138 | | - // array_filter removes the null values from the array |
139 | | - return array_values(array_filter($mounts)); |
| 66 | + return $this->getMountsFromSuperShares($userId, $superShares, $loader, $user); |
140 | 67 | } |
141 | 68 |
|
142 | 69 | /** |
@@ -192,7 +119,7 @@ private function buildSuperShares(array $allShares, IUser $user) { |
192 | 119 | $groupedShares = $this->groupShares($allShares); |
193 | 120 |
|
194 | 121 | foreach ($groupedShares as $shares) { |
195 | | - if (\count($shares) === 0) { |
| 122 | + if (count($shares) === 0) { |
196 | 123 | continue; |
197 | 124 | } |
198 | 125 |
|
@@ -317,4 +244,114 @@ public function adjustTarget( |
317 | 244 | ); |
318 | 245 | } |
319 | 246 | } |
| 247 | + /** |
| 248 | + * @param string $userId |
| 249 | + * @param array $superShares |
| 250 | + * @param IStorageFactory $loader |
| 251 | + * @param IUser $user |
| 252 | + * @return array |
| 253 | + * @throws Exception |
| 254 | + */ |
| 255 | + public function getMountsFromSuperShares( |
| 256 | + string $userId, |
| 257 | + array $superShares, |
| 258 | + IStorageFactory $loader, |
| 259 | + IUser $user, |
| 260 | + ): array { |
| 261 | + $allMounts = $this->mountManager->getAll(); |
| 262 | + $mounts = []; |
| 263 | + $view = new View('/' . $userId . '/files'); |
| 264 | + $ownerViews = []; |
| 265 | + $sharingDisabledForUser |
| 266 | + = $this->shareManager->sharingDisabledForUser($userId); |
| 267 | + /** @var CappedMemoryCache<bool> $folderExistCache */ |
| 268 | + $foldersExistCache = new CappedMemoryCache(); |
| 269 | + |
| 270 | + $validShareCache |
| 271 | + = $this->cacheFactory->createLocal('share-valid-mountpoint-max'); |
| 272 | + $maxValidatedShare = $validShareCache->get($userId) ?? 0; |
| 273 | + $newMaxValidatedShare = $maxValidatedShare; |
| 274 | + |
| 275 | + foreach ($superShares as $share) { |
| 276 | + [$parentShare, $groupedShares] = $share; |
| 277 | + try { |
| 278 | + if ($parentShare->getStatus() !== IShare::STATUS_ACCEPTED |
| 279 | + && ($parentShare->getShareType() === IShare::TYPE_GROUP |
| 280 | + || $parentShare->getShareType() === IShare::TYPE_USERGROUP |
| 281 | + || $parentShare->getShareType() === IShare::TYPE_USER) |
| 282 | + ) { |
| 283 | + continue; |
| 284 | + } |
| 285 | + |
| 286 | + $owner = $parentShare->getShareOwner(); |
| 287 | + if (!isset($ownerViews[$owner])) { |
| 288 | + $ownerViews[$owner] = new View('/' . $owner . '/files'); |
| 289 | + } |
| 290 | + $shareId = (int)$parentShare->getId(); |
| 291 | + $mount = new SharedMount( |
| 292 | + '\OCA\Files_Sharing\SharedStorage', |
| 293 | + $allMounts, |
| 294 | + [ |
| 295 | + 'user' => $userId, |
| 296 | + // parent share |
| 297 | + 'superShare' => $parentShare, |
| 298 | + // children/component of the superShare |
| 299 | + 'groupedShares' => $groupedShares, |
| 300 | + 'ownerView' => $ownerViews[$owner], |
| 301 | + 'sharingDisabledForUser' => $sharingDisabledForUser |
| 302 | + ], |
| 303 | + $loader, |
| 304 | + $view, |
| 305 | + $foldersExistCache, |
| 306 | + $this->eventDispatcher, |
| 307 | + $user, |
| 308 | + $shareId <= $maxValidatedShare, |
| 309 | + ); |
| 310 | + |
| 311 | + $newMaxValidatedShare = max($shareId, $newMaxValidatedShare); |
| 312 | + |
| 313 | + $event = new ShareMountedEvent($mount); |
| 314 | + $this->eventDispatcher->dispatchTyped($event); |
| 315 | + |
| 316 | + $mounts[$mount->getMountPoint()] |
| 317 | + = $allMounts[$mount->getMountPoint()] = $mount; |
| 318 | + foreach ($event->getAdditionalMounts() as $additionalMount) { |
| 319 | + $allMounts[$additionalMount->getMountPoint()] |
| 320 | + = $mounts[$additionalMount->getMountPoint()] |
| 321 | + = $additionalMount; |
| 322 | + } |
| 323 | + } catch (Exception $e) { |
| 324 | + $this->logger->error( |
| 325 | + 'Error while trying to create shared mount', |
| 326 | + [ |
| 327 | + 'app' => 'files_sharing', |
| 328 | + 'exception' => $e, |
| 329 | + ], |
| 330 | + ); |
| 331 | + } |
| 332 | + } |
| 333 | + |
| 334 | + $validShareCache->set($userId, $newMaxValidatedShare, 24 * 60 * 60); |
| 335 | + |
| 336 | + // array_filter removes the null values from the array |
| 337 | + return array_values(array_filter($mounts)); |
| 338 | + } |
| 339 | + |
| 340 | + /** |
| 341 | + * Filters out shares owned or shared by the user and ones for which the |
| 342 | + * user has no permissions. |
| 343 | + * |
| 344 | + * @param IShare[] $shares |
| 345 | + * @return IShare[] |
| 346 | + */ |
| 347 | + public function filterShares(array $shares, string $userId): array { |
| 348 | + return array_filter( |
| 349 | + $shares, |
| 350 | + static function (IShare $share) use ($userId) { |
| 351 | + return $share->getPermissions() > 0 |
| 352 | + && $share->getShareOwner() !== $userId |
| 353 | + && $share->getSharedBy() !== $userId; |
| 354 | + } |
| 355 | + ); |
| 356 | + } |
320 | 357 | } |
0 commit comments