Merge pull request #19233 from nextcloud/enh/transferownership_move
Add move (and firstlogin) option to transferownership service
This commit is contained in:
commit
906348ca14
|
@ -77,6 +77,11 @@ class TransferOwnership extends Command {
|
||||||
InputOption::VALUE_REQUIRED,
|
InputOption::VALUE_REQUIRED,
|
||||||
'selectively provide the path to transfer. For example --path="folder_name"',
|
'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'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,7 +104,8 @@ class TransferOwnership extends Command {
|
||||||
$sourceUserObject,
|
$sourceUserObject,
|
||||||
$destinationUserObject,
|
$destinationUserObject,
|
||||||
ltrim($input->getOption('path'), '/'),
|
ltrim($input->getOption('path'), '/'),
|
||||||
$output
|
$output,
|
||||||
|
$input->getOption('move') === true
|
||||||
);
|
);
|
||||||
} catch (TransferOwnershipException $e) {
|
} catch (TransferOwnershipException $e) {
|
||||||
$output->writeln("<error>" . $e->getMessage() . "</error>");
|
$output->writeln("<error>" . $e->getMessage() . "</error>");
|
||||||
|
|
|
@ -71,24 +71,33 @@ class OwnershipTransferService {
|
||||||
* @param IUser $destinationUser
|
* @param IUser $destinationUser
|
||||||
* @param string $path
|
* @param string $path
|
||||||
*
|
*
|
||||||
|
* @param OutputInterface|null $output
|
||||||
|
* @param bool $move
|
||||||
* @throws TransferOwnershipException
|
* @throws TransferOwnershipException
|
||||||
|
* @throws \OC\User\NoUserException
|
||||||
*/
|
*/
|
||||||
public function transfer(IUser $sourceUser,
|
public function transfer(IUser $sourceUser,
|
||||||
IUser $destinationUser,
|
IUser $destinationUser,
|
||||||
string $path,
|
string $path,
|
||||||
?OutputInterface $output = null): void {
|
?OutputInterface $output = null,
|
||||||
|
bool $move = false,
|
||||||
|
bool $firstLogin = false): void {
|
||||||
$output = $output ?? new NullOutput();
|
$output = $output ?? new NullOutput();
|
||||||
$sourceUid = $sourceUser->getUID();
|
$sourceUid = $sourceUser->getUID();
|
||||||
$destinationUid = $destinationUser->getUID();
|
$destinationUid = $destinationUser->getUID();
|
||||||
$sourcePath = rtrim($sourceUid . '/files/' . $path, '/');
|
$sourcePath = rtrim($sourceUid . '/files/' . $path, '/');
|
||||||
|
|
||||||
// target user has to be ready
|
// 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);
|
throw new TransferOwnershipException("The target user is not ready to accept files. The user has at least to have logged in once.", 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($move) {
|
||||||
|
$finalTarget = "$destinationUid/files/";
|
||||||
|
} else {
|
||||||
$date = date('Y-m-d H-i-s');
|
$date = date('Y-m-d H-i-s');
|
||||||
$finalTarget = "$destinationUid/files/transferred from $sourceUid on $date";
|
$finalTarget = "$destinationUid/files/transferred from $sourceUid on $date";
|
||||||
|
}
|
||||||
|
|
||||||
// setup filesystem
|
// setup filesystem
|
||||||
Filesystem::initMountPoints($sourceUid);
|
Filesystem::initMountPoints($sourceUid);
|
||||||
|
@ -99,6 +108,17 @@ class OwnershipTransferService {
|
||||||
throw new TransferOwnershipException("Unknown path provided: $path", 1);
|
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
|
// analyse source folder
|
||||||
$this->analyse(
|
$this->analyse(
|
||||||
$sourceUid,
|
$sourceUid,
|
||||||
|
@ -273,7 +293,7 @@ class OwnershipTransferService {
|
||||||
}
|
}
|
||||||
} catch (\OCP\Files\NotFoundException $e) {
|
} catch (\OCP\Files\NotFoundException $e) {
|
||||||
$output->writeln('<error>Share with id ' . $share->getId() . ' points at deleted file, skipping</error>');
|
$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>');
|
$output->writeln('<error>Could not restore share with id ' . $share->getId() . ':' . $e->getTraceAsString() . '</error>');
|
||||||
}
|
}
|
||||||
$progress->advance();
|
$progress->advance();
|
||||||
|
|
Loading…
Reference in New Issue