TransferOwnership.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OCA\Files\Command;
  9. use OCA\Files\Exception\TransferOwnershipException;
  10. use OCA\Files\Service\OwnershipTransferService;
  11. use OCP\IConfig;
  12. use OCP\IUser;
  13. use OCP\IUserManager;
  14. use Symfony\Component\Console\Command\Command;
  15. use Symfony\Component\Console\Input\InputArgument;
  16. use Symfony\Component\Console\Input\InputInterface;
  17. use Symfony\Component\Console\Input\InputOption;
  18. use Symfony\Component\Console\Output\OutputInterface;
  19. class TransferOwnership extends Command {
  20. public function __construct(
  21. private IUserManager $userManager,
  22. private OwnershipTransferService $transferService,
  23. private IConfig $config,
  24. ) {
  25. parent::__construct();
  26. }
  27. protected function configure(): void {
  28. $this
  29. ->setName('files:transfer-ownership')
  30. ->setDescription('All files and folders are moved to another user - outgoing shares and incoming user file shares (optionally) are moved as well.')
  31. ->addArgument(
  32. 'source-user',
  33. InputArgument::REQUIRED,
  34. 'owner of files which shall be moved'
  35. )
  36. ->addArgument(
  37. 'destination-user',
  38. InputArgument::REQUIRED,
  39. 'user who will be the new owner of the files'
  40. )
  41. ->addOption(
  42. 'path',
  43. null,
  44. InputOption::VALUE_REQUIRED,
  45. 'selectively provide the path to transfer. For example --path="folder_name"',
  46. ''
  47. )->addOption(
  48. 'move',
  49. null,
  50. InputOption::VALUE_NONE,
  51. 'move data from source user to root directory of destination user, which must be empty'
  52. )->addOption(
  53. 'transfer-incoming-shares',
  54. null,
  55. InputOption::VALUE_OPTIONAL,
  56. 'transfer incoming user file shares to destination user. Usage: --transfer-incoming-shares=1 (value required)',
  57. '2'
  58. );
  59. }
  60. protected function execute(InputInterface $input, OutputInterface $output): int {
  61. /**
  62. * Check if source and destination users are same. If they are same then just ignore the transfer.
  63. */
  64. if ($input->getArgument(('source-user')) === $input->getArgument('destination-user')) {
  65. $output->writeln("<error>Ownership can't be transferred when Source and Destination users are the same user. Please check your input.</error>");
  66. return self::FAILURE;
  67. }
  68. $sourceUserObject = $this->userManager->get($input->getArgument('source-user'));
  69. $destinationUserObject = $this->userManager->get($input->getArgument('destination-user'));
  70. if (!$sourceUserObject instanceof IUser) {
  71. $output->writeln('<error>Unknown source user ' . $input->getArgument('source-user') . '</error>');
  72. return self::FAILURE;
  73. }
  74. if (!$destinationUserObject instanceof IUser) {
  75. $output->writeln('<error>Unknown destination user ' . $input->getArgument('destination-user') . '</error>');
  76. return self::FAILURE;
  77. }
  78. try {
  79. $includeIncomingArgument = $input->getOption('transfer-incoming-shares');
  80. switch ($includeIncomingArgument) {
  81. case '0':
  82. $includeIncoming = false;
  83. break;
  84. case '1':
  85. $includeIncoming = true;
  86. break;
  87. case '2':
  88. $includeIncoming = $this->config->getSystemValue('transferIncomingShares', false);
  89. if (gettype($includeIncoming) !== 'boolean') {
  90. $output->writeln("<error> config.php: 'transfer-incoming-shares': wrong usage. Transfer aborted.</error>");
  91. return self::FAILURE;
  92. }
  93. break;
  94. default:
  95. $output->writeln('<error>Option --transfer-incoming-shares: wrong usage. Transfer aborted.</error>');
  96. return self::FAILURE;
  97. }
  98. $this->transferService->transfer(
  99. $sourceUserObject,
  100. $destinationUserObject,
  101. ltrim($input->getOption('path'), '/'),
  102. $output,
  103. $input->getOption('move') === true,
  104. false,
  105. $includeIncoming
  106. );
  107. } catch (TransferOwnershipException $e) {
  108. $output->writeln('<error>' . $e->getMessage() . '</error>');
  109. return $e->getCode() !== 0 ? $e->getCode() : self::FAILURE;
  110. }
  111. return self::SUCCESS;
  112. }
  113. }