Completed
Push — master ( 6293a1...dc96c0 )
by Jörn Friedrich
34:05 queued 21:15
created

ListUsers::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 0
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Phil Davis <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2018, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OC\Core\Command\User;
23
24
use OC\Core\Command\Base;
25
use OCP\IUser;
26
use OCP\IUserManager;
27
use Symfony\Component\Console\Input\InputInterface;
28
use Symfony\Component\Console\Input\InputOption;
29
use Symfony\Component\Console\Output\OutputInterface;
30
use Symfony\Component\Console\Input\InputArgument;
31
32
class ListUsers extends Base {
33
	/** @var \OCP\IUserManager */
34
	protected $userManager;
35
36
	const ATTRIBUTES = [
37
		'uid',
38
		'displayName',
39
		'email',
40
		'quota',
41
		'enabled',
42
		'lastLogin',
43
		'home',
44
		'backend',
45
		'cloudId',
46
		'searchTerms'
47
	];
48
49
	/**
50
	 * @param IUserManager $userManager
51
	 */
52
	public function __construct(IUserManager $userManager) {
53
		parent::__construct();
54
		$this->userManager = $userManager;
55
	}
56
57
	protected function configure() {
58
		parent::configure();
59
60
		$this
61
			->setName('user:list')
62
			->setDescription('List users and their attributes.')
63
			->addArgument(
64
				'search-pattern',
65
				InputArgument::OPTIONAL,
66
				'Restrict the list to users whose user ID contains the optional search pattern.'
67
			)
68
			->addOption(
69
				'attributes',
70
				'a',
71
				InputOption::VALUE_IS_ARRAY|InputOption::VALUE_REQUIRED,
72
				'Attributes to include from '.\implode(', ', self::ATTRIBUTES),
73
				['displayName']
74
			)
75
		;
76
	}
77
78
	/**
79
	 * If only a single attribute should be listed omit the key to make it fit in one row
80
	 */
81
	private function add(&$row, $key, $val, $useKey) {
82
		if ($useKey) {
83
			$row[$key] = $val;
84
		} else {
85
			$row = $val;
86
		}
87
	}
88
89
	protected function execute(InputInterface $input, OutputInterface $output) {
90
		$userNameSubString = $input->getArgument('search-pattern');
91
		$attributes = \array_map('mb_strtolower', $input->getOption('attributes'));
92
		$useKey = \count($attributes) > 1
93
			|| $input->getOption('output') !== self::OUTPUT_FORMAT_PLAIN;
94
		$users = $this->userManager->search($userNameSubString);
95
		$users = \array_map(function(IUser $user) use ($output, $attributes, $useKey) {
96
			if ($output->isVerbose()) {
97
				// include all attributes
98
				$row = [
99
					'uid' => $user->getUID(),
100
					'displayName' => $user->getDisplayName(),
101
					'email' => $user->getEMailAddress(),
102
					'quota' => $user->getQuota(),
103
					'enabled' => $user->isEnabled(),
104
					'lastLogin' => $user->getLastLogin(),
105
					'home' => $user->getHome(),
106
					'backend' => $user->getBackendClassName(),
107
					'cloudId' => $user->getCloudId(),
108
					'searchTerms' => $user->getSearchTerms(),
109
				];
110
			} else {
111
				// include only specified attributes
112
				$row = [];
113
				foreach ($attributes as $attribute) {
114
					switch ($attribute) {
115
						case 'uid':
116
							$this->add($row, 'uid', $user->getUID(), $useKey);
117
							break;
118
						case 'displayname':
119
							$this->add($row, 'displayName', $user->getDisplayName(), $useKey);
120
							break;
121
						case 'email':
122
							$this->add($row, 'email', $user->getEMailAddress(), $useKey);
123
							break;
124
						case 'quota':
125
							$this->add($row, 'quota', $user->getQuota(), $useKey);
126
							break;
127
						case 'enabled':
128
							$this->add($row, 'enabled', $user->isEnabled(), $useKey);
129
							break;
130
						case 'lastlogin':
131
							$this->add($row, 'lastLogin', $user->getLastLogin(), $useKey);
132
							break;
133
						case 'home':
134
							$this->add($row, 'home', $user->getHome(), $useKey);
135
							break;
136
						case 'backend':
137
							$this->add($row, 'backend', $user->getBackendClassName(), $useKey);
138
							break;
139
						case 'cloudid':
140
							$this->add($row, 'cloudId', $user->getCloudId(), $useKey);
141
							break;
142
						case 'searchterms':
143
							$this->add($row, 'searchTerms', $user->getSearchTerms(), $useKey);
144
							break;
145
						default:
146
							throw new \UnexpectedValueException("Unknown attribute $attribute");
147
					}
148
				}
149
			}
150
			return $row;
151
		}, $users);
152
		parent::writeArrayInOutputFormat($input, $output, $users, self::DEFAULT_OUTPUT_PREFIX, true);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (writeArrayInOutputFormat() instead of execute()). Are you sure this is correct? If so, you might want to change this to $this->writeArrayInOutputFormat().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
153
	}
154
}
155