-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
[stable27] fix(files): Also restore shares after ownership transfer for object storage #44920
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
Merged
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| * @author Roeland Jago Douma <[email protected]> | ||
| * @author Sascha Wiswedel <[email protected]> | ||
| * @author Tobia De Koninck <[email protected]> | ||
| * @author Ferdinand Thiessen <[email protected]> | ||
| * | ||
| * @license GNU AGPL version 3 or any later version | ||
| * | ||
|
|
@@ -33,6 +34,7 @@ | |
| namespace OCA\Files\Service; | ||
|
|
||
| use Closure; | ||
| use OC\Encryption\Manager as EncryptionManager; | ||
| use OC\Files\Filesystem; | ||
| use OC\Files\View; | ||
| use OCA\Files\Exception\TransferOwnershipException; | ||
|
|
@@ -41,6 +43,7 @@ | |
| use OCP\Files\FileInfo; | ||
| use OCP\Files\IHomeStorage; | ||
| use OCP\Files\InvalidPathException; | ||
| use OCP\Files\IRootFolder; | ||
| use OCP\Files\Mount\IMountManager; | ||
| use OCP\IUser; | ||
| use OCP\IUserManager; | ||
|
|
@@ -58,8 +61,7 @@ | |
|
|
||
| class OwnershipTransferService { | ||
|
|
||
| /** @var IEncryptionManager */ | ||
| private $encryptionManager; | ||
| private IEncryptionManager|EncryptionManager $encryptionManager; | ||
|
|
||
| /** @var IShareManager */ | ||
| private $shareManager; | ||
|
|
@@ -183,10 +185,12 @@ public function transfer(IUser $sourceUser, | |
| $output | ||
| ); | ||
|
|
||
| $destinationPath = $finalTarget . '/' . $path; | ||
| // restore the shares | ||
| $this->restoreShares( | ||
| $sourceUid, | ||
| $destinationUid, | ||
| $destinationPath, | ||
| $shares, | ||
| $output | ||
| ); | ||
|
|
@@ -289,7 +293,21 @@ private function collectUsersShares(string $sourceUid, | |
| $shares = []; | ||
| $progress = new ProgressBar($output); | ||
|
|
||
| foreach ([IShare::TYPE_GROUP, IShare::TYPE_USER, IShare::TYPE_LINK, IShare::TYPE_REMOTE, IShare::TYPE_ROOM, IShare::TYPE_EMAIL, IShare::TYPE_CIRCLE, IShare::TYPE_DECK, IShare::TYPE_SCIENCEMESH] as $shareType) { | ||
| $normalizedPath = Filesystem::normalizePath($path); | ||
|
|
||
| $supportedShareTypes = [ | ||
| IShare::TYPE_GROUP, | ||
| IShare::TYPE_USER, | ||
| IShare::TYPE_LINK, | ||
| IShare::TYPE_REMOTE, | ||
| IShare::TYPE_ROOM, | ||
| IShare::TYPE_EMAIL, | ||
| IShare::TYPE_CIRCLE, | ||
| IShare::TYPE_DECK, | ||
| IShare::TYPE_SCIENCEMESH, | ||
| ]; | ||
|
|
||
| foreach ($supportedShareTypes as $shareType) { | ||
| $offset = 0; | ||
| while (true) { | ||
| $sharePage = $this->shareManager->getSharesBy($sourceUid, $shareType, null, true, 50, $offset); | ||
|
|
@@ -298,17 +316,17 @@ private function collectUsersShares(string $sourceUid, | |
| break; | ||
| } | ||
| if ($path !== "$sourceUid/files") { | ||
| $sharePage = array_filter($sharePage, function (IShare $share) use ($view, $path) { | ||
| $sharePage = array_filter($sharePage, function (IShare $share) use ($view, $normalizedPath) { | ||
| try { | ||
| $relativePath = $view->getPath($share->getNodeId()); | ||
| $singleFileTranfer = $view->is_file($path); | ||
| $singleFileTranfer = $view->is_file($normalizedPath); | ||
| if ($singleFileTranfer) { | ||
| return Filesystem::normalizePath($relativePath) === Filesystem::normalizePath($path); | ||
| return Filesystem::normalizePath($relativePath) === $normalizedPath; | ||
| } | ||
|
|
||
| return mb_strpos( | ||
| Filesystem::normalizePath($relativePath . '/', false), | ||
| Filesystem::normalizePath($path . '/', false)) === 0; | ||
| $normalizedPath . '/') === 0; | ||
| } catch (\Exception $e) { | ||
| return false; | ||
| } | ||
|
|
@@ -321,7 +339,11 @@ private function collectUsersShares(string $sourceUid, | |
|
|
||
| $progress->finish(); | ||
| $output->writeln(''); | ||
| return $shares; | ||
|
|
||
| return array_map(fn (IShare $share) => [ | ||
| 'share' => $share, | ||
| 'suffix' => substr(Filesystem::normalizePath($view->getPath($share->getNodeId())), strlen($normalizedPath)), | ||
| ], $shares); | ||
| } | ||
|
|
||
| private function collectIncomingShares(string $sourceUid, | ||
|
|
@@ -384,14 +406,18 @@ protected function transferFiles(string $sourceUid, | |
| } | ||
| } | ||
|
|
||
| private function restoreShares(string $sourceUid, | ||
| string $destinationUid, | ||
| array $shares, | ||
| OutputInterface $output) { | ||
| private function restoreShares( | ||
| string $sourceUid, | ||
| string $destinationUid, | ||
| string $targetLocation, | ||
| array $shares, | ||
| OutputInterface $output, | ||
| ) { | ||
| $output->writeln("Restoring shares ..."); | ||
| $progress = new ProgressBar($output, count($shares)); | ||
| $rootFolder = \OCP\Server::get(IRootFolder::class); | ||
|
|
||
| foreach ($shares as $share) { | ||
| foreach ($shares as ['share' => $share, 'suffix' => $suffix]) { | ||
| try { | ||
| if ($share->getShareType() === IShare::TYPE_USER && | ||
| $share->getSharedWith() === $destinationUid) { | ||
|
|
@@ -419,7 +445,19 @@ private function restoreShares(string $sourceUid, | |
| // trigger refetching of the node so that the new owner and mountpoint are taken into account | ||
| // otherwise the checks on the share update will fail due to the original node not being available in the new user scope | ||
| $this->userMountCache->clear(); | ||
| $share->setNodeId($share->getNode()->getId()); | ||
|
|
||
| try { | ||
| // Try to get the "old" id. | ||
| // Normally the ID is preserved, | ||
| // but for transferes between different storages the ID might change | ||
| $newNodeId = $share->getNode()->getId(); | ||
| } catch (\OCP\Files\NotFoundException) { | ||
| // ID has changed due to transfer between different storages | ||
| // Try to get the new ID from the target path and suffix of the share | ||
| $node = $rootFolder->get(Filesystem::normalizePath($targetLocation . '/' . $suffix)); | ||
| $newNodeId = $node->getId(); | ||
| } | ||
| $share->setNodeId($newNodeId); | ||
|
|
||
| $this->shareManager->updateShare($share); | ||
| } | ||
|
|
||
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.
Check notice
Code scanning / Psalm
MissingReturnType