Skip to content

Commit 12e4462

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

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

apps/files_sharing/lib/Listener/SharesUpdatedListener.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use OCP\Files\Config\ICachedMountInfo;
1818
use OCP\Files\Config\IUserMountCache;
1919
use OCP\Files\Events\Node\FilesystemTornDownEvent;
20+
use OCP\Files\Storage\IStorageFactory;
2021
use OCP\Group\Events\UserAddedEvent;
2122
use OCP\Group\Events\UserRemovedEvent;
2223
use OCP\IUser;
@@ -37,6 +38,7 @@ public function __construct(
3738
private readonly IUserMountCache $userMountCache,
3839
private readonly MountProvider $shareMountProvider,
3940
private readonly ShareTargetValidator $shareTargetValidator,
41+
private readonly IStorageFactory $storageFactory,
4042
) {
4143
$this->updatedUsers = new CappedMemoryCache();
4244
}
@@ -46,38 +48,52 @@ public function handle(Event $event): void {
4648
}
4749
if ($event instanceof UserShareAccessUpdatedEvent) {
4850
foreach ($event->getUsers() as $user) {
49-
$this->updateForUser($user);
51+
$this->updateForUser($user, true);
5052
}
5153
}
5254
if ($event instanceof UserAddedEvent || $event instanceof UserRemovedEvent) {
53-
$this->updateForUser($event->getUser());
55+
$this->updateForUser($event->getUser(), true);
5456
}
55-
if ($event instanceof ShareCreatedEvent || $event instanceof BeforeShareDeletedEvent) {
57+
if ($event instanceof ShareCreatedEvent) {
5658
foreach ($this->shareManager->getUsersForShare($event->getShare()) as $user) {
57-
$this->updateForUser($user);
59+
$this->updateForUser($user, true);
60+
}
61+
}
62+
if ($event instanceof BeforeShareDeletedEvent) {
63+
foreach ($this->shareManager->getUsersForShare($event->getShare()) as $user) {
64+
$this->updateForUser($user, false, [$event->getShare()]);
5865
}
5966
}
6067
}
6168

62-
private function updateForUser(IUser $user): void {
69+
private function updateForUser(IUser $user, bool $verifyMountPoints, array $ignoreShares = []): void {
6370
if (isset($this->updatedUsers[$user->getUID()])) {
6471
return;
6572
}
6673
$this->updatedUsers[$user->getUID()] = true;
67-
6874
$cachedMounts = $this->userMountCache->getMountsForUser($user);
75+
$shareMounts = array_filter($cachedMounts, fn (ICachedMountInfo $mount) => $mount->getMountProvider() === MountProvider::class);
6976
$mountPoints = array_map(fn (ICachedMountInfo $mount) => $mount->getMountPoint(), $cachedMounts);
7077
$mountsByPath = array_combine($mountPoints, $cachedMounts);
7178

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

81+
$mountsChanged = count($shares) !== count($shareMounts);
7482
foreach ($shares as &$share) {
7583
[$parentShare, $groupedShares] = $share;
7684
$mountPoint = '/' . $user->getUID() . '/files/' . trim($parentShare->getTarget(), '/') . '/';
7785
$mountKey = $parentShare->getNodeId() . '::' . $mountPoint;
7886
if (!isset($cachedMounts[$mountKey])) {
79-
$this->shareTargetValidator->verifyMountPoint($user, $parentShare, $mountsByPath, $groupedShares);
87+
$mountsChanged = true;
88+
if ($verifyMountPoints) {
89+
$this->shareTargetValidator->verifyMountPoint($user, $parentShare, $mountsByPath, $groupedShares);
90+
}
8091
}
8192
}
93+
94+
if ($mountsChanged) {
95+
$newMounts = $this->shareMountProvider->getMountsFromSuperShares($user, $shares, $this->storageFactory);
96+
$this->userMountCache->registerMounts($user, $newMounts, [MountProvider::class]);
97+
}
8298
}
8399
}

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)