1
0

TransferOwnership.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Carla Schroder <carla@owncloud.com>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Sujith Haridasan <sujith.h@gmail.com>
  12. * @author Sujith H <sharidasan@owncloud.com>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. * @author Tobia De Koninck <LEDfan@users.noreply.github.com>
  15. * @author Vincent Petry <vincent@nextcloud.com>
  16. *
  17. * @license AGPL-3.0
  18. *
  19. * This code is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Affero General Public License, version 3,
  21. * as published by the Free Software Foundation.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License, version 3,
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>
  30. *
  31. */
  32. namespace OCA\Files\Command;
  33. use OCA\Files\Exception\TransferOwnershipException;
  34. use OCA\Files\Service\OwnershipTransferService;
  35. use OCP\IUser;
  36. use OCP\IUserManager;
  37. use OCP\IConfig;
  38. use Symfony\Component\Console\Command\Command;
  39. use Symfony\Component\Console\Input\InputArgument;
  40. use Symfony\Component\Console\Input\InputInterface;
  41. use Symfony\Component\Console\Input\InputOption;
  42. use Symfony\Component\Console\Output\OutputInterface;
  43. class TransferOwnership extends Command {
  44. public function __construct(
  45. private IUserManager $userManager,
  46. private OwnershipTransferService $transferService,
  47. private IConfig $config,
  48. ) {
  49. parent::__construct();
  50. }
  51. protected function configure(): void {
  52. $this
  53. ->setName('files:transfer-ownership')
  54. ->setDescription('All files and folders are moved to another user - outgoing shares and incoming user file shares (optionally) are moved as well.')
  55. ->addArgument(
  56. 'source-user',
  57. InputArgument::REQUIRED,
  58. 'owner of files which shall be moved'
  59. )
  60. ->addArgument(
  61. 'destination-user',
  62. InputArgument::REQUIRED,
  63. 'user who will be the new owner of the files'
  64. )
  65. ->addOption(
  66. 'path',
  67. null,
  68. InputOption::VALUE_REQUIRED,
  69. 'selectively provide the path to transfer. For example --path="folder_name"',
  70. ''
  71. )->addOption(
  72. 'move',
  73. null,
  74. InputOption::VALUE_NONE,
  75. 'move data from source user to root directory of destination user, which must be empty'
  76. )->addOption(
  77. 'transfer-incoming-shares',
  78. null,
  79. InputOption::VALUE_OPTIONAL,
  80. 'transfer incoming user file shares to destination user. Usage: --transfer-incoming-shares=1 (value required)',
  81. '2'
  82. );
  83. }
  84. protected function execute(InputInterface $input, OutputInterface $output): int {
  85. /**
  86. * Check if source and destination users are same. If they are same then just ignore the transfer.
  87. */
  88. if ($input->getArgument(('source-user')) === $input->getArgument('destination-user')) {
  89. $output->writeln("<error>Ownership can't be transferred when Source and Destination users are the same user. Please check your input.</error>");
  90. return self::FAILURE;
  91. }
  92. $sourceUserObject = $this->userManager->get($input->getArgument('source-user'));
  93. $destinationUserObject = $this->userManager->get($input->getArgument('destination-user'));
  94. if (!$sourceUserObject instanceof IUser) {
  95. $output->writeln("<error>Unknown source user " . $input->getArgument('source-user') . "</error>");
  96. return self::FAILURE;
  97. }
  98. if (!$destinationUserObject instanceof IUser) {
  99. $output->writeln("<error>Unknown destination user " . $input->getArgument('destination-user') . "</error>");
  100. return self::FAILURE;
  101. }
  102. try {
  103. $includeIncomingArgument = $input->getOption('transfer-incoming-shares');
  104. switch ($includeIncomingArgument) {
  105. case '0':
  106. $includeIncoming = false;
  107. break;
  108. case '1':
  109. $includeIncoming = true;
  110. break;
  111. case '2':
  112. $includeIncoming = $this->config->getSystemValue('transferIncomingShares', false);
  113. if (gettype($includeIncoming) !== 'boolean') {
  114. $output->writeln("<error> config.php: 'transfer-incoming-shares': wrong usage. Transfer aborted.</error>");
  115. return self::FAILURE;
  116. }
  117. break;
  118. default:
  119. $output->writeln("<error>Option --transfer-incoming-shares: wrong usage. Transfer aborted.</error>");
  120. return self::FAILURE;
  121. }
  122. $this->transferService->transfer(
  123. $sourceUserObject,
  124. $destinationUserObject,
  125. ltrim($input->getOption('path'), '/'),
  126. $output,
  127. $input->getOption('move') === true,
  128. false,
  129. $includeIncoming
  130. );
  131. } catch (TransferOwnershipException $e) {
  132. $output->writeln("<error>" . $e->getMessage() . "</error>");
  133. return $e->getCode() !== 0 ? $e->getCode() : self::FAILURE;
  134. }
  135. return self::SUCCESS;
  136. }
  137. }