Skip to content

Commit ff28ac7

Browse files
committed
refactor(files_sharing): apply DRY for user and node ID in MountProvider
Signed-off-by: Salvatore Martire <[email protected]>
1 parent 6719f5a commit ff28ac7

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

apps/files_sharing/lib/MountProvider.php

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,32 +46,34 @@ public function __construct(
4646
* @return IMountPoint[]
4747
*/
4848
public function getMountsForUser(IUser $user, IStorageFactory $loader) {
49+
$userId = $user->getUID();
4950
$shares = array_merge(
50-
$this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_USER, null, -1),
51-
$this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_GROUP, null, -1),
52-
$this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_CIRCLE, null, -1),
53-
$this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_ROOM, null, -1),
54-
$this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_DECK, null, -1),
55-
$this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_SCIENCEMESH, null, -1),
51+
$this->shareManager->getSharedWith($userId, IShare::TYPE_USER, null, -1),
52+
$this->shareManager->getSharedWith($userId, IShare::TYPE_GROUP, null, -1),
53+
$this->shareManager->getSharedWith($userId, IShare::TYPE_CIRCLE, null, -1),
54+
$this->shareManager->getSharedWith($userId, IShare::TYPE_ROOM, null, -1),
55+
$this->shareManager->getSharedWith($userId, IShare::TYPE_DECK, null, -1),
56+
$this->shareManager->getSharedWith($userId, IShare::TYPE_SCIENCEMESH, null, -1),
5657
);
5758

58-
// filter out excluded shares and group shares that includes self
59-
$shares = array_filter($shares, function (IShare $share) use ($user) {
60-
return $share->getPermissions() > 0 && $share->getShareOwner() !== $user->getUID() && $share->getSharedBy() !== $user->getUID();
59+
// filter out shares owned or shared by the user and ones for which
60+
// the user has no permissions
61+
$shares = array_filter($shares, function (IShare $share) use ($userId) {
62+
return $share->getPermissions() > 0 && $share->getShareOwner() !== $userId && $share->getSharedBy() !== $userId;
6163
});
6264

6365
$superShares = $this->buildSuperShares($shares, $user);
6466

6567
$allMounts = $this->mountManager->getAll();
6668
$mounts = [];
67-
$view = new View('/' . $user->getUID() . '/files');
69+
$view = new View('/' . $userId . '/files');
6870
$ownerViews = [];
69-
$sharingDisabledForUser = $this->shareManager->sharingDisabledForUser($user->getUID());
71+
$sharingDisabledForUser = $this->shareManager->sharingDisabledForUser($userId);
7072
/** @var CappedMemoryCache<bool> $folderExistCache */
7173
$foldersExistCache = new CappedMemoryCache();
7274

7375
$validShareCache = $this->cacheFactory->createLocal('share-valid-mountpoint-max');
74-
$maxValidatedShare = $validShareCache->get($user->getUID()) ?? 0;
76+
$maxValidatedShare = $validShareCache->get($userId) ?? 0;
7577
$newMaxValidatedShare = $maxValidatedShare;
7678

7779
foreach ($superShares as $share) {
@@ -95,7 +97,7 @@ public function getMountsForUser(IUser $user, IStorageFactory $loader) {
9597
'\OCA\Files_Sharing\SharedStorage',
9698
$allMounts,
9799
[
98-
'user' => $user->getUID(),
100+
'user' => $userId,
99101
// parent share
100102
'superShare' => $parentShare,
101103
// children/component of the superShare
@@ -131,7 +133,7 @@ public function getMountsForUser(IUser $user, IStorageFactory $loader) {
131133
}
132134
}
133135

134-
$validShareCache->set($user->getUID(), $newMaxValidatedShare, 24 * 60 * 60);
136+
$validShareCache->set($userId, $newMaxValidatedShare, 24 * 60 * 60);
135137

136138
// array_filter removes the null values from the array
137139
return array_values(array_filter($mounts));
@@ -148,10 +150,11 @@ private function groupShares(array $shares) {
148150
$tmp = [];
149151

150152
foreach ($shares as $share) {
151-
if (!isset($tmp[$share->getNodeId()])) {
152-
$tmp[$share->getNodeId()] = [];
153+
$nodeId = $share->getNodeId();
154+
if (!isset($tmp[$nodeId])) {
155+
$tmp[$nodeId] = [];
153156
}
154-
$tmp[$share->getNodeId()][] = $share;
157+
$tmp[$nodeId][] = $share;
155158
}
156159

157160
$result = [];

build/psalm-baseline.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,11 +1686,11 @@
16861686
<file src="apps/files_sharing/lib/MountProvider.php">
16871687
<InternalClass>
16881688
<code><![CDATA[new View('/' . $parentShare->getShareOwner() . '/files')]]></code>
1689-
<code><![CDATA[new View('/' . $user->getUID() . '/files')]]></code>
1689+
<code><![CDATA[new View('/' . $userId . '/files')]]></code>
16901690
</InternalClass>
16911691
<InternalMethod>
16921692
<code><![CDATA[new View('/' . $parentShare->getShareOwner() . '/files')]]></code>
1693-
<code><![CDATA[new View('/' . $user->getUID() . '/files')]]></code>
1693+
<code><![CDATA[new View('/' . $userId . '/files')]]></code>
16941694
</InternalMethod>
16951695
<RedundantFunctionCall>
16961696
<code><![CDATA[array_values]]></code>

0 commit comments

Comments
 (0)