2019-12-03 21:57:53 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2019-11-20 13:41:31 +03:00
|
|
|
|
2015-11-18 15:14:48 +03:00
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Carla Schroder <carla@owncloud.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
* @author Sujith H <sharidasan@owncloud.com>
|
2015-11-18 15:14:48 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Tobia De Koninck <LEDfan@users.noreply.github.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
2015-11-18 15:14:48 +03:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-11-18 15:14:48 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Files\Command;
|
|
|
|
|
2019-11-20 13:41:31 +03:00
|
|
|
use OCA\Files\Exception\TransferOwnershipException;
|
|
|
|
use OCA\Files\Service\OwnershipTransferService;
|
2016-10-17 12:42:54 +03:00
|
|
|
use OCP\IUser;
|
2015-11-18 15:14:48 +03:00
|
|
|
use OCP\IUserManager;
|
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
2019-11-22 22:52:10 +03:00
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
2015-11-18 15:14:48 +03:00
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
|
|
|
class TransferOwnership extends Command {
|
|
|
|
|
2019-11-20 13:41:31 +03:00
|
|
|
/** @var IUserManager */
|
2015-11-18 15:14:48 +03:00
|
|
|
private $userManager;
|
|
|
|
|
2019-11-20 13:41:31 +03:00
|
|
|
/** @var OwnershipTransferService */
|
|
|
|
private $transferService;
|
2015-11-18 15:14:48 +03:00
|
|
|
|
2019-11-13 13:22:19 +03:00
|
|
|
public function __construct(IUserManager $userManager,
|
2019-11-20 13:41:31 +03:00
|
|
|
OwnershipTransferService $transferService) {
|
2015-11-18 15:14:48 +03:00
|
|
|
parent::__construct();
|
2019-11-20 13:41:31 +03:00
|
|
|
$this->userManager = $userManager;
|
|
|
|
$this->transferService = $transferService;
|
2015-11-18 15:14:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function configure() {
|
|
|
|
$this
|
|
|
|
->setName('files:transfer-ownership')
|
2016-03-17 02:14:25 +03:00
|
|
|
->setDescription('All files and folders are moved to another user - shares are moved as well.')
|
2015-11-18 15:14:48 +03:00
|
|
|
->addArgument(
|
|
|
|
'source-user',
|
|
|
|
InputArgument::REQUIRED,
|
|
|
|
'owner of files which shall be moved'
|
|
|
|
)
|
|
|
|
->addArgument(
|
|
|
|
'destination-user',
|
|
|
|
InputArgument::REQUIRED,
|
|
|
|
'user who will be the new owner of the files'
|
2017-03-09 12:54:33 +03:00
|
|
|
)
|
|
|
|
->addOption(
|
|
|
|
'path',
|
|
|
|
null,
|
|
|
|
InputOption::VALUE_REQUIRED,
|
2017-03-23 02:39:08 +03:00
|
|
|
'selectively provide the path to transfer. For example --path="folder_name"',
|
|
|
|
''
|
2020-01-03 10:43:39 +03:00
|
|
|
)->addOption(
|
|
|
|
'move',
|
|
|
|
null,
|
|
|
|
InputOption::VALUE_NONE,
|
|
|
|
'move data from source user to root directory of destination user, which must be empty'
|
|
|
|
);
|
2015-11-18 15:14:48 +03:00
|
|
|
}
|
|
|
|
|
2020-06-26 16:12:11 +03:00
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int {
|
2020-10-09 08:20:03 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if source and destination users are same. If they are same then just ignore the transfer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ($input->getArgument(('source-user')) === $input->getArgument('destination-user')) {
|
|
|
|
$output->writeln("<error>Ownership can't be transferred when Source and Destination users are the same user. Please check your input.</error>");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-10-04 13:28:41 +03:00
|
|
|
$sourceUserObject = $this->userManager->get($input->getArgument('source-user'));
|
|
|
|
$destinationUserObject = $this->userManager->get($input->getArgument('destination-user'));
|
2016-10-17 12:42:54 +03:00
|
|
|
|
2016-10-04 13:28:41 +03:00
|
|
|
if (!$sourceUserObject instanceof IUser) {
|
2019-11-20 13:41:31 +03:00
|
|
|
$output->writeln("<error>Unknown source user " . $input->getArgument('source-user') . "</error>");
|
2016-11-07 15:22:11 +03:00
|
|
|
return 1;
|
2015-11-18 15:14:48 +03:00
|
|
|
}
|
2016-10-17 12:42:54 +03:00
|
|
|
|
2016-10-04 13:28:41 +03:00
|
|
|
if (!$destinationUserObject instanceof IUser) {
|
2019-11-20 13:41:31 +03:00
|
|
|
$output->writeln("<error>Unknown destination user " . $input->getArgument('destination-user') . "</error>");
|
2016-11-07 15:22:11 +03:00
|
|
|
return 1;
|
2016-04-14 18:32:25 +03:00
|
|
|
}
|
2016-10-17 12:42:54 +03:00
|
|
|
|
2019-11-20 13:41:31 +03:00
|
|
|
try {
|
|
|
|
$this->transferService->transfer(
|
|
|
|
$sourceUserObject,
|
|
|
|
$destinationUserObject,
|
|
|
|
ltrim($input->getOption('path'), '/'),
|
2020-01-03 10:43:39 +03:00
|
|
|
$output,
|
|
|
|
$input->getOption('move') === true
|
2019-11-20 13:41:31 +03:00
|
|
|
);
|
|
|
|
} catch (TransferOwnershipException $e) {
|
|
|
|
$output->writeln("<error>" . $e->getMessage() . "</error>");
|
|
|
|
return $e->getCode() !== 0 ? $e->getCode() : 1;
|
2015-11-18 15:14:48 +03:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:41:31 +03:00
|
|
|
return 0;
|
2015-11-18 15:14:48 +03:00
|
|
|
}
|
|
|
|
}
|