Completed
Push — master ( c1632c...027069 )
by Joas
22:03 queued 08:37
created

UsersController::deleteUser()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 6
Ratio 27.27 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 12
c 1
b 0
f 0
nc 4
nop 1
dl 6
loc 22
rs 8.6737
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Joas Schilling <[email protected]>
7
 * @author Lukas Reschke <[email protected]>
8
 * @author michag86 <[email protected]>
9
 * @author Morris Jobke <[email protected]>
10
 * @author Roeland Jago Douma <[email protected]>
11
 * @author Thomas Müller <[email protected]>
12
 * @author Tom Needham <[email protected]>
13
 *
14
 * @license AGPL-3.0
15
 *
16
 * This code is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License, version 3,
18
 * as published by the Free Software Foundation.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License, version 3,
26
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
27
 *
28
 */
29
30
namespace OCA\Provisioning_API\Controller;
31
32
use \OC_Helper;
33
use OCP\AppFramework\Http\DataResponse;
34
use OCP\AppFramework\OCS\OCSException;
35
use OCP\AppFramework\OCS\OCSForbiddenException;
36
use OCP\AppFramework\OCS\OCSNotFoundException;
37
use OCP\AppFramework\OCSController;
38
use OCP\Files\NotFoundException;
39
use OCP\IConfig;
40
use OCP\IGroupManager;
41
use OCP\ILogger;
42
use OCP\IRequest;
43
use OCP\IUserManager;
44
use OCP\IUserSession;
45
46
class UsersController extends OCSController {
47
48
	/** @var IUserManager */
49
	private $userManager;
50
	/** @var IConfig */
51
	private $config;
52
	/** @var IGroupManager|\OC\Group\Manager */ // FIXME Requires a method that is not on the interface
53
	private $groupManager;
54
	/** @var IUserSession */
55
	private $userSession;
56
	/** @var ILogger */
57
	private $logger;
58
59
	/**
60
	 * @param string $appName
61
	 * @param IRequest $request
62
	 * @param IUserManager $userManager
63
	 * @param IConfig $config
64
	 * @param IGroupManager $groupManager
65
	 * @param IUserSession $userSession
66
	 * @param ILogger $logger
67
	 */
68 View Code Duplication
	public function __construct($appName,
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
								IRequest $request,
70
								IUserManager $userManager,
71
								IConfig $config,
72
								IGroupManager $groupManager,
73
								IUserSession $userSession,
74
								ILogger $logger) {
75
		parent::__construct($appName, $request);
76
77
		$this->userManager = $userManager;
78
		$this->config = $config;
79
		$this->groupManager = $groupManager;
80
		$this->userSession = $userSession;
81
		$this->logger = $logger;
82
	}
83
84
	/**
85
	 * @NoAdminRequired
86
	 *
87
	 * returns a list of users
88
	 *
89
	 * @param string $search
90
	 * @param int $limit
91
	 * @param int $offset
92
	 * @return DataResponse
93
	 */
94
	public function getUsers($search = '', $limit = null, $offset = null) {
95
		$user = $this->userSession->getUser();
96
97
		// Admin? Or SubAdmin?
98
		$uid = $user->getUID();
99
		$subAdminManager = $this->groupManager->getSubAdmin();
100
		if($this->groupManager->isAdmin($uid)){
101
			$users = $this->userManager->search($search, $limit, $offset);
102
		} else if ($subAdminManager->isSubAdmin($user)) {
103
			$subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user);
104
			foreach ($subAdminOfGroups as $key => $group) {
105
				$subAdminOfGroups[$key] = $group->getGID();
106
			}
107
108
			if($offset === null) {
109
				$offset = 0; 
110
			}
111
112
			$users = [];
113
			foreach ($subAdminOfGroups as $group) {
114
				$users = array_merge($users, $this->groupManager->displayNamesInGroup($group, $search));
115
			}
116
117
			$users = array_slice($users, $offset, $limit);
118
		}
119
120
		$users = array_keys($users);
0 ignored issues
show
Bug introduced by
The variable $users does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
121
122
		return new DataResponse([
123
			'users' => $users
124
		]);
125
	}
126
127
	/**
128
	 * @NoAdminRequired
129
	 *
130
	 * @param string $userid
131
	 * @param string $password
132
	 * @param array $groups
133
	 * @return DataResponse
134
	 * @throws OCSException
135
	 */
136
	public function addUser($userid, $password, $groups = null) {
137
		$user = $this->userSession->getUser();
138
		$isAdmin = $this->groupManager->isAdmin($user->getUID());
139
		$subAdminManager = $this->groupManager->getSubAdmin();
140
141
		if($this->userManager->userExists($userid)) {
142
			$this->logger->error('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']);
143
			throw new OCSException('User already exists', 102);
144
		}
145
146
		if(is_array($groups)) {
147
			foreach ($groups as $group) {
148
				if(!$this->groupManager->groupExists($group)) {
149
					throw new OCSException('group '.$group.' does not exist', 104);
150
				}
151
				if(!$isAdmin && !$subAdminManager->isSubAdminofGroup($user, $this->groupManager->get($group))) {
152
					throw new OCSException('insufficient privileges for group '. $group, 105);
153
				}
154
			}
155
		} else {
156
			if(!$isAdmin) {
157
				throw new OCSException('no group specified (required for subadmins)', 106);
158
			}
159
		}
160
		
161
		try {
162
			$newUser = $this->userManager->createUser($userid, $password);
163
			$this->logger->info('Successful addUser call with userid: '.$userid, ['app' => 'ocs_api']);
164
165
			if (is_array($groups)) {
166
				foreach ($groups as $group) {
167
					$this->groupManager->get($group)->addUser($newUser);
0 ignored issues
show
Bug introduced by
It seems like $newUser defined by $this->userManager->crea...ser($userid, $password) on line 162 can also be of type boolean; however, OCP\IGroup::addUser() does only seem to accept object<OCP\IUser>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
168
					$this->logger->info('Added userid '.$userid.' to group '.$group, ['app' => 'ocs_api']);
169
				}
170
			}
171
			return new DataResponse();
172
		} catch (\Exception $e) {
173
			$this->logger->error('Failed addUser attempt with exception: '.$e->getMessage(), ['app' => 'ocs_api']);
174
			throw new OCSException('Bad request', 101);
175
		}
176
	}
177
178
	/**
179
	 * @NoAdminRequired
180
	 * @NoSubAdminRequired
181
	 *
182
	 * gets user info
183
	 *
184
	 * @param string $userId
185
	 * @return DataResponse
186
	 * @throws OCSException
187
	 */
188
	public function getUser($userId) {
189
		$currentLoggedInUser = $this->userSession->getUser();
190
191
		$data = [];
192
193
		// Check if the target user exists
194
		$targetUserObject = $this->userManager->get($userId);
195
		if($targetUserObject === null) {
196
			throw new OCSException('The requested user could not be found', \OCP\API::RESPOND_NOT_FOUND);
197
		}
198
199
		// Admin? Or SubAdmin?
200
		if($this->groupManager->isAdmin($currentLoggedInUser->getUID())
201
			|| $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) {
202
			$data['enabled'] = $this->config->getUserValue($userId, 'core', 'enabled', 'true');
203
		} else {
204
			// Check they are looking up themselves
205
			if($currentLoggedInUser->getUID() !== $userId) {
206
				throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
207
			}
208
		}
209
210
		// Find the data
211
		$data['quota'] = $this->fillStorageInfo($userId);
212
		$data['email'] = $targetUserObject->getEMailAddress();
213
		$data['displayname'] = $targetUserObject->getDisplayName();
214
215
		return new DataResponse($data);
216
	}
217
218
	/**
219
	 * @NoAdminRequired
220
	 * @NoSubAdminRequired
221
	 *
222
	 * edit users
223
	 *
224
	 * @param string $userId
225
	 * @param string $key
226
	 * @param string $value
227
	 * @return DataResponse
228
	 * @throws OCSException
229
	 * @throws OCSForbiddenException
230
	 */
231
	public function editUser($userId, $key, $value) {
232
		$currentLoggedInUser = $this->userSession->getUser();
233
234
		$targetUser = $this->userManager->get($userId);
235
		if($targetUser === null) {
236
			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
237
		}
238
239
		$permittedFields = [];
240
		if($userId === $currentLoggedInUser->getUID()) {
241
			// Editing self (display, email)
242
			$permittedFields[] = 'display';
243
			$permittedFields[] = 'email';
244
			$permittedFields[] = 'password';
245
			// If admin they can edit their own quota
246
			if($this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
247
				$permittedFields[] = 'quota';
248
			}
249
		} else {
250
			// Check if admin / subadmin
251
			$subAdminManager = $this->groupManager->getSubAdmin();
252
			if($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
253
			|| $this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
254
				// They have permissions over the user
255
				$permittedFields[] = 'display';
256
				$permittedFields[] = 'quota';
257
				$permittedFields[] = 'password';
258
				$permittedFields[] = 'email';
259
			} else {
260
				// No rights
261
				throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
262
			}
263
		}
264
		// Check if permitted to edit this field
265
		if(!in_array($key, $permittedFields)) {
266
			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
267
		}
268
		// Process the edit
269
		switch($key) {
270
			case 'display':
271
				$targetUser->setDisplayName($value);
272
				break;
273
			case 'quota':
274
				$quota = $value;
275
				if($quota !== 'none' and $quota !== 'default') {
276
					if (is_numeric($quota)) {
277
						$quota = floatval($quota);
278
					} else {
279
						$quota = \OCP\Util::computerFileSize($quota);
280
					}
281
					if ($quota === false) {
282
						throw new OCSException('Invalid quota value '.$value, 103);
283
					}
284
					if($quota === 0) {
285
						$quota = 'default';
286
					}else if($quota === -1) {
287
						$quota = 'none';
288
					} else {
289
						$quota = \OCP\Util::humanFileSize($quota);
290
					}
291
				}
292
				$targetUser->setQuota($quota);
293
				break;
294
			case 'password':
295
				$targetUser->setPassword($value);
296
				break;
297
			case 'email':
298
				if(filter_var($value, FILTER_VALIDATE_EMAIL)) {
299
					$targetUser->setEMailAddress($value);
300
				} else {
301
					throw new OCSException('', 102);
302
				}
303
				break;
304
			default:
305
				throw new OCSException('', 103);
306
		}
307
		return new DataResponse();
308
	}
309
310
	/**
311
	 * @NoAdminRequired
312
	 *
313
	 * @param string $userId
314
	 * @return DataResponse
315
	 * @throws OCSException
316
	 * @throws OCSForbiddenException
317
	 */
318
	public function deleteUser($userId) {
319
		$currentLoggedInUser = $this->userSession->getUser();
320
321
		$targetUser = $this->userManager->get($userId);
322
323 View Code Duplication
		if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
324
			throw new OCSException('', 101);
325
		}
326
327
		// If not permitted
328
		$subAdminManager = $this->groupManager->getSubAdmin();
329 View Code Duplication
		if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
330
			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
331
		}
332
333
		// Go ahead with the delete
334
		if($targetUser->delete()) {
335
			return new DataResponse();
336
		} else {
337
			throw new OCSException('', 101);
338
		}
339
	}
340
341
	/**
342
	 * @NoAdminRequired
343
	 *
344
	 * @param string $userId
345
	 * @return DataResponse
346
	 */
347
	public function disableUser($userId) {
348
		return $this->setEnabled($userId, false);
349
	}
350
351
	/**
352
	 * @NoAdminRequired
353
	 *
354
	 * @param string $userId
355
	 * @return DataResponse
356
	 */
357
	public function enableUser($userId) {
358
		return $this->setEnabled($userId, true);
359
	}
360
361
	/**
362
	 * @param string $userId
363
	 * @param bool $value
364
	 * @return DataResponse
365
	 * @throws OCSException
366
	 * @throws OCSForbiddenException
367
	 */
368
	private function setEnabled($userId, $value) {
369
		$currentLoggedInUser = $this->userSession->getUser();
370
371
		$targetUser = $this->userManager->get($userId);
372 View Code Duplication
		if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
373
			throw new OCSException('', 101);
374
		}
375
376
		// If not permitted
377
		$subAdminManager = $this->groupManager->getSubAdmin();
378 View Code Duplication
		if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
379
			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
380
		}
381
382
		// enable/disable the user now
383
		$targetUser->setEnabled($value);
384
		return new DataResponse();
385
	}
386
387
	/**
388
	 * @NoAdminRequired
389
	 * @NoSubAdminRequired
390
	 *
391
	 * @param string $userId
392
	 * @return DataResponse
393
	 * @throws OCSForbiddenException
394
	 * @throws OCSNotFoundException
395
	 */
396
	public function getUsersGroups($userId) {
397
		$loggedInUser = $this->userSession->getUser();
398
399
		$targetUser = $this->userManager->get($userId);
400
		if($targetUser === null) {
401
			throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND);
402
		}
403
404
		if($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) {
405
			// Self lookup or admin lookup
406
			return new DataResponse([
407
				'groups' => $this->groupManager->getUserGroupIds($targetUser)
408
			]);
409
		} else {
410
			$subAdminManager = $this->groupManager->getSubAdmin();
411
412
			// Looking up someone else
413
			if($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) {
414
				// Return the group that the method caller is subadmin of for the user in question
415
				$getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);
416
				foreach ($getSubAdminsGroups as $key => $group) {
417
					$getSubAdminsGroups[$key] = $group->getGID();
418
				}
419
				$groups = array_intersect(
420
					$getSubAdminsGroups,
421
					$this->groupManager->getUserGroupIds($targetUser)
422
				);
423
				return new DataResponse(['groups' => $groups]);
424
			} else {
425
				// Not permitted
426
				throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
427
			}
428
		}
429
		
430
	}
431
432
	/**
433
	 * @param string $userId
434
	 * @param string $groupid
435
	 * @return DataResponse
436
	 * @throws OCSException
437
	 */
438
	public function addToGroup($userId, $groupid = '') {
439
		if($groupid === '') {
440
			throw new OCSException('', 101);
441
		}
442
443
		$group = $this->groupManager->get($groupid);
444
		$targetUser = $this->userManager->get($userId);
445
		if($group === null) {
446
			throw new OCSException('', 102);
447
		}
448
		if($targetUser === null) {
449
			throw new OCSException('', 103);
450
		}
451
452
		// Add user to group
453
		$group->addUser($targetUser);
454
		return new DataResponse();
455
	}
456
457
	/**
458
	 * @NoAdminRequired
459
	 *
460
	 * @param string userId
461
	 * @param string $groupid
462
	 * @return DataResponse
463
	 * @throws OCSException
464
	 */
465
	public function removeFromGroup($userId, $groupid) {
466
		$loggedInUser = $this->userSession->getUser();
467
468
		if($groupid === null) {
469
			throw new OCSException('', 101);
470
		}
471
472
		$group = $this->groupManager->get($groupid);
473
		if($group === null) {
474
			throw new OCSException('', 102);
475
		}
476
477
		$targetUser = $this->userManager->get($userId);
478
		if($targetUser === null) {
479
			throw new OCSException('', 103);
480
		}
481
482
		// If they're not an admin, check they are a subadmin of the group in question
483
		$subAdminManager = $this->groupManager->getSubAdmin();
484
		if(!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminofGroup($loggedInUser, $group)) {
485
			throw new OCSException('', 104);
486
		}
487
		// Check they aren't removing themselves from 'admin' or their 'subadmin; group
488
		if($userId === $loggedInUser->getUID()) {
489
			if($this->groupManager->isAdmin($loggedInUser->getUID())) {
490
				if($group->getGID() === 'admin') {
491
					throw new OCSException('Cannot remove yourself from the admin group', 105);
492
				}
493
			} else {
494
				// Not an admin, check they are not removing themself from their subadmin group
495
				$subAdminGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);
496
				foreach ($subAdminGroups as $key => $group) {
497
					$subAdminGroups[$key] = $group->getGID();
498
				}
499
500
				if(in_array($group->getGID(), $subAdminGroups, true)) {
501
					throw new OCSException('Cannot remove yourself from this group as you are a SubAdmin', 105);
502
				}
503
			}
504
		}
505
506
		// Remove user from group
507
		$group->removeUser($targetUser);
508
		return new DataResponse();
509
	}
510
511
	/**
512
	 * Creates a subadmin
513
	 *
514
	 * @param string $userId
515
	 * @param string $groupid
516
	 * @return DataResponse
517
	 * @throws OCSException
518
	 */
519
	public function addSubAdmin($userId, $groupid) {
520
		$group = $this->groupManager->get($groupid);
521
		$user = $this->userManager->get($userId);
522
523
		// Check if the user exists
524
		if($user === null) {
525
			throw new OCSException('User does not exist', 101);
526
		}
527
		// Check if group exists
528
		if($group === null) {
529
			throw new OCSException('Group:'.$groupid.' does not exist',  102);
530
		}
531
		// Check if trying to make subadmin of admin group
532
		if(strtolower($groupid) === 'admin') {
533
			throw new OCSException('Cannot create subadmins for admin group', 103);
534
		}
535
536
		$subAdminManager = $this->groupManager->getSubAdmin();
537
538
		// We cannot be subadmin twice
539
		if ($subAdminManager->isSubAdminofGroup($user, $group)) {
540
			return new DataResponse();
541
		}
542
		// Go
543
		if($subAdminManager->createSubAdmin($user, $group)) {
544
			return new DataResponse();
545
		} else {
546
			throw new OCSException('Unknown error occurred', 103);
547
		}
548
	}
549
550
	/**
551
	 * Removes a subadmin from a group
552
	 *
553
	 * @param string $userId
554
	 * @param string $groupid
555
	 * @return DataResponse
556
	 * @throws OCSException
557
	 */
558
	public function removeSubAdmin($userId, $groupid) {
559
		$group = $this->groupManager->get($groupid);
560
		$user = $this->userManager->get($userId);
561
		$subAdminManager = $this->groupManager->getSubAdmin();
562
563
		// Check if the user exists
564
		if($user === null) {
565
			throw new OCSException('User does not exist', 101);
566
		}
567
		// Check if the group exists
568
		if($group === null) {
569
			throw new OCSException('Group does not exist', 101);
570
		}
571
		// Check if they are a subadmin of this said group
572
		if(!$subAdminManager->isSubAdminofGroup($user, $group)) {
573
			throw new OCSException('User is not a subadmin of this group', 102);
574
		}
575
576
		// Go
577
		if($subAdminManager->deleteSubAdmin($user, $group)) {
578
			return new DataResponse();
579
		} else {
580
			throw new OCSException('Unknown error occurred', 103);
581
		}
582
	}
583
584
	/**
585
	 * Get the groups a user is a subadmin of
586
	 *
587
	 * @param string $userId
588
	 * @return DataResponse
589
	 * @throws OCSException
590
	 */
591
	public function getUserSubAdminGroups($userId) {
592
		$user = $this->userManager->get($userId);
593
		// Check if the user exists
594
		if($user === null) {
595
			throw new OCSException('User does not exist', 101);
596
		}
597
598
		// Get the subadmin groups
599
		$groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user);
600
		foreach ($groups as $key => $group) {
601
			$groups[$key] = $group->getGID();
602
		}
603
604
		if(!$groups) {
605
			throw new OCSException('Unknown error occurred', 102);
606
		} else {
607
			return new DataResponse($groups);
608
		}
609
	}
610
611
	/**
612
	 * @param string $userId
613
	 * @return array
614
	 * @throws \OCP\Files\NotFoundException
615
	 */
616
	protected function fillStorageInfo($userId) {
617
		try {
618
			\OC_Util::tearDownFS();
619
			\OC_Util::setupFS($userId);
620
			$storage = OC_Helper::getStorageInfo('/');
621
			$data = [
622
				'free' => $storage['free'],
623
				'used' => $storage['used'],
624
				'total' => $storage['total'],
625
				'relative' => $storage['relative'],
626
			];
627
		} catch (NotFoundException $ex) {
628
			$data = [];
629
		}
630
		return $data;
631
	}
632
}
633