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