Completed
Push — master ( 16afaa...5d4864 )
by Joas
17:00 queued 09:09
created

UsersController::addToGroup()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 25
Code Lines 15

Duplication

Lines 3
Ratio 12 %

Importance

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