Skip to content

Commit d2aece4

Browse files
committed
feat: implement authoritative mount provider for share provider
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent 3f9849d commit d2aece4

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

apps/files_sharing/lib/Listener/SharesUpdatedListener.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function __construct(
3737
private readonly IUserMountCache $userMountCache,
3838
private readonly MountProvider $shareMountProvider,
3939
private readonly ShareTargetValidator $shareTargetValidator,
40+
private readonly IStorageFactory $storageFactory,
4041
) {
4142
$this->updatedUsers = new CappedMemoryCache();
4243
}
@@ -46,38 +47,52 @@ public function handle(Event $event): void {
4647
}
4748
if ($event instanceof UserShareAccessUpdatedEvent) {
4849
foreach ($event->getUsers() as $user) {
49-
$this->updateForUser($user);
50+
$this->updateForUser($user, true);
5051
}
5152
}
5253
if ($event instanceof UserAddedEvent || $event instanceof UserRemovedEvent) {
53-
$this->updateForUser($event->getUser());
54+
$this->updateForUser($event->getUser(), true);
5455
}
55-
if ($event instanceof ShareCreatedEvent || $event instanceof BeforeShareDeletedEvent) {
56+
if ($event instanceof ShareCreatedEvent) {
5657
foreach ($this->shareManager->getUsersForShare($event->getShare()) as $user) {
57-
$this->updateForUser($user);
58+
$this->updateForUser($user, true);
59+
}
60+
}
61+
if ($event instanceof BeforeShareDeletedEvent) {
62+
foreach ($this->shareManager->getUsersForShare($event->getShare()) as $user) {
63+
$this->updateForUser($user, false, [$event->getShare()]);
5864
}
5965
}
6066
}
6167

62-
private function updateForUser(IUser $user): void {
68+
private function updateForUser(IUser $user, bool $verifyMountPoints, array $ignoreShares = []): void {
6369
if (isset($this->updatedUsers[$user->getUID()])) {
6470
return;
6571
}
6672
$this->updatedUsers[$user->getUID()] = true;
67-
6873
$cachedMounts = $this->userMountCache->getMountsForUser($user);
74+
$shareMounts = array_filter($cachedMounts, fn (ICachedMountInfo $mount) => $mount->getMountProvider() === MountProvider::class);
6975
$mountPoints = array_map(fn (ICachedMountInfo $mount) => $mount->getMountPoint(), $cachedMounts);
7076
$mountsByPath = array_combine($mountPoints, $cachedMounts);
7177

72-
$shares = $this->shareMountProvider->getSuperSharesForUser($user);
78+
$shares = $this->shareMountProvider->getSuperSharesForUser($user, $ignoreShares);
7379

80+
$mountsChanged = count($shares) !== count($shareMounts);
7481
foreach ($shares as &$share) {
7582
[$parentShare, $groupedShares] = $share;
7683
$mountPoint = '/' . $user->getUID() . '/files/' . trim($parentShare->getTarget(), '/') . '/';
7784
$mountKey = $parentShare->getNodeId() . '::' . $mountPoint;
7885
if (!isset($cachedMounts[$mountKey])) {
79-
$this->shareTargetValidator->verifyMountPoint($user, $parentShare, $mountsByPath, $groupedShares);
86+
$mountsChanged = true;
87+
if ($verifyMountPoints) {
88+
$this->shareTargetValidator->verifyMountPoint($user, $parentShare, $mountsByPath, $groupedShares);
89+
}
8090
}
8191
}
92+
93+
if ($mountsChanged) {
94+
$newMounts = $this->shareMountProvider->getMountsFromSuperShares($user, $shares, $this->storageFactory);
95+
$this->userMountCache->registerMounts($user, $newMounts, [MountProvider::class]);
96+
}
8297
}
8398
}

apps/files_sharing/lib/MountProvider.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use OCA\Files_Sharing\Event\ShareMountedEvent;
1414
use OCP\Cache\CappedMemoryCache;
1515
use OCP\EventDispatcher\IEventDispatcher;
16+
use OCP\Files\Config\IAuthoritativeMountProvider;
1617
use OCP\Files\Config\IMountProvider;
1718
use OCP\Files\Config\IPartialMountProvider;
1819
use OCP\Files\Mount\IMountManager;
@@ -28,7 +29,7 @@
2829

2930
use function count;
3031

31-
class MountProvider implements IMountProvider, IPartialMountProvider {
32+
class MountProvider implements IMountProvider, IAuthoritativeMountProvider, IPartialMountProvider {
3233
/**
3334
* @param IConfig $config
3435
* @param IManager $shareManager
@@ -57,9 +58,10 @@ public function getMountsForUser(IUser $user, IStorageFactory $loader) {
5758

5859
/**
5960
* @param IUser $user
61+
* @param list<IShare> $excludeShares
6062
* @return list<array{IShare, array<IShare>}> Tuple of [superShare, groupedShares]
6163
*/
62-
public function getSuperSharesForUser(IUser $user): array {
64+
public function getSuperSharesForUser(IUser $user, array $excludeShares = []): array {
6365
$userId = $user->getUID();
6466
$shares = $this->mergeIterables(
6567
$this->shareManager->getSharedWith($userId, IShare::TYPE_USER, null, -1),
@@ -69,7 +71,8 @@ public function getSuperSharesForUser(IUser $user): array {
6971
$this->shareManager->getSharedWith($userId, IShare::TYPE_DECK, null, -1),
7072
);
7173

72-
$shares = $this->filterShares($shares, $userId);
74+
$excludeShareIds = array_map(fn (IShare $share) => $share->getFullId(), $excludeShares);
75+
$shares = $this->filterShares($shares, $userId, $excludeShareIds);
7376
return $this->buildSuperShares($shares, $user);
7477
}
7578

@@ -340,14 +343,16 @@ public function getMountsFromSuperShares(
340343
* user has no permissions.
341344
*
342345
* @param iterable<IShare> $shares
346+
* @param list<string> $excludeShareIds
343347
* @return iterable<IShare>
344348
*/
345-
private function filterShares(iterable $shares, string $userId): iterable {
349+
private function filterShares(iterable $shares, string $userId, array $excludeShareIds = []): iterable {
346350
foreach ($shares as $share) {
347351
if (
348352
$share->getPermissions() > 0
349353
&& $share->getShareOwner() !== $userId
350354
&& $share->getSharedBy() !== $userId
355+
&& !in_array($share->getFullId(), $excludeShareIds)
351356
) {
352357
yield $share;
353358
}

0 commit comments

Comments
 (0)