Completed
Pull Request — master (#8847)
by John
18:40
created
apps/provisioning_api/appinfo/routes.php 1 patch
Indentation   +37 added lines, -37 removed lines patch added patch discarded remove patch
@@ -25,44 +25,44 @@
 block discarded – undo
25 25
  */
26 26
 
27 27
 return [
28
-	'ocs' => [
29
-		// Apps
30
-		['root' => '/cloud', 'name' => 'Apps#getApps', 'url' => '/apps', 'verb' => 'GET'],
31
-		['root' => '/cloud', 'name' => 'Apps#getAppInfo', 'url' => '/apps/{app}', 'verb' => 'GET'],
32
-		['root' => '/cloud', 'name' => 'Apps#enable', 'url' => '/apps/{app}', 'verb' => 'POST'],
33
-		['root' => '/cloud', 'name' => 'Apps#disable', 'url' => '/apps/{app}', 'verb' => 'DELETE'],
28
+    'ocs' => [
29
+        // Apps
30
+        ['root' => '/cloud', 'name' => 'Apps#getApps', 'url' => '/apps', 'verb' => 'GET'],
31
+        ['root' => '/cloud', 'name' => 'Apps#getAppInfo', 'url' => '/apps/{app}', 'verb' => 'GET'],
32
+        ['root' => '/cloud', 'name' => 'Apps#enable', 'url' => '/apps/{app}', 'verb' => 'POST'],
33
+        ['root' => '/cloud', 'name' => 'Apps#disable', 'url' => '/apps/{app}', 'verb' => 'DELETE'],
34 34
 
35
-		// Groups
36
-		['root' => '/cloud', 'name' => 'Groups#getGroups', 'url' => '/groups', 'verb' => 'GET'],
37
-		['root' => '/cloud', 'name' => 'Groups#getGroup', 'url' => '/groups/{groupId}', 'verb' => 'GET'],
38
-		['root' => '/cloud', 'name' => 'Groups#addGroup', 'url' => '/groups', 'verb' => 'POST'],
39
-		['root' => '/cloud', 'name' => 'Groups#deleteGroup', 'url' => '/groups/{groupId}', 'verb' => 'DELETE'],
40
-		['root' => '/cloud', 'name' => 'Groups#getSubAdminsOfGroup', 'url' => '/groups/{groupId}/subadmins', 'verb' => 'GET'],
35
+        // Groups
36
+        ['root' => '/cloud', 'name' => 'Groups#getGroups', 'url' => '/groups', 'verb' => 'GET'],
37
+        ['root' => '/cloud', 'name' => 'Groups#getGroup', 'url' => '/groups/{groupId}', 'verb' => 'GET'],
38
+        ['root' => '/cloud', 'name' => 'Groups#addGroup', 'url' => '/groups', 'verb' => 'POST'],
39
+        ['root' => '/cloud', 'name' => 'Groups#deleteGroup', 'url' => '/groups/{groupId}', 'verb' => 'DELETE'],
40
+        ['root' => '/cloud', 'name' => 'Groups#getSubAdminsOfGroup', 'url' => '/groups/{groupId}/subadmins', 'verb' => 'GET'],
41 41
 
42
-		// Users
43
-		['root' => '/cloud', 'name' => 'Users#getUsers', 'url' => '/users', 'verb' => 'GET'],
44
-		['root' => '/cloud', 'name' => 'Users#getUsersDetails', 'url' => '/users/details', 'verb' => 'GET'],
45
-		['root' => '/cloud', 'name' => 'Users#addUser', 'url' => '/users', 'verb' => 'POST'],
46
-		['root' => '/cloud', 'name' => 'Users#getUser', 'url' => '/users/{userId}', 'verb' => 'GET'],
47
-		['root' => '/cloud', 'name' => 'Users#getCurrentUser', 'url' => '/user', 'verb' => 'GET'],
48
-		['root' => '/cloud', 'name' => 'Users#getEditableFields', 'url' => '/user/fields', 'verb' => 'GET'],
49
-		['root' => '/cloud', 'name' => 'Users#editUser', 'url' => '/users/{userId}', 'verb' => 'PUT'],
50
-		['root' => '/cloud', 'name' => 'Users#deleteUser', 'url' => '/users/{userId}', 'verb' => 'DELETE'],
51
-		['root' => '/cloud', 'name' => 'Users#enableUser', 'url' => '/users/{userId}/enable', 'verb' => 'PUT'],
52
-		['root' => '/cloud', 'name' => 'Users#disableUser', 'url' => '/users/{userId}/disable', 'verb' => 'PUT'],
53
-		['root' => '/cloud', 'name' => 'Users#getUsersGroups', 'url' => '/users/{userId}/groups', 'verb' => 'GET'],
54
-		['root' => '/cloud', 'name' => 'Users#addToGroup', 'url' => '/users/{userId}/groups', 'verb' => 'POST'],
55
-		['root' => '/cloud', 'name' => 'Users#removeFromGroup', 'url' => '/users/{userId}/groups', 'verb' => 'DELETE'],
56
-		['root' => '/cloud', 'name' => 'Users#getUserSubAdminGroups', 'url' => '/users/{userId}/subadmins', 'verb' => 'GET'],
57
-		['root' => '/cloud', 'name' => 'Users#addSubAdmin', 'url' => '/users/{userId}/subadmins', 'verb' => 'POST'],
58
-		['root' => '/cloud', 'name' => 'Users#removeSubAdmin', 'url' => '/users/{userId}/subadmins', 'verb' => 'DELETE'],
59
-		['root' => '/cloud', 'name' => 'Users#resendWelcomeMessage', 'url' => '/users/{userId}/welcome', 'verb' => 'POST'],
42
+        // Users
43
+        ['root' => '/cloud', 'name' => 'Users#getUsers', 'url' => '/users', 'verb' => 'GET'],
44
+        ['root' => '/cloud', 'name' => 'Users#getUsersDetails', 'url' => '/users/details', 'verb' => 'GET'],
45
+        ['root' => '/cloud', 'name' => 'Users#addUser', 'url' => '/users', 'verb' => 'POST'],
46
+        ['root' => '/cloud', 'name' => 'Users#getUser', 'url' => '/users/{userId}', 'verb' => 'GET'],
47
+        ['root' => '/cloud', 'name' => 'Users#getCurrentUser', 'url' => '/user', 'verb' => 'GET'],
48
+        ['root' => '/cloud', 'name' => 'Users#getEditableFields', 'url' => '/user/fields', 'verb' => 'GET'],
49
+        ['root' => '/cloud', 'name' => 'Users#editUser', 'url' => '/users/{userId}', 'verb' => 'PUT'],
50
+        ['root' => '/cloud', 'name' => 'Users#deleteUser', 'url' => '/users/{userId}', 'verb' => 'DELETE'],
51
+        ['root' => '/cloud', 'name' => 'Users#enableUser', 'url' => '/users/{userId}/enable', 'verb' => 'PUT'],
52
+        ['root' => '/cloud', 'name' => 'Users#disableUser', 'url' => '/users/{userId}/disable', 'verb' => 'PUT'],
53
+        ['root' => '/cloud', 'name' => 'Users#getUsersGroups', 'url' => '/users/{userId}/groups', 'verb' => 'GET'],
54
+        ['root' => '/cloud', 'name' => 'Users#addToGroup', 'url' => '/users/{userId}/groups', 'verb' => 'POST'],
55
+        ['root' => '/cloud', 'name' => 'Users#removeFromGroup', 'url' => '/users/{userId}/groups', 'verb' => 'DELETE'],
56
+        ['root' => '/cloud', 'name' => 'Users#getUserSubAdminGroups', 'url' => '/users/{userId}/subadmins', 'verb' => 'GET'],
57
+        ['root' => '/cloud', 'name' => 'Users#addSubAdmin', 'url' => '/users/{userId}/subadmins', 'verb' => 'POST'],
58
+        ['root' => '/cloud', 'name' => 'Users#removeSubAdmin', 'url' => '/users/{userId}/subadmins', 'verb' => 'DELETE'],
59
+        ['root' => '/cloud', 'name' => 'Users#resendWelcomeMessage', 'url' => '/users/{userId}/welcome', 'verb' => 'POST'],
60 60
 
61
-		// Config
62
-		['name' => 'AppConfig#getApps', 'url' => '/api/v1/config/apps', 'verb' => 'GET'],
63
-		['name' => 'AppConfig#getKeys', 'url' => '/api/v1/config/apps/{app}', 'verb' => 'GET'],
64
-		['name' => 'AppConfig#getValue', 'url' => '/api/v1/config/apps/{app}/{key}', 'verb' => 'GET'],
65
-		['name' => 'AppConfig#setValue', 'url' => '/api/v1/config/apps/{app}/{key}', 'verb' => 'POST'],
66
-		['name' => 'AppConfig#deleteKey', 'url' => '/api/v1/config/apps/{app}/{key}', 'verb' => 'DELETE'],
67
-	],
61
+        // Config
62
+        ['name' => 'AppConfig#getApps', 'url' => '/api/v1/config/apps', 'verb' => 'GET'],
63
+        ['name' => 'AppConfig#getKeys', 'url' => '/api/v1/config/apps/{app}', 'verb' => 'GET'],
64
+        ['name' => 'AppConfig#getValue', 'url' => '/api/v1/config/apps/{app}/{key}', 'verb' => 'GET'],
65
+        ['name' => 'AppConfig#setValue', 'url' => '/api/v1/config/apps/{app}/{key}', 'verb' => 'POST'],
66
+        ['name' => 'AppConfig#deleteKey', 'url' => '/api/v1/config/apps/{app}/{key}', 'verb' => 'DELETE'],
67
+    ],
68 68
 ];
Please login to merge, or discard this patch.
apps/provisioning_api/lib/Controller/UsersController.php 2 patches
Indentation   +897 added lines, -897 removed lines patch added patch discarded remove patch
@@ -53,901 +53,901 @@
 block discarded – undo
53 53
 
54 54
 class UsersController extends OCSController {
55 55
 
56
-	/** @var IUserManager */
57
-	private $userManager;
58
-	/** @var IConfig */
59
-	private $config;
60
-	/** @var IAppManager */
61
-	private $appManager;
62
-	/** @var IGroupManager|\OC\Group\Manager */ // FIXME Requires a method that is not on the interface
63
-	private $groupManager;
64
-	/** @var IUserSession */
65
-	private $userSession;
66
-	/** @var AccountManager */
67
-	private $accountManager;
68
-	/** @var ILogger */
69
-	private $logger;
70
-	/** @var IFactory */
71
-	private $l10nFactory;
72
-	/** @var NewUserMailHelper */
73
-	private $newUserMailHelper;
74
-	/** @var FederatedFileSharingFactory */
75
-	private $federatedFileSharingFactory;
76
-
77
-	/**
78
-	 * @param string $appName
79
-	 * @param IRequest $request
80
-	 * @param IUserManager $userManager
81
-	 * @param IConfig $config
82
-	 * @param IAppManager $appManager
83
-	 * @param IGroupManager $groupManager
84
-	 * @param IUserSession $userSession
85
-	 * @param AccountManager $accountManager
86
-	 * @param ILogger $logger
87
-	 * @param IFactory $l10nFactory
88
-	 * @param NewUserMailHelper $newUserMailHelper
89
-	 * @param FederatedFileSharingFactory $federatedFileSharingFactory
90
-	 */
91
-	public function __construct(string $appName,
92
-								IRequest $request,
93
-								IUserManager $userManager,
94
-								IConfig $config,
95
-								IAppManager $appManager,
96
-								IGroupManager $groupManager,
97
-								IUserSession $userSession,
98
-								AccountManager $accountManager,
99
-								ILogger $logger,
100
-								IFactory $l10nFactory,
101
-								NewUserMailHelper $newUserMailHelper,
102
-								FederatedFileSharingFactory $federatedFileSharingFactory) {
103
-		parent::__construct($appName, $request);
104
-
105
-		$this->userManager = $userManager;
106
-		$this->config = $config;
107
-		$this->appManager = $appManager;
108
-		$this->groupManager = $groupManager;
109
-		$this->userSession = $userSession;
110
-		$this->accountManager = $accountManager;
111
-		$this->logger = $logger;
112
-		$this->l10nFactory = $l10nFactory;
113
-		$this->newUserMailHelper = $newUserMailHelper;
114
-		$this->federatedFileSharingFactory = $federatedFileSharingFactory;
115
-	}
116
-
117
-	/**
118
-	 * @NoAdminRequired
119
-	 *
120
-	 * returns a list of users
121
-	 *
122
-	 * @param string $search
123
-	 * @param int $limit
124
-	 * @param int $offset
125
-	 * @return DataResponse
126
-	 */
127
-	public function getUsers(string $search = '', $limit = null, $offset = null): DataResponse {
128
-		$user = $this->userSession->getUser();
129
-		$users = [];
130
-
131
-		// Admin? Or SubAdmin?
132
-		$uid = $user->getUID();
133
-		$subAdminManager = $this->groupManager->getSubAdmin();
134
-		if($this->groupManager->isAdmin($uid)){
135
-			$users = $this->userManager->search($search, $limit, $offset);
136
-		} else if ($subAdminManager->isSubAdmin($user)) {
137
-			$subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user);
138
-			foreach ($subAdminOfGroups as $key => $group) {
139
-				$subAdminOfGroups[$key] = $group->getGID();
140
-			}
141
-
142
-			if($offset === null) {
143
-				$offset = 0;
144
-			}
145
-
146
-			$users = [];
147
-			foreach ($subAdminOfGroups as $group) {
148
-				$users = array_merge($users, $this->groupManager->displayNamesInGroup($group, $search));
149
-			}
150
-
151
-			$users = array_slice($users, $offset, $limit);
152
-		}
153
-
154
-		$users = array_keys($users);
155
-
156
-		return new DataResponse([
157
-			'users' => $users
158
-		]);
159
-	}
160
-
161
-	/**
162
-	 * @NoAdminRequired
163
-	 *
164
-	 * returns a list of users and their data
165
-	 *
166
-	 * @param string $search
167
-	 * @param int $offset
168
-	 * @return DataResponse
169
-	 */
170
-	public function getUsersDetails(string $search = '', $offset = null): DataResponse {
171
-		// Limit set to 25 for performance issues 
172
-		$limit = 25;
173
-		$user = $this->userSession->getUser();
174
-		$users = [];
175
-
176
-		// Admin? Or SubAdmin?
177
-		$uid = $user->getUID();
178
-		$subAdminManager = $this->groupManager->getSubAdmin();
179
-		if($this->groupManager->isAdmin($uid)){
180
-			$users = $this->userManager->search($search, $limit, $offset);
181
-		} else if ($subAdminManager->isSubAdmin($user)) {
182
-			$subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user);
183
-			foreach ($subAdminOfGroups as $key => $group) {
184
-				$subAdminOfGroups[$key] = $group->getGID();
185
-			}
186
-
187
-			if($offset === null) {
188
-				$offset = 0;
189
-			}
190
-
191
-			$users = [];
192
-			foreach ($subAdminOfGroups as $group) {
193
-				$users = array_merge($users, $this->groupManager->displayNamesInGroup($group, $search));
194
-			}
195
-
196
-			$users = array_slice($users, $offset, $limit);
197
-		}
198
-
199
-		$users = array_keys($users);
200
-		$usersDetails = [];
201
-		foreach ($users as $key => $userId) {
202
-			$usersDetails[$userId] = $this->getUserData($userId, false);
203
-		}
204
-
205
-		return new DataResponse([
206
-			'users' => $usersDetails
207
-		]);
208
-	}
209
-
210
-	/**
211
-	 * @PasswordConfirmationRequired
212
-	 * @NoAdminRequired
213
-	 *
214
-	 * @param string $userid
215
-	 * @param string $password
216
-	 * @param array $groups
217
-	 * @return DataResponse
218
-	 * @throws OCSException
219
-	 */
220
-	public function addUser(string $userid, string $password, array $groups = []): DataResponse {
221
-		$user = $this->userSession->getUser();
222
-		$isAdmin = $this->groupManager->isAdmin($user->getUID());
223
-		$subAdminManager = $this->groupManager->getSubAdmin();
224
-
225
-		if($this->userManager->userExists($userid)) {
226
-			$this->logger->error('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']);
227
-			throw new OCSException('User already exists', 102);
228
-		}
229
-
230
-		if($groups !== []) {
231
-			foreach ($groups as $group) {
232
-				if(!$this->groupManager->groupExists($group)) {
233
-					throw new OCSException('group '.$group.' does not exist', 104);
234
-				}
235
-				if(!$isAdmin && !$subAdminManager->isSubAdminofGroup($user, $this->groupManager->get($group))) {
236
-					throw new OCSException('insufficient privileges for group '. $group, 105);
237
-				}
238
-			}
239
-		} else {
240
-			if(!$isAdmin) {
241
-				throw new OCSException('no group specified (required for subadmins)', 106);
242
-			}
243
-		}
244
-
245
-		try {
246
-			$newUser = $this->userManager->createUser($userid, $password);
247
-			$this->logger->info('Successful addUser call with userid: ' . $userid, ['app' => 'ocs_api']);
248
-
249
-			foreach ($groups as $group) {
250
-				$this->groupManager->get($group)->addUser($newUser);
251
-				$this->logger->info('Added userid ' . $userid . ' to group ' . $group, ['app' => 'ocs_api']);
252
-			}
253
-
254
-			return new DataResponse();
255
-		} catch (HintException $e ) {
256
-			$this->logger->logException($e, [
257
-				'message' => 'Failed addUser attempt with hint exception.',
258
-				'level' => \OCP\Util::WARN,
259
-				'app' => 'ocs_api',
260
-			]);
261
-			throw new OCSException($e->getHint(), 107);
262
-		} catch (\Exception $e) {
263
-			$this->logger->logException($e, [
264
-				'message' => 'Failed addUser attempt with exception.',
265
-				'level' => \OCP\Util::ERROR,
266
-				'app' => 'ocs_api',
267
-			]);
268
-			throw new OCSException('Bad request', 101);
269
-		}
270
-	}
271
-
272
-	/**
273
-	 * @NoAdminRequired
274
-	 * @NoSubAdminRequired
275
-	 *
276
-	 * gets user info
277
-	 *
278
-	 * @param string $userId
279
-	 * @return DataResponse
280
-	 * @throws OCSException
281
-	 */
282
-	public function getUser(string $userId): DataResponse {
283
-		$data = $this->getUserData($userId);
284
-		return new DataResponse($data);
285
-	}
286
-
287
-	/**
288
-	 * @NoAdminRequired
289
-	 * @NoSubAdminRequired
290
-	 *
291
-	 * gets user info from the currently logged in user
292
-	 *
293
-	 * @return DataResponse
294
-	 * @throws OCSException
295
-	 */
296
-	public function getCurrentUser(): DataResponse {
297
-		$user = $this->userSession->getUser();
298
-		if ($user) {
299
-			$data =  $this->getUserData($user->getUID());
300
-			// rename "displayname" to "display-name" only for this call to keep
301
-			// the API stable.
302
-			$data['display-name'] = $data['displayname'];
303
-			unset($data['displayname']);
304
-			return new DataResponse($data);
305
-
306
-		}
307
-
308
-		throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
309
-	}
310
-
311
-	/**
312
-	 * creates a array with all user data
313
-	 *
314
-	 * @param $userId
315
-	 * @return array
316
-	 * @throws OCSException
317
-	 */
318
-	protected function getUserData(string $userId, bool $throw = true): array {
319
-		$currentLoggedInUser = $this->userSession->getUser();
320
-
321
-		$data = [];
322
-
323
-		// Check if the target user exists
324
-		$targetUserObject = $this->userManager->get($userId);
325
-		if($targetUserObject === null) {
326
-			throw new OCSException('The requested user could not be found', \OCP\API::RESPOND_NOT_FOUND);
327
-		}
328
-
329
-		// Admin? Or SubAdmin?
330
-		if($this->groupManager->isAdmin($currentLoggedInUser->getUID())
331
-			|| $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) {
332
-
333
-			$data['enabled'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'enabled', 'true');
334
-
335
-		} else {
336
-			// Check they are looking up themselves
337
-			if($currentLoggedInUser->getUID() !== $targetUserObject->getUID() && $throw) {
338
-				throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
339
-			} else {
340
-				return $data;
341
-			}
342
-		}
343
-
344
-		// Get groups data
345
-		$userAccount = $this->accountManager->getUser($targetUserObject);
346
-		$groups = $this->groupManager->getUserGroups($targetUserObject);
347
-		$gids = [];
348
-		foreach ($groups as $group) {
349
-			$gids[] = $group->getDisplayName();
350
-		}
351
-
352
-		// Find the data
353
-		$data['id'] = $targetUserObject->getUID();
354
-		$data['storageLocation'] = $targetUserObject->getHome();
355
-		$data['lastLogin'] = $targetUserObject->getLastLogin() * 1000;
356
-		$data['backend'] = $targetUserObject->getBackendClassName();
357
-		$data['subadmins'] = $this->getUserSubAdminGroupsData($targetUserObject->getUID());
358
-		$data['quota'] = $this->fillStorageInfo($targetUserObject->getUID());
359
-		$data[AccountManager::PROPERTY_EMAIL] = $targetUserObject->getEMailAddress();
360
-		$data[AccountManager::PROPERTY_DISPLAYNAME] = $targetUserObject->getDisplayName();
361
-		$data[AccountManager::PROPERTY_PHONE] = $userAccount[AccountManager::PROPERTY_PHONE]['value'];
362
-		$data[AccountManager::PROPERTY_ADDRESS] = $userAccount[AccountManager::PROPERTY_ADDRESS]['value'];
363
-		$data[AccountManager::PROPERTY_WEBSITE] = $userAccount[AccountManager::PROPERTY_WEBSITE]['value'];
364
-		$data[AccountManager::PROPERTY_TWITTER] = $userAccount[AccountManager::PROPERTY_TWITTER]['value'];
365
-		$data['groups'] = $gids;
366
-		$data['language'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'lang');
367
-
368
-		return $data;
369
-	}
370
-
371
-	/**
372
-	 * @NoAdminRequired
373
-	 * @NoSubAdminRequired
374
-	 */
375
-	public function getEditableFields(): DataResponse {
376
-		$permittedFields = [];
377
-
378
-		// Editing self (display, email)
379
-		if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) {
380
-			$permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME;
381
-			$permittedFields[] = AccountManager::PROPERTY_EMAIL;
382
-		}
383
-
384
-		if ($this->appManager->isEnabledForUser('federatedfilesharing')) {
385
-			$federatedFileSharing = $this->federatedFileSharingFactory->get();
386
-			$shareProvider = $federatedFileSharing->getFederatedShareProvider();
387
-			if ($shareProvider->isLookupServerUploadEnabled()) {
388
-				$permittedFields[] = AccountManager::PROPERTY_PHONE;
389
-				$permittedFields[] = AccountManager::PROPERTY_ADDRESS;
390
-				$permittedFields[] = AccountManager::PROPERTY_WEBSITE;
391
-				$permittedFields[] = AccountManager::PROPERTY_TWITTER;
392
-			}
393
-		}
394
-
395
-		return new DataResponse($permittedFields);
396
-	}
397
-
398
-	/**
399
-	 * @NoAdminRequired
400
-	 * @NoSubAdminRequired
401
-	 * @PasswordConfirmationRequired
402
-	 *
403
-	 * edit users
404
-	 *
405
-	 * @param string $userId
406
-	 * @param string $key
407
-	 * @param string $value
408
-	 * @return DataResponse
409
-	 * @throws OCSException
410
-	 */
411
-	public function editUser(string $userId, string $key, string $value): DataResponse {
412
-		$currentLoggedInUser = $this->userSession->getUser();
413
-
414
-		$targetUser = $this->userManager->get($userId);
415
-		if($targetUser === null) {
416
-			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
417
-		}
418
-
419
-		$permittedFields = [];
420
-		if($targetUser->getUID() === $currentLoggedInUser->getUID()) {
421
-			// Editing self (display, email)
422
-			if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) {
423
-				$permittedFields[] = 'display';
424
-				$permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME;
425
-				$permittedFields[] = AccountManager::PROPERTY_EMAIL;
426
-			}
427
-
428
-			$permittedFields[] = 'password';
429
-			if ($this->config->getSystemValue('force_language', false) === false ||
430
-				$this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
431
-				$permittedFields[] = 'language';
432
-			}
433
-
434
-			if ($this->appManager->isEnabledForUser('federatedfilesharing')) {
435
-				$federatedFileSharing = new \OCA\FederatedFileSharing\AppInfo\Application();
436
-				$shareProvider = $federatedFileSharing->getFederatedShareProvider();
437
-				if ($shareProvider->isLookupServerUploadEnabled()) {
438
-					$permittedFields[] = AccountManager::PROPERTY_PHONE;
439
-					$permittedFields[] = AccountManager::PROPERTY_ADDRESS;
440
-					$permittedFields[] = AccountManager::PROPERTY_WEBSITE;
441
-					$permittedFields[] = AccountManager::PROPERTY_TWITTER;
442
-				}
443
-			}
444
-
445
-			// If admin they can edit their own quota
446
-			if($this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
447
-				$permittedFields[] = 'quota';
448
-			}
449
-		} else {
450
-			// Check if admin / subadmin
451
-			$subAdminManager = $this->groupManager->getSubAdmin();
452
-			if($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
453
-			|| $this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
454
-				// They have permissions over the user
455
-				$permittedFields[] = 'display';
456
-				$permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME;
457
-				$permittedFields[] = AccountManager::PROPERTY_EMAIL;
458
-				$permittedFields[] = 'password';
459
-				$permittedFields[] = 'language';
460
-				$permittedFields[] = AccountManager::PROPERTY_PHONE;
461
-				$permittedFields[] = AccountManager::PROPERTY_ADDRESS;
462
-				$permittedFields[] = AccountManager::PROPERTY_WEBSITE;
463
-				$permittedFields[] = AccountManager::PROPERTY_TWITTER;
464
-				$permittedFields[] = 'quota';
465
-			} else {
466
-				// No rights
467
-				throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
468
-			}
469
-		}
470
-		// Check if permitted to edit this field
471
-		if(!in_array($key, $permittedFields)) {
472
-			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
473
-		}
474
-		// Process the edit
475
-		switch($key) {
476
-			case 'display':
477
-			case AccountManager::PROPERTY_DISPLAYNAME:
478
-				$targetUser->setDisplayName($value);
479
-				break;
480
-			case 'quota':
481
-				$quota = $value;
482
-				if($quota !== 'none' && $quota !== 'default') {
483
-					if (is_numeric($quota)) {
484
-						$quota = (float) $quota;
485
-					} else {
486
-						$quota = \OCP\Util::computerFileSize($quota);
487
-					}
488
-					if ($quota === false) {
489
-						throw new OCSException('Invalid quota value '.$value, 103);
490
-					}
491
-					if($quota === 0) {
492
-						$quota = 'default';
493
-					}else if($quota === -1) {
494
-						$quota = 'none';
495
-					} else {
496
-						$quota = \OCP\Util::humanFileSize($quota);
497
-					}
498
-				}
499
-				$targetUser->setQuota($quota);
500
-				break;
501
-			case 'password':
502
-				$targetUser->setPassword($value);
503
-				break;
504
-			case 'language':
505
-				$languagesCodes = $this->l10nFactory->findAvailableLanguages();
506
-				if (!in_array($value, $languagesCodes, true) && $value !== 'en') {
507
-					throw new OCSException('Invalid language', 102);
508
-				}
509
-				$this->config->setUserValue($targetUser->getUID(), 'core', 'lang', $value);
510
-				break;
511
-			case AccountManager::PROPERTY_EMAIL:
512
-				if(filter_var($value, FILTER_VALIDATE_EMAIL)) {
513
-					$targetUser->setEMailAddress($value);
514
-				} else {
515
-					throw new OCSException('', 102);
516
-				}
517
-				break;
518
-			case AccountManager::PROPERTY_PHONE:
519
-			case AccountManager::PROPERTY_ADDRESS:
520
-			case AccountManager::PROPERTY_WEBSITE:
521
-			case AccountManager::PROPERTY_TWITTER:
522
-				$userAccount = $this->accountManager->getUser($targetUser);
523
-				if ($userAccount[$key]['value'] !== $value) {
524
-					$userAccount[$key]['value'] = $value;
525
-					$this->accountManager->updateUser($targetUser, $userAccount);
526
-				}
527
-				break;
528
-			default:
529
-				throw new OCSException('', 103);
530
-		}
531
-		return new DataResponse();
532
-	}
533
-
534
-	/**
535
-	 * @PasswordConfirmationRequired
536
-	 * @NoAdminRequired
537
-	 *
538
-	 * @param string $userId
539
-	 * @return DataResponse
540
-	 * @throws OCSException
541
-	 */
542
-	public function deleteUser(string $userId): DataResponse {
543
-		$currentLoggedInUser = $this->userSession->getUser();
544
-
545
-		$targetUser = $this->userManager->get($userId);
546
-
547
-		if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
548
-			throw new OCSException('', 101);
549
-		}
550
-
551
-		// If not permitted
552
-		$subAdminManager = $this->groupManager->getSubAdmin();
553
-		if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
554
-			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
555
-		}
556
-
557
-		// Go ahead with the delete
558
-		if($targetUser->delete()) {
559
-			return new DataResponse();
560
-		} else {
561
-			throw new OCSException('', 101);
562
-		}
563
-	}
564
-
565
-	/**
566
-	 * @PasswordConfirmationRequired
567
-	 * @NoAdminRequired
568
-	 *
569
-	 * @param string $userId
570
-	 * @return DataResponse
571
-	 * @throws OCSException
572
-	 * @throws OCSForbiddenException
573
-	 */
574
-	public function disableUser(string $userId): DataResponse {
575
-		return $this->setEnabled($userId, false);
576
-	}
577
-
578
-	/**
579
-	 * @PasswordConfirmationRequired
580
-	 * @NoAdminRequired
581
-	 *
582
-	 * @param string $userId
583
-	 * @return DataResponse
584
-	 * @throws OCSException
585
-	 * @throws OCSForbiddenException
586
-	 */
587
-	public function enableUser(string $userId): DataResponse {
588
-		return $this->setEnabled($userId, true);
589
-	}
590
-
591
-	/**
592
-	 * @param string $userId
593
-	 * @param bool $value
594
-	 * @return DataResponse
595
-	 * @throws OCSException
596
-	 */
597
-	private function setEnabled(string $userId, bool $value): DataResponse {
598
-		$currentLoggedInUser = $this->userSession->getUser();
599
-
600
-		$targetUser = $this->userManager->get($userId);
601
-		if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
602
-			throw new OCSException('', 101);
603
-		}
604
-
605
-		// If not permitted
606
-		$subAdminManager = $this->groupManager->getSubAdmin();
607
-		if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
608
-			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
609
-		}
610
-
611
-		// enable/disable the user now
612
-		$targetUser->setEnabled($value);
613
-		return new DataResponse();
614
-	}
615
-
616
-	/**
617
-	 * @NoAdminRequired
618
-	 * @NoSubAdminRequired
619
-	 *
620
-	 * @param string $userId
621
-	 * @return DataResponse
622
-	 * @throws OCSException
623
-	 */
624
-	public function getUsersGroups(string $userId): DataResponse {
625
-		$loggedInUser = $this->userSession->getUser();
626
-
627
-		$targetUser = $this->userManager->get($userId);
628
-		if($targetUser === null) {
629
-			throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND);
630
-		}
631
-
632
-		if($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) {
633
-			// Self lookup or admin lookup
634
-			return new DataResponse([
635
-				'groups' => $this->groupManager->getUserGroupIds($targetUser)
636
-			]);
637
-		} else {
638
-			$subAdminManager = $this->groupManager->getSubAdmin();
639
-
640
-			// Looking up someone else
641
-			if($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) {
642
-				// Return the group that the method caller is subadmin of for the user in question
643
-				/** @var IGroup[] $getSubAdminsGroups */
644
-				$getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);
645
-				foreach ($getSubAdminsGroups as $key => $group) {
646
-					$getSubAdminsGroups[$key] = $group->getGID();
647
-				}
648
-				$groups = array_intersect(
649
-					$getSubAdminsGroups,
650
-					$this->groupManager->getUserGroupIds($targetUser)
651
-				);
652
-				return new DataResponse(['groups' => $groups]);
653
-			} else {
654
-				// Not permitted
655
-				throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
656
-			}
657
-		}
658
-
659
-	}
660
-
661
-	/**
662
-	 * @PasswordConfirmationRequired
663
-	 * @NoAdminRequired
664
-	 *
665
-	 * @param string $userId
666
-	 * @param string $groupid
667
-	 * @return DataResponse
668
-	 * @throws OCSException
669
-	 */
670
-	public function addToGroup(string $userId, string $groupid = ''): DataResponse {
671
-		if($groupid === '') {
672
-			throw new OCSException('', 101);
673
-		}
674
-
675
-		$group = $this->groupManager->get($groupid);
676
-		$targetUser = $this->userManager->get($userId);
677
-		if($group === null) {
678
-			throw new OCSException('', 102);
679
-		}
680
-		if($targetUser === null) {
681
-			throw new OCSException('', 103);
682
-		}
683
-
684
-		// If they're not an admin, check they are a subadmin of the group in question
685
-		$loggedInUser = $this->userSession->getUser();
686
-		$subAdminManager = $this->groupManager->getSubAdmin();
687
-		if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) {
688
-			throw new OCSException('', 104);
689
-		}
690
-
691
-		// Add user to group
692
-		$group->addUser($targetUser);
693
-		return new DataResponse();
694
-	}
695
-
696
-	/**
697
-	 * @PasswordConfirmationRequired
698
-	 * @NoAdminRequired
699
-	 *
700
-	 * @param string $userId
701
-	 * @param string $groupid
702
-	 * @return DataResponse
703
-	 * @throws OCSException
704
-	 */
705
-	public function removeFromGroup(string $userId, string $groupid): DataResponse {
706
-		$loggedInUser = $this->userSession->getUser();
707
-
708
-		if($groupid === null || trim($groupid) === '') {
709
-			throw new OCSException('', 101);
710
-		}
711
-
712
-		$group = $this->groupManager->get($groupid);
713
-		if($group === null) {
714
-			throw new OCSException('', 102);
715
-		}
716
-
717
-		$targetUser = $this->userManager->get($userId);
718
-		if($targetUser === null) {
719
-			throw new OCSException('', 103);
720
-		}
721
-
722
-		// If they're not an admin, check they are a subadmin of the group in question
723
-		$subAdminManager = $this->groupManager->getSubAdmin();
724
-		if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) {
725
-			throw new OCSException('', 104);
726
-		}
727
-
728
-		// Check they aren't removing themselves from 'admin' or their 'subadmin; group
729
-		if ($targetUser->getUID() === $loggedInUser->getUID()) {
730
-			if ($this->groupManager->isAdmin($loggedInUser->getUID())) {
731
-				if ($group->getGID() === 'admin') {
732
-					throw new OCSException('Cannot remove yourself from the admin group', 105);
733
-				}
734
-			} else {
735
-				// Not an admin, so the user must be a subadmin of this group, but that is not allowed.
736
-				throw new OCSException('Cannot remove yourself from this group as you are a SubAdmin', 105);
737
-			}
738
-
739
-		} else if (!$this->groupManager->isAdmin($loggedInUser->getUID())) {
740
-			/** @var IGroup[] $subAdminGroups */
741
-			$subAdminGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);
742
-			$subAdminGroups = array_map(function (IGroup $subAdminGroup) {
743
-				return $subAdminGroup->getGID();
744
-			}, $subAdminGroups);
745
-			$userGroups = $this->groupManager->getUserGroupIds($targetUser);
746
-			$userSubAdminGroups = array_intersect($subAdminGroups, $userGroups);
747
-
748
-			if (count($userSubAdminGroups) <= 1) {
749
-				// Subadmin must not be able to remove a user from all their subadmin groups.
750
-				throw new OCSException('Cannot remove user from this group as this is the only remaining group you are a SubAdmin of', 105);
751
-			}
752
-		}
753
-
754
-		// Remove user from group
755
-		$group->removeUser($targetUser);
756
-		return new DataResponse();
757
-	}
758
-
759
-	/**
760
-	 * Creates a subadmin
761
-	 *
762
-	 * @PasswordConfirmationRequired
763
-	 *
764
-	 * @param string $userId
765
-	 * @param string $groupid
766
-	 * @return DataResponse
767
-	 * @throws OCSException
768
-	 */
769
-	public function addSubAdmin(string $userId, string $groupid): DataResponse {
770
-		$group = $this->groupManager->get($groupid);
771
-		$user = $this->userManager->get($userId);
772
-
773
-		// Check if the user exists
774
-		if($user === null) {
775
-			throw new OCSException('User does not exist', 101);
776
-		}
777
-		// Check if group exists
778
-		if($group === null) {
779
-			throw new OCSException('Group does not exist',  102);
780
-		}
781
-		// Check if trying to make subadmin of admin group
782
-		if($group->getGID() === 'admin') {
783
-			throw new OCSException('Cannot create subadmins for admin group', 103);
784
-		}
785
-
786
-		$subAdminManager = $this->groupManager->getSubAdmin();
787
-
788
-		// We cannot be subadmin twice
789
-		if ($subAdminManager->isSubAdminofGroup($user, $group)) {
790
-			return new DataResponse();
791
-		}
792
-		// Go
793
-		if($subAdminManager->createSubAdmin($user, $group)) {
794
-			return new DataResponse();
795
-		} else {
796
-			throw new OCSException('Unknown error occurred', 103);
797
-		}
798
-	}
799
-
800
-	/**
801
-	 * Removes a subadmin from a group
802
-	 *
803
-	 * @PasswordConfirmationRequired
804
-	 *
805
-	 * @param string $userId
806
-	 * @param string $groupid
807
-	 * @return DataResponse
808
-	 * @throws OCSException
809
-	 */
810
-	public function removeSubAdmin(string $userId, string $groupid): DataResponse {
811
-		$group = $this->groupManager->get($groupid);
812
-		$user = $this->userManager->get($userId);
813
-		$subAdminManager = $this->groupManager->getSubAdmin();
814
-
815
-		// Check if the user exists
816
-		if($user === null) {
817
-			throw new OCSException('User does not exist', 101);
818
-		}
819
-		// Check if the group exists
820
-		if($group === null) {
821
-			throw new OCSException('Group does not exist', 101);
822
-		}
823
-		// Check if they are a subadmin of this said group
824
-		if(!$subAdminManager->isSubAdminOfGroup($user, $group)) {
825
-			throw new OCSException('User is not a subadmin of this group', 102);
826
-		}
827
-
828
-		// Go
829
-		if($subAdminManager->deleteSubAdmin($user, $group)) {
830
-			return new DataResponse();
831
-		} else {
832
-			throw new OCSException('Unknown error occurred', 103);
833
-		}
834
-	}
835
-
836
-	/**
837
-	 * Get the groups a user is a subadmin of
838
-	 *
839
-	 * @param string $userId
840
-	 * @return array
841
-	 * @throws OCSException
842
-	 */
843
-	protected function getUserSubAdminGroupsData(string $userId): array {
844
-		$user = $this->userManager->get($userId);
845
-		// Check if the user exists
846
-		if($user === null) {
847
-			throw new OCSException('User does not exist', 101);
848
-		}
849
-
850
-		// Get the subadmin groups
851
-		$subAdminGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user);
852
-		$groups = [];
853
-		foreach ($subAdminGroups as $key => $group) {
854
-			$groups[] = $group->getGID();
855
-		}
856
-
857
-		return $groups;
858
-	}
859
-
860
-	/**
861
-	 * Get the groups a user is a subadmin of
862
-	 *
863
-	 * @param string $userId
864
-	 * @return DataResponse
865
-	 * @throws OCSException
866
-	 */
867
-	public function getUserSubAdminGroups(string $userId): DataResponse {
868
-		$groups = $this->getUserSubAdminGroupsData($userId);
869
-
870
-		if(!$groups) {
871
-			throw new OCSException('Unknown error occurred', 102);
872
-		} else {
873
-			return $groups;
874
-		}
875
-	}
876
-
877
-	/**
878
-	 * @param string $userId
879
-	 * @return array
880
-	 * @throws \OCP\Files\NotFoundException
881
-	 */
882
-	protected function fillStorageInfo(string $userId): array {
883
-		try {
884
-			\OC_Util::tearDownFS();
885
-			\OC_Util::setupFS($userId);
886
-			$storage = OC_Helper::getStorageInfo('/');
887
-			$data = [
888
-				'free' => $storage['free'],
889
-				'used' => $storage['used'],
890
-				'total' => $storage['total'],
891
-				'relative' => $storage['relative'],
892
-				'quota' => $storage['quota'],
893
-			];
894
-		} catch (NotFoundException $ex) {
895
-			$data = [];
896
-		}
897
-		return $data;
898
-	}
899
-
900
-	/**
901
-	 * @NoAdminRequired
902
-	 * @PasswordConfirmationRequired
903
-	 *
904
-	 * resend welcome message
905
-	 *
906
-	 * @param string $userId
907
-	 * @return DataResponse
908
-	 * @throws OCSException
909
-	 */
910
-	public function resendWelcomeMessage(string $userId): DataResponse {
911
-		$currentLoggedInUser = $this->userSession->getUser();
912
-
913
-		$targetUser = $this->userManager->get($userId);
914
-		if($targetUser === null) {
915
-			throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND);
916
-		}
917
-
918
-		// Check if admin / subadmin
919
-		$subAdminManager = $this->groupManager->getSubAdmin();
920
-		if(!$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
921
-			&& !$this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
922
-			// No rights
923
-			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
924
-		}
925
-
926
-		$email = $targetUser->getEMailAddress();
927
-		if ($email === '' || $email === null) {
928
-			throw new OCSException('Email address not available', 101);
929
-		}
930
-		$username = $targetUser->getUID();
931
-		$lang = $this->config->getUserValue($username, 'core', 'lang', 'en');
932
-		if (!$this->l10nFactory->languageExists('settings', $lang)) {
933
-			$lang = 'en';
934
-		}
935
-
936
-		$l10n = $this->l10nFactory->get('settings', $lang);
937
-
938
-		try {
939
-			$this->newUserMailHelper->setL10N($l10n);
940
-			$emailTemplate = $this->newUserMailHelper->generateTemplate($targetUser, false);
941
-			$this->newUserMailHelper->sendMail($targetUser, $emailTemplate);
942
-		} catch(\Exception $e) {
943
-			$this->logger->logException($e, [
944
-				'message' => "Can't send new user mail to $email",
945
-				'level' => \OCP\Util::ERROR,
946
-				'app' => 'settings',
947
-			]);
948
-			throw new OCSException('Sending email failed', 102);
949
-		}
950
-
951
-		return new DataResponse();
952
-	}
56
+    /** @var IUserManager */
57
+    private $userManager;
58
+    /** @var IConfig */
59
+    private $config;
60
+    /** @var IAppManager */
61
+    private $appManager;
62
+    /** @var IGroupManager|\OC\Group\Manager */ // FIXME Requires a method that is not on the interface
63
+    private $groupManager;
64
+    /** @var IUserSession */
65
+    private $userSession;
66
+    /** @var AccountManager */
67
+    private $accountManager;
68
+    /** @var ILogger */
69
+    private $logger;
70
+    /** @var IFactory */
71
+    private $l10nFactory;
72
+    /** @var NewUserMailHelper */
73
+    private $newUserMailHelper;
74
+    /** @var FederatedFileSharingFactory */
75
+    private $federatedFileSharingFactory;
76
+
77
+    /**
78
+     * @param string $appName
79
+     * @param IRequest $request
80
+     * @param IUserManager $userManager
81
+     * @param IConfig $config
82
+     * @param IAppManager $appManager
83
+     * @param IGroupManager $groupManager
84
+     * @param IUserSession $userSession
85
+     * @param AccountManager $accountManager
86
+     * @param ILogger $logger
87
+     * @param IFactory $l10nFactory
88
+     * @param NewUserMailHelper $newUserMailHelper
89
+     * @param FederatedFileSharingFactory $federatedFileSharingFactory
90
+     */
91
+    public function __construct(string $appName,
92
+                                IRequest $request,
93
+                                IUserManager $userManager,
94
+                                IConfig $config,
95
+                                IAppManager $appManager,
96
+                                IGroupManager $groupManager,
97
+                                IUserSession $userSession,
98
+                                AccountManager $accountManager,
99
+                                ILogger $logger,
100
+                                IFactory $l10nFactory,
101
+                                NewUserMailHelper $newUserMailHelper,
102
+                                FederatedFileSharingFactory $federatedFileSharingFactory) {
103
+        parent::__construct($appName, $request);
104
+
105
+        $this->userManager = $userManager;
106
+        $this->config = $config;
107
+        $this->appManager = $appManager;
108
+        $this->groupManager = $groupManager;
109
+        $this->userSession = $userSession;
110
+        $this->accountManager = $accountManager;
111
+        $this->logger = $logger;
112
+        $this->l10nFactory = $l10nFactory;
113
+        $this->newUserMailHelper = $newUserMailHelper;
114
+        $this->federatedFileSharingFactory = $federatedFileSharingFactory;
115
+    }
116
+
117
+    /**
118
+     * @NoAdminRequired
119
+     *
120
+     * returns a list of users
121
+     *
122
+     * @param string $search
123
+     * @param int $limit
124
+     * @param int $offset
125
+     * @return DataResponse
126
+     */
127
+    public function getUsers(string $search = '', $limit = null, $offset = null): DataResponse {
128
+        $user = $this->userSession->getUser();
129
+        $users = [];
130
+
131
+        // Admin? Or SubAdmin?
132
+        $uid = $user->getUID();
133
+        $subAdminManager = $this->groupManager->getSubAdmin();
134
+        if($this->groupManager->isAdmin($uid)){
135
+            $users = $this->userManager->search($search, $limit, $offset);
136
+        } else if ($subAdminManager->isSubAdmin($user)) {
137
+            $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user);
138
+            foreach ($subAdminOfGroups as $key => $group) {
139
+                $subAdminOfGroups[$key] = $group->getGID();
140
+            }
141
+
142
+            if($offset === null) {
143
+                $offset = 0;
144
+            }
145
+
146
+            $users = [];
147
+            foreach ($subAdminOfGroups as $group) {
148
+                $users = array_merge($users, $this->groupManager->displayNamesInGroup($group, $search));
149
+            }
150
+
151
+            $users = array_slice($users, $offset, $limit);
152
+        }
153
+
154
+        $users = array_keys($users);
155
+
156
+        return new DataResponse([
157
+            'users' => $users
158
+        ]);
159
+    }
160
+
161
+    /**
162
+     * @NoAdminRequired
163
+     *
164
+     * returns a list of users and their data
165
+     *
166
+     * @param string $search
167
+     * @param int $offset
168
+     * @return DataResponse
169
+     */
170
+    public function getUsersDetails(string $search = '', $offset = null): DataResponse {
171
+        // Limit set to 25 for performance issues 
172
+        $limit = 25;
173
+        $user = $this->userSession->getUser();
174
+        $users = [];
175
+
176
+        // Admin? Or SubAdmin?
177
+        $uid = $user->getUID();
178
+        $subAdminManager = $this->groupManager->getSubAdmin();
179
+        if($this->groupManager->isAdmin($uid)){
180
+            $users = $this->userManager->search($search, $limit, $offset);
181
+        } else if ($subAdminManager->isSubAdmin($user)) {
182
+            $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user);
183
+            foreach ($subAdminOfGroups as $key => $group) {
184
+                $subAdminOfGroups[$key] = $group->getGID();
185
+            }
186
+
187
+            if($offset === null) {
188
+                $offset = 0;
189
+            }
190
+
191
+            $users = [];
192
+            foreach ($subAdminOfGroups as $group) {
193
+                $users = array_merge($users, $this->groupManager->displayNamesInGroup($group, $search));
194
+            }
195
+
196
+            $users = array_slice($users, $offset, $limit);
197
+        }
198
+
199
+        $users = array_keys($users);
200
+        $usersDetails = [];
201
+        foreach ($users as $key => $userId) {
202
+            $usersDetails[$userId] = $this->getUserData($userId, false);
203
+        }
204
+
205
+        return new DataResponse([
206
+            'users' => $usersDetails
207
+        ]);
208
+    }
209
+
210
+    /**
211
+     * @PasswordConfirmationRequired
212
+     * @NoAdminRequired
213
+     *
214
+     * @param string $userid
215
+     * @param string $password
216
+     * @param array $groups
217
+     * @return DataResponse
218
+     * @throws OCSException
219
+     */
220
+    public function addUser(string $userid, string $password, array $groups = []): DataResponse {
221
+        $user = $this->userSession->getUser();
222
+        $isAdmin = $this->groupManager->isAdmin($user->getUID());
223
+        $subAdminManager = $this->groupManager->getSubAdmin();
224
+
225
+        if($this->userManager->userExists($userid)) {
226
+            $this->logger->error('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']);
227
+            throw new OCSException('User already exists', 102);
228
+        }
229
+
230
+        if($groups !== []) {
231
+            foreach ($groups as $group) {
232
+                if(!$this->groupManager->groupExists($group)) {
233
+                    throw new OCSException('group '.$group.' does not exist', 104);
234
+                }
235
+                if(!$isAdmin && !$subAdminManager->isSubAdminofGroup($user, $this->groupManager->get($group))) {
236
+                    throw new OCSException('insufficient privileges for group '. $group, 105);
237
+                }
238
+            }
239
+        } else {
240
+            if(!$isAdmin) {
241
+                throw new OCSException('no group specified (required for subadmins)', 106);
242
+            }
243
+        }
244
+
245
+        try {
246
+            $newUser = $this->userManager->createUser($userid, $password);
247
+            $this->logger->info('Successful addUser call with userid: ' . $userid, ['app' => 'ocs_api']);
248
+
249
+            foreach ($groups as $group) {
250
+                $this->groupManager->get($group)->addUser($newUser);
251
+                $this->logger->info('Added userid ' . $userid . ' to group ' . $group, ['app' => 'ocs_api']);
252
+            }
253
+
254
+            return new DataResponse();
255
+        } catch (HintException $e ) {
256
+            $this->logger->logException($e, [
257
+                'message' => 'Failed addUser attempt with hint exception.',
258
+                'level' => \OCP\Util::WARN,
259
+                'app' => 'ocs_api',
260
+            ]);
261
+            throw new OCSException($e->getHint(), 107);
262
+        } catch (\Exception $e) {
263
+            $this->logger->logException($e, [
264
+                'message' => 'Failed addUser attempt with exception.',
265
+                'level' => \OCP\Util::ERROR,
266
+                'app' => 'ocs_api',
267
+            ]);
268
+            throw new OCSException('Bad request', 101);
269
+        }
270
+    }
271
+
272
+    /**
273
+     * @NoAdminRequired
274
+     * @NoSubAdminRequired
275
+     *
276
+     * gets user info
277
+     *
278
+     * @param string $userId
279
+     * @return DataResponse
280
+     * @throws OCSException
281
+     */
282
+    public function getUser(string $userId): DataResponse {
283
+        $data = $this->getUserData($userId);
284
+        return new DataResponse($data);
285
+    }
286
+
287
+    /**
288
+     * @NoAdminRequired
289
+     * @NoSubAdminRequired
290
+     *
291
+     * gets user info from the currently logged in user
292
+     *
293
+     * @return DataResponse
294
+     * @throws OCSException
295
+     */
296
+    public function getCurrentUser(): DataResponse {
297
+        $user = $this->userSession->getUser();
298
+        if ($user) {
299
+            $data =  $this->getUserData($user->getUID());
300
+            // rename "displayname" to "display-name" only for this call to keep
301
+            // the API stable.
302
+            $data['display-name'] = $data['displayname'];
303
+            unset($data['displayname']);
304
+            return new DataResponse($data);
305
+
306
+        }
307
+
308
+        throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
309
+    }
310
+
311
+    /**
312
+     * creates a array with all user data
313
+     *
314
+     * @param $userId
315
+     * @return array
316
+     * @throws OCSException
317
+     */
318
+    protected function getUserData(string $userId, bool $throw = true): array {
319
+        $currentLoggedInUser = $this->userSession->getUser();
320
+
321
+        $data = [];
322
+
323
+        // Check if the target user exists
324
+        $targetUserObject = $this->userManager->get($userId);
325
+        if($targetUserObject === null) {
326
+            throw new OCSException('The requested user could not be found', \OCP\API::RESPOND_NOT_FOUND);
327
+        }
328
+
329
+        // Admin? Or SubAdmin?
330
+        if($this->groupManager->isAdmin($currentLoggedInUser->getUID())
331
+            || $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) {
332
+
333
+            $data['enabled'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'enabled', 'true');
334
+
335
+        } else {
336
+            // Check they are looking up themselves
337
+            if($currentLoggedInUser->getUID() !== $targetUserObject->getUID() && $throw) {
338
+                throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
339
+            } else {
340
+                return $data;
341
+            }
342
+        }
343
+
344
+        // Get groups data
345
+        $userAccount = $this->accountManager->getUser($targetUserObject);
346
+        $groups = $this->groupManager->getUserGroups($targetUserObject);
347
+        $gids = [];
348
+        foreach ($groups as $group) {
349
+            $gids[] = $group->getDisplayName();
350
+        }
351
+
352
+        // Find the data
353
+        $data['id'] = $targetUserObject->getUID();
354
+        $data['storageLocation'] = $targetUserObject->getHome();
355
+        $data['lastLogin'] = $targetUserObject->getLastLogin() * 1000;
356
+        $data['backend'] = $targetUserObject->getBackendClassName();
357
+        $data['subadmins'] = $this->getUserSubAdminGroupsData($targetUserObject->getUID());
358
+        $data['quota'] = $this->fillStorageInfo($targetUserObject->getUID());
359
+        $data[AccountManager::PROPERTY_EMAIL] = $targetUserObject->getEMailAddress();
360
+        $data[AccountManager::PROPERTY_DISPLAYNAME] = $targetUserObject->getDisplayName();
361
+        $data[AccountManager::PROPERTY_PHONE] = $userAccount[AccountManager::PROPERTY_PHONE]['value'];
362
+        $data[AccountManager::PROPERTY_ADDRESS] = $userAccount[AccountManager::PROPERTY_ADDRESS]['value'];
363
+        $data[AccountManager::PROPERTY_WEBSITE] = $userAccount[AccountManager::PROPERTY_WEBSITE]['value'];
364
+        $data[AccountManager::PROPERTY_TWITTER] = $userAccount[AccountManager::PROPERTY_TWITTER]['value'];
365
+        $data['groups'] = $gids;
366
+        $data['language'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'lang');
367
+
368
+        return $data;
369
+    }
370
+
371
+    /**
372
+     * @NoAdminRequired
373
+     * @NoSubAdminRequired
374
+     */
375
+    public function getEditableFields(): DataResponse {
376
+        $permittedFields = [];
377
+
378
+        // Editing self (display, email)
379
+        if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) {
380
+            $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME;
381
+            $permittedFields[] = AccountManager::PROPERTY_EMAIL;
382
+        }
383
+
384
+        if ($this->appManager->isEnabledForUser('federatedfilesharing')) {
385
+            $federatedFileSharing = $this->federatedFileSharingFactory->get();
386
+            $shareProvider = $federatedFileSharing->getFederatedShareProvider();
387
+            if ($shareProvider->isLookupServerUploadEnabled()) {
388
+                $permittedFields[] = AccountManager::PROPERTY_PHONE;
389
+                $permittedFields[] = AccountManager::PROPERTY_ADDRESS;
390
+                $permittedFields[] = AccountManager::PROPERTY_WEBSITE;
391
+                $permittedFields[] = AccountManager::PROPERTY_TWITTER;
392
+            }
393
+        }
394
+
395
+        return new DataResponse($permittedFields);
396
+    }
397
+
398
+    /**
399
+     * @NoAdminRequired
400
+     * @NoSubAdminRequired
401
+     * @PasswordConfirmationRequired
402
+     *
403
+     * edit users
404
+     *
405
+     * @param string $userId
406
+     * @param string $key
407
+     * @param string $value
408
+     * @return DataResponse
409
+     * @throws OCSException
410
+     */
411
+    public function editUser(string $userId, string $key, string $value): DataResponse {
412
+        $currentLoggedInUser = $this->userSession->getUser();
413
+
414
+        $targetUser = $this->userManager->get($userId);
415
+        if($targetUser === null) {
416
+            throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
417
+        }
418
+
419
+        $permittedFields = [];
420
+        if($targetUser->getUID() === $currentLoggedInUser->getUID()) {
421
+            // Editing self (display, email)
422
+            if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) {
423
+                $permittedFields[] = 'display';
424
+                $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME;
425
+                $permittedFields[] = AccountManager::PROPERTY_EMAIL;
426
+            }
427
+
428
+            $permittedFields[] = 'password';
429
+            if ($this->config->getSystemValue('force_language', false) === false ||
430
+                $this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
431
+                $permittedFields[] = 'language';
432
+            }
433
+
434
+            if ($this->appManager->isEnabledForUser('federatedfilesharing')) {
435
+                $federatedFileSharing = new \OCA\FederatedFileSharing\AppInfo\Application();
436
+                $shareProvider = $federatedFileSharing->getFederatedShareProvider();
437
+                if ($shareProvider->isLookupServerUploadEnabled()) {
438
+                    $permittedFields[] = AccountManager::PROPERTY_PHONE;
439
+                    $permittedFields[] = AccountManager::PROPERTY_ADDRESS;
440
+                    $permittedFields[] = AccountManager::PROPERTY_WEBSITE;
441
+                    $permittedFields[] = AccountManager::PROPERTY_TWITTER;
442
+                }
443
+            }
444
+
445
+            // If admin they can edit their own quota
446
+            if($this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
447
+                $permittedFields[] = 'quota';
448
+            }
449
+        } else {
450
+            // Check if admin / subadmin
451
+            $subAdminManager = $this->groupManager->getSubAdmin();
452
+            if($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
453
+            || $this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
454
+                // They have permissions over the user
455
+                $permittedFields[] = 'display';
456
+                $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME;
457
+                $permittedFields[] = AccountManager::PROPERTY_EMAIL;
458
+                $permittedFields[] = 'password';
459
+                $permittedFields[] = 'language';
460
+                $permittedFields[] = AccountManager::PROPERTY_PHONE;
461
+                $permittedFields[] = AccountManager::PROPERTY_ADDRESS;
462
+                $permittedFields[] = AccountManager::PROPERTY_WEBSITE;
463
+                $permittedFields[] = AccountManager::PROPERTY_TWITTER;
464
+                $permittedFields[] = 'quota';
465
+            } else {
466
+                // No rights
467
+                throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
468
+            }
469
+        }
470
+        // Check if permitted to edit this field
471
+        if(!in_array($key, $permittedFields)) {
472
+            throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
473
+        }
474
+        // Process the edit
475
+        switch($key) {
476
+            case 'display':
477
+            case AccountManager::PROPERTY_DISPLAYNAME:
478
+                $targetUser->setDisplayName($value);
479
+                break;
480
+            case 'quota':
481
+                $quota = $value;
482
+                if($quota !== 'none' && $quota !== 'default') {
483
+                    if (is_numeric($quota)) {
484
+                        $quota = (float) $quota;
485
+                    } else {
486
+                        $quota = \OCP\Util::computerFileSize($quota);
487
+                    }
488
+                    if ($quota === false) {
489
+                        throw new OCSException('Invalid quota value '.$value, 103);
490
+                    }
491
+                    if($quota === 0) {
492
+                        $quota = 'default';
493
+                    }else if($quota === -1) {
494
+                        $quota = 'none';
495
+                    } else {
496
+                        $quota = \OCP\Util::humanFileSize($quota);
497
+                    }
498
+                }
499
+                $targetUser->setQuota($quota);
500
+                break;
501
+            case 'password':
502
+                $targetUser->setPassword($value);
503
+                break;
504
+            case 'language':
505
+                $languagesCodes = $this->l10nFactory->findAvailableLanguages();
506
+                if (!in_array($value, $languagesCodes, true) && $value !== 'en') {
507
+                    throw new OCSException('Invalid language', 102);
508
+                }
509
+                $this->config->setUserValue($targetUser->getUID(), 'core', 'lang', $value);
510
+                break;
511
+            case AccountManager::PROPERTY_EMAIL:
512
+                if(filter_var($value, FILTER_VALIDATE_EMAIL)) {
513
+                    $targetUser->setEMailAddress($value);
514
+                } else {
515
+                    throw new OCSException('', 102);
516
+                }
517
+                break;
518
+            case AccountManager::PROPERTY_PHONE:
519
+            case AccountManager::PROPERTY_ADDRESS:
520
+            case AccountManager::PROPERTY_WEBSITE:
521
+            case AccountManager::PROPERTY_TWITTER:
522
+                $userAccount = $this->accountManager->getUser($targetUser);
523
+                if ($userAccount[$key]['value'] !== $value) {
524
+                    $userAccount[$key]['value'] = $value;
525
+                    $this->accountManager->updateUser($targetUser, $userAccount);
526
+                }
527
+                break;
528
+            default:
529
+                throw new OCSException('', 103);
530
+        }
531
+        return new DataResponse();
532
+    }
533
+
534
+    /**
535
+     * @PasswordConfirmationRequired
536
+     * @NoAdminRequired
537
+     *
538
+     * @param string $userId
539
+     * @return DataResponse
540
+     * @throws OCSException
541
+     */
542
+    public function deleteUser(string $userId): DataResponse {
543
+        $currentLoggedInUser = $this->userSession->getUser();
544
+
545
+        $targetUser = $this->userManager->get($userId);
546
+
547
+        if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
548
+            throw new OCSException('', 101);
549
+        }
550
+
551
+        // If not permitted
552
+        $subAdminManager = $this->groupManager->getSubAdmin();
553
+        if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
554
+            throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
555
+        }
556
+
557
+        // Go ahead with the delete
558
+        if($targetUser->delete()) {
559
+            return new DataResponse();
560
+        } else {
561
+            throw new OCSException('', 101);
562
+        }
563
+    }
564
+
565
+    /**
566
+     * @PasswordConfirmationRequired
567
+     * @NoAdminRequired
568
+     *
569
+     * @param string $userId
570
+     * @return DataResponse
571
+     * @throws OCSException
572
+     * @throws OCSForbiddenException
573
+     */
574
+    public function disableUser(string $userId): DataResponse {
575
+        return $this->setEnabled($userId, false);
576
+    }
577
+
578
+    /**
579
+     * @PasswordConfirmationRequired
580
+     * @NoAdminRequired
581
+     *
582
+     * @param string $userId
583
+     * @return DataResponse
584
+     * @throws OCSException
585
+     * @throws OCSForbiddenException
586
+     */
587
+    public function enableUser(string $userId): DataResponse {
588
+        return $this->setEnabled($userId, true);
589
+    }
590
+
591
+    /**
592
+     * @param string $userId
593
+     * @param bool $value
594
+     * @return DataResponse
595
+     * @throws OCSException
596
+     */
597
+    private function setEnabled(string $userId, bool $value): DataResponse {
598
+        $currentLoggedInUser = $this->userSession->getUser();
599
+
600
+        $targetUser = $this->userManager->get($userId);
601
+        if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
602
+            throw new OCSException('', 101);
603
+        }
604
+
605
+        // If not permitted
606
+        $subAdminManager = $this->groupManager->getSubAdmin();
607
+        if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
608
+            throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
609
+        }
610
+
611
+        // enable/disable the user now
612
+        $targetUser->setEnabled($value);
613
+        return new DataResponse();
614
+    }
615
+
616
+    /**
617
+     * @NoAdminRequired
618
+     * @NoSubAdminRequired
619
+     *
620
+     * @param string $userId
621
+     * @return DataResponse
622
+     * @throws OCSException
623
+     */
624
+    public function getUsersGroups(string $userId): DataResponse {
625
+        $loggedInUser = $this->userSession->getUser();
626
+
627
+        $targetUser = $this->userManager->get($userId);
628
+        if($targetUser === null) {
629
+            throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND);
630
+        }
631
+
632
+        if($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) {
633
+            // Self lookup or admin lookup
634
+            return new DataResponse([
635
+                'groups' => $this->groupManager->getUserGroupIds($targetUser)
636
+            ]);
637
+        } else {
638
+            $subAdminManager = $this->groupManager->getSubAdmin();
639
+
640
+            // Looking up someone else
641
+            if($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) {
642
+                // Return the group that the method caller is subadmin of for the user in question
643
+                /** @var IGroup[] $getSubAdminsGroups */
644
+                $getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);
645
+                foreach ($getSubAdminsGroups as $key => $group) {
646
+                    $getSubAdminsGroups[$key] = $group->getGID();
647
+                }
648
+                $groups = array_intersect(
649
+                    $getSubAdminsGroups,
650
+                    $this->groupManager->getUserGroupIds($targetUser)
651
+                );
652
+                return new DataResponse(['groups' => $groups]);
653
+            } else {
654
+                // Not permitted
655
+                throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
656
+            }
657
+        }
658
+
659
+    }
660
+
661
+    /**
662
+     * @PasswordConfirmationRequired
663
+     * @NoAdminRequired
664
+     *
665
+     * @param string $userId
666
+     * @param string $groupid
667
+     * @return DataResponse
668
+     * @throws OCSException
669
+     */
670
+    public function addToGroup(string $userId, string $groupid = ''): DataResponse {
671
+        if($groupid === '') {
672
+            throw new OCSException('', 101);
673
+        }
674
+
675
+        $group = $this->groupManager->get($groupid);
676
+        $targetUser = $this->userManager->get($userId);
677
+        if($group === null) {
678
+            throw new OCSException('', 102);
679
+        }
680
+        if($targetUser === null) {
681
+            throw new OCSException('', 103);
682
+        }
683
+
684
+        // If they're not an admin, check they are a subadmin of the group in question
685
+        $loggedInUser = $this->userSession->getUser();
686
+        $subAdminManager = $this->groupManager->getSubAdmin();
687
+        if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) {
688
+            throw new OCSException('', 104);
689
+        }
690
+
691
+        // Add user to group
692
+        $group->addUser($targetUser);
693
+        return new DataResponse();
694
+    }
695
+
696
+    /**
697
+     * @PasswordConfirmationRequired
698
+     * @NoAdminRequired
699
+     *
700
+     * @param string $userId
701
+     * @param string $groupid
702
+     * @return DataResponse
703
+     * @throws OCSException
704
+     */
705
+    public function removeFromGroup(string $userId, string $groupid): DataResponse {
706
+        $loggedInUser = $this->userSession->getUser();
707
+
708
+        if($groupid === null || trim($groupid) === '') {
709
+            throw new OCSException('', 101);
710
+        }
711
+
712
+        $group = $this->groupManager->get($groupid);
713
+        if($group === null) {
714
+            throw new OCSException('', 102);
715
+        }
716
+
717
+        $targetUser = $this->userManager->get($userId);
718
+        if($targetUser === null) {
719
+            throw new OCSException('', 103);
720
+        }
721
+
722
+        // If they're not an admin, check they are a subadmin of the group in question
723
+        $subAdminManager = $this->groupManager->getSubAdmin();
724
+        if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) {
725
+            throw new OCSException('', 104);
726
+        }
727
+
728
+        // Check they aren't removing themselves from 'admin' or their 'subadmin; group
729
+        if ($targetUser->getUID() === $loggedInUser->getUID()) {
730
+            if ($this->groupManager->isAdmin($loggedInUser->getUID())) {
731
+                if ($group->getGID() === 'admin') {
732
+                    throw new OCSException('Cannot remove yourself from the admin group', 105);
733
+                }
734
+            } else {
735
+                // Not an admin, so the user must be a subadmin of this group, but that is not allowed.
736
+                throw new OCSException('Cannot remove yourself from this group as you are a SubAdmin', 105);
737
+            }
738
+
739
+        } else if (!$this->groupManager->isAdmin($loggedInUser->getUID())) {
740
+            /** @var IGroup[] $subAdminGroups */
741
+            $subAdminGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);
742
+            $subAdminGroups = array_map(function (IGroup $subAdminGroup) {
743
+                return $subAdminGroup->getGID();
744
+            }, $subAdminGroups);
745
+            $userGroups = $this->groupManager->getUserGroupIds($targetUser);
746
+            $userSubAdminGroups = array_intersect($subAdminGroups, $userGroups);
747
+
748
+            if (count($userSubAdminGroups) <= 1) {
749
+                // Subadmin must not be able to remove a user from all their subadmin groups.
750
+                throw new OCSException('Cannot remove user from this group as this is the only remaining group you are a SubAdmin of', 105);
751
+            }
752
+        }
753
+
754
+        // Remove user from group
755
+        $group->removeUser($targetUser);
756
+        return new DataResponse();
757
+    }
758
+
759
+    /**
760
+     * Creates a subadmin
761
+     *
762
+     * @PasswordConfirmationRequired
763
+     *
764
+     * @param string $userId
765
+     * @param string $groupid
766
+     * @return DataResponse
767
+     * @throws OCSException
768
+     */
769
+    public function addSubAdmin(string $userId, string $groupid): DataResponse {
770
+        $group = $this->groupManager->get($groupid);
771
+        $user = $this->userManager->get($userId);
772
+
773
+        // Check if the user exists
774
+        if($user === null) {
775
+            throw new OCSException('User does not exist', 101);
776
+        }
777
+        // Check if group exists
778
+        if($group === null) {
779
+            throw new OCSException('Group does not exist',  102);
780
+        }
781
+        // Check if trying to make subadmin of admin group
782
+        if($group->getGID() === 'admin') {
783
+            throw new OCSException('Cannot create subadmins for admin group', 103);
784
+        }
785
+
786
+        $subAdminManager = $this->groupManager->getSubAdmin();
787
+
788
+        // We cannot be subadmin twice
789
+        if ($subAdminManager->isSubAdminofGroup($user, $group)) {
790
+            return new DataResponse();
791
+        }
792
+        // Go
793
+        if($subAdminManager->createSubAdmin($user, $group)) {
794
+            return new DataResponse();
795
+        } else {
796
+            throw new OCSException('Unknown error occurred', 103);
797
+        }
798
+    }
799
+
800
+    /**
801
+     * Removes a subadmin from a group
802
+     *
803
+     * @PasswordConfirmationRequired
804
+     *
805
+     * @param string $userId
806
+     * @param string $groupid
807
+     * @return DataResponse
808
+     * @throws OCSException
809
+     */
810
+    public function removeSubAdmin(string $userId, string $groupid): DataResponse {
811
+        $group = $this->groupManager->get($groupid);
812
+        $user = $this->userManager->get($userId);
813
+        $subAdminManager = $this->groupManager->getSubAdmin();
814
+
815
+        // Check if the user exists
816
+        if($user === null) {
817
+            throw new OCSException('User does not exist', 101);
818
+        }
819
+        // Check if the group exists
820
+        if($group === null) {
821
+            throw new OCSException('Group does not exist', 101);
822
+        }
823
+        // Check if they are a subadmin of this said group
824
+        if(!$subAdminManager->isSubAdminOfGroup($user, $group)) {
825
+            throw new OCSException('User is not a subadmin of this group', 102);
826
+        }
827
+
828
+        // Go
829
+        if($subAdminManager->deleteSubAdmin($user, $group)) {
830
+            return new DataResponse();
831
+        } else {
832
+            throw new OCSException('Unknown error occurred', 103);
833
+        }
834
+    }
835
+
836
+    /**
837
+     * Get the groups a user is a subadmin of
838
+     *
839
+     * @param string $userId
840
+     * @return array
841
+     * @throws OCSException
842
+     */
843
+    protected function getUserSubAdminGroupsData(string $userId): array {
844
+        $user = $this->userManager->get($userId);
845
+        // Check if the user exists
846
+        if($user === null) {
847
+            throw new OCSException('User does not exist', 101);
848
+        }
849
+
850
+        // Get the subadmin groups
851
+        $subAdminGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user);
852
+        $groups = [];
853
+        foreach ($subAdminGroups as $key => $group) {
854
+            $groups[] = $group->getGID();
855
+        }
856
+
857
+        return $groups;
858
+    }
859
+
860
+    /**
861
+     * Get the groups a user is a subadmin of
862
+     *
863
+     * @param string $userId
864
+     * @return DataResponse
865
+     * @throws OCSException
866
+     */
867
+    public function getUserSubAdminGroups(string $userId): DataResponse {
868
+        $groups = $this->getUserSubAdminGroupsData($userId);
869
+
870
+        if(!$groups) {
871
+            throw new OCSException('Unknown error occurred', 102);
872
+        } else {
873
+            return $groups;
874
+        }
875
+    }
876
+
877
+    /**
878
+     * @param string $userId
879
+     * @return array
880
+     * @throws \OCP\Files\NotFoundException
881
+     */
882
+    protected function fillStorageInfo(string $userId): array {
883
+        try {
884
+            \OC_Util::tearDownFS();
885
+            \OC_Util::setupFS($userId);
886
+            $storage = OC_Helper::getStorageInfo('/');
887
+            $data = [
888
+                'free' => $storage['free'],
889
+                'used' => $storage['used'],
890
+                'total' => $storage['total'],
891
+                'relative' => $storage['relative'],
892
+                'quota' => $storage['quota'],
893
+            ];
894
+        } catch (NotFoundException $ex) {
895
+            $data = [];
896
+        }
897
+        return $data;
898
+    }
899
+
900
+    /**
901
+     * @NoAdminRequired
902
+     * @PasswordConfirmationRequired
903
+     *
904
+     * resend welcome message
905
+     *
906
+     * @param string $userId
907
+     * @return DataResponse
908
+     * @throws OCSException
909
+     */
910
+    public function resendWelcomeMessage(string $userId): DataResponse {
911
+        $currentLoggedInUser = $this->userSession->getUser();
912
+
913
+        $targetUser = $this->userManager->get($userId);
914
+        if($targetUser === null) {
915
+            throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND);
916
+        }
917
+
918
+        // Check if admin / subadmin
919
+        $subAdminManager = $this->groupManager->getSubAdmin();
920
+        if(!$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
921
+            && !$this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
922
+            // No rights
923
+            throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
924
+        }
925
+
926
+        $email = $targetUser->getEMailAddress();
927
+        if ($email === '' || $email === null) {
928
+            throw new OCSException('Email address not available', 101);
929
+        }
930
+        $username = $targetUser->getUID();
931
+        $lang = $this->config->getUserValue($username, 'core', 'lang', 'en');
932
+        if (!$this->l10nFactory->languageExists('settings', $lang)) {
933
+            $lang = 'en';
934
+        }
935
+
936
+        $l10n = $this->l10nFactory->get('settings', $lang);
937
+
938
+        try {
939
+            $this->newUserMailHelper->setL10N($l10n);
940
+            $emailTemplate = $this->newUserMailHelper->generateTemplate($targetUser, false);
941
+            $this->newUserMailHelper->sendMail($targetUser, $emailTemplate);
942
+        } catch(\Exception $e) {
943
+            $this->logger->logException($e, [
944
+                'message' => "Can't send new user mail to $email",
945
+                'level' => \OCP\Util::ERROR,
946
+                'app' => 'settings',
947
+            ]);
948
+            throw new OCSException('Sending email failed', 102);
949
+        }
950
+
951
+        return new DataResponse();
952
+    }
953 953
 }
Please login to merge, or discard this patch.
Spacing   +57 added lines, -57 removed lines patch added patch discarded remove patch
@@ -1,5 +1,5 @@  discard block
 block discarded – undo
1 1
 <?php
2
-declare(strict_types=1);
2
+declare(strict_types = 1);
3 3
 /**
4 4
  * @copyright Copyright (c) 2016, ownCloud, Inc.
5 5
  *
@@ -131,7 +131,7 @@  discard block
 block discarded – undo
131 131
 		// Admin? Or SubAdmin?
132 132
 		$uid = $user->getUID();
133 133
 		$subAdminManager = $this->groupManager->getSubAdmin();
134
-		if($this->groupManager->isAdmin($uid)){
134
+		if ($this->groupManager->isAdmin($uid)) {
135 135
 			$users = $this->userManager->search($search, $limit, $offset);
136 136
 		} else if ($subAdminManager->isSubAdmin($user)) {
137 137
 			$subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user);
@@ -139,7 +139,7 @@  discard block
 block discarded – undo
139 139
 				$subAdminOfGroups[$key] = $group->getGID();
140 140
 			}
141 141
 
142
-			if($offset === null) {
142
+			if ($offset === null) {
143 143
 				$offset = 0;
144 144
 			}
145 145
 
@@ -176,7 +176,7 @@  discard block
 block discarded – undo
176 176
 		// Admin? Or SubAdmin?
177 177
 		$uid = $user->getUID();
178 178
 		$subAdminManager = $this->groupManager->getSubAdmin();
179
-		if($this->groupManager->isAdmin($uid)){
179
+		if ($this->groupManager->isAdmin($uid)) {
180 180
 			$users = $this->userManager->search($search, $limit, $offset);
181 181
 		} else if ($subAdminManager->isSubAdmin($user)) {
182 182
 			$subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user);
@@ -184,7 +184,7 @@  discard block
 block discarded – undo
184 184
 				$subAdminOfGroups[$key] = $group->getGID();
185 185
 			}
186 186
 
187
-			if($offset === null) {
187
+			if ($offset === null) {
188 188
 				$offset = 0;
189 189
 			}
190 190
 
@@ -222,37 +222,37 @@  discard block
 block discarded – undo
222 222
 		$isAdmin = $this->groupManager->isAdmin($user->getUID());
223 223
 		$subAdminManager = $this->groupManager->getSubAdmin();
224 224
 
225
-		if($this->userManager->userExists($userid)) {
225
+		if ($this->userManager->userExists($userid)) {
226 226
 			$this->logger->error('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']);
227 227
 			throw new OCSException('User already exists', 102);
228 228
 		}
229 229
 
230
-		if($groups !== []) {
230
+		if ($groups !== []) {
231 231
 			foreach ($groups as $group) {
232
-				if(!$this->groupManager->groupExists($group)) {
232
+				if (!$this->groupManager->groupExists($group)) {
233 233
 					throw new OCSException('group '.$group.' does not exist', 104);
234 234
 				}
235
-				if(!$isAdmin && !$subAdminManager->isSubAdminofGroup($user, $this->groupManager->get($group))) {
236
-					throw new OCSException('insufficient privileges for group '. $group, 105);
235
+				if (!$isAdmin && !$subAdminManager->isSubAdminofGroup($user, $this->groupManager->get($group))) {
236
+					throw new OCSException('insufficient privileges for group '.$group, 105);
237 237
 				}
238 238
 			}
239 239
 		} else {
240
-			if(!$isAdmin) {
240
+			if (!$isAdmin) {
241 241
 				throw new OCSException('no group specified (required for subadmins)', 106);
242 242
 			}
243 243
 		}
244 244
 
245 245
 		try {
246 246
 			$newUser = $this->userManager->createUser($userid, $password);
247
-			$this->logger->info('Successful addUser call with userid: ' . $userid, ['app' => 'ocs_api']);
247
+			$this->logger->info('Successful addUser call with userid: '.$userid, ['app' => 'ocs_api']);
248 248
 
249 249
 			foreach ($groups as $group) {
250 250
 				$this->groupManager->get($group)->addUser($newUser);
251
-				$this->logger->info('Added userid ' . $userid . ' to group ' . $group, ['app' => 'ocs_api']);
251
+				$this->logger->info('Added userid '.$userid.' to group '.$group, ['app' => 'ocs_api']);
252 252
 			}
253 253
 
254 254
 			return new DataResponse();
255
-		} catch (HintException $e ) {
255
+		} catch (HintException $e) {
256 256
 			$this->logger->logException($e, [
257 257
 				'message' => 'Failed addUser attempt with hint exception.',
258 258
 				'level' => \OCP\Util::WARN,
@@ -296,7 +296,7 @@  discard block
 block discarded – undo
296 296
 	public function getCurrentUser(): DataResponse {
297 297
 		$user = $this->userSession->getUser();
298 298
 		if ($user) {
299
-			$data =  $this->getUserData($user->getUID());
299
+			$data = $this->getUserData($user->getUID());
300 300
 			// rename "displayname" to "display-name" only for this call to keep
301 301
 			// the API stable.
302 302
 			$data['display-name'] = $data['displayname'];
@@ -322,19 +322,19 @@  discard block
 block discarded – undo
322 322
 
323 323
 		// Check if the target user exists
324 324
 		$targetUserObject = $this->userManager->get($userId);
325
-		if($targetUserObject === null) {
325
+		if ($targetUserObject === null) {
326 326
 			throw new OCSException('The requested user could not be found', \OCP\API::RESPOND_NOT_FOUND);
327 327
 		}
328 328
 
329 329
 		// Admin? Or SubAdmin?
330
-		if($this->groupManager->isAdmin($currentLoggedInUser->getUID())
330
+		if ($this->groupManager->isAdmin($currentLoggedInUser->getUID())
331 331
 			|| $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) {
332 332
 
333 333
 			$data['enabled'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'enabled', 'true');
334 334
 
335 335
 		} else {
336 336
 			// Check they are looking up themselves
337
-			if($currentLoggedInUser->getUID() !== $targetUserObject->getUID() && $throw) {
337
+			if ($currentLoggedInUser->getUID() !== $targetUserObject->getUID() && $throw) {
338 338
 				throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
339 339
 			} else {
340 340
 				return $data;
@@ -412,12 +412,12 @@  discard block
 block discarded – undo
412 412
 		$currentLoggedInUser = $this->userSession->getUser();
413 413
 
414 414
 		$targetUser = $this->userManager->get($userId);
415
-		if($targetUser === null) {
415
+		if ($targetUser === null) {
416 416
 			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
417 417
 		}
418 418
 
419 419
 		$permittedFields = [];
420
-		if($targetUser->getUID() === $currentLoggedInUser->getUID()) {
420
+		if ($targetUser->getUID() === $currentLoggedInUser->getUID()) {
421 421
 			// Editing self (display, email)
422 422
 			if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) {
423 423
 				$permittedFields[] = 'display';
@@ -443,13 +443,13 @@  discard block
 block discarded – undo
443 443
 			}
444 444
 
445 445
 			// If admin they can edit their own quota
446
-			if($this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
446
+			if ($this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
447 447
 				$permittedFields[] = 'quota';
448 448
 			}
449 449
 		} else {
450 450
 			// Check if admin / subadmin
451 451
 			$subAdminManager = $this->groupManager->getSubAdmin();
452
-			if($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
452
+			if ($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
453 453
 			|| $this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
454 454
 				// They have permissions over the user
455 455
 				$permittedFields[] = 'display';
@@ -468,18 +468,18 @@  discard block
 block discarded – undo
468 468
 			}
469 469
 		}
470 470
 		// Check if permitted to edit this field
471
-		if(!in_array($key, $permittedFields)) {
471
+		if (!in_array($key, $permittedFields)) {
472 472
 			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
473 473
 		}
474 474
 		// Process the edit
475
-		switch($key) {
475
+		switch ($key) {
476 476
 			case 'display':
477 477
 			case AccountManager::PROPERTY_DISPLAYNAME:
478 478
 				$targetUser->setDisplayName($value);
479 479
 				break;
480 480
 			case 'quota':
481 481
 				$quota = $value;
482
-				if($quota !== 'none' && $quota !== 'default') {
482
+				if ($quota !== 'none' && $quota !== 'default') {
483 483
 					if (is_numeric($quota)) {
484 484
 						$quota = (float) $quota;
485 485
 					} else {
@@ -488,9 +488,9 @@  discard block
 block discarded – undo
488 488
 					if ($quota === false) {
489 489
 						throw new OCSException('Invalid quota value '.$value, 103);
490 490
 					}
491
-					if($quota === 0) {
491
+					if ($quota === 0) {
492 492
 						$quota = 'default';
493
-					}else if($quota === -1) {
493
+					} else if ($quota === -1) {
494 494
 						$quota = 'none';
495 495
 					} else {
496 496
 						$quota = \OCP\Util::humanFileSize($quota);
@@ -509,7 +509,7 @@  discard block
 block discarded – undo
509 509
 				$this->config->setUserValue($targetUser->getUID(), 'core', 'lang', $value);
510 510
 				break;
511 511
 			case AccountManager::PROPERTY_EMAIL:
512
-				if(filter_var($value, FILTER_VALIDATE_EMAIL)) {
512
+				if (filter_var($value, FILTER_VALIDATE_EMAIL)) {
513 513
 					$targetUser->setEMailAddress($value);
514 514
 				} else {
515 515
 					throw new OCSException('', 102);
@@ -544,18 +544,18 @@  discard block
 block discarded – undo
544 544
 
545 545
 		$targetUser = $this->userManager->get($userId);
546 546
 
547
-		if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
547
+		if ($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
548 548
 			throw new OCSException('', 101);
549 549
 		}
550 550
 
551 551
 		// If not permitted
552 552
 		$subAdminManager = $this->groupManager->getSubAdmin();
553
-		if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
553
+		if (!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
554 554
 			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
555 555
 		}
556 556
 
557 557
 		// Go ahead with the delete
558
-		if($targetUser->delete()) {
558
+		if ($targetUser->delete()) {
559 559
 			return new DataResponse();
560 560
 		} else {
561 561
 			throw new OCSException('', 101);
@@ -598,13 +598,13 @@  discard block
 block discarded – undo
598 598
 		$currentLoggedInUser = $this->userSession->getUser();
599 599
 
600 600
 		$targetUser = $this->userManager->get($userId);
601
-		if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
601
+		if ($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
602 602
 			throw new OCSException('', 101);
603 603
 		}
604 604
 
605 605
 		// If not permitted
606 606
 		$subAdminManager = $this->groupManager->getSubAdmin();
607
-		if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
607
+		if (!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
608 608
 			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
609 609
 		}
610 610
 
@@ -625,11 +625,11 @@  discard block
 block discarded – undo
625 625
 		$loggedInUser = $this->userSession->getUser();
626 626
 
627 627
 		$targetUser = $this->userManager->get($userId);
628
-		if($targetUser === null) {
628
+		if ($targetUser === null) {
629 629
 			throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND);
630 630
 		}
631 631
 
632
-		if($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) {
632
+		if ($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) {
633 633
 			// Self lookup or admin lookup
634 634
 			return new DataResponse([
635 635
 				'groups' => $this->groupManager->getUserGroupIds($targetUser)
@@ -638,7 +638,7 @@  discard block
 block discarded – undo
638 638
 			$subAdminManager = $this->groupManager->getSubAdmin();
639 639
 
640 640
 			// Looking up someone else
641
-			if($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) {
641
+			if ($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) {
642 642
 				// Return the group that the method caller is subadmin of for the user in question
643 643
 				/** @var IGroup[] $getSubAdminsGroups */
644 644
 				$getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);
@@ -668,16 +668,16 @@  discard block
 block discarded – undo
668 668
 	 * @throws OCSException
669 669
 	 */
670 670
 	public function addToGroup(string $userId, string $groupid = ''): DataResponse {
671
-		if($groupid === '') {
671
+		if ($groupid === '') {
672 672
 			throw new OCSException('', 101);
673 673
 		}
674 674
 
675 675
 		$group = $this->groupManager->get($groupid);
676 676
 		$targetUser = $this->userManager->get($userId);
677
-		if($group === null) {
677
+		if ($group === null) {
678 678
 			throw new OCSException('', 102);
679 679
 		}
680
-		if($targetUser === null) {
680
+		if ($targetUser === null) {
681 681
 			throw new OCSException('', 103);
682 682
 		}
683 683
 
@@ -705,17 +705,17 @@  discard block
 block discarded – undo
705 705
 	public function removeFromGroup(string $userId, string $groupid): DataResponse {
706 706
 		$loggedInUser = $this->userSession->getUser();
707 707
 
708
-		if($groupid === null || trim($groupid) === '') {
708
+		if ($groupid === null || trim($groupid) === '') {
709 709
 			throw new OCSException('', 101);
710 710
 		}
711 711
 
712 712
 		$group = $this->groupManager->get($groupid);
713
-		if($group === null) {
713
+		if ($group === null) {
714 714
 			throw new OCSException('', 102);
715 715
 		}
716 716
 
717 717
 		$targetUser = $this->userManager->get($userId);
718
-		if($targetUser === null) {
718
+		if ($targetUser === null) {
719 719
 			throw new OCSException('', 103);
720 720
 		}
721 721
 
@@ -739,7 +739,7 @@  discard block
 block discarded – undo
739 739
 		} else if (!$this->groupManager->isAdmin($loggedInUser->getUID())) {
740 740
 			/** @var IGroup[] $subAdminGroups */
741 741
 			$subAdminGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);
742
-			$subAdminGroups = array_map(function (IGroup $subAdminGroup) {
742
+			$subAdminGroups = array_map(function(IGroup $subAdminGroup) {
743 743
 				return $subAdminGroup->getGID();
744 744
 			}, $subAdminGroups);
745 745
 			$userGroups = $this->groupManager->getUserGroupIds($targetUser);
@@ -771,15 +771,15 @@  discard block
 block discarded – undo
771 771
 		$user = $this->userManager->get($userId);
772 772
 
773 773
 		// Check if the user exists
774
-		if($user === null) {
774
+		if ($user === null) {
775 775
 			throw new OCSException('User does not exist', 101);
776 776
 		}
777 777
 		// Check if group exists
778
-		if($group === null) {
779
-			throw new OCSException('Group does not exist',  102);
778
+		if ($group === null) {
779
+			throw new OCSException('Group does not exist', 102);
780 780
 		}
781 781
 		// Check if trying to make subadmin of admin group
782
-		if($group->getGID() === 'admin') {
782
+		if ($group->getGID() === 'admin') {
783 783
 			throw new OCSException('Cannot create subadmins for admin group', 103);
784 784
 		}
785 785
 
@@ -790,7 +790,7 @@  discard block
 block discarded – undo
790 790
 			return new DataResponse();
791 791
 		}
792 792
 		// Go
793
-		if($subAdminManager->createSubAdmin($user, $group)) {
793
+		if ($subAdminManager->createSubAdmin($user, $group)) {
794 794
 			return new DataResponse();
795 795
 		} else {
796 796
 			throw new OCSException('Unknown error occurred', 103);
@@ -813,20 +813,20 @@  discard block
 block discarded – undo
813 813
 		$subAdminManager = $this->groupManager->getSubAdmin();
814 814
 
815 815
 		// Check if the user exists
816
-		if($user === null) {
816
+		if ($user === null) {
817 817
 			throw new OCSException('User does not exist', 101);
818 818
 		}
819 819
 		// Check if the group exists
820
-		if($group === null) {
820
+		if ($group === null) {
821 821
 			throw new OCSException('Group does not exist', 101);
822 822
 		}
823 823
 		// Check if they are a subadmin of this said group
824
-		if(!$subAdminManager->isSubAdminOfGroup($user, $group)) {
824
+		if (!$subAdminManager->isSubAdminOfGroup($user, $group)) {
825 825
 			throw new OCSException('User is not a subadmin of this group', 102);
826 826
 		}
827 827
 
828 828
 		// Go
829
-		if($subAdminManager->deleteSubAdmin($user, $group)) {
829
+		if ($subAdminManager->deleteSubAdmin($user, $group)) {
830 830
 			return new DataResponse();
831 831
 		} else {
832 832
 			throw new OCSException('Unknown error occurred', 103);
@@ -843,7 +843,7 @@  discard block
 block discarded – undo
843 843
 	protected function getUserSubAdminGroupsData(string $userId): array {
844 844
 		$user = $this->userManager->get($userId);
845 845
 		// Check if the user exists
846
-		if($user === null) {
846
+		if ($user === null) {
847 847
 			throw new OCSException('User does not exist', 101);
848 848
 		}
849 849
 
@@ -867,7 +867,7 @@  discard block
 block discarded – undo
867 867
 	public function getUserSubAdminGroups(string $userId): DataResponse {
868 868
 		$groups = $this->getUserSubAdminGroupsData($userId);
869 869
 
870
-		if(!$groups) {
870
+		if (!$groups) {
871 871
 			throw new OCSException('Unknown error occurred', 102);
872 872
 		} else {
873 873
 			return $groups;
@@ -911,13 +911,13 @@  discard block
 block discarded – undo
911 911
 		$currentLoggedInUser = $this->userSession->getUser();
912 912
 
913 913
 		$targetUser = $this->userManager->get($userId);
914
-		if($targetUser === null) {
914
+		if ($targetUser === null) {
915 915
 			throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND);
916 916
 		}
917 917
 
918 918
 		// Check if admin / subadmin
919 919
 		$subAdminManager = $this->groupManager->getSubAdmin();
920
-		if(!$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
920
+		if (!$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
921 921
 			&& !$this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
922 922
 			// No rights
923 923
 			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
@@ -939,7 +939,7 @@  discard block
 block discarded – undo
939 939
 			$this->newUserMailHelper->setL10N($l10n);
940 940
 			$emailTemplate = $this->newUserMailHelper->generateTemplate($targetUser, false);
941 941
 			$this->newUserMailHelper->sendMail($targetUser, $emailTemplate);
942
-		} catch(\Exception $e) {
942
+		} catch (\Exception $e) {
943 943
 			$this->logger->logException($e, [
944 944
 				'message' => "Can't send new user mail to $email",
945 945
 				'level' => \OCP\Util::ERROR,
Please login to merge, or discard this patch.