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