-
-
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
Draft
icewind1991
wants to merge
8
commits into
master
Choose a base branch
from
share-update-check-single
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+334
−51
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
34b388e
fix: only validate mounts for new share
icewind1991 8e51d6c
feat: add event for user home mount having being setup
icewind1991 2a04110
chore: move share recipient validation logic to a separate class
icewind1991 278e38a
feat: postpone receiving share validation after processing a certain …
icewind1991 1e46178
test: add test for delayed share validate
icewind1991 3f9e857
fix: disable share resolve postpone in tests
icewind1991 d2f2135
feat: export getData for public FileInfo interface
icewind1991 4b00db9
fix: clear in-memory cached mounts for user when adding/removing mounts
icewind1991 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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?