diff --git a/apps/files/lib/Service/OwnershipTransferService.php b/apps/files/lib/Service/OwnershipTransferService.php index 535822b999c0b..88e0976bb60bd 100644 --- a/apps/files/lib/Service/OwnershipTransferService.php +++ b/apps/files/lib/Service/OwnershipTransferService.php @@ -35,10 +35,12 @@ use OC\Files\View; use OCA\Files\Exception\TransferOwnershipException; use OCP\Encryption\IManager as IEncryptionManager; +use OCP\Files\Config\IUserMountCache; use OCP\Files\FileInfo; use OCP\Files\IHomeStorage; use OCP\Files\InvalidPathException; use OCP\Files\Mount\IMountManager; +use OCP\Files\NotFoundException; use OCP\IUser; use OCP\Share\IManager as IShareManager; use OCP\Share\IShare; @@ -63,12 +65,17 @@ class OwnershipTransferService { /** @var IMountManager */ private $mountManager; + /** @var IUserMountCache */ + private $userMountCache; + public function __construct(IEncryptionManager $manager, IShareManager $shareManager, - IMountManager $mountManager) { + IMountManager $mountManager, + IUserMountCache $userMountCache) { $this->encryptionManager = $manager; $this->shareManager = $shareManager; $this->mountManager = $mountManager; + $this->userMountCache = $userMountCache; } /** @@ -149,6 +156,7 @@ public function transfer(IUser $sourceUser, // collect all the shares $shares = $this->collectUsersShares( $sourceUid, + $sourcePath, $output ); @@ -161,6 +169,8 @@ public function transfer(IUser $sourceUser, $output ); + $view = new View(); + // restore the shares $this->restoreShares( $sourceUid, @@ -234,6 +244,7 @@ function (FileInfo $fileInfo) use ($progress) { } private function collectUsersShares(string $sourceUid, + string $path = null, OutputInterface $output): array { $output->writeln("Collecting all share information for files and folders of $sourceUid ..."); @@ -247,6 +258,16 @@ private function collectUsersShares(string $sourceUid, if (empty($sharePage)) { break; } + if ($path) { + $sharePage = array_filter($sharePage, function (IShare $share) use ($path) { + try { + $node = $share->getNode(); + return \OC\Files\Filesystem::normalizePath($path) === $node->getPath(); + } catch (\Exception $e) { + return false; + } + }); + } $shares = array_merge($shares, $sharePage); $offset += 50; } @@ -307,6 +328,12 @@ private function restoreShares(string $sourceUid, $share->setSharedBy($destinationUid); } + + // 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()); + $this->shareManager->updateShare($share); } } catch (\OCP\Files\NotFoundException $e) { diff --git a/lib/private/Files/Config/UserMountCache.php b/lib/private/Files/Config/UserMountCache.php index 9cf3b43a431b0..835775cb73acd 100644 --- a/lib/private/Files/Config/UserMountCache.php +++ b/lib/private/Files/Config/UserMountCache.php @@ -409,4 +409,9 @@ public function getUsedSpaceForUsers(array $users) { $result->closeCursor(); return $results; } + + public function clear() { + $this->cacheInfoCache = new CappedMemoryCache(); + $this->mountsForUsers = new CappedMemoryCache(); + } }