diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2019-11-20 11:41:31 +0100 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2019-11-26 08:50:19 +0100 |
commit | 07c5f3775c3985d45eaf3ad41863cd2e2a0e1aad (patch) | |
tree | 5f24dabe56fc1562346429b97903663d6f27b92f /apps/files/lib/Command | |
parent | 0fab27f459d3276a2559cdee5693bfec8c2cf1c9 (diff) | |
download | nextcloud-server-07c5f3775c3985d45eaf3ad41863cd2e2a0e1aad.tar.gz nextcloud-server-07c5f3775c3985d45eaf3ad41863cd2e2a0e1aad.zip |
Extract transfer ownership logic into a reusable service
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'apps/files/lib/Command')
-rw-r--r-- | apps/files/lib/Command/TransferOwnership.php | 243 |
1 files changed, 23 insertions, 220 deletions
diff --git a/apps/files/lib/Command/TransferOwnership.php b/apps/files/lib/Command/TransferOwnership.php index 5f51a80d998..aee3246f2f2 100644 --- a/apps/files/lib/Command/TransferOwnership.php +++ b/apps/files/lib/Command/TransferOwnership.php @@ -1,4 +1,5 @@ -<?php +<?php declare(strict_types=1); + /** * @copyright Copyright (c) 2016, ownCloud, Inc. * @@ -28,18 +29,11 @@ namespace OCA\Files\Command; -use OC\Files\Filesystem; -use OC\Files\View; -use OCP\Files\FileInfo; -use OCP\Files\IHomeStorage; -use OCP\Files\IRootFolder; -use OCP\Files\Mount\IMountManager; +use OCA\Files\Exception\TransferOwnershipException; +use OCA\Files\Service\OwnershipTransferService; use OCP\IUser; use OCP\IUserManager; -use OCP\Share\IManager; -use OCP\Share\IShare; use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -47,44 +41,17 @@ use Symfony\Component\Console\Output\OutputInterface; class TransferOwnership extends Command { - /** @var IUserManager $userManager */ + /** @var IUserManager */ private $userManager; - /** @var IManager */ - private $shareManager; - - /** @var IMountManager */ - private $mountManager; - - /** @var FileInfo[] */ - private $allFiles = []; - - /** @var FileInfo[] */ - private $encryptedFiles = []; - - /** @var IShare[] */ - private $shares = []; - - /** @var string */ - private $sourceUser; - - /** @var string */ - private $destinationUser; - - /** @var string */ - private $sourcePath; - - /** @var string */ - private $finalTarget; + /** @var OwnershipTransferService */ + private $transferService; public function __construct(IUserManager $userManager, - IManager $shareManager, - IMountManager $mountManager, - IRootFolder $rootFolder) { - $this->userManager = $userManager; - $this->shareManager = $shareManager; - $this->mountManager = $mountManager; + OwnershipTransferService $transferService) { parent::__construct(); + $this->userManager = $userManager; + $this->transferService = $transferService; } protected function configure() { @@ -115,192 +82,28 @@ class TransferOwnership extends Command { $destinationUserObject = $this->userManager->get($input->getArgument('destination-user')); if (!$sourceUserObject instanceof IUser) { - $output->writeln("<error>Unknown source user $this->sourceUser</error>"); + $output->writeln("<error>Unknown source user " . $input->getArgument('source-user') . "</error>"); return 1; } if (!$destinationUserObject instanceof IUser) { - $output->writeln("<error>Unknown destination user $this->destinationUser</error>"); + $output->writeln("<error>Unknown destination user " . $input->getArgument('destination-user') . "</error>"); return 1; } - $this->sourceUser = $sourceUserObject->getUID(); - $this->destinationUser = $destinationUserObject->getUID(); - $sourcePathOption = ltrim($input->getOption('path'), '/'); - $this->sourcePath = rtrim($this->sourceUser . '/files/' . $sourcePathOption, '/'); - - // target user has to be ready - if (!\OC::$server->getEncryptionManager()->isReadyForUser($this->destinationUser)) { - $output->writeln("<error>The target user is not ready to accept files. The user has at least to be logged in once.</error>"); - return 2; - } - - $date = date('Y-m-d H-i-s'); - $this->finalTarget = "$this->destinationUser/files/transferred from $this->sourceUser on $date"; - - // setup filesystem - Filesystem::initMountPoints($this->sourceUser); - Filesystem::initMountPoints($this->destinationUser); - - $view = new View(); - if (!$view->is_dir($this->sourcePath)) { - $output->writeln("<error>Unknown path provided: $sourcePathOption</error>"); - return 1; - } - - // analyse source folder - $this->analyse($output); - - // collect all the shares - $this->collectUsersShares($output); - - // transfer the files - $this->transfer($output); - - // restore the shares - $this->restoreShares($output); - } - - private function walkFiles(View $view, $path, \Closure $callBack) { - foreach ($view->getDirectoryContent($path) as $fileInfo) { - if (!$callBack($fileInfo)) { - return; - } - if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) { - $this->walkFiles($view, $fileInfo->getPath(), $callBack); - } - } - } - - /** - * @param OutputInterface $output - * @throws \Exception - */ - protected function analyse(OutputInterface $output) { - $view = new View(); - - $output->writeln('Validating quota'); - $size = $view->getFileInfo($this->sourcePath, false)->getSize(false); - $freeSpace = $view->free_space($this->destinationUser . '/files/'); - if ($size > $freeSpace) { - $output->writeln('<error>Target user does not have enough free space available</error>'); - throw new \Exception('Execution terminated'); - } - - $output->writeln("Analysing files of $this->sourceUser ..."); - $progress = new ProgressBar($output); - $progress->start(); - $self = $this; - - $this->walkFiles($view, $this->sourcePath, - function (FileInfo $fileInfo) use ($progress, $self) { - if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) { - // only analyze into folders from main storage, - if (!$fileInfo->getStorage()->instanceOfStorage(IHomeStorage::class)) { - return false; - } - return true; - } - $progress->advance(); - $this->allFiles[] = $fileInfo; - if ($fileInfo->isEncrypted()) { - $this->encryptedFiles[] = $fileInfo; - } - return true; - }); - $progress->finish(); - $output->writeln(''); - - // no file is allowed to be encrypted - if (!empty($this->encryptedFiles)) { - $output->writeln("<error>Some files are encrypted - please decrypt them first</error>"); - foreach($this->encryptedFiles as $encryptedFile) { - /** @var FileInfo $encryptedFile */ - $output->writeln(" " . $encryptedFile->getPath()); - } - throw new \Exception('Execution terminated.'); - } - } - - /** - * @param OutputInterface $output - */ - private function collectUsersShares(OutputInterface $output) { - $output->writeln("Collecting all share information for files and folder of $this->sourceUser ..."); - - $progress = new ProgressBar($output, count($this->shares)); - foreach([\OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_LINK, \OCP\Share::SHARE_TYPE_REMOTE, \OCP\Share::SHARE_TYPE_ROOM] as $shareType) { - $offset = 0; - while (true) { - $sharePage = $this->shareManager->getSharesBy($this->sourceUser, $shareType, null, true, 50, $offset); - $progress->advance(count($sharePage)); - if (empty($sharePage)) { - break; - } - $this->shares = array_merge($this->shares, $sharePage); - $offset += 50; - } + try { + $this->transferService->transfer( + $sourceUserObject, + $destinationUserObject, + ltrim($input->getOption('path'), '/'), + $output + ); + } catch (TransferOwnershipException $e) { + $output->writeln("<error>" . $e->getMessage() . "</error>"); + return $e->getCode() !== 0 ? $e->getCode() : 1; } - $progress->finish(); - $output->writeln(''); + return 0; } - /** - * @param OutputInterface $output - */ - protected function transfer(OutputInterface $output) { - $view = new View(); - $output->writeln("Transferring files to $this->finalTarget ..."); - - // This change will help user to transfer the folder specified using --path option. - // Else only the content inside folder is transferred which is not correct. - if($this->sourcePath !== "$this->sourceUser/files") { - $view->mkdir($this->finalTarget); - $this->finalTarget = $this->finalTarget . '/' . basename($this->sourcePath); - } - $view->rename($this->sourcePath, $this->finalTarget); - if (!is_dir("$this->sourceUser/files")) { - // because the files folder is moved away we need to recreate it - $view->mkdir("$this->sourceUser/files"); - } - } - - /** - * @param OutputInterface $output - */ - private function restoreShares(OutputInterface $output) { - $output->writeln("Restoring shares ..."); - $progress = new ProgressBar($output, count($this->shares)); - - foreach($this->shares as $share) { - try { - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && - $share->getSharedWith() === $this->destinationUser) { - // Unmount the shares before deleting, so we don't try to get the storage later on. - $shareMountPoint = $this->mountManager->find('/' . $this->destinationUser . '/files' . $share->getTarget()); - if ($shareMountPoint) { - $this->mountManager->removeMount($shareMountPoint->getMountPoint()); - } - $this->shareManager->deleteShare($share); - } else { - if ($share->getShareOwner() === $this->sourceUser) { - $share->setShareOwner($this->destinationUser); - } - if ($share->getSharedBy() === $this->sourceUser) { - $share->setSharedBy($this->destinationUser); - } - - $this->shareManager->updateShare($share); - } - } catch (\OCP\Files\NotFoundException $e) { - $output->writeln('<error>Share with id ' . $share->getId() . ' points at deleted file, skipping</error>'); - } catch (\Exception $e) { - $output->writeln('<error>Could not restore share with id ' . $share->getId() . ':' . $e->getTraceAsString() . '</error>'); - } - $progress->advance(); - } - $progress->finish(); - $output->writeln(''); - } } |