-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Active share validation/authoritative mount improvements #57760
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
34b388e
8e51d6c
2a04110
278e38a
1e46178
3f9e857
d2f2135
4b00db9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,16 +8,16 @@ | |
|
|
||
| namespace OCA\Files_Sharing\Listener; | ||
|
|
||
| use OCA\Files_Sharing\AppInfo\Application; | ||
| use OCA\Files_Sharing\Config\ConfigLexicon; | ||
| use OCA\Files_Sharing\Event\UserShareAccessUpdatedEvent; | ||
| use OCA\Files_Sharing\MountProvider; | ||
| use OCA\Files_Sharing\ShareTargetValidator; | ||
| use OCA\Files_Sharing\ShareRecipientUpdater; | ||
| use OCP\Config\IUserConfig; | ||
| use OCP\EventDispatcher\Event; | ||
| use OCP\EventDispatcher\IEventListener; | ||
| use OCP\Files\Config\ICachedMountInfo; | ||
| use OCP\Files\Config\IUserMountCache; | ||
| use OCP\Files\Storage\IStorageFactory; | ||
| use OCP\Group\Events\UserAddedEvent; | ||
| use OCP\Group\Events\UserRemovedEvent; | ||
| use OCP\IAppConfig; | ||
| use OCP\IUser; | ||
| use OCP\Share\Events\BeforeShareDeletedEvent; | ||
| use OCP\Share\Events\ShareCreatedEvent; | ||
|
|
@@ -30,71 +30,79 @@ | |
| * @template-implements IEventListener<UserAddedEvent|UserRemovedEvent|ShareCreatedEvent|ShareTransferredEvent|BeforeShareDeletedEvent|UserShareAccessUpdatedEvent> | ||
| */ | ||
| class SharesUpdatedListener implements IEventListener { | ||
| private array $inUpdate = []; | ||
| /** | ||
| * for how many users do we update the share date immediately, | ||
| * before just marking the other users when we know the relevant share | ||
| */ | ||
| private int $cutOffMarkAllSingleShare; | ||
| /** | ||
| * for how many users do we update the share date immediately, | ||
| * before just marking the other users when the relevant shares are unknown | ||
| */ | ||
| private int $cutOffMarkAllShares ; | ||
|
|
||
| private int $updatedCount = 0; | ||
|
|
||
| public function __construct( | ||
| private readonly IManager $shareManager, | ||
| private readonly IUserMountCache $userMountCache, | ||
| private readonly MountProvider $shareMountProvider, | ||
| private readonly ShareTargetValidator $shareTargetValidator, | ||
| private readonly IStorageFactory $storageFactory, | ||
| private readonly ShareRecipientUpdater $shareUpdater, | ||
| private readonly IUserConfig $userConfig, | ||
| IAppConfig $appConfig, | ||
| ) { | ||
| $this->cutOffMarkAllSingleShare = $appConfig->getValueInt(Application::APP_ID, ConfigLexicon::UPDATE_SINGLE_CUTOFF, 10); | ||
| $this->cutOffMarkAllShares = $appConfig->getValueInt(Application::APP_ID, ConfigLexicon::UPDATE_ALL_CUTOFF, 3); | ||
| } | ||
|
|
||
| public function handle(Event $event): void { | ||
| if ($event instanceof UserShareAccessUpdatedEvent) { | ||
| foreach ($event->getUsers() as $user) { | ||
| $this->updateForUser($user, true); | ||
| $this->updateOrMarkUser($user, true); | ||
| } | ||
| } | ||
| if ($event instanceof UserAddedEvent || $event instanceof UserRemovedEvent) { | ||
| $this->updateForUser($event->getUser(), true); | ||
| $this->updateOrMarkUser($event->getUser(), true); | ||
| } | ||
| if ( | ||
| $event instanceof ShareCreatedEvent | ||
| || $event instanceof ShareTransferredEvent | ||
| ) { | ||
| foreach ($this->shareManager->getUsersForShare($event->getShare()) as $user) { | ||
| $this->updateForUser($user, true); | ||
| if ($event instanceof ShareCreatedEvent || $event instanceof ShareTransferredEvent) { | ||
| $share = $event->getShare(); | ||
| $shareTarget = $share->getTarget(); | ||
| foreach ($this->shareManager->getUsersForShare($share) as $user) { | ||
| if ($share->getSharedBy() !== $user->getUID()) { | ||
| $this->updatedCount++; | ||
| if ($this->cutOffMarkAllSingleShare === -1 || $this->updatedCount <= $this->cutOffMarkAllSingleShare) { | ||
| $this->shareUpdater->updateForShare($user, $share); | ||
| // Share target validation might have changed the target, restore it for the next user | ||
| $share->setTarget($shareTarget); | ||
| } else { | ||
| $this->markUserForRefresh($user); | ||
| } | ||
|
Comment on lines
+71
to
+77
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure that a user count make sense. I would rather base it on a time limit. |
||
| } | ||
| } | ||
| } | ||
| if ($event instanceof BeforeShareDeletedEvent) { | ||
| foreach ($this->shareManager->getUsersForShare($event->getShare()) as $user) { | ||
| $this->updateForUser($user, false, [$event->getShare()]); | ||
| $this->updateOrMarkUser($user, false, [$event->getShare()]); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private function updateForUser(IUser $user, bool $verifyMountPoints, array $ignoreShares = []): void { | ||
| // prevent recursion | ||
| if (isset($this->inUpdate[$user->getUID()])) { | ||
| return; | ||
| private function updateOrMarkUser(IUser $user, bool $verifyMountPoints, array $ignoreShares = []): void { | ||
| $this->updatedCount++; | ||
| if ($this->cutOffMarkAllShares === -1 || $this->updatedCount <= $this->cutOffMarkAllShares) { | ||
| $this->shareUpdater->updateForUser($user, $verifyMountPoints, $ignoreShares); | ||
| } else { | ||
| $this->markUserForRefresh($user); | ||
| } | ||
| $this->inUpdate[$user->getUID()] = true; | ||
| $cachedMounts = $this->userMountCache->getMountsForUser($user); | ||
| $shareMounts = array_filter($cachedMounts, fn (ICachedMountInfo $mount) => $mount->getMountProvider() === MountProvider::class); | ||
| $mountPoints = array_map(fn (ICachedMountInfo $mount) => $mount->getMountPoint(), $cachedMounts); | ||
| $mountsByPath = array_combine($mountPoints, $cachedMounts); | ||
|
|
||
| $shares = $this->shareMountProvider->getSuperSharesForUser($user, $ignoreShares); | ||
| } | ||
|
|
||
| $mountsChanged = count($shares) !== count($shareMounts); | ||
| foreach ($shares as &$share) { | ||
| [$parentShare, $groupedShares] = $share; | ||
| $mountPoint = '/' . $user->getUID() . '/files/' . trim($parentShare->getTarget(), '/') . '/'; | ||
| $mountKey = $parentShare->getNodeId() . '::' . $mountPoint; | ||
| if (!isset($cachedMounts[$mountKey])) { | ||
| $mountsChanged = true; | ||
| if ($verifyMountPoints) { | ||
| $this->shareTargetValidator->verifyMountPoint($user, $parentShare, $mountsByPath, $groupedShares); | ||
| } | ||
| } | ||
| } | ||
| private function markUserForRefresh(IUser $user): void { | ||
| $this->userConfig->setValueBool($user->getUID(), Application::APP_ID, ConfigLexicon::USER_NEEDS_SHARE_REFRESH, true); | ||
| } | ||
|
|
||
| if ($mountsChanged) { | ||
| $newMounts = $this->shareMountProvider->getMountsFromSuperShares($user, $shares, $this->storageFactory); | ||
| $this->userMountCache->registerMounts($user, $newMounts, [MountProvider::class]); | ||
| } | ||
| public function setCutOffMarkAllSingleShare(int $cutOffMarkAllSingleShare): void { | ||
| $this->cutOffMarkAllSingleShare = $cutOffMarkAllSingleShare; | ||
| } | ||
|
|
||
| unset($this->inUpdate[$user->getUID()]); | ||
| public function setCutOffMarkAllShares(int $cutOffMarkAllShares): void { | ||
| $this->cutOffMarkAllShares = $cutOffMarkAllShares; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Files_Sharing\Listener; | ||
|
|
||
| use OCA\Files_Sharing\AppInfo\Application; | ||
| use OCA\Files_Sharing\Config\ConfigLexicon; | ||
| use OCA\Files_Sharing\ShareRecipientUpdater; | ||
| use OCP\Config\IUserConfig; | ||
| use OCP\EventDispatcher\Event; | ||
| use OCP\EventDispatcher\IEventListener; | ||
| use OCP\Files\Events\UserHomeSetupEvent; | ||
|
|
||
| /** | ||
| * Listen to the users filesystem setup being started, to perform any receiving share | ||
| * work that was postponed. | ||
| * | ||
| * @template-implements IEventListener<UserHomeSetupEvent> | ||
| */ | ||
| class UserHomeSetupListener implements IEventListener { | ||
| public function __construct( | ||
| private readonly ShareRecipientUpdater $updater, | ||
| private readonly IUserConfig $userConfig, | ||
| ) { | ||
| } | ||
|
|
||
| public function handle(Event $event): void { | ||
| if (!$event instanceof UserHomeSetupEvent) { | ||
| return; | ||
| } | ||
|
|
||
| $user = $event->getUser(); | ||
| if ($this->userConfig->getValueBool($user->getUID(), Application::APP_ID, ConfigLexicon::USER_NEEDS_SHARE_REFRESH)) { | ||
| $this->updater->updateForUser($user); | ||
| $this->userConfig->setValueBool($user->getUID(), Application::APP_ID, ConfigLexicon::USER_NEEDS_SHARE_REFRESH, false); | ||
| } | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Files_Sharing; | ||
|
|
||
| use OCP\Files\Config\ICachedMountInfo; | ||
| use OCP\Files\Config\IUserMountCache; | ||
| use OCP\Files\Storage\IStorageFactory; | ||
| use OCP\IUser; | ||
| use OCP\Share\IShare; | ||
|
|
||
| class ShareRecipientUpdater { | ||
| private array $inUpdate = []; | ||
|
|
||
| public function __construct( | ||
| private readonly IUserMountCache $userMountCache, | ||
| private readonly MountProvider $shareMountProvider, | ||
| private readonly ShareTargetValidator $shareTargetValidator, | ||
| private readonly IStorageFactory $storageFactory, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * Validate all received shares for a user | ||
| */ | ||
| public function updateForUser(IUser $user, bool $verifyMountPoints = true, array $ignoreShares = []): void { | ||
| // prevent recursion | ||
| if (isset($this->inUpdate[$user->getUID()])) { | ||
| return; | ||
| } | ||
| $this->inUpdate[$user->getUID()] = true; | ||
|
|
||
| $cachedMounts = $this->userMountCache->getMountsForUser($user); | ||
| $shareMounts = array_filter($cachedMounts, fn (ICachedMountInfo $mount) => $mount->getMountProvider() === MountProvider::class); | ||
| $mountPoints = array_map(fn (ICachedMountInfo $mount) => $mount->getMountPoint(), $cachedMounts); | ||
| $mountsByPath = array_combine($mountPoints, $cachedMounts); | ||
|
|
||
| $shares = $this->shareMountProvider->getSuperSharesForUser($user, $ignoreShares); | ||
|
|
||
| $mountsChanged = count($shares) !== count($shareMounts); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this mean? Should it be set to default, given that the loop might set it to true? |
||
| foreach ($shares as &$share) { | ||
| [$parentShare, $groupedShares] = $share; | ||
| $mountPoint = '/' . $user->getUID() . '/files/' . trim($parentShare->getTarget(), '/') . '/'; | ||
| $mountKey = $parentShare->getNodeId() . '::' . $mountPoint; | ||
| if (!isset($cachedMounts[$mountKey])) { | ||
| $mountsChanged = true; | ||
| if ($verifyMountPoints) { | ||
| $this->shareTargetValidator->verifyMountPoint($user, $parentShare, $mountsByPath, $groupedShares); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if ($mountsChanged) { | ||
| $newMounts = $this->shareMountProvider->getMountsFromSuperShares($user, $shares, $this->storageFactory); | ||
| $this->userMountCache->registerMounts($user, $newMounts, [MountProvider::class]); | ||
| } | ||
|
|
||
| unset($this->inUpdate[$user->getUID()]); | ||
| } | ||
|
|
||
| /** | ||
| * Validate a single received share for a user | ||
| */ | ||
| public function updateForShare(IUser $user, IShare $share): void { | ||
| $cachedMounts = $this->userMountCache->getMountsForUser($user); | ||
| $mountPoints = array_map(fn (ICachedMountInfo $mount) => $mount->getMountPoint(), $cachedMounts); | ||
| $mountsByPath = array_combine($mountPoints, $cachedMounts); | ||
|
|
||
| $target = $this->shareTargetValidator->verifyMountPoint($user, $share, $mountsByPath, [$share]); | ||
| $mountPoint = '/' . $user->getUID() . '/files/' . trim($target, '/') . '/'; | ||
|
|
||
| $this->userMountCache->addMount($user, $mountPoint, $share->getNode()->getData(), MountProvider::class); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Date or data?