Passed
Push — master ( 78da95...deb499 )
by Morris
21:03 queued 10:24
created

TransferOwnership::execute()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 22
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 38
rs 8.9457
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2016, ownCloud, Inc.
7
 *
8
 * @author Carla Schroder <[email protected]>
9
 * @author Christoph Wurst <[email protected]>
10
 * @author Joas Schilling <[email protected]>
11
 * @author Morris Jobke <[email protected]>
12
 * @author Roeland Jago Douma <[email protected]>
13
 * @author Sujith H <[email protected]>
14
 * @author Thomas Müller <[email protected]>
15
 * @author Tobia De Koninck <[email protected]>
16
 * @author Vincent Petry <[email protected]>
17
 *
18
 * @license AGPL-3.0
19
 *
20
 * This code is free software: you can redistribute it and/or modify
21
 * it under the terms of the GNU Affero General Public License, version 3,
22
 * as published by the Free Software Foundation.
23
 *
24
 * This program is distributed in the hope that it will be useful,
25
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27
 * GNU Affero General Public License for more details.
28
 *
29
 * You should have received a copy of the GNU Affero General Public License, version 3,
30
 * along with this program. If not, see <http://www.gnu.org/licenses/>
31
 *
32
 */
33
34
namespace OCA\Files\Command;
35
36
use OCA\Files\Exception\TransferOwnershipException;
37
use OCA\Files\Service\OwnershipTransferService;
38
use OCP\IUser;
39
use OCP\IUserManager;
40
use Symfony\Component\Console\Command\Command;
41
use Symfony\Component\Console\Input\InputArgument;
42
use Symfony\Component\Console\Input\InputInterface;
43
use Symfony\Component\Console\Input\InputOption;
44
use Symfony\Component\Console\Output\OutputInterface;
45
46
class TransferOwnership extends Command {
47
48
	/** @var IUserManager */
49
	private $userManager;
50
51
	/** @var OwnershipTransferService */
52
	private $transferService;
53
54
	public function __construct(IUserManager $userManager,
55
								OwnershipTransferService $transferService) {
56
		parent::__construct();
57
		$this->userManager = $userManager;
58
		$this->transferService = $transferService;
59
	}
60
61
	protected function configure() {
62
		$this
63
			->setName('files:transfer-ownership')
64
			->setDescription('All files and folders are moved to another user - shares are moved as well.')
65
			->addArgument(
66
				'source-user',
67
				InputArgument::REQUIRED,
68
				'owner of files which shall be moved'
69
			)
70
			->addArgument(
71
				'destination-user',
72
				InputArgument::REQUIRED,
73
				'user who will be the new owner of the files'
74
			)
75
			->addOption(
76
				'path',
77
				null,
78
				InputOption::VALUE_REQUIRED,
79
				'selectively provide the path to transfer. For example --path="folder_name"',
80
				''
81
			)->addOption(
82
				'move',
83
				null,
84
				InputOption::VALUE_NONE,
85
				'move data from source user to root directory of destination user, which must be empty'
86
		);
87
	}
88
89
	protected function execute(InputInterface $input, OutputInterface $output): int {
90
91
		/**
92
		 * Check if source and destination users are same. If they are same then just ignore the transfer.
93
		 */
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
100
		$sourceUserObject = $this->userManager->get($input->getArgument('source-user'));
101
		$destinationUserObject = $this->userManager->get($input->getArgument('destination-user'));
102
103
		if (!$sourceUserObject instanceof IUser) {
104
			$output->writeln("<error>Unknown source user " . $input->getArgument('source-user') . "</error>");
0 ignored issues
show
Bug introduced by
Are you sure $input->getArgument('source-user') of type null|string|string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

104
			$output->writeln("<error>Unknown source user " . /** @scrutinizer ignore-type */ $input->getArgument('source-user') . "</error>");
Loading history...
105
			return 1;
106
		}
107
108
		if (!$destinationUserObject instanceof IUser) {
109
			$output->writeln("<error>Unknown destination user " . $input->getArgument('destination-user') . "</error>");
0 ignored issues
show
Bug introduced by
Are you sure $input->getArgument('destination-user') of type null|string|string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

109
			$output->writeln("<error>Unknown destination user " . /** @scrutinizer ignore-type */ $input->getArgument('destination-user') . "</error>");
Loading history...
110
			return 1;
111
		}
112
113
		try {
114
			$this->transferService->transfer(
115
				$sourceUserObject,
116
				$destinationUserObject,
117
				ltrim($input->getOption('path'), '/'),
118
				$output,
119
				$input->getOption('move') === true
120
			);
121
		} catch (TransferOwnershipException $e) {
122
			$output->writeln("<error>" . $e->getMessage() . "</error>");
123
			return $e->getCode() !== 0 ? $e->getCode() : 1;
124
		}
125
126
		return 0;
127
	}
128
}
129