diff options
author | Roeland Jago Douma <rullzer@users.noreply.github.com> | 2020-02-03 21:06:42 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-03 21:06:42 +0100 |
commit | b784162761dbb3a0551f3baec8056bea8886fe94 (patch) | |
tree | ba5aa009a7955c4c6b38ddfc1c7f415142095146 | |
parent | be01ff127fe9a484806312f23e6671968e9aa5b8 (diff) | |
parent | 38735c2bd08496dddcc104c23b5945105a4a5a13 (diff) | |
download | nextcloud-server-b784162761dbb3a0551f3baec8056bea8886fe94.tar.gz nextcloud-server-b784162761dbb3a0551f3baec8056bea8886fe94.zip |
Merge pull request #19279 from nextcloud/backport/19233/stable18
[stable18] Add move (and firstlogin) option to transferownership service
-rw-r--r-- | apps/files/lib/Command/TransferOwnership.php | 10 | ||||
-rw-r--r-- | apps/files/lib/Service/OwnershipTransferService.php | 30 |
2 files changed, 33 insertions, 7 deletions
diff --git a/apps/files/lib/Command/TransferOwnership.php b/apps/files/lib/Command/TransferOwnership.php index cad1b1b46cb..2675a7dd1bb 100644 --- a/apps/files/lib/Command/TransferOwnership.php +++ b/apps/files/lib/Command/TransferOwnership.php @@ -77,7 +77,12 @@ class TransferOwnership extends Command { InputOption::VALUE_REQUIRED, 'selectively provide the path to transfer. For example --path="folder_name"', '' - ); + )->addOption( + 'move', + null, + InputOption::VALUE_NONE, + 'move data from source user to root directory of destination user, which must be empty' + ); } protected function execute(InputInterface $input, OutputInterface $output) { @@ -99,7 +104,8 @@ class TransferOwnership extends Command { $sourceUserObject, $destinationUserObject, ltrim($input->getOption('path'), '/'), - $output + $output, + $input->getOption('move') === true ); } catch (TransferOwnershipException $e) { $output->writeln("<error>" . $e->getMessage() . "</error>"); diff --git a/apps/files/lib/Service/OwnershipTransferService.php b/apps/files/lib/Service/OwnershipTransferService.php index 8530edd17b1..f316216814f 100644 --- a/apps/files/lib/Service/OwnershipTransferService.php +++ b/apps/files/lib/Service/OwnershipTransferService.php @@ -71,24 +71,33 @@ class OwnershipTransferService { * @param IUser $destinationUser * @param string $path * + * @param OutputInterface|null $output + * @param bool $move * @throws TransferOwnershipException + * @throws \OC\User\NoUserException */ public function transfer(IUser $sourceUser, IUser $destinationUser, string $path, - ?OutputInterface $output = null): void { + ?OutputInterface $output = null, + bool $move = false, + bool $firstLogin = false): 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)) { + if ($destinationUser->getLastLogin() === 0 || !$this->encryptionManager->isReadyForUser($destinationUid)) { throw new TransferOwnershipException("The target user is not ready to accept files. The user has at least to have logged in once.", 2); } - $date = date('Y-m-d H-i-s'); - $finalTarget = "$destinationUid/files/transferred from $sourceUid on $date"; + if ($move) { + $finalTarget = "$destinationUid/files/"; + } else { + $date = date('Y-m-d H-i-s'); + $finalTarget = "$destinationUid/files/transferred from $sourceUid on $date"; + } // setup filesystem Filesystem::initMountPoints($sourceUid); @@ -99,6 +108,17 @@ class OwnershipTransferService { throw new TransferOwnershipException("Unknown path provided: $path", 1); } + if ($move && ( + !$view->is_dir($finalTarget) || ( + !$firstLogin && + count($view->getDirectoryContent($finalTarget)) > 0 + ) + ) + ) { + throw new TransferOwnershipException("Destination path does not exists or is not empty", 1); + } + + // analyse source folder $this->analyse( $sourceUid, @@ -273,7 +293,7 @@ class OwnershipTransferService { } } catch (\OCP\Files\NotFoundException $e) { $output->writeln('<error>Share with id ' . $share->getId() . ' points at deleted file, skipping</error>'); - } catch (\Exception $e) { + } catch (\Throwable $e) { $output->writeln('<error>Could not restore share with id ' . $share->getId() . ':' . $e->getTraceAsString() . '</error>'); } $progress->advance(); |