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

Info   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 53
dl 0
loc 88
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 14 1
A getStorageInfo() 0 14 2
A __construct() 0 4 1
A completeArgumentValues() 0 5 2
A execute() 0 22 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Robin Appelman <[email protected]>
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 * @author Morris Jobke <[email protected]>
7
 * @author Robin Appelman <[email protected]>
8
 *
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
namespace OC\Core\Command\User;
26
27
use OC\Core\Command\Base;
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\Input\InputOption;
35
use Symfony\Component\Console\Output\OutputInterface;
36
37
class Info extends Base {
38
	/** @var IUserManager */
39
	protected $userManager;
40
	/** @var IGroupManager */
41
	protected $groupManager;
42
43
	/**
44
	 * @param IUserManager $userManager
45
	 * @param IGroupManager $groupManager
46
	 */
47
	public function __construct(IUserManager $userManager, IGroupManager $groupManager) {
48
		$this->userManager = $userManager;
49
		$this->groupManager = $groupManager;
50
		parent::__construct();
51
	}
52
53
	protected function configure() {
54
		$this
55
			->setName('user:info')
56
			->setDescription('show user info')
57
			->addArgument(
58
				'user',
59
				InputArgument::REQUIRED,
60
				'user to show'
61
			)->addOption(
62
				'output',
63
				null,
64
				InputOption::VALUE_OPTIONAL,
65
				'Output format (plain, json or json_pretty, default is plain)',
66
				$this->defaultOutputFormat
67
			);
68
	}
69
70
	protected function execute(InputInterface $input, OutputInterface $output): int {
71
		$user = $this->userManager->get($input->getArgument('user'));
72
		if (is_null($user)) {
73
			$output->writeln('<error>user not found</error>');
74
			return 1;
75
		}
76
		$groups = $this->groupManager->getUserGroupIds($user);
77
		$data = [
78
			'user_id' => $user->getUID(),
79
			'display_name' => $user->getDisplayName(),
80
			'email' => (string)$user->getSystemEMailAddress(),
81
			'cloud_id' => $user->getCloudId(),
82
			'enabled' => $user->isEnabled(),
83
			'groups' => $groups,
84
			'quota' => $user->getQuota(),
85
			'storage' => $this->getStorageInfo($user),
86
			'last_seen' => date(\DateTimeInterface::ATOM, $user->getLastLogin()), // ISO-8601
87
			'user_directory' => $user->getHome(),
88
			'backend' => $user->getBackendClassName()
89
		];
90
		$this->writeArrayInOutputFormat($input, $output, $data);
91
		return 0;
92
	}
93
94
	/**
95
	 * @param IUser $user
96
	 * @return array
97
	 */
98
	protected function getStorageInfo(IUser $user): array {
99
		\OC_Util::tearDownFS();
100
		\OC_Util::setupFS($user->getUID());
101
		try {
102
			$storage = \OC_Helper::getStorageInfo('/');
103
		} catch (\OCP\Files\NotFoundException $e) {
104
			return [];
105
		}
106
		return [
107
			'free' => $storage['free'],
108
			'used' => $storage['used'],
109
			'total' => $storage['total'],
110
			'relative' => $storage['relative'],
111
			'quota' => $storage['quota'],
112
		];
113
	}
114
115
	/**
116
	 * @param string $argumentName
117
	 * @param CompletionContext $context
118
	 * @return string[]
119
	 */
120
	public function completeArgumentValues($argumentName, CompletionContext $context) {
121
		if ($argumentName === 'user') {
122
			return array_map(static fn (IUser $user) => $user->getUID(), $this->userManager->search($context->getCurrentWord()));
123
		}
124
		return [];
125
	}
126
}
127