1
0

TransferOwnership.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. /** @var IUserManager */
  45. private $userManager;
  46. /** @var OwnershipTransferService */
  47. private $transferService;
  48. /** @var IConfig */
  49. private $config;
  50. public function __construct(IUserManager $userManager,
  51. OwnershipTransferService $transferService,
  52. IConfig $config) {
  53. parent::__construct();
  54. $this->userManager = $userManager;
  55. $this->transferService = $transferService;
  56. $this->config = $config;
  57. }
  58. protected function configure() {
  59. $this
  60. ->setName('files:transfer-ownership')
  61. ->setDescription('All files and folders are moved to another user - outgoing shares and incoming user file shares (optionally) are moved as well.')
  62. ->addArgument(
  63. 'source-user',
  64. InputArgument::REQUIRED,
  65. 'owner of files which shall be moved'
  66. )
  67. ->addArgument(
  68. 'destination-user',
  69. InputArgument::REQUIRED,
  70. 'user who will be the new owner of the files'
  71. )
  72. ->addOption(
  73. 'path',
  74. null,
  75. InputOption::VALUE_REQUIRED,
  76. 'selectively provide the path to transfer. For example --path="folder_name"',
  77. ''
  78. )->addOption(
  79. 'move',
  80. null,
  81. InputOption::VALUE_NONE,
  82. 'move data from source user to root directory of destination user, which must be empty'
  83. )->addOption(
  84. 'transfer-incoming-shares',
  85. null,
  86. InputOption::VALUE_OPTIONAL,
  87. 'transfer incoming user file shares to destination user. Usage: --transfer-incoming-shares=1 (value required)',
  88. '2'
  89. );
  90. }
  91. protected function execute(InputInterface $input, OutputInterface $output): int {
  92. /**
  93. * Check if source and destination users are same. If they are same then just ignore the transfer.
  94. */
  95. if ($input->getArgument(('source-user')) === $input->getArgument('destination-user')) {
  96. $output->writeln("<error>Ownership can't be transferred when Source and Destination users are the same user. Please check your input.</error>");
  97. return 1;
  98. }
  99. $sourceUserObject = $this->userManager->get($input->getArgument('source-user'));
  100. $destinationUserObject = $this->userManager->get($input->getArgument('destination-user'));
  101. if (!$sourceUserObject instanceof IUser) {
  102. $output->writeln("<error>Unknown source user " . $input->getArgument('source-user') . "</error>");
  103. return 1;
  104. }
  105. if (!$destinationUserObject instanceof IUser) {
  106. $output->writeln("<error>Unknown destination user " . $input->getArgument('destination-user') . "</error>");
  107. return 1;
  108. }
  109. try {
  110. $includeIncomingArgument = $input->getOption('transfer-incoming-shares');
  111. switch ($includeIncomingArgument) {
  112. case '0':
  113. $includeIncoming = false;
  114. break;
  115. case '1':
  116. $includeIncoming = true;
  117. break;
  118. case '2':
  119. $includeIncoming = $this->config->getSystemValue('transferIncomingShares', false);
  120. if (gettype($includeIncoming) !== 'boolean') {
  121. $output->writeln("<error> config.php: 'transfer-incoming-shares': wrong usage. Transfer aborted.</error>");
  122. return 1;
  123. }
  124. break;
  125. default:
  126. $output->writeln("<error>Option --transfer-incoming-shares: wrong usage. Transfer aborted.</error>");
  127. return 1;
  128. break;
  129. }
  130. $this->transferService->transfer(
  131. $sourceUserObject,
  132. $destinationUserObject,
  133. ltrim($input->getOption('path'), '/'),
  134. $output,
  135. $input->getOption('move') === true,
  136. false,
  137. $includeIncoming
  138. );
  139. } catch (TransferOwnershipException $e) {
  140. $output->writeln("<error>" . $e->getMessage() . "</error>");
  141. return $e->getCode() !== 0 ? $e->getCode() : 1;
  142. }
  143. return 0;
  144. }
  145. }