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

Disable   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A completeArgumentValues() 0 11 2
A execute() 0 10 2
A __construct() 0 3 1
A configure() 0 8 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 * @author Roeland Jago Douma <[email protected]>
7
 * @author Thomas Müller <[email protected]>
8
 *
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
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, version 3,
21
 * along with this program. If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
namespace OC\Core\Command\User;
25
26
use OC\Core\Command\Base;
27
use OCP\IUser;
28
use OCP\IUserManager;
29
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
30
use Symfony\Component\Console\Input\InputArgument;
31
use Symfony\Component\Console\Input\InputInterface;
32
use Symfony\Component\Console\Output\OutputInterface;
33
34
class Disable extends Base {
35
	/** @var IUserManager */
36
	protected $userManager;
37
38
	/**
39
	 * @param IUserManager $userManager
40
	 */
41
	public function __construct(IUserManager $userManager) {
42
		$this->userManager = $userManager;
43
		parent::__construct();
44
	}
45
46
	protected function configure() {
47
		$this
48
			->setName('user:disable')
49
			->setDescription('disables the specified user')
50
			->addArgument(
51
				'uid',
52
				InputArgument::REQUIRED,
53
				'the username'
54
			);
55
	}
56
57
	protected function execute(InputInterface $input, OutputInterface $output): int {
58
		$user = $this->userManager->get($input->getArgument('uid'));
59
		if (is_null($user)) {
60
			$output->writeln('<error>User does not exist</error>');
61
			return 1;
62
		}
63
64
		$user->setEnabled(false);
65
		$output->writeln('<info>The specified user is disabled</info>');
66
		return 0;
67
	}
68
69
	/**
70
	 * @param string $argumentName
71
	 * @param CompletionContext $context
72
	 * @return string[]
73
	 */
74
	public function completeArgumentValues($argumentName, CompletionContext $context) {
75
		if ($argumentName === 'uid') {
76
			return array_map(
77
				static fn (IUser $user) => $user->getUID(),
78
				array_filter(
79
					$this->userManager->search($context->getCurrentWord()),
80
					static fn (IUser $user) => $user->isEnabled()
81
				)
82
			);
83
		}
84
		return [];
85
	}
86
}
87