Passed
Push — master ( c233ac...ceefe0 )
by Joas
14:23 queued 13s
created

RemoveUser::completeArgumentValues()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 2
dl 0
loc 13
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Robin Appelman <[email protected]>
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 * @author Robin Appelman <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
namespace OC\Core\Command\Group;
25
26
use OC\Core\Command\Base;
27
use OCP\IGroup;
28
use OCP\IGroupManager;
29
use OCP\IUser;
30
use OCP\IUserManager;
31
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
32
use Symfony\Component\Console\Input\InputArgument;
33
use Symfony\Component\Console\Input\InputInterface;
34
use Symfony\Component\Console\Output\OutputInterface;
35
36
class RemoveUser extends Base {
37
	/** @var IUserManager */
38
	protected $userManager;
39
	/** @var IGroupManager */
40
	protected $groupManager;
41
42
	/**
43
	 * @param IUserManager $userManager
44
	 * @param IGroupManager $groupManager
45
	 */
46
	public function __construct(IUserManager $userManager, IGroupManager $groupManager) {
47
		$this->userManager = $userManager;
48
		$this->groupManager = $groupManager;
49
		parent::__construct();
50
	}
51
52
	protected function configure() {
53
		$this
54
			->setName('group:removeuser')
55
			->setDescription('remove a user from a group')
56
			->addArgument(
57
				'group',
58
				InputArgument::REQUIRED,
59
				'group to remove the user from'
60
			)->addArgument(
61
				'user',
62
				InputArgument::REQUIRED,
63
				'user to remove from the group'
64
			);
65
	}
66
67
	protected function execute(InputInterface $input, OutputInterface $output): int {
68
		$group = $this->groupManager->get($input->getArgument('group'));
69
		if (is_null($group)) {
70
			$output->writeln('<error>group not found</error>');
71
			return 1;
72
		}
73
		$user = $this->userManager->get($input->getArgument('user'));
74
		if (is_null($user)) {
75
			$output->writeln('<error>user not found</error>');
76
			return 1;
77
		}
78
		$group->removeUser($user);
79
		return 0;
80
	}
81
82
	/**
83
	 * @param string $argumentName
84
	 * @param CompletionContext $context
85
	 * @return string[]
86
	 */
87
	public function completeArgumentValues($argumentName, CompletionContext $context) {
88
		if ($argumentName === 'group') {
89
			return array_map(static fn (IGroup $group) => $group->getGID(), $this->groupManager->search($context->getCurrentWord()));
90
		}
91
		if ($argumentName === 'user') {
92
			$groupId = $context->getWordAtIndex($context->getWordIndex() - 1);
93
			$group = $this->groupManager->get($groupId);
94
			if ($group === null) {
95
				return [];
96
			}
97
			return array_map(static fn (IUser $user) => $user->getUID(), $group->searchUsers($context->getCurrentWord()));
98
		}
99
		return [];
100
	}
101
}
102