Transfer shares of the transferred root node

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2020-07-31 11:10:48 +02:00
parent ca2573c99e
commit 14c3f1ebd8
No known key found for this signature in database
GPG Key ID: 4C614C6ED2CDE6DF
3 changed files with 49 additions and 3 deletions

View File

@ -35,12 +35,14 @@ use OC\Files\Filesystem;
use OC\Files\View; use OC\Files\View;
use OCA\Files\Exception\TransferOwnershipException; use OCA\Files\Exception\TransferOwnershipException;
use OCP\Encryption\IManager as IEncryptionManager; use OCP\Encryption\IManager as IEncryptionManager;
use OCP\Files\Config\IUserMountCache;
use OCP\Files\FileInfo; use OCP\Files\FileInfo;
use OCP\Files\IHomeStorage; use OCP\Files\IHomeStorage;
use OCP\Files\InvalidPathException; use OCP\Files\InvalidPathException;
use OCP\Files\Mount\IMountManager; use OCP\Files\Mount\IMountManager;
use OCP\IUser; use OCP\IUser;
use OCP\Share\IManager as IShareManager; use OCP\Share\IManager as IShareManager;
use OCP\Share\IShare;
use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\NullOutput; use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
@ -62,12 +64,17 @@ class OwnershipTransferService {
/** @var IMountManager */ /** @var IMountManager */
private $mountManager; private $mountManager;
/** @var IUserMountCache */
private $userMountCache;
public function __construct(IEncryptionManager $manager, public function __construct(IEncryptionManager $manager,
IShareManager $shareManager, IShareManager $shareManager,
IMountManager $mountManager) { IMountManager $mountManager,
IUserMountCache $userMountCache) {
$this->encryptionManager = $manager; $this->encryptionManager = $manager;
$this->shareManager = $shareManager; $this->shareManager = $shareManager;
$this->mountManager = $mountManager; $this->mountManager = $mountManager;
$this->userMountCache = $userMountCache;
} }
/** /**
@ -148,7 +155,9 @@ class OwnershipTransferService {
// collect all the shares // collect all the shares
$shares = $this->collectUsersShares( $shares = $this->collectUsersShares(
$sourceUid, $sourceUid,
$output $output,
$view,
$sourcePath
); );
// transfer the files // transfer the files
@ -233,7 +242,9 @@ class OwnershipTransferService {
} }
private function collectUsersShares(string $sourceUid, private function collectUsersShares(string $sourceUid,
OutputInterface $output): array { OutputInterface $output,
View $view,
?string $path = null): array {
$output->writeln("Collecting all share information for files and folders of $sourceUid ..."); $output->writeln("Collecting all share information for files and folders of $sourceUid ...");
$shares = []; $shares = [];
@ -246,6 +257,23 @@ class OwnershipTransferService {
if (empty($sharePage)) { if (empty($sharePage)) {
break; break;
} }
if ($path !== null) {
$sharePage = array_filter($sharePage, function (IShare $share) use ($view, $path) {
try {
$relativePath = $view->getPath($share->getNodeId());
$singleFileTranfer = $view->is_file($path);
if ($singleFileTranfer) {
return Filesystem::normalizePath($relativePath) === Filesystem::normalizePath($path);
}
return mb_strpos(
Filesystem::normalizePath($relativePath . '/', false),
Filesystem::normalizePath($path . '/', false)) === 0;
} catch (\Exception $e) {
return false;
}
});
}
$shares = array_merge($shares, $sharePage); $shares = array_merge($shares, $sharePage);
$offset += 50; $offset += 50;
} }
@ -306,6 +334,12 @@ class OwnershipTransferService {
$share->setSharedBy($destinationUid); $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); $this->shareManager->updateShare($share);
} }
} catch (\OCP\Files\NotFoundException $e) { } catch (\OCP\Files\NotFoundException $e) {

View File

@ -409,4 +409,9 @@ class UserMountCache implements IUserMountCache {
$result->closeCursor(); $result->closeCursor();
return $results; return $results;
} }
public function clear(): void {
$this->cacheInfoCache = new CappedMemoryCache();
$this->mountsForUsers = new CappedMemoryCache();
}
} }

View File

@ -117,4 +117,11 @@ interface IUserMountCache {
* @since 13.0.0 * @since 13.0.0
*/ */
public function getUsedSpaceForUsers(array $users); public function getUsedSpaceForUsers(array $users);
/**
* Clear all entries from the in-memory cache
*
* @since 20.0.0
*/
public function clear(): void;
} }