Skip to content

Commit ecb5d82

Browse files
committed
feat: implement authoritative mount provider for share provider
Signed-off-by: Robin Appelman <[email protected]>
1 parent a4af999 commit ecb5d82

3 files changed

Lines changed: 41 additions & 14 deletions

File tree

apps/files_sharing/lib/Listener/SharesUpdatedListener.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use OCP\EventDispatcher\IEventListener;
1616
use OCP\Files\Config\ICachedMountInfo;
1717
use OCP\Files\Config\IUserMountCache;
18+
use OCP\Files\Storage\IStorageFactory;
1819
use OCP\Group\Events\UserAddedEvent;
1920
use OCP\Group\Events\UserRemovedEvent;
2021
use OCP\IUser;
@@ -33,34 +34,50 @@ public function __construct(
3334
private readonly IUserMountCache $userMountCache,
3435
private readonly MountProvider $shareMountProvider,
3536
private readonly ShareTargetValidator $shareTargetValidator,
37+
private readonly IStorageFactory $storageFactory,
3638
) {
3739
}
3840

3941
public function handle(Event $event): void {
4042
if ($event instanceof UserAddedEvent || $event instanceof UserRemovedEvent || $event instanceof UserShareAccessUpdatedEvent) {
41-
$this->updateForUser($event->getUser());
43+
$this->updateForUser($event->getUser(), true);
4244
}
43-
if ($event instanceof ShareCreatedEvent || $event instanceof BeforeShareDeletedEvent) {
45+
if ($event instanceof ShareCreatedEvent) {
4446
foreach ($this->shareManager->getUsersForShare($event->getShare()) as $user) {
45-
$this->updateForUser($user);
47+
$this->updateForUser($user, true);
48+
}
49+
}
50+
if ($event instanceof BeforeShareDeletedEvent) {
51+
foreach ($this->shareManager->getUsersForShare($event->getShare()) as $user) {
52+
$this->updateForUser($user, false, [$event->getShare()]);
4653
}
4754
}
4855
}
4956

50-
private function updateForUser(IUser $user): void {
57+
private function updateForUser(IUser $user, bool $verifyMountPoints, array $ignoreShares = []): void {
5158
$cachedMounts = $this->userMountCache->getMountsForUser($user);
59+
$shareMounts = array_filter($cachedMounts, fn (ICachedMountInfo $mount) => $mount->getMountProvider() === MountProvider::class);
5260
$mountPoints = array_map(fn (ICachedMountInfo $mount) => $mount->getMountPoint(), $cachedMounts);
5361
$mountsByPath = array_combine($mountPoints, $cachedMounts);
5462

55-
$shares = $this->shareMountProvider->getSuperSharesForUser($user);
63+
$shares = $this->shareMountProvider->getSuperSharesForUser($user, $ignoreShares);
5664

65+
$mountsChanged = count($shares) !== count($shareMounts);
5766
foreach ($shares as &$share) {
5867
[$parentShare, $groupedShares] = $share;
5968
$mountPoint = '/' . $user->getUID() . '/files/' . trim($parentShare->getTarget(), '/') . '/';
6069
$mountKey = $parentShare->getNodeId() . '::' . $mountPoint;
6170
if (!isset($cachedMounts[$mountKey])) {
62-
$this->shareTargetValidator->verifyMountPoint($user, $parentShare, $mountsByPath, $groupedShares);
71+
$mountsChanged = true;
72+
if ($verifyMountPoints) {
73+
$this->shareTargetValidator->verifyMountPoint($user, $parentShare, $mountsByPath, $groupedShares);
74+
}
6375
}
6476
}
77+
78+
if ($mountsChanged) {
79+
$newMounts = $this->shareMountProvider->getMountsFromSuperShares($user, $shares, $this->storageFactory);
80+
$this->userMountCache->registerMounts($user, $newMounts, [MountProvider::class]);
81+
}
6582
}
6683
}

apps/files_sharing/lib/MountProvider.php

Lines changed: 11 additions & 6 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\Mount\IMountManager;
1819
use OCP\Files\Mount\IMountPoint;
@@ -26,7 +27,7 @@
2627
use Psr\Log\LoggerInterface;
2728
use function count;
2829

29-
class MountProvider implements IMountProvider {
30+
class MountProvider implements IMountProvider, IAuthoritativeMountProvider {
3031
/**
3132
* @param IConfig $config
3233
* @param IManager $shareManager
@@ -55,9 +56,10 @@ public function getMountsForUser(IUser $user, IStorageFactory $loader) {
5556

5657
/**
5758
* @param IUser $user
59+
* @param list<IShare> $excludeShares
5860
* @return list<array{IShare, array<IShare>}> Tuple of [superShare, groupedShares]
5961
*/
60-
public function getSuperSharesForUser(IUser $user): array {
62+
public function getSuperSharesForUser(IUser $user, array $excludeShares = []): array {
6163
$userId = $user->getUID();
6264
$shares = array_merge(
6365
$this->shareManager->getSharedWith($userId, IShare::TYPE_USER, null, -1),
@@ -67,7 +69,8 @@ public function getSuperSharesForUser(IUser $user): array {
6769
$this->shareManager->getSharedWith($userId, IShare::TYPE_DECK, null, -1),
6870
);
6971

70-
$shares = $this->filterShares($shares, $userId);
72+
$excludeShareIds = array_map(fn (IShare $share) => $share->getFullId(), $excludeShares);
73+
$shares = $this->filterShares($shares, $userId, $excludeShareIds);
7174
return $this->buildSuperShares($shares, $user);
7275
}
7376

@@ -343,15 +346,17 @@ public function getMountsFromSuperShares(
343346
* user has no permissions.
344347
*
345348
* @param IShare[] $shares
349+
* @param list<string> $excludeShareIds
346350
* @return IShare[]
347351
*/
348-
private function filterShares(array $shares, string $userId): array {
352+
private function filterShares(array $shares, string $userId, array $excludeShareIds): array {
349353
return array_filter(
350354
$shares,
351-
static function (IShare $share) use ($userId) {
355+
static function (IShare $share) use ($userId, $excludeShareIds) {
352356
return $share->getPermissions() > 0
353357
&& $share->getShareOwner() !== $userId
354-
&& $share->getSharedBy() !== $userId;
358+
&& $share->getSharedBy() !== $userId
359+
&& !in_array($share->getFullId(), $excludeShareIds);
355360
}
356361
);
357362
}

lib/private/Files/SetupManager.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -670,8 +670,13 @@ public function setupForProvider(string $path, array $providers): void {
670670
}
671671

672672
if ($this->fullSetupRequired($user)) {
673-
$this->setupForUser($user);
674-
return;
673+
if ($this->optimizeAuthoritativeProviders) {
674+
$this->updateNonAuthoritativeProviders($user);
675+
$this->markUserMountsCached($user);
676+
} else {
677+
$this->setupForUser($user);
678+
return;
679+
}
675680
}
676681

677682
$this->eventLogger->start('fs:setup:user:providers', 'Setup filesystem for ' . implode(', ', $providers));

0 commit comments

Comments
 (0)