diff options
-rw-r--r-- | apps/files/composer/composer/autoload_classmap.php | 2 | ||||
-rw-r--r-- | apps/files/composer/composer/autoload_static.php | 2 | ||||
-rw-r--r-- | apps/files/lib/Command/TransferOwnership.php | 243 | ||||
-rw-r--r-- | apps/files/lib/Exception/TransferOwnershipException.php | 30 | ||||
-rw-r--r-- | apps/files/lib/Service/OwnershipTransferService.php | 276 |
5 files changed, 333 insertions, 220 deletions
diff --git a/apps/files/composer/composer/autoload_classmap.php b/apps/files/composer/composer/autoload_classmap.php index e241d34225c..6a6584b013f 100644 --- a/apps/files/composer/composer/autoload_classmap.php +++ b/apps/files/composer/composer/autoload_classmap.php @@ -34,7 +34,9 @@ return array( 'OCA\\Files\\Controller\\ViewController' => $baseDir . '/../lib/Controller/ViewController.php', 'OCA\\Files\\Event\\LoadAdditionalScriptsEvent' => $baseDir . '/../lib/Event/LoadAdditionalScriptsEvent.php', 'OCA\\Files\\Event\\LoadSidebar' => $baseDir . '/../lib/Event/LoadSidebar.php', + 'OCA\\Files\\Exception\\TransferOwnershipException' => $baseDir . '/../lib/Exception/TransferOwnershipException.php', 'OCA\\Files\\Helper' => $baseDir . '/../lib/Helper.php', 'OCA\\Files\\Listener\\LegacyLoadAdditionalScriptsAdapter' => $baseDir . '/../lib/Listener/LegacyLoadAdditionalScriptsAdapter.php', + 'OCA\\Files\\Service\\OwnershipTransferService' => $baseDir . '/../lib/Service/OwnershipTransferService.php', 'OCA\\Files\\Service\\TagService' => $baseDir . '/../lib/Service/TagService.php', ); diff --git a/apps/files/composer/composer/autoload_static.php b/apps/files/composer/composer/autoload_static.php index c4944e7b980..b1ba7fdc540 100644 --- a/apps/files/composer/composer/autoload_static.php +++ b/apps/files/composer/composer/autoload_static.php @@ -49,8 +49,10 @@ class ComposerStaticInitFiles 'OCA\\Files\\Controller\\ViewController' => __DIR__ . '/..' . '/../lib/Controller/ViewController.php', 'OCA\\Files\\Event\\LoadAdditionalScriptsEvent' => __DIR__ . '/..' . '/../lib/Event/LoadAdditionalScriptsEvent.php', 'OCA\\Files\\Event\\LoadSidebar' => __DIR__ . '/..' . '/../lib/Event/LoadSidebar.php', + 'OCA\\Files\\Exception\\TransferOwnershipException' => __DIR__ . '/..' . '/../lib/Exception/TransferOwnershipException.php', 'OCA\\Files\\Helper' => __DIR__ . '/..' . '/../lib/Helper.php', 'OCA\\Files\\Listener\\LegacyLoadAdditionalScriptsAdapter' => __DIR__ . '/..' . '/../lib/Listener/LegacyLoadAdditionalScriptsAdapter.php', + 'OCA\\Files\\Service\\OwnershipTransferService' => __DIR__ . '/..' . '/../lib/Service/OwnershipTransferService.php', 'OCA\\Files\\Service\\TagService' => __DIR__ . '/..' . '/../lib/Service/TagService.php', ); 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(''); - } } diff --git a/apps/files/lib/Exception/TransferOwnershipException.php b/apps/files/lib/Exception/TransferOwnershipException.php new file mode 100644 index 00000000000..ac4f4cc96c2 --- /dev/null +++ b/apps/files/lib/Exception/TransferOwnershipException.php @@ -0,0 +1,30 @@ +<?php declare(strict_types=1); + +/** + * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +namespace OCA\Files\Exception; + +use Exception; + +class TransferOwnershipException extends Exception { + +} diff --git a/apps/files/lib/Service/OwnershipTransferService.php b/apps/files/lib/Service/OwnershipTransferService.php new file mode 100644 index 00000000000..0c5c25237b9 --- /dev/null +++ b/apps/files/lib/Service/OwnershipTransferService.php @@ -0,0 +1,276 @@ +<?php declare(strict_types=1); + +/** + * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +namespace OCA\Files\Service; + +use Closure; +use OC\Files\Filesystem; +use OC\Files\View; +use OCA\Files\Exception\TransferOwnershipException; +use OCP\Encryption\IManager as IEncryptionManager; +use OCP\Files\FileInfo; +use OCP\Files\IHomeStorage; +use OCP\Files\Mount\IMountManager; +use OCP\IUser; +use OCP\Share\IManager as IShareManager; +use Symfony\Component\Console\Helper\ProgressBar; +use Symfony\Component\Console\Output\NullOutput; +use Symfony\Component\Console\Output\OutputInterface; +use function array_merge; +use function basename; +use function count; +use function date; +use function is_dir; +use function rtrim; + +class OwnershipTransferService { + + /** @var IEncryptionManager */ + private $encryptionManager; + + /** @var IShareManager */ + private $shareManager; + + /** @var IMountManager */ + private $mountManager; + + public function __construct(IEncryptionManager $manager, + IShareManager $shareManager, + IMountManager $mountManager) { + $this->encryptionManager = $manager; + $this->shareManager = $shareManager; + $this->mountManager = $mountManager; + } + + /** + * @param IUser $sourceUser + * @param IUser $destinationUser + * @param string $path + * + * @throws TransferOwnershipException + */ + public function transfer(IUser $sourceUser, + IUser $destinationUser, + string $path, + ?OutputInterface $output = null): void { + $output = $output ?? new NullOutput(); + $sourceUid = $sourceUser->getUID(); + $destinationUid = $destinationUser->getUID(); + $sourcePath = rtrim($sourceUid . '/files/' . $path, '/'); + + // target user has to be ready + if (!$this->encryptionManager->isReadyForUser($destinationUid)) { + throw new TransferOwnershipException("The target user is not ready to accept files. The user has at least to be logged in once.", 2); + } + + $date = date('Y-m-d H-i-s'); + $finalTarget = "$destinationUid/files/transferred from $sourceUid on $date"; + + // setup filesystem + Filesystem::initMountPoints($sourceUid); + Filesystem::initMountPoints($destinationUid); + + $view = new View(); + if (!$view->is_dir($sourcePath)) { + throw new TransferOwnershipException("Unknown path provided: $path", 1); + } + + // analyse source folder + $this->analyse( + $sourceUid, + $destinationUid, + $sourcePath, + $view, + $output + ); + + // collect all the shares + $shares = $this->collectUsersShares( + $sourceUid, + $output + ); + + // transfer the files + $this->transferFiles( + $sourceUid, + $sourcePath, + $finalTarget, + $view, + $output + ); + + // restore the shares + $this->restoreShares( + $sourceUid, + $destinationUid, + $shares, + $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(string $sourceUid, + string $destinationUid, + string $sourcePath, + View $view, + OutputInterface $output): void { + $output->writeln('Validating quota'); + $size = $view->getFileInfo($sourcePath, false)->getSize(false); + $freeSpace = $view->free_space($destinationUid . '/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 $sourceUid ..."); + $progress = new ProgressBar($output); + $progress->start(); + + $encryptedFiles = []; + $this->walkFiles($view, $sourcePath, + function (FileInfo $fileInfo) use ($progress) { + 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(); + if ($fileInfo->isEncrypted()) { + $encryptedFiles[] = $fileInfo; + } + return true; + }); + $progress->finish(); + $output->writeln(''); + + // no file is allowed to be encrypted + if (!empty($encryptedFiles)) { + $output->writeln("<error>Some files are encrypted - please decrypt them first</error>"); + foreach ($encryptedFiles as $encryptedFile) { + /** @var FileInfo $encryptedFile */ + $output->writeln(" " . $encryptedFile->getPath()); + } + throw new \Exception('Execution terminated.'); + } + } + + private function collectUsersShares(string $sourceUid, + OutputInterface $output): array { + $output->writeln("Collecting all share information for files and folder of $sourceUid ..."); + + $shares = []; + $progress = new ProgressBar($output); + 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($sourceUid, $shareType, null, true, 50, $offset); + $progress->advance(count($sharePage)); + if (empty($sharePage)) { + break; + } + $shares = array_merge($shares, $sharePage); + $offset += 50; + } + } + + $progress->finish(); + $output->writeln(''); + return $shares; + } + + protected function transferFiles(string $sourceUid, + string $sourcePath, + string $finalTarget, + View $view, + OutputInterface $output): void { + $output->writeln("Transferring files to $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 ($sourcePath !== "$sourceUid/files") { + $view->mkdir($finalTarget); + $finalTarget = $finalTarget . '/' . basename($sourcePath); + } + $view->rename($sourcePath, $finalTarget); + if (!is_dir("$sourceUid/files")) { + // because the files folder is moved away we need to recreate it + $view->mkdir("$sourceUid/files"); + } + } + + private function restoreShares(string $sourceUid, + string $destinationUid, + array $shares, + OutputInterface $output) { + $output->writeln("Restoring shares ..."); + $progress = new ProgressBar($output, count($shares)); + + foreach ($shares as $share) { + try { + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && + $share->getSharedWith() === $destinationUid) { + // Unmount the shares before deleting, so we don't try to get the storage later on. + $shareMountPoint = $this->mountManager->find('/' . $destinationUid . '/files' . $share->getTarget()); + if ($shareMountPoint) { + $this->mountManager->removeMount($shareMountPoint->getMountPoint()); + } + $this->shareManager->deleteShare($share); + } else { + if ($share->getShareOwner() === $sourceUid) { + $share->setShareOwner($destinationUid); + } + if ($share->getSharedBy() === $sourceUid) { + $share->setSharedBy($destinationUid); + } + + $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(''); + } + +} |