Completed
Push — master ( 9e21a5...a2e16f )
by Lukas
23:37 queued 10:05
created
apps/provisioning_api/lib/Controller/UsersController.php 1 patch
Indentation   +672 added lines, -672 removed lines patch added patch discarded remove patch
@@ -46,676 +46,676 @@
 block discarded – undo
46 46
 
47 47
 class UsersController extends OCSController {
48 48
 
49
-	/** @var IUserManager */
50
-	private $userManager;
51
-	/** @var IConfig */
52
-	private $config;
53
-	/** @var IGroupManager|\OC\Group\Manager */ // FIXME Requires a method that is not on the interface
54
-	private $groupManager;
55
-	/** @var IUserSession */
56
-	private $userSession;
57
-	/** @var AccountManager */
58
-	private $accountManager;
59
-	/** @var ILogger */
60
-	private $logger;
61
-
62
-	/**
63
-	 * @param string $appName
64
-	 * @param IRequest $request
65
-	 * @param IUserManager $userManager
66
-	 * @param IConfig $config
67
-	 * @param IGroupManager $groupManager
68
-	 * @param IUserSession $userSession
69
-	 * @param AccountManager $accountManager
70
-	 * @param ILogger $logger
71
-	 */
72
-	public function __construct($appName,
73
-								IRequest $request,
74
-								IUserManager $userManager,
75
-								IConfig $config,
76
-								IGroupManager $groupManager,
77
-								IUserSession $userSession,
78
-								AccountManager $accountManager,
79
-								ILogger $logger) {
80
-		parent::__construct($appName, $request);
81
-
82
-		$this->userManager = $userManager;
83
-		$this->config = $config;
84
-		$this->groupManager = $groupManager;
85
-		$this->userSession = $userSession;
86
-		$this->accountManager = $accountManager;
87
-		$this->logger = $logger;
88
-	}
89
-
90
-	/**
91
-	 * @NoAdminRequired
92
-	 *
93
-	 * returns a list of users
94
-	 *
95
-	 * @param string $search
96
-	 * @param int $limit
97
-	 * @param int $offset
98
-	 * @return DataResponse
99
-	 */
100
-	public function getUsers($search = '', $limit = null, $offset = null) {
101
-		$user = $this->userSession->getUser();
102
-		$users = [];
103
-
104
-		// Admin? Or SubAdmin?
105
-		$uid = $user->getUID();
106
-		$subAdminManager = $this->groupManager->getSubAdmin();
107
-		if($this->groupManager->isAdmin($uid)){
108
-			$users = $this->userManager->search($search, $limit, $offset);
109
-		} else if ($subAdminManager->isSubAdmin($user)) {
110
-			$subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user);
111
-			foreach ($subAdminOfGroups as $key => $group) {
112
-				$subAdminOfGroups[$key] = $group->getGID();
113
-			}
114
-
115
-			if($offset === null) {
116
-				$offset = 0;
117
-			}
118
-
119
-			$users = [];
120
-			foreach ($subAdminOfGroups as $group) {
121
-				$users = array_merge($users, $this->groupManager->displayNamesInGroup($group, $search));
122
-			}
123
-
124
-			$users = array_slice($users, $offset, $limit);
125
-		}
126
-
127
-		$users = array_keys($users);
128
-
129
-		return new DataResponse([
130
-			'users' => $users
131
-		]);
132
-	}
133
-
134
-	/**
135
-	 * @PasswordConfirmationRequired
136
-	 * @NoAdminRequired
137
-	 *
138
-	 * @param string $userid
139
-	 * @param string $password
140
-	 * @param array $groups
141
-	 * @return DataResponse
142
-	 * @throws OCSException
143
-	 */
144
-	public function addUser($userid, $password, $groups = null) {
145
-		$user = $this->userSession->getUser();
146
-		$isAdmin = $this->groupManager->isAdmin($user->getUID());
147
-		$subAdminManager = $this->groupManager->getSubAdmin();
148
-
149
-		if($this->userManager->userExists($userid)) {
150
-			$this->logger->error('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']);
151
-			throw new OCSException('User already exists', 102);
152
-		}
153
-
154
-		if(is_array($groups)) {
155
-			foreach ($groups as $group) {
156
-				if(!$this->groupManager->groupExists($group)) {
157
-					throw new OCSException('group '.$group.' does not exist', 104);
158
-				}
159
-				if(!$isAdmin && !$subAdminManager->isSubAdminofGroup($user, $this->groupManager->get($group))) {
160
-					throw new OCSException('insufficient privileges for group '. $group, 105);
161
-				}
162
-			}
163
-		} else {
164
-			if(!$isAdmin) {
165
-				throw new OCSException('no group specified (required for subadmins)', 106);
166
-			}
167
-		}
168
-
169
-		try {
170
-			$newUser = $this->userManager->createUser($userid, $password);
171
-			$this->logger->info('Successful addUser call with userid: '.$userid, ['app' => 'ocs_api']);
172
-
173
-			if (is_array($groups)) {
174
-				foreach ($groups as $group) {
175
-					$this->groupManager->get($group)->addUser($newUser);
176
-					$this->logger->info('Added userid '.$userid.' to group '.$group, ['app' => 'ocs_api']);
177
-				}
178
-			}
179
-			return new DataResponse();
180
-		} catch (\Exception $e) {
181
-			$this->logger->error('Failed addUser attempt with exception: '.$e->getMessage(), ['app' => 'ocs_api']);
182
-			throw new OCSException('Bad request', 101);
183
-		}
184
-	}
185
-
186
-	/**
187
-	 * @NoAdminRequired
188
-	 * @NoSubAdminRequired
189
-	 *
190
-	 * gets user info
191
-	 *
192
-	 * @param string $userId
193
-	 * @return DataResponse
194
-	 * @throws OCSException
195
-	 */
196
-	public function getUser($userId) {
197
-		$data = $this->getUserData($userId);
198
-		return new DataResponse($data);
199
-	}
200
-
201
-	/**
202
-	 * @NoAdminRequired
203
-	 * @NoSubAdminRequired
204
-	 *
205
-	 * gets user info from the currently logged in user
206
-	 *
207
-	 * @return DataResponse
208
-	 * @throws OCSException
209
-	 */
210
-	public function getCurrentUser() {
211
-		$user = $this->userSession->getUser();
212
-		if ($user) {
213
-			$data =  $this->getUserData($user->getUID());
214
-			// rename "displayname" to "display-name" only for this call to keep
215
-			// the API stable.
216
-			$data['display-name'] = $data['displayname'];
217
-			unset($data['displayname']);
218
-			return new DataResponse($data);
219
-
220
-		}
221
-
222
-		throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
223
-	}
224
-
225
-	/**
226
-	 * creates a array with all user data
227
-	 *
228
-	 * @param $userId
229
-	 * @return array
230
-	 * @throws OCSException
231
-	 */
232
-	protected function getUserData($userId) {
233
-		$currentLoggedInUser = $this->userSession->getUser();
234
-
235
-		$data = [];
236
-
237
-		// Check if the target user exists
238
-		$targetUserObject = $this->userManager->get($userId);
239
-		if($targetUserObject === null) {
240
-			throw new OCSException('The requested user could not be found', \OCP\API::RESPOND_NOT_FOUND);
241
-		}
242
-
243
-		// Admin? Or SubAdmin?
244
-		if($this->groupManager->isAdmin($currentLoggedInUser->getUID())
245
-			|| $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) {
246
-			$data['enabled'] = $this->config->getUserValue($userId, 'core', 'enabled', 'true');
247
-		} else {
248
-			// Check they are looking up themselves
249
-			if($currentLoggedInUser->getUID() !== $userId) {
250
-				throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
251
-			}
252
-		}
253
-
254
-		$userAccount = $this->accountManager->getUser($targetUserObject);
255
-		$groups = $this->groupManager->getUserGroups($targetUserObject);
256
-		$gids = [];
257
-		foreach ($groups as $group) {
258
-			$gids[] = $group->getDisplayName();
259
-		}
260
-
261
-		// Find the data
262
-		$data['id'] = $targetUserObject->getUID();
263
-		$data['quota'] = $this->fillStorageInfo($userId);
264
-		$data['email'] = $targetUserObject->getEMailAddress();
265
-		$data['displayname'] = $targetUserObject->getDisplayName();
266
-		$data['phone'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_PHONE]['value'];
267
-		$data['address'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_ADDRESS]['value'];
268
-		$data['webpage'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_WEBSITE]['value'];
269
-		$data['twitter'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_TWITTER]['value'];
270
-		$data['groups'] = $gids;
271
-
272
-		return $data;
273
-	}
274
-
275
-	/**
276
-	 * @NoAdminRequired
277
-	 * @NoSubAdminRequired
278
-	 * @PasswordConfirmationRequired
279
-	 *
280
-	 * edit users
281
-	 *
282
-	 * @param string $userId
283
-	 * @param string $key
284
-	 * @param string $value
285
-	 * @return DataResponse
286
-	 * @throws OCSException
287
-	 * @throws OCSForbiddenException
288
-	 */
289
-	public function editUser($userId, $key, $value) {
290
-		$currentLoggedInUser = $this->userSession->getUser();
291
-
292
-		$targetUser = $this->userManager->get($userId);
293
-		if($targetUser === null) {
294
-			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
295
-		}
296
-
297
-		$permittedFields = [];
298
-		if($userId === $currentLoggedInUser->getUID()) {
299
-			// Editing self (display, email)
300
-			$permittedFields[] = 'display';
301
-			$permittedFields[] = 'email';
302
-			$permittedFields[] = 'password';
303
-			// If admin they can edit their own quota
304
-			if($this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
305
-				$permittedFields[] = 'quota';
306
-			}
307
-		} else {
308
-			// Check if admin / subadmin
309
-			$subAdminManager = $this->groupManager->getSubAdmin();
310
-			if($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
311
-			|| $this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
312
-				// They have permissions over the user
313
-				$permittedFields[] = 'display';
314
-				$permittedFields[] = 'quota';
315
-				$permittedFields[] = 'password';
316
-				$permittedFields[] = 'email';
317
-			} else {
318
-				// No rights
319
-				throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
320
-			}
321
-		}
322
-		// Check if permitted to edit this field
323
-		if(!in_array($key, $permittedFields)) {
324
-			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
325
-		}
326
-		// Process the edit
327
-		switch($key) {
328
-			case 'display':
329
-				$targetUser->setDisplayName($value);
330
-				break;
331
-			case 'quota':
332
-				$quota = $value;
333
-				if($quota !== 'none' && $quota !== 'default') {
334
-					if (is_numeric($quota)) {
335
-						$quota = (float) $quota;
336
-					} else {
337
-						$quota = \OCP\Util::computerFileSize($quota);
338
-					}
339
-					if ($quota === false) {
340
-						throw new OCSException('Invalid quota value '.$value, 103);
341
-					}
342
-					if($quota === 0) {
343
-						$quota = 'default';
344
-					}else if($quota === -1) {
345
-						$quota = 'none';
346
-					} else {
347
-						$quota = \OCP\Util::humanFileSize($quota);
348
-					}
349
-				}
350
-				$targetUser->setQuota($quota);
351
-				break;
352
-			case 'password':
353
-				$targetUser->setPassword($value);
354
-				break;
355
-			case 'email':
356
-				if(filter_var($value, FILTER_VALIDATE_EMAIL)) {
357
-					$targetUser->setEMailAddress($value);
358
-				} else {
359
-					throw new OCSException('', 102);
360
-				}
361
-				break;
362
-			default:
363
-				throw new OCSException('', 103);
364
-		}
365
-		return new DataResponse();
366
-	}
367
-
368
-	/**
369
-	 * @PasswordConfirmationRequired
370
-	 * @NoAdminRequired
371
-	 *
372
-	 * @param string $userId
373
-	 * @return DataResponse
374
-	 * @throws OCSException
375
-	 * @throws OCSForbiddenException
376
-	 */
377
-	public function deleteUser($userId) {
378
-		$currentLoggedInUser = $this->userSession->getUser();
379
-
380
-		$targetUser = $this->userManager->get($userId);
381
-
382
-		if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
383
-			throw new OCSException('', 101);
384
-		}
385
-
386
-		// If not permitted
387
-		$subAdminManager = $this->groupManager->getSubAdmin();
388
-		if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
389
-			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
390
-		}
391
-
392
-		// Go ahead with the delete
393
-		if($targetUser->delete()) {
394
-			return new DataResponse();
395
-		} else {
396
-			throw new OCSException('', 101);
397
-		}
398
-	}
399
-
400
-	/**
401
-	 * @PasswordConfirmationRequired
402
-	 * @NoAdminRequired
403
-	 *
404
-	 * @param string $userId
405
-	 * @return DataResponse
406
-	 * @throws OCSException
407
-	 * @throws OCSForbiddenException
408
-	 */
409
-	public function disableUser($userId) {
410
-		return $this->setEnabled($userId, false);
411
-	}
412
-
413
-	/**
414
-	 * @PasswordConfirmationRequired
415
-	 * @NoAdminRequired
416
-	 *
417
-	 * @param string $userId
418
-	 * @return DataResponse
419
-	 * @throws OCSException
420
-	 * @throws OCSForbiddenException
421
-	 */
422
-	public function enableUser($userId) {
423
-		return $this->setEnabled($userId, true);
424
-	}
425
-
426
-	/**
427
-	 * @param string $userId
428
-	 * @param bool $value
429
-	 * @return DataResponse
430
-	 * @throws OCSException
431
-	 * @throws OCSForbiddenException
432
-	 */
433
-	private function setEnabled($userId, $value) {
434
-		$currentLoggedInUser = $this->userSession->getUser();
435
-
436
-		$targetUser = $this->userManager->get($userId);
437
-		if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
438
-			throw new OCSException('', 101);
439
-		}
440
-
441
-		// If not permitted
442
-		$subAdminManager = $this->groupManager->getSubAdmin();
443
-		if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
444
-			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
445
-		}
446
-
447
-		// enable/disable the user now
448
-		$targetUser->setEnabled($value);
449
-		return new DataResponse();
450
-	}
451
-
452
-	/**
453
-	 * @NoAdminRequired
454
-	 * @NoSubAdminRequired
455
-	 *
456
-	 * @param string $userId
457
-	 * @return DataResponse
458
-	 * @throws OCSException
459
-	 */
460
-	public function getUsersGroups($userId) {
461
-		$loggedInUser = $this->userSession->getUser();
462
-
463
-		$targetUser = $this->userManager->get($userId);
464
-		if($targetUser === null) {
465
-			throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND);
466
-		}
467
-
468
-		if($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) {
469
-			// Self lookup or admin lookup
470
-			return new DataResponse([
471
-				'groups' => $this->groupManager->getUserGroupIds($targetUser)
472
-			]);
473
-		} else {
474
-			$subAdminManager = $this->groupManager->getSubAdmin();
475
-
476
-			// Looking up someone else
477
-			if($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) {
478
-				// Return the group that the method caller is subadmin of for the user in question
479
-				/** @var IGroup[] $getSubAdminsGroups */
480
-				$getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);
481
-				foreach ($getSubAdminsGroups as $key => $group) {
482
-					$getSubAdminsGroups[$key] = $group->getGID();
483
-				}
484
-				$groups = array_intersect(
485
-					$getSubAdminsGroups,
486
-					$this->groupManager->getUserGroupIds($targetUser)
487
-				);
488
-				return new DataResponse(['groups' => $groups]);
489
-			} else {
490
-				// Not permitted
491
-				throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
492
-			}
493
-		}
494
-
495
-	}
496
-
497
-	/**
498
-	 * @PasswordConfirmationRequired
499
-	 * @NoAdminRequired
500
-	 *
501
-	 * @param string $userId
502
-	 * @param string $groupid
503
-	 * @return DataResponse
504
-	 * @throws OCSException
505
-	 */
506
-	public function addToGroup($userId, $groupid = '') {
507
-		if($groupid === '') {
508
-			throw new OCSException('', 101);
509
-		}
510
-
511
-		$group = $this->groupManager->get($groupid);
512
-		$targetUser = $this->userManager->get($userId);
513
-		if($group === null) {
514
-			throw new OCSException('', 102);
515
-		}
516
-		if($targetUser === null) {
517
-			throw new OCSException('', 103);
518
-		}
519
-
520
-		// If they're not an admin, check they are a subadmin of the group in question
521
-		$loggedInUser = $this->userSession->getUser();
522
-		$subAdminManager = $this->groupManager->getSubAdmin();
523
-		if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) {
524
-			throw new OCSException('', 104);
525
-		}
526
-
527
-		// Add user to group
528
-		$group->addUser($targetUser);
529
-		return new DataResponse();
530
-	}
531
-
532
-	/**
533
-	 * @PasswordConfirmationRequired
534
-	 * @NoAdminRequired
535
-	 *
536
-	 * @param string $userId
537
-	 * @param string $groupid
538
-	 * @return DataResponse
539
-	 * @throws OCSException
540
-	 */
541
-	public function removeFromGroup($userId, $groupid) {
542
-		$loggedInUser = $this->userSession->getUser();
543
-
544
-		if($groupid === null) {
545
-			throw new OCSException('', 101);
546
-		}
547
-
548
-		$group = $this->groupManager->get($groupid);
549
-		if($group === null) {
550
-			throw new OCSException('', 102);
551
-		}
552
-
553
-		$targetUser = $this->userManager->get($userId);
554
-		if($targetUser === null) {
555
-			throw new OCSException('', 103);
556
-		}
557
-
558
-		// If they're not an admin, check they are a subadmin of the group in question
559
-		$subAdminManager = $this->groupManager->getSubAdmin();
560
-		if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) {
561
-			throw new OCSException('', 104);
562
-		}
563
-
564
-		// Check they aren't removing themselves from 'admin' or their 'subadmin; group
565
-		if ($userId === $loggedInUser->getUID()) {
566
-			if ($this->groupManager->isAdmin($loggedInUser->getUID())) {
567
-				if ($group->getGID() === 'admin') {
568
-					throw new OCSException('Cannot remove yourself from the admin group', 105);
569
-				}
570
-			} else {
571
-				// Not an admin, so the user must be a subadmin of this group, but that is not allowed.
572
-				throw new OCSException('Cannot remove yourself from this group as you are a SubAdmin', 105);
573
-			}
574
-
575
-		} else if (!$this->groupManager->isAdmin($loggedInUser->getUID())) {
576
-			/** @var IGroup[] $subAdminGroups */
577
-			$subAdminGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);
578
-			$subAdminGroups = array_map(function (IGroup $subAdminGroup) {
579
-				return $subAdminGroup->getGID();
580
-			}, $subAdminGroups);
581
-			$userGroups = $this->groupManager->getUserGroupIds($targetUser);
582
-			$userSubAdminGroups = array_intersect($subAdminGroups, $userGroups);
583
-
584
-			if (count($userSubAdminGroups) <= 1) {
585
-				// Subadmin must not be able to remove a user from all their subadmin groups.
586
-				throw new OCSException('Cannot remove user from this group as this is the only remaining group you are a SubAdmin of', 105);
587
-			}
588
-		}
589
-
590
-		// Remove user from group
591
-		$group->removeUser($targetUser);
592
-		return new DataResponse();
593
-	}
594
-
595
-	/**
596
-	 * Creates a subadmin
597
-	 *
598
-	 * @PasswordConfirmationRequired
599
-	 *
600
-	 * @param string $userId
601
-	 * @param string $groupid
602
-	 * @return DataResponse
603
-	 * @throws OCSException
604
-	 */
605
-	public function addSubAdmin($userId, $groupid) {
606
-		$group = $this->groupManager->get($groupid);
607
-		$user = $this->userManager->get($userId);
608
-
609
-		// Check if the user exists
610
-		if($user === null) {
611
-			throw new OCSException('User does not exist', 101);
612
-		}
613
-		// Check if group exists
614
-		if($group === null) {
615
-			throw new OCSException('Group:'.$groupid.' does not exist',  102);
616
-		}
617
-		// Check if trying to make subadmin of admin group
618
-		if(strtolower($groupid) === 'admin') {
619
-			throw new OCSException('Cannot create subadmins for admin group', 103);
620
-		}
621
-
622
-		$subAdminManager = $this->groupManager->getSubAdmin();
623
-
624
-		// We cannot be subadmin twice
625
-		if ($subAdminManager->isSubAdminofGroup($user, $group)) {
626
-			return new DataResponse();
627
-		}
628
-		// Go
629
-		if($subAdminManager->createSubAdmin($user, $group)) {
630
-			return new DataResponse();
631
-		} else {
632
-			throw new OCSException('Unknown error occurred', 103);
633
-		}
634
-	}
635
-
636
-	/**
637
-	 * Removes a subadmin from a group
638
-	 *
639
-	 * @PasswordConfirmationRequired
640
-	 *
641
-	 * @param string $userId
642
-	 * @param string $groupid
643
-	 * @return DataResponse
644
-	 * @throws OCSException
645
-	 */
646
-	public function removeSubAdmin($userId, $groupid) {
647
-		$group = $this->groupManager->get($groupid);
648
-		$user = $this->userManager->get($userId);
649
-		$subAdminManager = $this->groupManager->getSubAdmin();
650
-
651
-		// Check if the user exists
652
-		if($user === null) {
653
-			throw new OCSException('User does not exist', 101);
654
-		}
655
-		// Check if the group exists
656
-		if($group === null) {
657
-			throw new OCSException('Group does not exist', 101);
658
-		}
659
-		// Check if they are a subadmin of this said group
660
-		if(!$subAdminManager->isSubAdminofGroup($user, $group)) {
661
-			throw new OCSException('User is not a subadmin of this group', 102);
662
-		}
663
-
664
-		// Go
665
-		if($subAdminManager->deleteSubAdmin($user, $group)) {
666
-			return new DataResponse();
667
-		} else {
668
-			throw new OCSException('Unknown error occurred', 103);
669
-		}
670
-	}
671
-
672
-	/**
673
-	 * Get the groups a user is a subadmin of
674
-	 *
675
-	 * @param string $userId
676
-	 * @return DataResponse
677
-	 * @throws OCSException
678
-	 */
679
-	public function getUserSubAdminGroups($userId) {
680
-		$user = $this->userManager->get($userId);
681
-		// Check if the user exists
682
-		if($user === null) {
683
-			throw new OCSException('User does not exist', 101);
684
-		}
685
-
686
-		// Get the subadmin groups
687
-		$groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user);
688
-		foreach ($groups as $key => $group) {
689
-			$groups[$key] = $group->getGID();
690
-		}
691
-
692
-		if(!$groups) {
693
-			throw new OCSException('Unknown error occurred', 102);
694
-		} else {
695
-			return new DataResponse($groups);
696
-		}
697
-	}
698
-
699
-	/**
700
-	 * @param string $userId
701
-	 * @return array
702
-	 * @throws \OCP\Files\NotFoundException
703
-	 */
704
-	protected function fillStorageInfo($userId) {
705
-		try {
706
-			\OC_Util::tearDownFS();
707
-			\OC_Util::setupFS($userId);
708
-			$storage = OC_Helper::getStorageInfo('/');
709
-			$data = [
710
-				'free' => $storage['free'],
711
-				'used' => $storage['used'],
712
-				'total' => $storage['total'],
713
-				'relative' => $storage['relative'],
714
-				'quota' => $storage['quota'],
715
-			];
716
-		} catch (NotFoundException $ex) {
717
-			$data = [];
718
-		}
719
-		return $data;
720
-	}
49
+    /** @var IUserManager */
50
+    private $userManager;
51
+    /** @var IConfig */
52
+    private $config;
53
+    /** @var IGroupManager|\OC\Group\Manager */ // FIXME Requires a method that is not on the interface
54
+    private $groupManager;
55
+    /** @var IUserSession */
56
+    private $userSession;
57
+    /** @var AccountManager */
58
+    private $accountManager;
59
+    /** @var ILogger */
60
+    private $logger;
61
+
62
+    /**
63
+     * @param string $appName
64
+     * @param IRequest $request
65
+     * @param IUserManager $userManager
66
+     * @param IConfig $config
67
+     * @param IGroupManager $groupManager
68
+     * @param IUserSession $userSession
69
+     * @param AccountManager $accountManager
70
+     * @param ILogger $logger
71
+     */
72
+    public function __construct($appName,
73
+                                IRequest $request,
74
+                                IUserManager $userManager,
75
+                                IConfig $config,
76
+                                IGroupManager $groupManager,
77
+                                IUserSession $userSession,
78
+                                AccountManager $accountManager,
79
+                                ILogger $logger) {
80
+        parent::__construct($appName, $request);
81
+
82
+        $this->userManager = $userManager;
83
+        $this->config = $config;
84
+        $this->groupManager = $groupManager;
85
+        $this->userSession = $userSession;
86
+        $this->accountManager = $accountManager;
87
+        $this->logger = $logger;
88
+    }
89
+
90
+    /**
91
+     * @NoAdminRequired
92
+     *
93
+     * returns a list of users
94
+     *
95
+     * @param string $search
96
+     * @param int $limit
97
+     * @param int $offset
98
+     * @return DataResponse
99
+     */
100
+    public function getUsers($search = '', $limit = null, $offset = null) {
101
+        $user = $this->userSession->getUser();
102
+        $users = [];
103
+
104
+        // Admin? Or SubAdmin?
105
+        $uid = $user->getUID();
106
+        $subAdminManager = $this->groupManager->getSubAdmin();
107
+        if($this->groupManager->isAdmin($uid)){
108
+            $users = $this->userManager->search($search, $limit, $offset);
109
+        } else if ($subAdminManager->isSubAdmin($user)) {
110
+            $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user);
111
+            foreach ($subAdminOfGroups as $key => $group) {
112
+                $subAdminOfGroups[$key] = $group->getGID();
113
+            }
114
+
115
+            if($offset === null) {
116
+                $offset = 0;
117
+            }
118
+
119
+            $users = [];
120
+            foreach ($subAdminOfGroups as $group) {
121
+                $users = array_merge($users, $this->groupManager->displayNamesInGroup($group, $search));
122
+            }
123
+
124
+            $users = array_slice($users, $offset, $limit);
125
+        }
126
+
127
+        $users = array_keys($users);
128
+
129
+        return new DataResponse([
130
+            'users' => $users
131
+        ]);
132
+    }
133
+
134
+    /**
135
+     * @PasswordConfirmationRequired
136
+     * @NoAdminRequired
137
+     *
138
+     * @param string $userid
139
+     * @param string $password
140
+     * @param array $groups
141
+     * @return DataResponse
142
+     * @throws OCSException
143
+     */
144
+    public function addUser($userid, $password, $groups = null) {
145
+        $user = $this->userSession->getUser();
146
+        $isAdmin = $this->groupManager->isAdmin($user->getUID());
147
+        $subAdminManager = $this->groupManager->getSubAdmin();
148
+
149
+        if($this->userManager->userExists($userid)) {
150
+            $this->logger->error('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']);
151
+            throw new OCSException('User already exists', 102);
152
+        }
153
+
154
+        if(is_array($groups)) {
155
+            foreach ($groups as $group) {
156
+                if(!$this->groupManager->groupExists($group)) {
157
+                    throw new OCSException('group '.$group.' does not exist', 104);
158
+                }
159
+                if(!$isAdmin && !$subAdminManager->isSubAdminofGroup($user, $this->groupManager->get($group))) {
160
+                    throw new OCSException('insufficient privileges for group '. $group, 105);
161
+                }
162
+            }
163
+        } else {
164
+            if(!$isAdmin) {
165
+                throw new OCSException('no group specified (required for subadmins)', 106);
166
+            }
167
+        }
168
+
169
+        try {
170
+            $newUser = $this->userManager->createUser($userid, $password);
171
+            $this->logger->info('Successful addUser call with userid: '.$userid, ['app' => 'ocs_api']);
172
+
173
+            if (is_array($groups)) {
174
+                foreach ($groups as $group) {
175
+                    $this->groupManager->get($group)->addUser($newUser);
176
+                    $this->logger->info('Added userid '.$userid.' to group '.$group, ['app' => 'ocs_api']);
177
+                }
178
+            }
179
+            return new DataResponse();
180
+        } catch (\Exception $e) {
181
+            $this->logger->error('Failed addUser attempt with exception: '.$e->getMessage(), ['app' => 'ocs_api']);
182
+            throw new OCSException('Bad request', 101);
183
+        }
184
+    }
185
+
186
+    /**
187
+     * @NoAdminRequired
188
+     * @NoSubAdminRequired
189
+     *
190
+     * gets user info
191
+     *
192
+     * @param string $userId
193
+     * @return DataResponse
194
+     * @throws OCSException
195
+     */
196
+    public function getUser($userId) {
197
+        $data = $this->getUserData($userId);
198
+        return new DataResponse($data);
199
+    }
200
+
201
+    /**
202
+     * @NoAdminRequired
203
+     * @NoSubAdminRequired
204
+     *
205
+     * gets user info from the currently logged in user
206
+     *
207
+     * @return DataResponse
208
+     * @throws OCSException
209
+     */
210
+    public function getCurrentUser() {
211
+        $user = $this->userSession->getUser();
212
+        if ($user) {
213
+            $data =  $this->getUserData($user->getUID());
214
+            // rename "displayname" to "display-name" only for this call to keep
215
+            // the API stable.
216
+            $data['display-name'] = $data['displayname'];
217
+            unset($data['displayname']);
218
+            return new DataResponse($data);
219
+
220
+        }
221
+
222
+        throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
223
+    }
224
+
225
+    /**
226
+     * creates a array with all user data
227
+     *
228
+     * @param $userId
229
+     * @return array
230
+     * @throws OCSException
231
+     */
232
+    protected function getUserData($userId) {
233
+        $currentLoggedInUser = $this->userSession->getUser();
234
+
235
+        $data = [];
236
+
237
+        // Check if the target user exists
238
+        $targetUserObject = $this->userManager->get($userId);
239
+        if($targetUserObject === null) {
240
+            throw new OCSException('The requested user could not be found', \OCP\API::RESPOND_NOT_FOUND);
241
+        }
242
+
243
+        // Admin? Or SubAdmin?
244
+        if($this->groupManager->isAdmin($currentLoggedInUser->getUID())
245
+            || $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) {
246
+            $data['enabled'] = $this->config->getUserValue($userId, 'core', 'enabled', 'true');
247
+        } else {
248
+            // Check they are looking up themselves
249
+            if($currentLoggedInUser->getUID() !== $userId) {
250
+                throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
251
+            }
252
+        }
253
+
254
+        $userAccount = $this->accountManager->getUser($targetUserObject);
255
+        $groups = $this->groupManager->getUserGroups($targetUserObject);
256
+        $gids = [];
257
+        foreach ($groups as $group) {
258
+            $gids[] = $group->getDisplayName();
259
+        }
260
+
261
+        // Find the data
262
+        $data['id'] = $targetUserObject->getUID();
263
+        $data['quota'] = $this->fillStorageInfo($userId);
264
+        $data['email'] = $targetUserObject->getEMailAddress();
265
+        $data['displayname'] = $targetUserObject->getDisplayName();
266
+        $data['phone'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_PHONE]['value'];
267
+        $data['address'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_ADDRESS]['value'];
268
+        $data['webpage'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_WEBSITE]['value'];
269
+        $data['twitter'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_TWITTER]['value'];
270
+        $data['groups'] = $gids;
271
+
272
+        return $data;
273
+    }
274
+
275
+    /**
276
+     * @NoAdminRequired
277
+     * @NoSubAdminRequired
278
+     * @PasswordConfirmationRequired
279
+     *
280
+     * edit users
281
+     *
282
+     * @param string $userId
283
+     * @param string $key
284
+     * @param string $value
285
+     * @return DataResponse
286
+     * @throws OCSException
287
+     * @throws OCSForbiddenException
288
+     */
289
+    public function editUser($userId, $key, $value) {
290
+        $currentLoggedInUser = $this->userSession->getUser();
291
+
292
+        $targetUser = $this->userManager->get($userId);
293
+        if($targetUser === null) {
294
+            throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
295
+        }
296
+
297
+        $permittedFields = [];
298
+        if($userId === $currentLoggedInUser->getUID()) {
299
+            // Editing self (display, email)
300
+            $permittedFields[] = 'display';
301
+            $permittedFields[] = 'email';
302
+            $permittedFields[] = 'password';
303
+            // If admin they can edit their own quota
304
+            if($this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
305
+                $permittedFields[] = 'quota';
306
+            }
307
+        } else {
308
+            // Check if admin / subadmin
309
+            $subAdminManager = $this->groupManager->getSubAdmin();
310
+            if($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
311
+            || $this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
312
+                // They have permissions over the user
313
+                $permittedFields[] = 'display';
314
+                $permittedFields[] = 'quota';
315
+                $permittedFields[] = 'password';
316
+                $permittedFields[] = 'email';
317
+            } else {
318
+                // No rights
319
+                throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
320
+            }
321
+        }
322
+        // Check if permitted to edit this field
323
+        if(!in_array($key, $permittedFields)) {
324
+            throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
325
+        }
326
+        // Process the edit
327
+        switch($key) {
328
+            case 'display':
329
+                $targetUser->setDisplayName($value);
330
+                break;
331
+            case 'quota':
332
+                $quota = $value;
333
+                if($quota !== 'none' && $quota !== 'default') {
334
+                    if (is_numeric($quota)) {
335
+                        $quota = (float) $quota;
336
+                    } else {
337
+                        $quota = \OCP\Util::computerFileSize($quota);
338
+                    }
339
+                    if ($quota === false) {
340
+                        throw new OCSException('Invalid quota value '.$value, 103);
341
+                    }
342
+                    if($quota === 0) {
343
+                        $quota = 'default';
344
+                    }else if($quota === -1) {
345
+                        $quota = 'none';
346
+                    } else {
347
+                        $quota = \OCP\Util::humanFileSize($quota);
348
+                    }
349
+                }
350
+                $targetUser->setQuota($quota);
351
+                break;
352
+            case 'password':
353
+                $targetUser->setPassword($value);
354
+                break;
355
+            case 'email':
356
+                if(filter_var($value, FILTER_VALIDATE_EMAIL)) {
357
+                    $targetUser->setEMailAddress($value);
358
+                } else {
359
+                    throw new OCSException('', 102);
360
+                }
361
+                break;
362
+            default:
363
+                throw new OCSException('', 103);
364
+        }
365
+        return new DataResponse();
366
+    }
367
+
368
+    /**
369
+     * @PasswordConfirmationRequired
370
+     * @NoAdminRequired
371
+     *
372
+     * @param string $userId
373
+     * @return DataResponse
374
+     * @throws OCSException
375
+     * @throws OCSForbiddenException
376
+     */
377
+    public function deleteUser($userId) {
378
+        $currentLoggedInUser = $this->userSession->getUser();
379
+
380
+        $targetUser = $this->userManager->get($userId);
381
+
382
+        if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
383
+            throw new OCSException('', 101);
384
+        }
385
+
386
+        // If not permitted
387
+        $subAdminManager = $this->groupManager->getSubAdmin();
388
+        if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
389
+            throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
390
+        }
391
+
392
+        // Go ahead with the delete
393
+        if($targetUser->delete()) {
394
+            return new DataResponse();
395
+        } else {
396
+            throw new OCSException('', 101);
397
+        }
398
+    }
399
+
400
+    /**
401
+     * @PasswordConfirmationRequired
402
+     * @NoAdminRequired
403
+     *
404
+     * @param string $userId
405
+     * @return DataResponse
406
+     * @throws OCSException
407
+     * @throws OCSForbiddenException
408
+     */
409
+    public function disableUser($userId) {
410
+        return $this->setEnabled($userId, false);
411
+    }
412
+
413
+    /**
414
+     * @PasswordConfirmationRequired
415
+     * @NoAdminRequired
416
+     *
417
+     * @param string $userId
418
+     * @return DataResponse
419
+     * @throws OCSException
420
+     * @throws OCSForbiddenException
421
+     */
422
+    public function enableUser($userId) {
423
+        return $this->setEnabled($userId, true);
424
+    }
425
+
426
+    /**
427
+     * @param string $userId
428
+     * @param bool $value
429
+     * @return DataResponse
430
+     * @throws OCSException
431
+     * @throws OCSForbiddenException
432
+     */
433
+    private function setEnabled($userId, $value) {
434
+        $currentLoggedInUser = $this->userSession->getUser();
435
+
436
+        $targetUser = $this->userManager->get($userId);
437
+        if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
438
+            throw new OCSException('', 101);
439
+        }
440
+
441
+        // If not permitted
442
+        $subAdminManager = $this->groupManager->getSubAdmin();
443
+        if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
444
+            throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
445
+        }
446
+
447
+        // enable/disable the user now
448
+        $targetUser->setEnabled($value);
449
+        return new DataResponse();
450
+    }
451
+
452
+    /**
453
+     * @NoAdminRequired
454
+     * @NoSubAdminRequired
455
+     *
456
+     * @param string $userId
457
+     * @return DataResponse
458
+     * @throws OCSException
459
+     */
460
+    public function getUsersGroups($userId) {
461
+        $loggedInUser = $this->userSession->getUser();
462
+
463
+        $targetUser = $this->userManager->get($userId);
464
+        if($targetUser === null) {
465
+            throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND);
466
+        }
467
+
468
+        if($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) {
469
+            // Self lookup or admin lookup
470
+            return new DataResponse([
471
+                'groups' => $this->groupManager->getUserGroupIds($targetUser)
472
+            ]);
473
+        } else {
474
+            $subAdminManager = $this->groupManager->getSubAdmin();
475
+
476
+            // Looking up someone else
477
+            if($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) {
478
+                // Return the group that the method caller is subadmin of for the user in question
479
+                /** @var IGroup[] $getSubAdminsGroups */
480
+                $getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);
481
+                foreach ($getSubAdminsGroups as $key => $group) {
482
+                    $getSubAdminsGroups[$key] = $group->getGID();
483
+                }
484
+                $groups = array_intersect(
485
+                    $getSubAdminsGroups,
486
+                    $this->groupManager->getUserGroupIds($targetUser)
487
+                );
488
+                return new DataResponse(['groups' => $groups]);
489
+            } else {
490
+                // Not permitted
491
+                throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
492
+            }
493
+        }
494
+
495
+    }
496
+
497
+    /**
498
+     * @PasswordConfirmationRequired
499
+     * @NoAdminRequired
500
+     *
501
+     * @param string $userId
502
+     * @param string $groupid
503
+     * @return DataResponse
504
+     * @throws OCSException
505
+     */
506
+    public function addToGroup($userId, $groupid = '') {
507
+        if($groupid === '') {
508
+            throw new OCSException('', 101);
509
+        }
510
+
511
+        $group = $this->groupManager->get($groupid);
512
+        $targetUser = $this->userManager->get($userId);
513
+        if($group === null) {
514
+            throw new OCSException('', 102);
515
+        }
516
+        if($targetUser === null) {
517
+            throw new OCSException('', 103);
518
+        }
519
+
520
+        // If they're not an admin, check they are a subadmin of the group in question
521
+        $loggedInUser = $this->userSession->getUser();
522
+        $subAdminManager = $this->groupManager->getSubAdmin();
523
+        if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) {
524
+            throw new OCSException('', 104);
525
+        }
526
+
527
+        // Add user to group
528
+        $group->addUser($targetUser);
529
+        return new DataResponse();
530
+    }
531
+
532
+    /**
533
+     * @PasswordConfirmationRequired
534
+     * @NoAdminRequired
535
+     *
536
+     * @param string $userId
537
+     * @param string $groupid
538
+     * @return DataResponse
539
+     * @throws OCSException
540
+     */
541
+    public function removeFromGroup($userId, $groupid) {
542
+        $loggedInUser = $this->userSession->getUser();
543
+
544
+        if($groupid === null) {
545
+            throw new OCSException('', 101);
546
+        }
547
+
548
+        $group = $this->groupManager->get($groupid);
549
+        if($group === null) {
550
+            throw new OCSException('', 102);
551
+        }
552
+
553
+        $targetUser = $this->userManager->get($userId);
554
+        if($targetUser === null) {
555
+            throw new OCSException('', 103);
556
+        }
557
+
558
+        // If they're not an admin, check they are a subadmin of the group in question
559
+        $subAdminManager = $this->groupManager->getSubAdmin();
560
+        if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) {
561
+            throw new OCSException('', 104);
562
+        }
563
+
564
+        // Check they aren't removing themselves from 'admin' or their 'subadmin; group
565
+        if ($userId === $loggedInUser->getUID()) {
566
+            if ($this->groupManager->isAdmin($loggedInUser->getUID())) {
567
+                if ($group->getGID() === 'admin') {
568
+                    throw new OCSException('Cannot remove yourself from the admin group', 105);
569
+                }
570
+            } else {
571
+                // Not an admin, so the user must be a subadmin of this group, but that is not allowed.
572
+                throw new OCSException('Cannot remove yourself from this group as you are a SubAdmin', 105);
573
+            }
574
+
575
+        } else if (!$this->groupManager->isAdmin($loggedInUser->getUID())) {
576
+            /** @var IGroup[] $subAdminGroups */
577
+            $subAdminGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);
578
+            $subAdminGroups = array_map(function (IGroup $subAdminGroup) {
579
+                return $subAdminGroup->getGID();
580
+            }, $subAdminGroups);
581
+            $userGroups = $this->groupManager->getUserGroupIds($targetUser);
582
+            $userSubAdminGroups = array_intersect($subAdminGroups, $userGroups);
583
+
584
+            if (count($userSubAdminGroups) <= 1) {
585
+                // Subadmin must not be able to remove a user from all their subadmin groups.
586
+                throw new OCSException('Cannot remove user from this group as this is the only remaining group you are a SubAdmin of', 105);
587
+            }
588
+        }
589
+
590
+        // Remove user from group
591
+        $group->removeUser($targetUser);
592
+        return new DataResponse();
593
+    }
594
+
595
+    /**
596
+     * Creates a subadmin
597
+     *
598
+     * @PasswordConfirmationRequired
599
+     *
600
+     * @param string $userId
601
+     * @param string $groupid
602
+     * @return DataResponse
603
+     * @throws OCSException
604
+     */
605
+    public function addSubAdmin($userId, $groupid) {
606
+        $group = $this->groupManager->get($groupid);
607
+        $user = $this->userManager->get($userId);
608
+
609
+        // Check if the user exists
610
+        if($user === null) {
611
+            throw new OCSException('User does not exist', 101);
612
+        }
613
+        // Check if group exists
614
+        if($group === null) {
615
+            throw new OCSException('Group:'.$groupid.' does not exist',  102);
616
+        }
617
+        // Check if trying to make subadmin of admin group
618
+        if(strtolower($groupid) === 'admin') {
619
+            throw new OCSException('Cannot create subadmins for admin group', 103);
620
+        }
621
+
622
+        $subAdminManager = $this->groupManager->getSubAdmin();
623
+
624
+        // We cannot be subadmin twice
625
+        if ($subAdminManager->isSubAdminofGroup($user, $group)) {
626
+            return new DataResponse();
627
+        }
628
+        // Go
629
+        if($subAdminManager->createSubAdmin($user, $group)) {
630
+            return new DataResponse();
631
+        } else {
632
+            throw new OCSException('Unknown error occurred', 103);
633
+        }
634
+    }
635
+
636
+    /**
637
+     * Removes a subadmin from a group
638
+     *
639
+     * @PasswordConfirmationRequired
640
+     *
641
+     * @param string $userId
642
+     * @param string $groupid
643
+     * @return DataResponse
644
+     * @throws OCSException
645
+     */
646
+    public function removeSubAdmin($userId, $groupid) {
647
+        $group = $this->groupManager->get($groupid);
648
+        $user = $this->userManager->get($userId);
649
+        $subAdminManager = $this->groupManager->getSubAdmin();
650
+
651
+        // Check if the user exists
652
+        if($user === null) {
653
+            throw new OCSException('User does not exist', 101);
654
+        }
655
+        // Check if the group exists
656
+        if($group === null) {
657
+            throw new OCSException('Group does not exist', 101);
658
+        }
659
+        // Check if they are a subadmin of this said group
660
+        if(!$subAdminManager->isSubAdminofGroup($user, $group)) {
661
+            throw new OCSException('User is not a subadmin of this group', 102);
662
+        }
663
+
664
+        // Go
665
+        if($subAdminManager->deleteSubAdmin($user, $group)) {
666
+            return new DataResponse();
667
+        } else {
668
+            throw new OCSException('Unknown error occurred', 103);
669
+        }
670
+    }
671
+
672
+    /**
673
+     * Get the groups a user is a subadmin of
674
+     *
675
+     * @param string $userId
676
+     * @return DataResponse
677
+     * @throws OCSException
678
+     */
679
+    public function getUserSubAdminGroups($userId) {
680
+        $user = $this->userManager->get($userId);
681
+        // Check if the user exists
682
+        if($user === null) {
683
+            throw new OCSException('User does not exist', 101);
684
+        }
685
+
686
+        // Get the subadmin groups
687
+        $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user);
688
+        foreach ($groups as $key => $group) {
689
+            $groups[$key] = $group->getGID();
690
+        }
691
+
692
+        if(!$groups) {
693
+            throw new OCSException('Unknown error occurred', 102);
694
+        } else {
695
+            return new DataResponse($groups);
696
+        }
697
+    }
698
+
699
+    /**
700
+     * @param string $userId
701
+     * @return array
702
+     * @throws \OCP\Files\NotFoundException
703
+     */
704
+    protected function fillStorageInfo($userId) {
705
+        try {
706
+            \OC_Util::tearDownFS();
707
+            \OC_Util::setupFS($userId);
708
+            $storage = OC_Helper::getStorageInfo('/');
709
+            $data = [
710
+                'free' => $storage['free'],
711
+                'used' => $storage['used'],
712
+                'total' => $storage['total'],
713
+                'relative' => $storage['relative'],
714
+                'quota' => $storage['quota'],
715
+            ];
716
+        } catch (NotFoundException $ex) {
717
+            $data = [];
718
+        }
719
+        return $data;
720
+    }
721 721
 }
Please login to merge, or discard this patch.