Passed
Push — master ( 456412...602de2 )
by Joas
13:17 queued 10s
created

AUserData::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 8
dl 0
loc 16
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2018 John Molakvoæ (skjnldsv) <[email protected]>
7
 *
8
 * @author Arthur Schiwon <[email protected]>
9
 * @author Christoph Wurst <[email protected]>
10
 * @author Georg Ehrke <[email protected]>
11
 * @author Joas Schilling <[email protected]>
12
 * @author John Molakvoæ (skjnldsv) <[email protected]>
13
 * @author Roeland Jago Douma <[email protected]>
14
 *
15
 * @license GNU AGPL version 3 or any later version
16
 *
17
 * This program is free software: you can redistribute it and/or modify
18
 * it under the terms of the GNU Affero General Public License as
19
 * published by the Free Software Foundation, either version 3 of the
20
 * License, or (at your option) any later version.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
 * GNU Affero General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU Affero General Public License
28
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29
 *
30
 */
31
32
namespace OCA\Provisioning_API\Controller;
33
34
use OC\Accounts\AccountManager;
35
use OC\Group\Manager;
36
use OC\User\Backend;
37
use OC\User\NoUserException;
38
use OC_Helper;
39
use OCP\Accounts\IAccountManager;
40
use OCP\AppFramework\OCS\OCSException;
41
use OCP\AppFramework\OCS\OCSNotFoundException;
42
use OCP\AppFramework\OCSController;
43
use OCP\Files\NotFoundException;
44
use OCP\IConfig;
45
use OCP\IGroupManager;
46
use OCP\IRequest;
47
use OCP\IUserManager;
48
use OCP\IUserSession;
49
use OCP\L10N\IFactory;
50
use OCP\User\Backend\ISetDisplayNameBackend;
51
use OCP\User\Backend\ISetPasswordBackend;
52
53
abstract class AUserData extends OCSController {
54
	public const SCOPE_SUFFIX = 'Scope';
55
56
	/** @var IUserManager */
57
	protected $userManager;
58
	/** @var IConfig */
59
	protected $config;
60
	/** @var IGroupManager|Manager */ // FIXME Requires a method that is not on the interface
61
	protected $groupManager;
62
	/** @var IUserSession */
63
	protected $userSession;
64
	/** @var AccountManager */
65
	protected $accountManager;
66
	/** @var IFactory */
67
	protected $l10nFactory;
68
69
	public function __construct(string $appName,
70
								IRequest $request,
71
								IUserManager $userManager,
72
								IConfig $config,
73
								IGroupManager $groupManager,
74
								IUserSession $userSession,
75
								AccountManager $accountManager,
76
								IFactory $l10nFactory) {
77
		parent::__construct($appName, $request);
78
79
		$this->userManager = $userManager;
80
		$this->config = $config;
81
		$this->groupManager = $groupManager;
82
		$this->userSession = $userSession;
83
		$this->accountManager = $accountManager;
84
		$this->l10nFactory = $l10nFactory;
85
	}
86
87
	/**
88
	 * creates a array with all user data
89
	 *
90
	 * @param string $userId
91
	 * @param bool $includeScopes
92
	 * @return array
93
	 * @throws NotFoundException
94
	 * @throws OCSException
95
	 * @throws OCSNotFoundException
96
	 */
97
	protected function getUserData(string $userId, bool $includeScopes = false): array {
98
		$currentLoggedInUser = $this->userSession->getUser();
99
100
		$data = [];
101
102
		// Check if the target user exists
103
		$targetUserObject = $this->userManager->get($userId);
104
		if ($targetUserObject === null) {
105
			throw new OCSNotFoundException('User does not exist');
106
		}
107
108
		// Should be at least Admin Or SubAdmin!
109
		if ($this->groupManager->isAdmin($currentLoggedInUser->getUID())
110
			|| $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) {
0 ignored issues
show
Bug introduced by
The method getSubAdmin() does not exist on OCP\IGroupManager. Since it exists in all sub-types, consider adding an abstract or default implementation to OCP\IGroupManager. ( Ignorable by Annotation )

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

110
			|| $this->groupManager->/** @scrutinizer ignore-call */ getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) {
Loading history...
111
			$data['enabled'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'enabled', 'true') === 'true';
112
		} else {
113
			// Check they are looking up themselves
114
			if ($currentLoggedInUser->getUID() !== $targetUserObject->getUID()) {
115
				return $data;
116
			}
117
		}
118
119
		// Get groups data
120
		$userAccount = $this->accountManager->getAccount($targetUserObject);
121
		$groups = $this->groupManager->getUserGroups($targetUserObject);
122
		$gids = [];
123
		foreach ($groups as $group) {
124
			$gids[] = $group->getGID();
125
		}
126
127
		try {
128
			# might be thrown by LDAP due to handling of users disappears
129
			# from the external source (reasons unknown to us)
130
			# cf. https://github.com/nextcloud/server/issues/12991
131
			$data['storageLocation'] = $targetUserObject->getHome();
132
		} catch (NoUserException $e) {
133
			throw new OCSNotFoundException($e->getMessage(), $e);
134
		}
135
136
		// Find the data
137
		$data['id'] = $targetUserObject->getUID();
138
		$data['lastLogin'] = $targetUserObject->getLastLogin() * 1000;
139
		$data['backend'] = $targetUserObject->getBackendClassName();
140
		$data['subadmin'] = $this->getUserSubAdminGroupsData($targetUserObject->getUID());
141
		$data['quota'] = $this->fillStorageInfo($targetUserObject->getUID());
142
143
		if ($includeScopes) {
144
			$data[IAccountManager::PROPERTY_AVATAR . self::SCOPE_SUFFIX] = $userAccount->getProperty(IAccountManager::PROPERTY_AVATAR)->getScope();
145
		}
146
147
		$data[IAccountManager::PROPERTY_EMAIL] = $targetUserObject->getEMailAddress();
148
		if ($includeScopes) {
149
			$data[IAccountManager::PROPERTY_EMAIL . self::SCOPE_SUFFIX] = $userAccount->getProperty(IAccountManager::PROPERTY_EMAIL)->getScope();
150
		}
151
		$data[IAccountManager::PROPERTY_DISPLAYNAME] = $targetUserObject->getDisplayName();
152
		if ($includeScopes) {
153
			$data[IAccountManager::PROPERTY_DISPLAYNAME . self::SCOPE_SUFFIX] = $userAccount->getProperty(IAccountManager::PROPERTY_DISPLAYNAME)->getScope();
154
		}
155
156
		foreach ([
157
			IAccountManager::PROPERTY_PHONE,
158
			IAccountManager::PROPERTY_ADDRESS,
159
			IAccountManager::PROPERTY_WEBSITE,
160
			IAccountManager::PROPERTY_TWITTER,
161
		] as $propertyName) {
162
			$property = $userAccount->getProperty($propertyName);
163
			$data[$propertyName] = $property->getValue();
164
			if ($includeScopes) {
165
				$data[$propertyName . self::SCOPE_SUFFIX] = $property->getScope();
166
			}
167
		}
168
169
		$data['groups'] = $gids;
170
		$data['language'] = $this->l10nFactory->getUserLanguage($targetUserObject);
171
		$data['locale'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'locale');
172
173
		$backend = $targetUserObject->getBackend();
174
		$data['backendCapabilities'] = [
175
			'setDisplayName' => $backend instanceof ISetDisplayNameBackend || $backend->implementsActions(Backend::SET_DISPLAYNAME),
176
			'setPassword' => $backend instanceof ISetPasswordBackend || $backend->implementsActions(Backend::SET_PASSWORD),
177
		];
178
179
		return $data;
180
	}
181
182
	/**
183
	 * Get the groups a user is a subadmin of
184
	 *
185
	 * @param string $userId
186
	 * @return array
187
	 * @throws OCSException
188
	 */
189
	protected function getUserSubAdminGroupsData(string $userId): array {
190
		$user = $this->userManager->get($userId);
191
		// Check if the user exists
192
		if ($user === null) {
193
			throw new OCSNotFoundException('User does not exist');
194
		}
195
196
		// Get the subadmin groups
197
		$subAdminGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user);
198
		$groups = [];
199
		foreach ($subAdminGroups as $key => $group) {
200
			$groups[] = $group->getGID();
201
		}
202
203
		return $groups;
204
	}
205
206
	/**
207
	 * @param string $userId
208
	 * @return array
209
	 * @throws OCSException
210
	 */
211
	protected function fillStorageInfo(string $userId): array {
212
		try {
213
			\OC_Util::tearDownFS();
214
			\OC_Util::setupFS($userId);
215
			$storage = OC_Helper::getStorageInfo('/');
216
			$data = [
217
				'free' => $storage['free'],
218
				'used' => $storage['used'],
219
				'total' => $storage['total'],
220
				'relative' => $storage['relative'],
221
				'quota' => $storage['quota'],
222
			];
223
		} catch (NotFoundException $ex) {
224
			// User fs is not setup yet
225
			$user = $this->userManager->get($userId);
226
			if ($user === null) {
227
				throw new OCSException('User does not exist', 101);
228
			}
229
			$quota = $user->getQuota();
230
			if ($quota !== 'none') {
231
				$quota = OC_Helper::computerFileSize($quota);
232
			}
233
			$data = [
234
				'quota' => $quota !== false ? $quota : 'none',
235
				'used' => 0
236
			];
237
		}
238
		return $data;
239
	}
240
}
241