Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions apps/files/lib/Command/TransferOwnership.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ protected function configure() {
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) {
Expand All @@ -99,7 +104,8 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$sourceUserObject,
$destinationUserObject,
ltrim($input->getOption('path'), '/'),
$output
$output,
$input->getOption('move') === true
);
} catch (TransferOwnershipException $e) {
$output->writeln("<error>" . $e->getMessage() . "</error>");
Expand Down
30 changes: 25 additions & 5 deletions apps/files/lib/Service/OwnershipTransferService.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,33 @@ public function __construct(IEncryptionManager $manager,
* @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);
Expand All @@ -99,6 +108,17 @@ public function transfer(IUser $sourceUser,
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,
Expand Down Expand Up @@ -273,7 +293,7 @@ private function restoreShares(string $sourceUid,
}
} 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();
Expand Down