Skip to content

Commit 84d256f

Browse files
committed
refactor(files_sharing): extract getMountsForUser logic
Signed-off-by: Salvatore Martire <[email protected]>
1 parent 4d5c9ef commit 84d256f

File tree

1 file changed

+115
-78
lines changed

1 file changed

+115
-78
lines changed

apps/files_sharing/lib/MountProvider.php

Lines changed: 115 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
namespace OCA\Files_Sharing;
99

10+
use Exception;
1011
use InvalidArgumentException;
1112
use OC\Files\View;
1213
use OCA\Files_Sharing\Event\ShareMountedEvent;
@@ -23,6 +24,7 @@
2324
use OCP\Share\IManager;
2425
use OCP\Share\IShare;
2526
use Psr\Log\LoggerInterface;
27+
use function count;
2628

2729
class MountProvider implements IMountProvider {
2830
/**
@@ -58,85 +60,10 @@ public function getMountsForUser(IUser $user, IStorageFactory $loader) {
5860
$this->shareManager->getSharedWith($userId, IShare::TYPE_SCIENCEMESH, null, -1),
5961
);
6062

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);
6764
$superShares = $this->buildSuperShares($shares, $user);
6865

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);
14067
}
14168

14269
/**
@@ -192,7 +119,7 @@ private function buildSuperShares(array $allShares, IUser $user) {
192119
$groupedShares = $this->groupShares($allShares);
193120

194121
foreach ($groupedShares as $shares) {
195-
if (\count($shares) === 0) {
122+
if (count($shares) === 0) {
196123
continue;
197124
}
198125

@@ -317,4 +244,114 @@ public function adjustTarget(
317244
);
318245
}
319246
}
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+
}
320357
}

0 commit comments

Comments
 (0)