Completed
Pull Request — master (#5223)
by Joas
81:52 queued 51:08
created
apps/provisioning_api/lib/Controller/UsersController.php 2 patches
Indentation   +769 added lines, -769 removed lines patch added patch discarded remove patch
@@ -49,773 +49,773 @@
 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
-
290
-		return $data;
291
-	}
292
-
293
-	/**
294
-	 * @NoAdminRequired
295
-	 * @NoSubAdminRequired
296
-	 * @PasswordConfirmationRequired
297
-	 *
298
-	 * edit users
299
-	 *
300
-	 * @param string $userId
301
-	 * @param string $key
302
-	 * @param string $value
303
-	 * @return DataResponse
304
-	 * @throws OCSException
305
-	 * @throws OCSForbiddenException
306
-	 */
307
-	public function editUser($userId, $key, $value) {
308
-		$currentLoggedInUser = $this->userSession->getUser();
309
-
310
-		$targetUser = $this->userManager->get($userId);
311
-		if($targetUser === null) {
312
-			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
313
-		}
314
-
315
-		$permittedFields = [];
316
-		if($targetUser->getUID() === $currentLoggedInUser->getUID()) {
317
-			// Editing self (display, email)
318
-			if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) {
319
-				$permittedFields[] = 'display';
320
-				$permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME;
321
-				$permittedFields[] = AccountManager::PROPERTY_EMAIL;
322
-			}
323
-
324
-			$permittedFields[] = 'password';
325
-
326
-			if ($this->appManager->isEnabledForUser('federatedfilesharing')) {
327
-				$federatedFileSharing = new \OCA\FederatedFileSharing\AppInfo\Application();
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
-			// If admin they can edit their own quota
338
-			if($this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
339
-				$permittedFields[] = 'quota';
340
-			}
341
-		} else {
342
-			// Check if admin / subadmin
343
-			$subAdminManager = $this->groupManager->getSubAdmin();
344
-			if($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
345
-			|| $this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
346
-				// They have permissions over the user
347
-				$permittedFields[] = 'display';
348
-				$permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME;
349
-				$permittedFields[] = AccountManager::PROPERTY_EMAIL;
350
-				$permittedFields[] = 'password';
351
-				$permittedFields[] = AccountManager::PROPERTY_PHONE;
352
-				$permittedFields[] = AccountManager::PROPERTY_ADDRESS;
353
-				$permittedFields[] = AccountManager::PROPERTY_WEBSITE;
354
-				$permittedFields[] = AccountManager::PROPERTY_TWITTER;
355
-				$permittedFields[] = 'quota';
356
-			} else {
357
-				// No rights
358
-				throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
359
-			}
360
-		}
361
-		// Check if permitted to edit this field
362
-		if(!in_array($key, $permittedFields)) {
363
-			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
364
-		}
365
-		// Process the edit
366
-		switch($key) {
367
-			case 'display':
368
-			case AccountManager::PROPERTY_DISPLAYNAME:
369
-				$targetUser->setDisplayName($value);
370
-				break;
371
-			case 'quota':
372
-				$quota = $value;
373
-				if($quota !== 'none' && $quota !== 'default') {
374
-					if (is_numeric($quota)) {
375
-						$quota = (float) $quota;
376
-					} else {
377
-						$quota = \OCP\Util::computerFileSize($quota);
378
-					}
379
-					if ($quota === false) {
380
-						throw new OCSException('Invalid quota value '.$value, 103);
381
-					}
382
-					if($quota === 0) {
383
-						$quota = 'default';
384
-					}else if($quota === -1) {
385
-						$quota = 'none';
386
-					} else {
387
-						$quota = \OCP\Util::humanFileSize($quota);
388
-					}
389
-				}
390
-				$targetUser->setQuota($quota);
391
-				break;
392
-			case 'password':
393
-				$targetUser->setPassword($value);
394
-				break;
395
-			case AccountManager::PROPERTY_EMAIL:
396
-				if(filter_var($value, FILTER_VALIDATE_EMAIL)) {
397
-					$targetUser->setEMailAddress($value);
398
-				} else {
399
-					throw new OCSException('', 102);
400
-				}
401
-				break;
402
-			case AccountManager::PROPERTY_PHONE:
403
-			case AccountManager::PROPERTY_ADDRESS:
404
-			case AccountManager::PROPERTY_WEBSITE:
405
-			case AccountManager::PROPERTY_TWITTER:
406
-				$userAccount = $this->accountManager->getUser($targetUser);
407
-				if ($userAccount[$key]['value'] !== $value) {
408
-					$userAccount[$key]['value'] = $value;
409
-					$this->accountManager->updateUser($targetUser, $userAccount);
410
-				}
411
-				break;
412
-			default:
413
-				throw new OCSException('', 103);
414
-		}
415
-		return new DataResponse();
416
-	}
417
-
418
-	/**
419
-	 * @PasswordConfirmationRequired
420
-	 * @NoAdminRequired
421
-	 *
422
-	 * @param string $userId
423
-	 * @return DataResponse
424
-	 * @throws OCSException
425
-	 * @throws OCSForbiddenException
426
-	 */
427
-	public function deleteUser($userId) {
428
-		$currentLoggedInUser = $this->userSession->getUser();
429
-
430
-		$targetUser = $this->userManager->get($userId);
431
-
432
-		if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
433
-			throw new OCSException('', 101);
434
-		}
435
-
436
-		// If not permitted
437
-		$subAdminManager = $this->groupManager->getSubAdmin();
438
-		if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
439
-			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
440
-		}
441
-
442
-		// Go ahead with the delete
443
-		if($targetUser->delete()) {
444
-			return new DataResponse();
445
-		} else {
446
-			throw new OCSException('', 101);
447
-		}
448
-	}
449
-
450
-	/**
451
-	 * @PasswordConfirmationRequired
452
-	 * @NoAdminRequired
453
-	 *
454
-	 * @param string $userId
455
-	 * @return DataResponse
456
-	 * @throws OCSException
457
-	 * @throws OCSForbiddenException
458
-	 */
459
-	public function disableUser($userId) {
460
-		return $this->setEnabled($userId, false);
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 enableUser($userId) {
473
-		return $this->setEnabled($userId, true);
474
-	}
475
-
476
-	/**
477
-	 * @param string $userId
478
-	 * @param bool $value
479
-	 * @return DataResponse
480
-	 * @throws OCSException
481
-	 * @throws OCSForbiddenException
482
-	 */
483
-	private function setEnabled($userId, $value) {
484
-		$currentLoggedInUser = $this->userSession->getUser();
485
-
486
-		$targetUser = $this->userManager->get($userId);
487
-		if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
488
-			throw new OCSException('', 101);
489
-		}
490
-
491
-		// If not permitted
492
-		$subAdminManager = $this->groupManager->getSubAdmin();
493
-		if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
494
-			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
495
-		}
496
-
497
-		// enable/disable the user now
498
-		$targetUser->setEnabled($value);
499
-		return new DataResponse();
500
-	}
501
-
502
-	/**
503
-	 * @NoAdminRequired
504
-	 * @NoSubAdminRequired
505
-	 *
506
-	 * @param string $userId
507
-	 * @return DataResponse
508
-	 * @throws OCSException
509
-	 */
510
-	public function getUsersGroups($userId) {
511
-		$loggedInUser = $this->userSession->getUser();
512
-
513
-		$targetUser = $this->userManager->get($userId);
514
-		if($targetUser === null) {
515
-			throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND);
516
-		}
517
-
518
-		if($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) {
519
-			// Self lookup or admin lookup
520
-			return new DataResponse([
521
-				'groups' => $this->groupManager->getUserGroupIds($targetUser)
522
-			]);
523
-		} else {
524
-			$subAdminManager = $this->groupManager->getSubAdmin();
525
-
526
-			// Looking up someone else
527
-			if($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) {
528
-				// Return the group that the method caller is subadmin of for the user in question
529
-				/** @var IGroup[] $getSubAdminsGroups */
530
-				$getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);
531
-				foreach ($getSubAdminsGroups as $key => $group) {
532
-					$getSubAdminsGroups[$key] = $group->getGID();
533
-				}
534
-				$groups = array_intersect(
535
-					$getSubAdminsGroups,
536
-					$this->groupManager->getUserGroupIds($targetUser)
537
-				);
538
-				return new DataResponse(['groups' => $groups]);
539
-			} else {
540
-				// Not permitted
541
-				throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
542
-			}
543
-		}
544
-
545
-	}
546
-
547
-	/**
548
-	 * @PasswordConfirmationRequired
549
-	 * @NoAdminRequired
550
-	 *
551
-	 * @param string $userId
552
-	 * @param string $groupid
553
-	 * @return DataResponse
554
-	 * @throws OCSException
555
-	 */
556
-	public function addToGroup($userId, $groupid = '') {
557
-		if($groupid === '') {
558
-			throw new OCSException('', 101);
559
-		}
560
-
561
-		$group = $this->groupManager->get($groupid);
562
-		$targetUser = $this->userManager->get($userId);
563
-		if($group === null) {
564
-			throw new OCSException('', 102);
565
-		}
566
-		if($targetUser === null) {
567
-			throw new OCSException('', 103);
568
-		}
569
-
570
-		// If they're not an admin, check they are a subadmin of the group in question
571
-		$loggedInUser = $this->userSession->getUser();
572
-		$subAdminManager = $this->groupManager->getSubAdmin();
573
-		if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) {
574
-			throw new OCSException('', 104);
575
-		}
576
-
577
-		// Add user to group
578
-		$group->addUser($targetUser);
579
-		return new DataResponse();
580
-	}
581
-
582
-	/**
583
-	 * @PasswordConfirmationRequired
584
-	 * @NoAdminRequired
585
-	 *
586
-	 * @param string $userId
587
-	 * @param string $groupid
588
-	 * @return DataResponse
589
-	 * @throws OCSException
590
-	 */
591
-	public function removeFromGroup($userId, $groupid) {
592
-		$loggedInUser = $this->userSession->getUser();
593
-
594
-		if($groupid === null) {
595
-			throw new OCSException('', 101);
596
-		}
597
-
598
-		$group = $this->groupManager->get($groupid);
599
-		if($group === null) {
600
-			throw new OCSException('', 102);
601
-		}
602
-
603
-		$targetUser = $this->userManager->get($userId);
604
-		if($targetUser === null) {
605
-			throw new OCSException('', 103);
606
-		}
607
-
608
-		// If they're not an admin, check they are a subadmin of the group in question
609
-		$subAdminManager = $this->groupManager->getSubAdmin();
610
-		if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) {
611
-			throw new OCSException('', 104);
612
-		}
613
-
614
-		// Check they aren't removing themselves from 'admin' or their 'subadmin; group
615
-		if ($targetUser->getUID() === $loggedInUser->getUID()) {
616
-			if ($this->groupManager->isAdmin($loggedInUser->getUID())) {
617
-				if ($group->getGID() === 'admin') {
618
-					throw new OCSException('Cannot remove yourself from the admin group', 105);
619
-				}
620
-			} else {
621
-				// Not an admin, so the user must be a subadmin of this group, but that is not allowed.
622
-				throw new OCSException('Cannot remove yourself from this group as you are a SubAdmin', 105);
623
-			}
624
-
625
-		} else if (!$this->groupManager->isAdmin($loggedInUser->getUID())) {
626
-			/** @var IGroup[] $subAdminGroups */
627
-			$subAdminGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);
628
-			$subAdminGroups = array_map(function (IGroup $subAdminGroup) {
629
-				return $subAdminGroup->getGID();
630
-			}, $subAdminGroups);
631
-			$userGroups = $this->groupManager->getUserGroupIds($targetUser);
632
-			$userSubAdminGroups = array_intersect($subAdminGroups, $userGroups);
633
-
634
-			if (count($userSubAdminGroups) <= 1) {
635
-				// Subadmin must not be able to remove a user from all their subadmin groups.
636
-				throw new OCSException('Cannot remove user from this group as this is the only remaining group you are a SubAdmin of', 105);
637
-			}
638
-		}
639
-
640
-		// Remove user from group
641
-		$group->removeUser($targetUser);
642
-		return new DataResponse();
643
-	}
644
-
645
-	/**
646
-	 * Creates a subadmin
647
-	 *
648
-	 * @PasswordConfirmationRequired
649
-	 *
650
-	 * @param string $userId
651
-	 * @param string $groupid
652
-	 * @return DataResponse
653
-	 * @throws OCSException
654
-	 */
655
-	public function addSubAdmin($userId, $groupid) {
656
-		$group = $this->groupManager->get($groupid);
657
-		$user = $this->userManager->get($userId);
658
-
659
-		// Check if the user exists
660
-		if($user === null) {
661
-			throw new OCSException('User does not exist', 101);
662
-		}
663
-		// Check if group exists
664
-		if($group === null) {
665
-			throw new OCSException('Group does not exist',  102);
666
-		}
667
-		// Check if trying to make subadmin of admin group
668
-		if($group->getGID() === 'admin') {
669
-			throw new OCSException('Cannot create subadmins for admin group', 103);
670
-		}
671
-
672
-		$subAdminManager = $this->groupManager->getSubAdmin();
673
-
674
-		// We cannot be subadmin twice
675
-		if ($subAdminManager->isSubAdminofGroup($user, $group)) {
676
-			return new DataResponse();
677
-		}
678
-		// Go
679
-		if($subAdminManager->createSubAdmin($user, $group)) {
680
-			return new DataResponse();
681
-		} else {
682
-			throw new OCSException('Unknown error occurred', 103);
683
-		}
684
-	}
685
-
686
-	/**
687
-	 * Removes a subadmin from a group
688
-	 *
689
-	 * @PasswordConfirmationRequired
690
-	 *
691
-	 * @param string $userId
692
-	 * @param string $groupid
693
-	 * @return DataResponse
694
-	 * @throws OCSException
695
-	 */
696
-	public function removeSubAdmin($userId, $groupid) {
697
-		$group = $this->groupManager->get($groupid);
698
-		$user = $this->userManager->get($userId);
699
-		$subAdminManager = $this->groupManager->getSubAdmin();
700
-
701
-		// Check if the user exists
702
-		if($user === null) {
703
-			throw new OCSException('User does not exist', 101);
704
-		}
705
-		// Check if the group exists
706
-		if($group === null) {
707
-			throw new OCSException('Group does not exist', 101);
708
-		}
709
-		// Check if they are a subadmin of this said group
710
-		if(!$subAdminManager->isSubAdminOfGroup($user, $group)) {
711
-			throw new OCSException('User is not a subadmin of this group', 102);
712
-		}
713
-
714
-		// Go
715
-		if($subAdminManager->deleteSubAdmin($user, $group)) {
716
-			return new DataResponse();
717
-		} else {
718
-			throw new OCSException('Unknown error occurred', 103);
719
-		}
720
-	}
721
-
722
-	/**
723
-	 * Get the groups a user is a subadmin of
724
-	 *
725
-	 * @param string $userId
726
-	 * @return DataResponse
727
-	 * @throws OCSException
728
-	 */
729
-	public function getUserSubAdminGroups($userId) {
730
-		$user = $this->userManager->get($userId);
731
-		// Check if the user exists
732
-		if($user === null) {
733
-			throw new OCSException('User does not exist', 101);
734
-		}
735
-
736
-		// Get the subadmin groups
737
-		$groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user);
738
-		foreach ($groups as $key => $group) {
739
-			$groups[$key] = $group->getGID();
740
-		}
741
-
742
-		if(!$groups) {
743
-			throw new OCSException('Unknown error occurred', 102);
744
-		} else {
745
-			return new DataResponse($groups);
746
-		}
747
-	}
748
-
749
-	/**
750
-	 * @param string $userId
751
-	 * @return array
752
-	 * @throws \OCP\Files\NotFoundException
753
-	 */
754
-	protected function fillStorageInfo($userId) {
755
-		try {
756
-			\OC_Util::tearDownFS();
757
-			\OC_Util::setupFS($userId);
758
-			$storage = OC_Helper::getStorageInfo('/');
759
-			$data = [
760
-				'free' => $storage['free'],
761
-				'used' => $storage['used'],
762
-				'total' => $storage['total'],
763
-				'relative' => $storage['relative'],
764
-				'quota' => $storage['quota'],
765
-			];
766
-		} catch (NotFoundException $ex) {
767
-			$data = [];
768
-		}
769
-		return $data;
770
-	}
771
-
772
-	/**
773
-	 * @NoAdminRequired
774
-	 * @PasswordConfirmationRequired
775
-	 *
776
-	 * resend welcome message
777
-	 *
778
-	 * @param string $userId
779
-	 * @return DataResponse
780
-	 * @throws OCSException
781
-	 */
782
-	public function resendWelcomeMessage($userId) {
783
-		$currentLoggedInUser = $this->userSession->getUser();
784
-
785
-		$targetUser = $this->userManager->get($userId);
786
-		if($targetUser === null) {
787
-			throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND);
788
-		}
789
-
790
-		// Check if admin / subadmin
791
-		$subAdminManager = $this->groupManager->getSubAdmin();
792
-		if(!$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
793
-			&& !$this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
794
-			// No rights
795
-			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
796
-		}
797
-
798
-		$email = $targetUser->getEMailAddress();
799
-		if ($email === '' || $email === null) {
800
-			throw new OCSException('Email address not available', 101);
801
-		}
802
-		$username = $targetUser->getUID();
803
-		$lang = $this->config->getUserValue($username, 'core', 'lang', 'en');
804
-		if (!$this->l10nFactory->languageExists('settings', $lang)) {
805
-			$lang = 'en';
806
-		}
807
-
808
-		$l10n = $this->l10nFactory->get('settings', $lang);
809
-
810
-		try {
811
-			$this->newUserMailHelper->setL10N($l10n);
812
-			$emailTemplate = $this->newUserMailHelper->generateTemplate($targetUser, false);
813
-			$this->newUserMailHelper->sendMail($targetUser, $emailTemplate);
814
-		} catch(\Exception $e) {
815
-			$this->logger->error("Can't send new user mail to $email: " . $e->getMessage(), array('app' => 'settings'));
816
-			throw new OCSException('Sending email failed', 102);
817
-		}
818
-
819
-		return new DataResponse();
820
-	}
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
+
290
+        return $data;
291
+    }
292
+
293
+    /**
294
+     * @NoAdminRequired
295
+     * @NoSubAdminRequired
296
+     * @PasswordConfirmationRequired
297
+     *
298
+     * edit users
299
+     *
300
+     * @param string $userId
301
+     * @param string $key
302
+     * @param string $value
303
+     * @return DataResponse
304
+     * @throws OCSException
305
+     * @throws OCSForbiddenException
306
+     */
307
+    public function editUser($userId, $key, $value) {
308
+        $currentLoggedInUser = $this->userSession->getUser();
309
+
310
+        $targetUser = $this->userManager->get($userId);
311
+        if($targetUser === null) {
312
+            throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
313
+        }
314
+
315
+        $permittedFields = [];
316
+        if($targetUser->getUID() === $currentLoggedInUser->getUID()) {
317
+            // Editing self (display, email)
318
+            if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) {
319
+                $permittedFields[] = 'display';
320
+                $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME;
321
+                $permittedFields[] = AccountManager::PROPERTY_EMAIL;
322
+            }
323
+
324
+            $permittedFields[] = 'password';
325
+
326
+            if ($this->appManager->isEnabledForUser('federatedfilesharing')) {
327
+                $federatedFileSharing = new \OCA\FederatedFileSharing\AppInfo\Application();
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
+            // If admin they can edit their own quota
338
+            if($this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
339
+                $permittedFields[] = 'quota';
340
+            }
341
+        } else {
342
+            // Check if admin / subadmin
343
+            $subAdminManager = $this->groupManager->getSubAdmin();
344
+            if($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
345
+            || $this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
346
+                // They have permissions over the user
347
+                $permittedFields[] = 'display';
348
+                $permittedFields[] = AccountManager::PROPERTY_DISPLAYNAME;
349
+                $permittedFields[] = AccountManager::PROPERTY_EMAIL;
350
+                $permittedFields[] = 'password';
351
+                $permittedFields[] = AccountManager::PROPERTY_PHONE;
352
+                $permittedFields[] = AccountManager::PROPERTY_ADDRESS;
353
+                $permittedFields[] = AccountManager::PROPERTY_WEBSITE;
354
+                $permittedFields[] = AccountManager::PROPERTY_TWITTER;
355
+                $permittedFields[] = 'quota';
356
+            } else {
357
+                // No rights
358
+                throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
359
+            }
360
+        }
361
+        // Check if permitted to edit this field
362
+        if(!in_array($key, $permittedFields)) {
363
+            throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
364
+        }
365
+        // Process the edit
366
+        switch($key) {
367
+            case 'display':
368
+            case AccountManager::PROPERTY_DISPLAYNAME:
369
+                $targetUser->setDisplayName($value);
370
+                break;
371
+            case 'quota':
372
+                $quota = $value;
373
+                if($quota !== 'none' && $quota !== 'default') {
374
+                    if (is_numeric($quota)) {
375
+                        $quota = (float) $quota;
376
+                    } else {
377
+                        $quota = \OCP\Util::computerFileSize($quota);
378
+                    }
379
+                    if ($quota === false) {
380
+                        throw new OCSException('Invalid quota value '.$value, 103);
381
+                    }
382
+                    if($quota === 0) {
383
+                        $quota = 'default';
384
+                    }else if($quota === -1) {
385
+                        $quota = 'none';
386
+                    } else {
387
+                        $quota = \OCP\Util::humanFileSize($quota);
388
+                    }
389
+                }
390
+                $targetUser->setQuota($quota);
391
+                break;
392
+            case 'password':
393
+                $targetUser->setPassword($value);
394
+                break;
395
+            case AccountManager::PROPERTY_EMAIL:
396
+                if(filter_var($value, FILTER_VALIDATE_EMAIL)) {
397
+                    $targetUser->setEMailAddress($value);
398
+                } else {
399
+                    throw new OCSException('', 102);
400
+                }
401
+                break;
402
+            case AccountManager::PROPERTY_PHONE:
403
+            case AccountManager::PROPERTY_ADDRESS:
404
+            case AccountManager::PROPERTY_WEBSITE:
405
+            case AccountManager::PROPERTY_TWITTER:
406
+                $userAccount = $this->accountManager->getUser($targetUser);
407
+                if ($userAccount[$key]['value'] !== $value) {
408
+                    $userAccount[$key]['value'] = $value;
409
+                    $this->accountManager->updateUser($targetUser, $userAccount);
410
+                }
411
+                break;
412
+            default:
413
+                throw new OCSException('', 103);
414
+        }
415
+        return new DataResponse();
416
+    }
417
+
418
+    /**
419
+     * @PasswordConfirmationRequired
420
+     * @NoAdminRequired
421
+     *
422
+     * @param string $userId
423
+     * @return DataResponse
424
+     * @throws OCSException
425
+     * @throws OCSForbiddenException
426
+     */
427
+    public function deleteUser($userId) {
428
+        $currentLoggedInUser = $this->userSession->getUser();
429
+
430
+        $targetUser = $this->userManager->get($userId);
431
+
432
+        if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
433
+            throw new OCSException('', 101);
434
+        }
435
+
436
+        // If not permitted
437
+        $subAdminManager = $this->groupManager->getSubAdmin();
438
+        if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
439
+            throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
440
+        }
441
+
442
+        // Go ahead with the delete
443
+        if($targetUser->delete()) {
444
+            return new DataResponse();
445
+        } else {
446
+            throw new OCSException('', 101);
447
+        }
448
+    }
449
+
450
+    /**
451
+     * @PasswordConfirmationRequired
452
+     * @NoAdminRequired
453
+     *
454
+     * @param string $userId
455
+     * @return DataResponse
456
+     * @throws OCSException
457
+     * @throws OCSForbiddenException
458
+     */
459
+    public function disableUser($userId) {
460
+        return $this->setEnabled($userId, false);
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 enableUser($userId) {
473
+        return $this->setEnabled($userId, true);
474
+    }
475
+
476
+    /**
477
+     * @param string $userId
478
+     * @param bool $value
479
+     * @return DataResponse
480
+     * @throws OCSException
481
+     * @throws OCSForbiddenException
482
+     */
483
+    private function setEnabled($userId, $value) {
484
+        $currentLoggedInUser = $this->userSession->getUser();
485
+
486
+        $targetUser = $this->userManager->get($userId);
487
+        if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
488
+            throw new OCSException('', 101);
489
+        }
490
+
491
+        // If not permitted
492
+        $subAdminManager = $this->groupManager->getSubAdmin();
493
+        if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
494
+            throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
495
+        }
496
+
497
+        // enable/disable the user now
498
+        $targetUser->setEnabled($value);
499
+        return new DataResponse();
500
+    }
501
+
502
+    /**
503
+     * @NoAdminRequired
504
+     * @NoSubAdminRequired
505
+     *
506
+     * @param string $userId
507
+     * @return DataResponse
508
+     * @throws OCSException
509
+     */
510
+    public function getUsersGroups($userId) {
511
+        $loggedInUser = $this->userSession->getUser();
512
+
513
+        $targetUser = $this->userManager->get($userId);
514
+        if($targetUser === null) {
515
+            throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND);
516
+        }
517
+
518
+        if($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) {
519
+            // Self lookup or admin lookup
520
+            return new DataResponse([
521
+                'groups' => $this->groupManager->getUserGroupIds($targetUser)
522
+            ]);
523
+        } else {
524
+            $subAdminManager = $this->groupManager->getSubAdmin();
525
+
526
+            // Looking up someone else
527
+            if($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) {
528
+                // Return the group that the method caller is subadmin of for the user in question
529
+                /** @var IGroup[] $getSubAdminsGroups */
530
+                $getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);
531
+                foreach ($getSubAdminsGroups as $key => $group) {
532
+                    $getSubAdminsGroups[$key] = $group->getGID();
533
+                }
534
+                $groups = array_intersect(
535
+                    $getSubAdminsGroups,
536
+                    $this->groupManager->getUserGroupIds($targetUser)
537
+                );
538
+                return new DataResponse(['groups' => $groups]);
539
+            } else {
540
+                // Not permitted
541
+                throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
542
+            }
543
+        }
544
+
545
+    }
546
+
547
+    /**
548
+     * @PasswordConfirmationRequired
549
+     * @NoAdminRequired
550
+     *
551
+     * @param string $userId
552
+     * @param string $groupid
553
+     * @return DataResponse
554
+     * @throws OCSException
555
+     */
556
+    public function addToGroup($userId, $groupid = '') {
557
+        if($groupid === '') {
558
+            throw new OCSException('', 101);
559
+        }
560
+
561
+        $group = $this->groupManager->get($groupid);
562
+        $targetUser = $this->userManager->get($userId);
563
+        if($group === null) {
564
+            throw new OCSException('', 102);
565
+        }
566
+        if($targetUser === null) {
567
+            throw new OCSException('', 103);
568
+        }
569
+
570
+        // If they're not an admin, check they are a subadmin of the group in question
571
+        $loggedInUser = $this->userSession->getUser();
572
+        $subAdminManager = $this->groupManager->getSubAdmin();
573
+        if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) {
574
+            throw new OCSException('', 104);
575
+        }
576
+
577
+        // Add user to group
578
+        $group->addUser($targetUser);
579
+        return new DataResponse();
580
+    }
581
+
582
+    /**
583
+     * @PasswordConfirmationRequired
584
+     * @NoAdminRequired
585
+     *
586
+     * @param string $userId
587
+     * @param string $groupid
588
+     * @return DataResponse
589
+     * @throws OCSException
590
+     */
591
+    public function removeFromGroup($userId, $groupid) {
592
+        $loggedInUser = $this->userSession->getUser();
593
+
594
+        if($groupid === null) {
595
+            throw new OCSException('', 101);
596
+        }
597
+
598
+        $group = $this->groupManager->get($groupid);
599
+        if($group === null) {
600
+            throw new OCSException('', 102);
601
+        }
602
+
603
+        $targetUser = $this->userManager->get($userId);
604
+        if($targetUser === null) {
605
+            throw new OCSException('', 103);
606
+        }
607
+
608
+        // If they're not an admin, check they are a subadmin of the group in question
609
+        $subAdminManager = $this->groupManager->getSubAdmin();
610
+        if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) {
611
+            throw new OCSException('', 104);
612
+        }
613
+
614
+        // Check they aren't removing themselves from 'admin' or their 'subadmin; group
615
+        if ($targetUser->getUID() === $loggedInUser->getUID()) {
616
+            if ($this->groupManager->isAdmin($loggedInUser->getUID())) {
617
+                if ($group->getGID() === 'admin') {
618
+                    throw new OCSException('Cannot remove yourself from the admin group', 105);
619
+                }
620
+            } else {
621
+                // Not an admin, so the user must be a subadmin of this group, but that is not allowed.
622
+                throw new OCSException('Cannot remove yourself from this group as you are a SubAdmin', 105);
623
+            }
624
+
625
+        } else if (!$this->groupManager->isAdmin($loggedInUser->getUID())) {
626
+            /** @var IGroup[] $subAdminGroups */
627
+            $subAdminGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);
628
+            $subAdminGroups = array_map(function (IGroup $subAdminGroup) {
629
+                return $subAdminGroup->getGID();
630
+            }, $subAdminGroups);
631
+            $userGroups = $this->groupManager->getUserGroupIds($targetUser);
632
+            $userSubAdminGroups = array_intersect($subAdminGroups, $userGroups);
633
+
634
+            if (count($userSubAdminGroups) <= 1) {
635
+                // Subadmin must not be able to remove a user from all their subadmin groups.
636
+                throw new OCSException('Cannot remove user from this group as this is the only remaining group you are a SubAdmin of', 105);
637
+            }
638
+        }
639
+
640
+        // Remove user from group
641
+        $group->removeUser($targetUser);
642
+        return new DataResponse();
643
+    }
644
+
645
+    /**
646
+     * Creates a subadmin
647
+     *
648
+     * @PasswordConfirmationRequired
649
+     *
650
+     * @param string $userId
651
+     * @param string $groupid
652
+     * @return DataResponse
653
+     * @throws OCSException
654
+     */
655
+    public function addSubAdmin($userId, $groupid) {
656
+        $group = $this->groupManager->get($groupid);
657
+        $user = $this->userManager->get($userId);
658
+
659
+        // Check if the user exists
660
+        if($user === null) {
661
+            throw new OCSException('User does not exist', 101);
662
+        }
663
+        // Check if group exists
664
+        if($group === null) {
665
+            throw new OCSException('Group does not exist',  102);
666
+        }
667
+        // Check if trying to make subadmin of admin group
668
+        if($group->getGID() === 'admin') {
669
+            throw new OCSException('Cannot create subadmins for admin group', 103);
670
+        }
671
+
672
+        $subAdminManager = $this->groupManager->getSubAdmin();
673
+
674
+        // We cannot be subadmin twice
675
+        if ($subAdminManager->isSubAdminofGroup($user, $group)) {
676
+            return new DataResponse();
677
+        }
678
+        // Go
679
+        if($subAdminManager->createSubAdmin($user, $group)) {
680
+            return new DataResponse();
681
+        } else {
682
+            throw new OCSException('Unknown error occurred', 103);
683
+        }
684
+    }
685
+
686
+    /**
687
+     * Removes a subadmin from a group
688
+     *
689
+     * @PasswordConfirmationRequired
690
+     *
691
+     * @param string $userId
692
+     * @param string $groupid
693
+     * @return DataResponse
694
+     * @throws OCSException
695
+     */
696
+    public function removeSubAdmin($userId, $groupid) {
697
+        $group = $this->groupManager->get($groupid);
698
+        $user = $this->userManager->get($userId);
699
+        $subAdminManager = $this->groupManager->getSubAdmin();
700
+
701
+        // Check if the user exists
702
+        if($user === null) {
703
+            throw new OCSException('User does not exist', 101);
704
+        }
705
+        // Check if the group exists
706
+        if($group === null) {
707
+            throw new OCSException('Group does not exist', 101);
708
+        }
709
+        // Check if they are a subadmin of this said group
710
+        if(!$subAdminManager->isSubAdminOfGroup($user, $group)) {
711
+            throw new OCSException('User is not a subadmin of this group', 102);
712
+        }
713
+
714
+        // Go
715
+        if($subAdminManager->deleteSubAdmin($user, $group)) {
716
+            return new DataResponse();
717
+        } else {
718
+            throw new OCSException('Unknown error occurred', 103);
719
+        }
720
+    }
721
+
722
+    /**
723
+     * Get the groups a user is a subadmin of
724
+     *
725
+     * @param string $userId
726
+     * @return DataResponse
727
+     * @throws OCSException
728
+     */
729
+    public function getUserSubAdminGroups($userId) {
730
+        $user = $this->userManager->get($userId);
731
+        // Check if the user exists
732
+        if($user === null) {
733
+            throw new OCSException('User does not exist', 101);
734
+        }
735
+
736
+        // Get the subadmin groups
737
+        $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user);
738
+        foreach ($groups as $key => $group) {
739
+            $groups[$key] = $group->getGID();
740
+        }
741
+
742
+        if(!$groups) {
743
+            throw new OCSException('Unknown error occurred', 102);
744
+        } else {
745
+            return new DataResponse($groups);
746
+        }
747
+    }
748
+
749
+    /**
750
+     * @param string $userId
751
+     * @return array
752
+     * @throws \OCP\Files\NotFoundException
753
+     */
754
+    protected function fillStorageInfo($userId) {
755
+        try {
756
+            \OC_Util::tearDownFS();
757
+            \OC_Util::setupFS($userId);
758
+            $storage = OC_Helper::getStorageInfo('/');
759
+            $data = [
760
+                'free' => $storage['free'],
761
+                'used' => $storage['used'],
762
+                'total' => $storage['total'],
763
+                'relative' => $storage['relative'],
764
+                'quota' => $storage['quota'],
765
+            ];
766
+        } catch (NotFoundException $ex) {
767
+            $data = [];
768
+        }
769
+        return $data;
770
+    }
771
+
772
+    /**
773
+     * @NoAdminRequired
774
+     * @PasswordConfirmationRequired
775
+     *
776
+     * resend welcome message
777
+     *
778
+     * @param string $userId
779
+     * @return DataResponse
780
+     * @throws OCSException
781
+     */
782
+    public function resendWelcomeMessage($userId) {
783
+        $currentLoggedInUser = $this->userSession->getUser();
784
+
785
+        $targetUser = $this->userManager->get($userId);
786
+        if($targetUser === null) {
787
+            throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND);
788
+        }
789
+
790
+        // Check if admin / subadmin
791
+        $subAdminManager = $this->groupManager->getSubAdmin();
792
+        if(!$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
793
+            && !$this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
794
+            // No rights
795
+            throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
796
+        }
797
+
798
+        $email = $targetUser->getEMailAddress();
799
+        if ($email === '' || $email === null) {
800
+            throw new OCSException('Email address not available', 101);
801
+        }
802
+        $username = $targetUser->getUID();
803
+        $lang = $this->config->getUserValue($username, 'core', 'lang', 'en');
804
+        if (!$this->l10nFactory->languageExists('settings', $lang)) {
805
+            $lang = 'en';
806
+        }
807
+
808
+        $l10n = $this->l10nFactory->get('settings', $lang);
809
+
810
+        try {
811
+            $this->newUserMailHelper->setL10N($l10n);
812
+            $emailTemplate = $this->newUserMailHelper->generateTemplate($targetUser, false);
813
+            $this->newUserMailHelper->sendMail($targetUser, $emailTemplate);
814
+        } catch(\Exception $e) {
815
+            $this->logger->error("Can't send new user mail to $email: " . $e->getMessage(), array('app' => 'settings'));
816
+            throw new OCSException('Sending email failed', 102);
817
+        }
818
+
819
+        return new DataResponse();
820
+    }
821 821
 }
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
 		}
@@ -308,12 +308,12 @@  discard block
 block discarded – undo
308 308
 		$currentLoggedInUser = $this->userSession->getUser();
309 309
 
310 310
 		$targetUser = $this->userManager->get($userId);
311
-		if($targetUser === null) {
311
+		if ($targetUser === null) {
312 312
 			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
313 313
 		}
314 314
 
315 315
 		$permittedFields = [];
316
-		if($targetUser->getUID() === $currentLoggedInUser->getUID()) {
316
+		if ($targetUser->getUID() === $currentLoggedInUser->getUID()) {
317 317
 			// Editing self (display, email)
318 318
 			if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) {
319 319
 				$permittedFields[] = 'display';
@@ -335,13 +335,13 @@  discard block
 block discarded – undo
335 335
 			}
336 336
 
337 337
 			// If admin they can edit their own quota
338
-			if($this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
338
+			if ($this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
339 339
 				$permittedFields[] = 'quota';
340 340
 			}
341 341
 		} else {
342 342
 			// Check if admin / subadmin
343 343
 			$subAdminManager = $this->groupManager->getSubAdmin();
344
-			if($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
344
+			if ($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
345 345
 			|| $this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
346 346
 				// They have permissions over the user
347 347
 				$permittedFields[] = 'display';
@@ -359,18 +359,18 @@  discard block
 block discarded – undo
359 359
 			}
360 360
 		}
361 361
 		// Check if permitted to edit this field
362
-		if(!in_array($key, $permittedFields)) {
362
+		if (!in_array($key, $permittedFields)) {
363 363
 			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
364 364
 		}
365 365
 		// Process the edit
366
-		switch($key) {
366
+		switch ($key) {
367 367
 			case 'display':
368 368
 			case AccountManager::PROPERTY_DISPLAYNAME:
369 369
 				$targetUser->setDisplayName($value);
370 370
 				break;
371 371
 			case 'quota':
372 372
 				$quota = $value;
373
-				if($quota !== 'none' && $quota !== 'default') {
373
+				if ($quota !== 'none' && $quota !== 'default') {
374 374
 					if (is_numeric($quota)) {
375 375
 						$quota = (float) $quota;
376 376
 					} else {
@@ -379,9 +379,9 @@  discard block
 block discarded – undo
379 379
 					if ($quota === false) {
380 380
 						throw new OCSException('Invalid quota value '.$value, 103);
381 381
 					}
382
-					if($quota === 0) {
382
+					if ($quota === 0) {
383 383
 						$quota = 'default';
384
-					}else if($quota === -1) {
384
+					} else if ($quota === -1) {
385 385
 						$quota = 'none';
386 386
 					} else {
387 387
 						$quota = \OCP\Util::humanFileSize($quota);
@@ -393,7 +393,7 @@  discard block
 block discarded – undo
393 393
 				$targetUser->setPassword($value);
394 394
 				break;
395 395
 			case AccountManager::PROPERTY_EMAIL:
396
-				if(filter_var($value, FILTER_VALIDATE_EMAIL)) {
396
+				if (filter_var($value, FILTER_VALIDATE_EMAIL)) {
397 397
 					$targetUser->setEMailAddress($value);
398 398
 				} else {
399 399
 					throw new OCSException('', 102);
@@ -429,18 +429,18 @@  discard block
 block discarded – undo
429 429
 
430 430
 		$targetUser = $this->userManager->get($userId);
431 431
 
432
-		if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
432
+		if ($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
433 433
 			throw new OCSException('', 101);
434 434
 		}
435 435
 
436 436
 		// If not permitted
437 437
 		$subAdminManager = $this->groupManager->getSubAdmin();
438
-		if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
438
+		if (!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
439 439
 			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
440 440
 		}
441 441
 
442 442
 		// Go ahead with the delete
443
-		if($targetUser->delete()) {
443
+		if ($targetUser->delete()) {
444 444
 			return new DataResponse();
445 445
 		} else {
446 446
 			throw new OCSException('', 101);
@@ -484,13 +484,13 @@  discard block
 block discarded – undo
484 484
 		$currentLoggedInUser = $this->userSession->getUser();
485 485
 
486 486
 		$targetUser = $this->userManager->get($userId);
487
-		if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
487
+		if ($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
488 488
 			throw new OCSException('', 101);
489 489
 		}
490 490
 
491 491
 		// If not permitted
492 492
 		$subAdminManager = $this->groupManager->getSubAdmin();
493
-		if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
493
+		if (!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
494 494
 			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
495 495
 		}
496 496
 
@@ -511,11 +511,11 @@  discard block
 block discarded – undo
511 511
 		$loggedInUser = $this->userSession->getUser();
512 512
 
513 513
 		$targetUser = $this->userManager->get($userId);
514
-		if($targetUser === null) {
514
+		if ($targetUser === null) {
515 515
 			throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND);
516 516
 		}
517 517
 
518
-		if($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) {
518
+		if ($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) {
519 519
 			// Self lookup or admin lookup
520 520
 			return new DataResponse([
521 521
 				'groups' => $this->groupManager->getUserGroupIds($targetUser)
@@ -524,7 +524,7 @@  discard block
 block discarded – undo
524 524
 			$subAdminManager = $this->groupManager->getSubAdmin();
525 525
 
526 526
 			// Looking up someone else
527
-			if($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) {
527
+			if ($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) {
528 528
 				// Return the group that the method caller is subadmin of for the user in question
529 529
 				/** @var IGroup[] $getSubAdminsGroups */
530 530
 				$getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);
@@ -554,16 +554,16 @@  discard block
 block discarded – undo
554 554
 	 * @throws OCSException
555 555
 	 */
556 556
 	public function addToGroup($userId, $groupid = '') {
557
-		if($groupid === '') {
557
+		if ($groupid === '') {
558 558
 			throw new OCSException('', 101);
559 559
 		}
560 560
 
561 561
 		$group = $this->groupManager->get($groupid);
562 562
 		$targetUser = $this->userManager->get($userId);
563
-		if($group === null) {
563
+		if ($group === null) {
564 564
 			throw new OCSException('', 102);
565 565
 		}
566
-		if($targetUser === null) {
566
+		if ($targetUser === null) {
567 567
 			throw new OCSException('', 103);
568 568
 		}
569 569
 
@@ -591,17 +591,17 @@  discard block
 block discarded – undo
591 591
 	public function removeFromGroup($userId, $groupid) {
592 592
 		$loggedInUser = $this->userSession->getUser();
593 593
 
594
-		if($groupid === null) {
594
+		if ($groupid === null) {
595 595
 			throw new OCSException('', 101);
596 596
 		}
597 597
 
598 598
 		$group = $this->groupManager->get($groupid);
599
-		if($group === null) {
599
+		if ($group === null) {
600 600
 			throw new OCSException('', 102);
601 601
 		}
602 602
 
603 603
 		$targetUser = $this->userManager->get($userId);
604
-		if($targetUser === null) {
604
+		if ($targetUser === null) {
605 605
 			throw new OCSException('', 103);
606 606
 		}
607 607
 
@@ -625,7 +625,7 @@  discard block
 block discarded – undo
625 625
 		} else if (!$this->groupManager->isAdmin($loggedInUser->getUID())) {
626 626
 			/** @var IGroup[] $subAdminGroups */
627 627
 			$subAdminGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);
628
-			$subAdminGroups = array_map(function (IGroup $subAdminGroup) {
628
+			$subAdminGroups = array_map(function(IGroup $subAdminGroup) {
629 629
 				return $subAdminGroup->getGID();
630 630
 			}, $subAdminGroups);
631 631
 			$userGroups = $this->groupManager->getUserGroupIds($targetUser);
@@ -657,15 +657,15 @@  discard block
 block discarded – undo
657 657
 		$user = $this->userManager->get($userId);
658 658
 
659 659
 		// Check if the user exists
660
-		if($user === null) {
660
+		if ($user === null) {
661 661
 			throw new OCSException('User does not exist', 101);
662 662
 		}
663 663
 		// Check if group exists
664
-		if($group === null) {
665
-			throw new OCSException('Group does not exist',  102);
664
+		if ($group === null) {
665
+			throw new OCSException('Group does not exist', 102);
666 666
 		}
667 667
 		// Check if trying to make subadmin of admin group
668
-		if($group->getGID() === 'admin') {
668
+		if ($group->getGID() === 'admin') {
669 669
 			throw new OCSException('Cannot create subadmins for admin group', 103);
670 670
 		}
671 671
 
@@ -676,7 +676,7 @@  discard block
 block discarded – undo
676 676
 			return new DataResponse();
677 677
 		}
678 678
 		// Go
679
-		if($subAdminManager->createSubAdmin($user, $group)) {
679
+		if ($subAdminManager->createSubAdmin($user, $group)) {
680 680
 			return new DataResponse();
681 681
 		} else {
682 682
 			throw new OCSException('Unknown error occurred', 103);
@@ -699,20 +699,20 @@  discard block
 block discarded – undo
699 699
 		$subAdminManager = $this->groupManager->getSubAdmin();
700 700
 
701 701
 		// Check if the user exists
702
-		if($user === null) {
702
+		if ($user === null) {
703 703
 			throw new OCSException('User does not exist', 101);
704 704
 		}
705 705
 		// Check if the group exists
706
-		if($group === null) {
706
+		if ($group === null) {
707 707
 			throw new OCSException('Group does not exist', 101);
708 708
 		}
709 709
 		// Check if they are a subadmin of this said group
710
-		if(!$subAdminManager->isSubAdminOfGroup($user, $group)) {
710
+		if (!$subAdminManager->isSubAdminOfGroup($user, $group)) {
711 711
 			throw new OCSException('User is not a subadmin of this group', 102);
712 712
 		}
713 713
 
714 714
 		// Go
715
-		if($subAdminManager->deleteSubAdmin($user, $group)) {
715
+		if ($subAdminManager->deleteSubAdmin($user, $group)) {
716 716
 			return new DataResponse();
717 717
 		} else {
718 718
 			throw new OCSException('Unknown error occurred', 103);
@@ -729,7 +729,7 @@  discard block
 block discarded – undo
729 729
 	public function getUserSubAdminGroups($userId) {
730 730
 		$user = $this->userManager->get($userId);
731 731
 		// Check if the user exists
732
-		if($user === null) {
732
+		if ($user === null) {
733 733
 			throw new OCSException('User does not exist', 101);
734 734
 		}
735 735
 
@@ -739,7 +739,7 @@  discard block
 block discarded – undo
739 739
 			$groups[$key] = $group->getGID();
740 740
 		}
741 741
 
742
-		if(!$groups) {
742
+		if (!$groups) {
743 743
 			throw new OCSException('Unknown error occurred', 102);
744 744
 		} else {
745 745
 			return new DataResponse($groups);
@@ -783,13 +783,13 @@  discard block
 block discarded – undo
783 783
 		$currentLoggedInUser = $this->userSession->getUser();
784 784
 
785 785
 		$targetUser = $this->userManager->get($userId);
786
-		if($targetUser === null) {
786
+		if ($targetUser === null) {
787 787
 			throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND);
788 788
 		}
789 789
 
790 790
 		// Check if admin / subadmin
791 791
 		$subAdminManager = $this->groupManager->getSubAdmin();
792
-		if(!$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
792
+		if (!$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
793 793
 			&& !$this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
794 794
 			// No rights
795 795
 			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
@@ -811,8 +811,8 @@  discard block
 block discarded – undo
811 811
 			$this->newUserMailHelper->setL10N($l10n);
812 812
 			$emailTemplate = $this->newUserMailHelper->generateTemplate($targetUser, false);
813 813
 			$this->newUserMailHelper->sendMail($targetUser, $emailTemplate);
814
-		} catch(\Exception $e) {
815
-			$this->logger->error("Can't send new user mail to $email: " . $e->getMessage(), array('app' => 'settings'));
814
+		} catch (\Exception $e) {
815
+			$this->logger->error("Can't send new user mail to $email: ".$e->getMessage(), array('app' => 'settings'));
816 816
 			throw new OCSException('Sending email failed', 102);
817 817
 		}
818 818
 
Please login to merge, or discard this patch.
settings/Controller/UsersController.php 2 patches
Indentation   +940 added lines, -940 removed lines patch added patch discarded remove patch
@@ -58,945 +58,945 @@
 block discarded – undo
58 58
  * @package OC\Settings\Controller
59 59
  */
60 60
 class UsersController extends Controller {
61
-	/** @var IL10N */
62
-	private $l10n;
63
-	/** @var IUserSession */
64
-	private $userSession;
65
-	/** @var bool */
66
-	private $isAdmin;
67
-	/** @var IUserManager */
68
-	private $userManager;
69
-	/** @var IGroupManager */
70
-	private $groupManager;
71
-	/** @var IConfig */
72
-	private $config;
73
-	/** @var ILogger */
74
-	private $log;
75
-	/** @var IMailer */
76
-	private $mailer;
77
-	/** @var bool contains the state of the encryption app */
78
-	private $isEncryptionAppEnabled;
79
-	/** @var bool contains the state of the admin recovery setting */
80
-	private $isRestoreEnabled = false;
81
-	/** @var IAppManager */
82
-	private $appManager;
83
-	/** @var IAvatarManager */
84
-	private $avatarManager;
85
-	/** @var AccountManager */
86
-	private $accountManager;
87
-	/** @var ISecureRandom */
88
-	private $secureRandom;
89
-	/** @var NewUserMailHelper */
90
-	private $newUserMailHelper;
91
-	/** @var ITimeFactory */
92
-	private $timeFactory;
93
-	/** @var ICrypto */
94
-	private $crypto;
95
-	/** @var Manager */
96
-	private $keyManager;
97
-	/** @var IJobList */
98
-	private $jobList;
99
-
100
-	/**
101
-	 * @param string $appName
102
-	 * @param IRequest $request
103
-	 * @param IUserManager $userManager
104
-	 * @param IGroupManager $groupManager
105
-	 * @param IUserSession $userSession
106
-	 * @param IConfig $config
107
-	 * @param bool $isAdmin
108
-	 * @param IL10N $l10n
109
-	 * @param ILogger $log
110
-	 * @param IMailer $mailer
111
-	 * @param IURLGenerator $urlGenerator
112
-	 * @param IAppManager $appManager
113
-	 * @param IAvatarManager $avatarManager
114
-	 * @param AccountManager $accountManager
115
-	 * @param ISecureRandom $secureRandom
116
-	 * @param NewUserMailHelper $newUserMailHelper
117
-	 * @param ITimeFactory $timeFactory
118
-	 * @param ICrypto $crypto
119
-	 * @param Manager $keyManager
120
-	 * @param IJobList $jobList
121
-	 */
122
-	public function __construct($appName,
123
-								IRequest $request,
124
-								IUserManager $userManager,
125
-								IGroupManager $groupManager,
126
-								IUserSession $userSession,
127
-								IConfig $config,
128
-								$isAdmin,
129
-								IL10N $l10n,
130
-								ILogger $log,
131
-								IMailer $mailer,
132
-								IURLGenerator $urlGenerator,
133
-								IAppManager $appManager,
134
-								IAvatarManager $avatarManager,
135
-								AccountManager $accountManager,
136
-								ISecureRandom $secureRandom,
137
-								NewUserMailHelper $newUserMailHelper,
138
-								ITimeFactory $timeFactory,
139
-								ICrypto $crypto,
140
-								Manager $keyManager,
141
-								IJobList $jobList) {
142
-		parent::__construct($appName, $request);
143
-		$this->userManager = $userManager;
144
-		$this->groupManager = $groupManager;
145
-		$this->userSession = $userSession;
146
-		$this->config = $config;
147
-		$this->isAdmin = $isAdmin;
148
-		$this->l10n = $l10n;
149
-		$this->log = $log;
150
-		$this->mailer = $mailer;
151
-		$this->appManager = $appManager;
152
-		$this->avatarManager = $avatarManager;
153
-		$this->accountManager = $accountManager;
154
-		$this->secureRandom = $secureRandom;
155
-		$this->newUserMailHelper = $newUserMailHelper;
156
-		$this->timeFactory = $timeFactory;
157
-		$this->crypto = $crypto;
158
-		$this->keyManager = $keyManager;
159
-		$this->jobList = $jobList;
160
-
161
-		// check for encryption state - TODO see formatUserForIndex
162
-		$this->isEncryptionAppEnabled = $appManager->isEnabledForUser('encryption');
163
-		if($this->isEncryptionAppEnabled) {
164
-			// putting this directly in empty is possible in PHP 5.5+
165
-			$result = $config->getAppValue('encryption', 'recoveryAdminEnabled', 0);
166
-			$this->isRestoreEnabled = !empty($result);
167
-		}
168
-	}
169
-
170
-	/**
171
-	 * @param IUser $user
172
-	 * @param array $userGroups
173
-	 * @return array
174
-	 */
175
-	private function formatUserForIndex(IUser $user, array $userGroups = null) {
176
-
177
-		// TODO: eliminate this encryption specific code below and somehow
178
-		// hook in additional user info from other apps
179
-
180
-		// recovery isn't possible if admin or user has it disabled and encryption
181
-		// is enabled - so we eliminate the else paths in the conditional tree
182
-		// below
183
-		$restorePossible = false;
184
-
185
-		if ($this->isEncryptionAppEnabled) {
186
-			if ($this->isRestoreEnabled) {
187
-				// check for the users recovery setting
188
-				$recoveryMode = $this->config->getUserValue($user->getUID(), 'encryption', 'recoveryEnabled', '0');
189
-				// method call inside empty is possible with PHP 5.5+
190
-				$recoveryModeEnabled = !empty($recoveryMode);
191
-				if ($recoveryModeEnabled) {
192
-					// user also has recovery mode enabled
193
-					$restorePossible = true;
194
-				}
195
-			}
196
-		} else {
197
-			// recovery is possible if encryption is disabled (plain files are
198
-			// available)
199
-			$restorePossible = true;
200
-		}
201
-
202
-		$subAdminGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user);
203
-		foreach($subAdminGroups as $key => $subAdminGroup) {
204
-			$subAdminGroups[$key] = $subAdminGroup->getGID();
205
-		}
206
-
207
-		$displayName = $user->getEMailAddress();
208
-		if (is_null($displayName)) {
209
-			$displayName = '';
210
-		}
211
-
212
-		$avatarAvailable = false;
213
-		try {
214
-			$avatarAvailable = $this->avatarManager->getAvatar($user->getUID())->exists();
215
-		} catch (\Exception $e) {
216
-			//No avatar yet
217
-		}
218
-
219
-		return [
220
-			'name' => $user->getUID(),
221
-			'displayname' => $user->getDisplayName(),
222
-			'groups' => (empty($userGroups)) ? $this->groupManager->getUserGroupIds($user) : $userGroups,
223
-			'subadmin' => $subAdminGroups,
224
-			'quota' => $user->getQuota(),
225
-			'storageLocation' => $user->getHome(),
226
-			'lastLogin' => $user->getLastLogin() * 1000,
227
-			'backend' => $user->getBackendClassName(),
228
-			'email' => $displayName,
229
-			'isRestoreDisabled' => !$restorePossible,
230
-			'isAvatarAvailable' => $avatarAvailable,
231
-			'isEnabled' => $user->isEnabled(),
232
-		];
233
-	}
234
-
235
-	/**
236
-	 * @param array $userIDs Array with schema [$uid => $displayName]
237
-	 * @return IUser[]
238
-	 */
239
-	private function getUsersForUID(array $userIDs) {
240
-		$users = [];
241
-		foreach ($userIDs as $uid => $displayName) {
242
-			$users[$uid] = $this->userManager->get($uid);
243
-		}
244
-		return $users;
245
-	}
246
-
247
-	/**
248
-	 * @NoAdminRequired
249
-	 *
250
-	 * @param int $offset
251
-	 * @param int $limit
252
-	 * @param string $gid GID to filter for
253
-	 * @param string $pattern Pattern to search for in the username
254
-	 * @param string $backend Backend to filter for (class-name)
255
-	 * @return DataResponse
256
-	 *
257
-	 * TODO: Tidy up and write unit tests - code is mainly static method calls
258
-	 */
259
-	public function index($offset = 0, $limit = 10, $gid = '', $pattern = '', $backend = '') {
260
-		// Remove backends
261
-		if(!empty($backend)) {
262
-			$activeBackends = $this->userManager->getBackends();
263
-			$this->userManager->clearBackends();
264
-			foreach($activeBackends as $singleActiveBackend) {
265
-				if($backend === get_class($singleActiveBackend)) {
266
-					$this->userManager->registerBackend($singleActiveBackend);
267
-					break;
268
-				}
269
-			}
270
-		}
271
-
272
-		$users = [];
273
-		if ($this->isAdmin) {
274
-			if($gid !== '' && $gid !== '_disabledUsers') {
275
-				$batch = $this->getUsersForUID($this->groupManager->displayNamesInGroup($gid, $pattern, $limit, $offset));
276
-			} else {
277
-				$batch = $this->userManager->search($pattern, $limit, $offset);
278
-			}
279
-
280
-			foreach ($batch as $user) {
281
-				if( ($gid !== '_disabledUsers' && $user->isEnabled()) ||
282
-					($gid === '_disabledUsers' && !$user->isEnabled())
283
-				) {
284
-					$users[] = $this->formatUserForIndex($user);
285
-				}
286
-			}
287
-
288
-		} else {
289
-			$subAdminOfGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($this->userSession->getUser());
290
-			// New class returns IGroup[] so convert back
291
-			$gids = [];
292
-			foreach ($subAdminOfGroups as $group) {
293
-				$gids[] = $group->getGID();
294
-			}
295
-			$subAdminOfGroups = $gids;
296
-
297
-			// Set the $gid parameter to an empty value if the subadmin has no rights to access a specific group
298
-			if($gid !== '' && $gid !== '_disabledUsers' && !in_array($gid, $subAdminOfGroups)) {
299
-				$gid = '';
300
-			}
301
-
302
-			// Batch all groups the user is subadmin of when a group is specified
303
-			$batch = [];
304
-			if($gid === '') {
305
-				foreach($subAdminOfGroups as $group) {
306
-					$groupUsers = $this->groupManager->displayNamesInGroup($group, $pattern, $limit, $offset);
307
-
308
-					foreach($groupUsers as $uid => $displayName) {
309
-						$batch[$uid] = $displayName;
310
-					}
311
-				}
312
-			} else {
313
-				$batch = $this->groupManager->displayNamesInGroup($gid, $pattern, $limit, $offset);
314
-			}
315
-			$batch = $this->getUsersForUID($batch);
316
-
317
-			foreach ($batch as $user) {
318
-				// Only add the groups, this user is a subadmin of
319
-				$userGroups = array_values(array_intersect(
320
-					$this->groupManager->getUserGroupIds($user),
321
-					$subAdminOfGroups
322
-				));
323
-				if( ($gid !== '_disabledUsers' && $user->isEnabled()) ||
324
-					($gid === '_disabledUsers' && !$user->isEnabled())
325
-				) {
326
-					$users[] = $this->formatUserForIndex($user, $userGroups);
327
-				}
328
-			}
329
-		}
330
-
331
-		return new DataResponse($users);
332
-	}
333
-
334
-	/**
335
-	 * @NoAdminRequired
336
-	 * @PasswordConfirmationRequired
337
-	 *
338
-	 * @param string $username
339
-	 * @param string $password
340
-	 * @param array $groups
341
-	 * @param string $email
342
-	 * @return DataResponse
343
-	 */
344
-	public function create($username, $password, array $groups=[], $email='') {
345
-		if($email !== '' && !$this->mailer->validateMailAddress($email)) {
346
-			return new DataResponse(
347
-				[
348
-					'message' => (string)$this->l10n->t('Invalid mail address')
349
-				],
350
-				Http::STATUS_UNPROCESSABLE_ENTITY
351
-			);
352
-		}
353
-
354
-		$currentUser = $this->userSession->getUser();
355
-
356
-		if (!$this->isAdmin) {
357
-			if (!empty($groups)) {
358
-				foreach ($groups as $key => $group) {
359
-					$groupObject = $this->groupManager->get($group);
360
-					if($groupObject === null) {
361
-						unset($groups[$key]);
362
-						continue;
363
-					}
364
-
365
-					if (!$this->groupManager->getSubAdmin()->isSubAdminofGroup($currentUser, $groupObject)) {
366
-						unset($groups[$key]);
367
-					}
368
-				}
369
-			}
370
-
371
-			if (empty($groups)) {
372
-				return new DataResponse(
373
-					[
374
-						'message' => $this->l10n->t('No valid group selected'),
375
-					],
376
-					Http::STATUS_FORBIDDEN
377
-				);
378
-			}
379
-		}
380
-
381
-		if ($this->userManager->userExists($username)) {
382
-			return new DataResponse(
383
-				[
384
-					'message' => (string)$this->l10n->t('A user with that name already exists.')
385
-				],
386
-				Http::STATUS_CONFLICT
387
-			);
388
-		}
389
-
390
-		$generatePasswordResetToken = false;
391
-		if ($password === '') {
392
-			if ($email === '') {
393
-				return new DataResponse(
394
-					[
395
-						'message' => (string)$this->l10n->t('To send a password link to the user an email address is required.')
396
-					],
397
-					Http::STATUS_UNPROCESSABLE_ENTITY
398
-				);
399
-			}
400
-
401
-			$password = $this->secureRandom->generate(32);
402
-			$generatePasswordResetToken = true;
403
-		}
404
-
405
-		try {
406
-			$user = $this->userManager->createUser($username, $password);
407
-		} catch (\Exception $exception) {
408
-			$message = $exception->getMessage();
409
-			if (!$message) {
410
-				$message = $this->l10n->t('Unable to create user.');
411
-			}
412
-			return new DataResponse(
413
-				[
414
-					'message' => (string) $message,
415
-				],
416
-				Http::STATUS_FORBIDDEN
417
-			);
418
-		}
419
-
420
-		if($user instanceof IUser) {
421
-			if($groups !== null) {
422
-				foreach($groups as $groupName) {
423
-					$group = $this->groupManager->get($groupName);
424
-
425
-					if(empty($group)) {
426
-						$group = $this->groupManager->createGroup($groupName);
427
-					}
428
-					$group->addUser($user);
429
-				}
430
-			}
431
-			/**
432
-			 * Send new user mail only if a mail is set
433
-			 */
434
-			if($email !== '') {
435
-				$user->setEMailAddress($email);
436
-				try {
437
-					$emailTemplate = $this->newUserMailHelper->generateTemplate($user, $generatePasswordResetToken);
438
-					$this->newUserMailHelper->sendMail($user, $emailTemplate);
439
-				} catch(\Exception $e) {
440
-					$this->log->error("Can't send new user mail to $email: " . $e->getMessage(), ['app' => 'settings']);
441
-				}
442
-			}
443
-			// fetch users groups
444
-			$userGroups = $this->groupManager->getUserGroupIds($user);
445
-
446
-			return new DataResponse(
447
-				$this->formatUserForIndex($user, $userGroups),
448
-				Http::STATUS_CREATED
449
-			);
450
-		}
451
-
452
-		return new DataResponse(
453
-			[
454
-				'message' => (string) $this->l10n->t('Unable to create user.')
455
-			],
456
-			Http::STATUS_FORBIDDEN
457
-		);
458
-
459
-	}
460
-
461
-	/**
462
-	 * @NoAdminRequired
463
-	 * @PasswordConfirmationRequired
464
-	 *
465
-	 * @param string $id
466
-	 * @return DataResponse
467
-	 */
468
-	public function destroy($id) {
469
-		$userId = $this->userSession->getUser()->getUID();
470
-		$user = $this->userManager->get($id);
471
-
472
-		if($userId === $id) {
473
-			return new DataResponse(
474
-				[
475
-					'status' => 'error',
476
-					'data' => [
477
-						'message' => (string) $this->l10n->t('Unable to delete user.')
478
-					]
479
-				],
480
-				Http::STATUS_FORBIDDEN
481
-			);
482
-		}
483
-
484
-		if(!$this->isAdmin && !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)) {
485
-			return new DataResponse(
486
-				[
487
-					'status' => 'error',
488
-					'data' => [
489
-						'message' => (string)$this->l10n->t('Authentication error')
490
-					]
491
-				],
492
-				Http::STATUS_FORBIDDEN
493
-			);
494
-		}
495
-
496
-		if($user) {
497
-			if($user->delete()) {
498
-				return new DataResponse(
499
-					[
500
-						'status' => 'success',
501
-						'data' => [
502
-							'username' => $id
503
-						]
504
-					],
505
-					Http::STATUS_NO_CONTENT
506
-				);
507
-			}
508
-		}
509
-
510
-		return new DataResponse(
511
-			[
512
-				'status' => 'error',
513
-				'data' => [
514
-					'message' => (string)$this->l10n->t('Unable to delete user.')
515
-				]
516
-			],
517
-			Http::STATUS_FORBIDDEN
518
-		);
519
-	}
520
-
521
-	/**
522
-	 * @NoAdminRequired
523
-	 *
524
-	 * @param string $id
525
-	 * @param int $enabled
526
-	 * @return DataResponse
527
-	 */
528
-	public function setEnabled($id, $enabled) {
529
-		$enabled = (bool)$enabled;
530
-		if($enabled) {
531
-			$errorMsgGeneral = (string) $this->l10n->t('Error while enabling user.');
532
-		} else {
533
-			$errorMsgGeneral = (string) $this->l10n->t('Error while disabling user.');
534
-		}
535
-
536
-		$userId = $this->userSession->getUser()->getUID();
537
-		$user = $this->userManager->get($id);
538
-
539
-		if ($userId === $id) {
540
-			return new DataResponse(
541
-				[
542
-					'status' => 'error',
543
-					'data' => [
544
-						'message' => $errorMsgGeneral
545
-					]
546
-				], Http::STATUS_FORBIDDEN
547
-			);
548
-		}
549
-
550
-		if($user) {
551
-			if (!$this->isAdmin && !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)) {
552
-				return new DataResponse(
553
-					[
554
-						'status' => 'error',
555
-						'data' => [
556
-							'message' => (string) $this->l10n->t('Authentication error')
557
-						]
558
-					],
559
-					Http::STATUS_FORBIDDEN
560
-				);
561
-			}
562
-
563
-			$user->setEnabled($enabled);
564
-			return new DataResponse(
565
-				[
566
-					'status' => 'success',
567
-					'data' => [
568
-						'username' => $id,
569
-						'enabled' => $enabled
570
-					]
571
-				]
572
-			);
573
-		} else {
574
-			return new DataResponse(
575
-				[
576
-					'status' => 'error',
577
-					'data' => [
578
-						'message' => $errorMsgGeneral
579
-					]
580
-				],
581
-				Http::STATUS_FORBIDDEN
582
-			);
583
-		}
584
-
585
-	}
586
-
587
-	/**
588
-	 * Set the mail address of a user
589
-	 *
590
-	 * @NoAdminRequired
591
-	 * @NoSubadminRequired
592
-	 * @PasswordConfirmationRequired
593
-	 *
594
-	 * @param string $account
595
-	 * @param bool $onlyVerificationCode only return verification code without updating the data
596
-	 * @return DataResponse
597
-	 */
598
-	public function getVerificationCode($account, $onlyVerificationCode) {
599
-
600
-		$user = $this->userSession->getUser();
601
-
602
-		if ($user === null) {
603
-			return new DataResponse([], Http::STATUS_BAD_REQUEST);
604
-		}
605
-
606
-		$accountData = $this->accountManager->getUser($user);
607
-		$cloudId = $user->getCloudId();
608
-		$message = "Use my Federated Cloud ID to share with me: " . $cloudId;
609
-		$signature = $this->signMessage($user, $message);
610
-
611
-		$code = $message . ' ' . $signature;
612
-		$codeMd5 = $message . ' ' . md5($signature);
613
-
614
-		switch ($account) {
615
-			case 'verify-twitter':
616
-				$accountData[AccountManager::PROPERTY_TWITTER]['verified'] = AccountManager::VERIFICATION_IN_PROGRESS;
617
-				$msg = $this->l10n->t('In order to verify your Twitter account, post the following tweet on Twitter (please make sure to post it without any line breaks):');
618
-				$code = $codeMd5;
619
-				$type = AccountManager::PROPERTY_TWITTER;
620
-				$data = $accountData[AccountManager::PROPERTY_TWITTER]['value'];
621
-				$accountData[AccountManager::PROPERTY_TWITTER]['signature'] = $signature;
622
-				break;
623
-			case 'verify-website':
624
-				$accountData[AccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::VERIFICATION_IN_PROGRESS;
625
-				$msg = $this->l10n->t('In order to verify your Website, store the following content in your web-root at \'.well-known/CloudIdVerificationCode.txt\' (please make sure that the complete text is in one line):');
626
-				$type = AccountManager::PROPERTY_WEBSITE;
627
-				$data = $accountData[AccountManager::PROPERTY_WEBSITE]['value'];
628
-				$accountData[AccountManager::PROPERTY_WEBSITE]['signature'] = $signature;
629
-				break;
630
-			default:
631
-				return new DataResponse([], Http::STATUS_BAD_REQUEST);
632
-		}
633
-
634
-		if ($onlyVerificationCode === false) {
635
-			$this->accountManager->updateUser($user, $accountData);
636
-
637
-			$this->jobList->add('OC\Settings\BackgroundJobs\VerifyUserData',
638
-				[
639
-					'verificationCode' => $code,
640
-					'data' => $data,
641
-					'type' => $type,
642
-					'uid' => $user->getUID(),
643
-					'try' => 0,
644
-					'lastRun' => $this->getCurrentTime()
645
-				]
646
-			);
647
-		}
648
-
649
-		return new DataResponse(['msg' => $msg, 'code' => $code]);
650
-	}
651
-
652
-	/**
653
-	 * get current timestamp
654
-	 *
655
-	 * @return int
656
-	 */
657
-	protected function getCurrentTime() {
658
-		return time();
659
-	}
660
-
661
-	/**
662
-	 * sign message with users private key
663
-	 *
664
-	 * @param IUser $user
665
-	 * @param string $message
666
-	 *
667
-	 * @return string base64 encoded signature
668
-	 */
669
-	protected function signMessage(IUser $user, $message) {
670
-		$privateKey = $this->keyManager->getKey($user)->getPrivate();
671
-		openssl_sign(json_encode($message), $signature, $privateKey, OPENSSL_ALGO_SHA512);
672
-		$signatureBase64 = base64_encode($signature);
673
-
674
-		return $signatureBase64;
675
-	}
676
-
677
-	/**
678
-	 * @NoAdminRequired
679
-	 * @NoSubadminRequired
680
-	 * @PasswordConfirmationRequired
681
-	 *
682
-	 * @param string $avatarScope
683
-	 * @param string $displayname
684
-	 * @param string $displaynameScope
685
-	 * @param string $phone
686
-	 * @param string $phoneScope
687
-	 * @param string $email
688
-	 * @param string $emailScope
689
-	 * @param string $website
690
-	 * @param string $websiteScope
691
-	 * @param string $address
692
-	 * @param string $addressScope
693
-	 * @param string $twitter
694
-	 * @param string $twitterScope
695
-	 * @return DataResponse
696
-	 */
697
-	public function setUserSettings($avatarScope,
698
-									$displayname,
699
-									$displaynameScope,
700
-									$phone,
701
-									$phoneScope,
702
-									$email,
703
-									$emailScope,
704
-									$website,
705
-									$websiteScope,
706
-									$address,
707
-									$addressScope,
708
-									$twitter,
709
-									$twitterScope
710
-	) {
711
-
712
-		if (!empty($email) && !$this->mailer->validateMailAddress($email)) {
713
-			return new DataResponse(
714
-				[
715
-					'status' => 'error',
716
-					'data' => [
717
-						'message' => (string) $this->l10n->t('Invalid mail address')
718
-					]
719
-				],
720
-				Http::STATUS_UNPROCESSABLE_ENTITY
721
-			);
722
-		}
723
-
724
-		$user = $this->userSession->getUser();
725
-
726
-		$data = $this->accountManager->getUser($user);
727
-
728
-		$data[AccountManager::PROPERTY_AVATAR] =  ['scope' => $avatarScope];
729
-		if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) {
730
-			$data[AccountManager::PROPERTY_DISPLAYNAME] = ['value' => $displayname, 'scope' => $displaynameScope];
731
-			$data[AccountManager::PROPERTY_EMAIL] = ['value' => $email, 'scope' => $emailScope];
732
-		}
733
-
734
-		if ($this->appManager->isEnabledForUser('federatedfilesharing')) {
735
-			$federatedFileSharing = new \OCA\FederatedFileSharing\AppInfo\Application();
736
-			$shareProvider = $federatedFileSharing->getFederatedShareProvider();
737
-			if ($shareProvider->isLookupServerUploadEnabled()) {
738
-				$data[AccountManager::PROPERTY_WEBSITE] = ['value' => $website, 'scope' => $websiteScope];
739
-				$data[AccountManager::PROPERTY_ADDRESS] = ['value' => $address, 'scope' => $addressScope];
740
-				$data[AccountManager::PROPERTY_PHONE] = ['value' => $phone, 'scope' => $phoneScope];
741
-				$data[AccountManager::PROPERTY_TWITTER] = ['value' => $twitter, 'scope' => $twitterScope];
742
-			}
743
-		}
744
-
745
-		try {
746
-			$this->saveUserSettings($user, $data);
747
-			return new DataResponse(
748
-				[
749
-					'status' => 'success',
750
-					'data' => [
751
-						'userId' => $user->getUID(),
752
-						'avatarScope' => $data[AccountManager::PROPERTY_AVATAR]['scope'],
753
-						'displayname' => $data[AccountManager::PROPERTY_DISPLAYNAME]['value'],
754
-						'displaynameScope' => $data[AccountManager::PROPERTY_DISPLAYNAME]['scope'],
755
-						'email' => $data[AccountManager::PROPERTY_EMAIL]['value'],
756
-						'emailScope' => $data[AccountManager::PROPERTY_EMAIL]['scope'],
757
-						'website' => $data[AccountManager::PROPERTY_WEBSITE]['value'],
758
-						'websiteScope' => $data[AccountManager::PROPERTY_WEBSITE]['scope'],
759
-						'address' => $data[AccountManager::PROPERTY_ADDRESS]['value'],
760
-						'addressScope' => $data[AccountManager::PROPERTY_ADDRESS]['scope'],
761
-						'message' => (string) $this->l10n->t('Settings saved')
762
-					]
763
-				],
764
-				Http::STATUS_OK
765
-			);
766
-		} catch (ForbiddenException $e) {
767
-			return new DataResponse([
768
-				'status' => 'error',
769
-				'data' => [
770
-					'message' => $e->getMessage()
771
-				],
772
-			]);
773
-		}
774
-
775
-	}
776
-
777
-
778
-	/**
779
-	 * update account manager with new user data
780
-	 *
781
-	 * @param IUser $user
782
-	 * @param array $data
783
-	 * @throws ForbiddenException
784
-	 */
785
-	protected function saveUserSettings(IUser $user, $data) {
786
-
787
-		// keep the user back-end up-to-date with the latest display name and email
788
-		// address
789
-		$oldDisplayName = $user->getDisplayName();
790
-		$oldDisplayName = is_null($oldDisplayName) ? '' : $oldDisplayName;
791
-		if (isset($data[AccountManager::PROPERTY_DISPLAYNAME]['value'])
792
-			&& $oldDisplayName !== $data[AccountManager::PROPERTY_DISPLAYNAME]['value']
793
-		) {
794
-			$result = $user->setDisplayName($data[AccountManager::PROPERTY_DISPLAYNAME]['value']);
795
-			if ($result === false) {
796
-				throw new ForbiddenException($this->l10n->t('Unable to change full name'));
797
-			}
798
-		}
799
-
800
-		$oldEmailAddress = $user->getEMailAddress();
801
-		$oldEmailAddress = is_null($oldEmailAddress) ? '' : $oldEmailAddress;
802
-		if (isset($data[AccountManager::PROPERTY_EMAIL]['value'])
803
-			&& $oldEmailAddress !== $data[AccountManager::PROPERTY_EMAIL]['value']
804
-		) {
805
-			// this is the only permission a backend provides and is also used
806
-			// for the permission of setting a email address
807
-			if (!$user->canChangeDisplayName()) {
808
-				throw new ForbiddenException($this->l10n->t('Unable to change email address'));
809
-			}
810
-			$user->setEMailAddress($data[AccountManager::PROPERTY_EMAIL]['value']);
811
-		}
812
-
813
-		$this->accountManager->updateUser($user, $data);
814
-	}
815
-
816
-	/**
817
-	 * Count all unique users visible for the current admin/subadmin.
818
-	 *
819
-	 * @NoAdminRequired
820
-	 *
821
-	 * @return DataResponse
822
-	 */
823
-	public function stats() {
824
-		$userCount = 0;
825
-		if ($this->isAdmin) {
826
-			$countByBackend = $this->userManager->countUsers();
827
-
828
-			if (!empty($countByBackend)) {
829
-				foreach ($countByBackend as $count) {
830
-					$userCount += $count;
831
-				}
832
-			}
833
-		} else {
834
-			$groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($this->userSession->getUser());
835
-
836
-			$uniqueUsers = [];
837
-			foreach ($groups as $group) {
838
-				foreach($group->getUsers() as $uid => $displayName) {
839
-					$uniqueUsers[$uid] = true;
840
-				}
841
-			}
842
-
843
-			$userCount = count($uniqueUsers);
844
-		}
845
-
846
-		return new DataResponse(
847
-			[
848
-				'totalUsers' => $userCount
849
-			]
850
-		);
851
-	}
852
-
853
-
854
-	/**
855
-	 * Set the displayName of a user
856
-	 *
857
-	 * @NoAdminRequired
858
-	 * @NoSubadminRequired
859
-	 * @PasswordConfirmationRequired
860
-	 * @todo merge into saveUserSettings
861
-	 *
862
-	 * @param string $username
863
-	 * @param string $displayName
864
-	 * @return DataResponse
865
-	 */
866
-	public function setDisplayName($username, $displayName) {
867
-		$currentUser = $this->userSession->getUser();
868
-		$user = $this->userManager->get($username);
869
-
870
-		if ($user === null ||
871
-			!$user->canChangeDisplayName() ||
872
-			(
873
-				!$this->groupManager->isAdmin($currentUser->getUID()) &&
874
-				!$this->groupManager->getSubAdmin()->isUserAccessible($currentUser, $user) &&
875
-				$currentUser->getUID() !== $username
876
-
877
-			)
878
-		) {
879
-			return new DataResponse([
880
-				'status' => 'error',
881
-				'data' => [
882
-					'message' => $this->l10n->t('Authentication error'),
883
-				],
884
-			]);
885
-		}
886
-
887
-		$userData = $this->accountManager->getUser($user);
888
-		$userData[AccountManager::PROPERTY_DISPLAYNAME]['value'] = $displayName;
889
-
890
-
891
-		try {
892
-			$this->saveUserSettings($user, $userData);
893
-			return new DataResponse([
894
-				'status' => 'success',
895
-				'data' => [
896
-					'message' => $this->l10n->t('Your full name has been changed.'),
897
-					'username' => $username,
898
-					'displayName' => $displayName,
899
-				],
900
-			]);
901
-		} catch (ForbiddenException $e) {
902
-			return new DataResponse([
903
-				'status' => 'error',
904
-				'data' => [
905
-					'message' => $e->getMessage(),
906
-					'displayName' => $user->getDisplayName(),
907
-				],
908
-			]);
909
-		}
910
-	}
911
-
912
-	/**
913
-	 * Set the mail address of a user
914
-	 *
915
-	 * @NoAdminRequired
916
-	 * @NoSubadminRequired
917
-	 * @PasswordConfirmationRequired
918
-	 *
919
-	 * @param string $id
920
-	 * @param string $mailAddress
921
-	 * @return DataResponse
922
-	 */
923
-	public function setEMailAddress($id, $mailAddress) {
924
-		$user = $this->userManager->get($id);
925
-		if (!$this->isAdmin
926
-			&& !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)
927
-		) {
928
-			return new DataResponse(
929
-				[
930
-					'status' => 'error',
931
-					'data' => [
932
-						'message' => (string) $this->l10n->t('Forbidden')
933
-					]
934
-				],
935
-				Http::STATUS_FORBIDDEN
936
-			);
937
-		}
938
-
939
-		if($mailAddress !== '' && !$this->mailer->validateMailAddress($mailAddress)) {
940
-			return new DataResponse(
941
-				[
942
-					'status' => 'error',
943
-					'data' => [
944
-						'message' => (string) $this->l10n->t('Invalid mail address')
945
-					]
946
-				],
947
-				Http::STATUS_UNPROCESSABLE_ENTITY
948
-			);
949
-		}
950
-
951
-		if (!$user) {
952
-			return new DataResponse(
953
-				[
954
-					'status' => 'error',
955
-					'data' => [
956
-						'message' => (string) $this->l10n->t('Invalid user')
957
-					]
958
-				],
959
-				Http::STATUS_UNPROCESSABLE_ENTITY
960
-			);
961
-		}
962
-		// this is the only permission a backend provides and is also used
963
-		// for the permission of setting a email address
964
-		if (!$user->canChangeDisplayName()) {
965
-			return new DataResponse(
966
-				[
967
-					'status' => 'error',
968
-					'data' => [
969
-						'message' => (string) $this->l10n->t('Unable to change mail address')
970
-					]
971
-				],
972
-				Http::STATUS_FORBIDDEN
973
-			);
974
-		}
975
-
976
-		$userData = $this->accountManager->getUser($user);
977
-		$userData[AccountManager::PROPERTY_EMAIL]['value'] = $mailAddress;
978
-
979
-		try {
980
-			$this->saveUserSettings($user, $userData);
981
-			return new DataResponse(
982
-				[
983
-					'status' => 'success',
984
-					'data' => [
985
-						'username' => $id,
986
-						'mailAddress' => $mailAddress,
987
-						'message' => (string) $this->l10n->t('Email saved')
988
-					]
989
-				],
990
-				Http::STATUS_OK
991
-			);
992
-		} catch (ForbiddenException $e) {
993
-			return new DataResponse([
994
-				'status' => 'error',
995
-				'data' => [
996
-					'message' => $e->getMessage()
997
-				],
998
-			]);
999
-		}
1000
-	}
61
+    /** @var IL10N */
62
+    private $l10n;
63
+    /** @var IUserSession */
64
+    private $userSession;
65
+    /** @var bool */
66
+    private $isAdmin;
67
+    /** @var IUserManager */
68
+    private $userManager;
69
+    /** @var IGroupManager */
70
+    private $groupManager;
71
+    /** @var IConfig */
72
+    private $config;
73
+    /** @var ILogger */
74
+    private $log;
75
+    /** @var IMailer */
76
+    private $mailer;
77
+    /** @var bool contains the state of the encryption app */
78
+    private $isEncryptionAppEnabled;
79
+    /** @var bool contains the state of the admin recovery setting */
80
+    private $isRestoreEnabled = false;
81
+    /** @var IAppManager */
82
+    private $appManager;
83
+    /** @var IAvatarManager */
84
+    private $avatarManager;
85
+    /** @var AccountManager */
86
+    private $accountManager;
87
+    /** @var ISecureRandom */
88
+    private $secureRandom;
89
+    /** @var NewUserMailHelper */
90
+    private $newUserMailHelper;
91
+    /** @var ITimeFactory */
92
+    private $timeFactory;
93
+    /** @var ICrypto */
94
+    private $crypto;
95
+    /** @var Manager */
96
+    private $keyManager;
97
+    /** @var IJobList */
98
+    private $jobList;
99
+
100
+    /**
101
+     * @param string $appName
102
+     * @param IRequest $request
103
+     * @param IUserManager $userManager
104
+     * @param IGroupManager $groupManager
105
+     * @param IUserSession $userSession
106
+     * @param IConfig $config
107
+     * @param bool $isAdmin
108
+     * @param IL10N $l10n
109
+     * @param ILogger $log
110
+     * @param IMailer $mailer
111
+     * @param IURLGenerator $urlGenerator
112
+     * @param IAppManager $appManager
113
+     * @param IAvatarManager $avatarManager
114
+     * @param AccountManager $accountManager
115
+     * @param ISecureRandom $secureRandom
116
+     * @param NewUserMailHelper $newUserMailHelper
117
+     * @param ITimeFactory $timeFactory
118
+     * @param ICrypto $crypto
119
+     * @param Manager $keyManager
120
+     * @param IJobList $jobList
121
+     */
122
+    public function __construct($appName,
123
+                                IRequest $request,
124
+                                IUserManager $userManager,
125
+                                IGroupManager $groupManager,
126
+                                IUserSession $userSession,
127
+                                IConfig $config,
128
+                                $isAdmin,
129
+                                IL10N $l10n,
130
+                                ILogger $log,
131
+                                IMailer $mailer,
132
+                                IURLGenerator $urlGenerator,
133
+                                IAppManager $appManager,
134
+                                IAvatarManager $avatarManager,
135
+                                AccountManager $accountManager,
136
+                                ISecureRandom $secureRandom,
137
+                                NewUserMailHelper $newUserMailHelper,
138
+                                ITimeFactory $timeFactory,
139
+                                ICrypto $crypto,
140
+                                Manager $keyManager,
141
+                                IJobList $jobList) {
142
+        parent::__construct($appName, $request);
143
+        $this->userManager = $userManager;
144
+        $this->groupManager = $groupManager;
145
+        $this->userSession = $userSession;
146
+        $this->config = $config;
147
+        $this->isAdmin = $isAdmin;
148
+        $this->l10n = $l10n;
149
+        $this->log = $log;
150
+        $this->mailer = $mailer;
151
+        $this->appManager = $appManager;
152
+        $this->avatarManager = $avatarManager;
153
+        $this->accountManager = $accountManager;
154
+        $this->secureRandom = $secureRandom;
155
+        $this->newUserMailHelper = $newUserMailHelper;
156
+        $this->timeFactory = $timeFactory;
157
+        $this->crypto = $crypto;
158
+        $this->keyManager = $keyManager;
159
+        $this->jobList = $jobList;
160
+
161
+        // check for encryption state - TODO see formatUserForIndex
162
+        $this->isEncryptionAppEnabled = $appManager->isEnabledForUser('encryption');
163
+        if($this->isEncryptionAppEnabled) {
164
+            // putting this directly in empty is possible in PHP 5.5+
165
+            $result = $config->getAppValue('encryption', 'recoveryAdminEnabled', 0);
166
+            $this->isRestoreEnabled = !empty($result);
167
+        }
168
+    }
169
+
170
+    /**
171
+     * @param IUser $user
172
+     * @param array $userGroups
173
+     * @return array
174
+     */
175
+    private function formatUserForIndex(IUser $user, array $userGroups = null) {
176
+
177
+        // TODO: eliminate this encryption specific code below and somehow
178
+        // hook in additional user info from other apps
179
+
180
+        // recovery isn't possible if admin or user has it disabled and encryption
181
+        // is enabled - so we eliminate the else paths in the conditional tree
182
+        // below
183
+        $restorePossible = false;
184
+
185
+        if ($this->isEncryptionAppEnabled) {
186
+            if ($this->isRestoreEnabled) {
187
+                // check for the users recovery setting
188
+                $recoveryMode = $this->config->getUserValue($user->getUID(), 'encryption', 'recoveryEnabled', '0');
189
+                // method call inside empty is possible with PHP 5.5+
190
+                $recoveryModeEnabled = !empty($recoveryMode);
191
+                if ($recoveryModeEnabled) {
192
+                    // user also has recovery mode enabled
193
+                    $restorePossible = true;
194
+                }
195
+            }
196
+        } else {
197
+            // recovery is possible if encryption is disabled (plain files are
198
+            // available)
199
+            $restorePossible = true;
200
+        }
201
+
202
+        $subAdminGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user);
203
+        foreach($subAdminGroups as $key => $subAdminGroup) {
204
+            $subAdminGroups[$key] = $subAdminGroup->getGID();
205
+        }
206
+
207
+        $displayName = $user->getEMailAddress();
208
+        if (is_null($displayName)) {
209
+            $displayName = '';
210
+        }
211
+
212
+        $avatarAvailable = false;
213
+        try {
214
+            $avatarAvailable = $this->avatarManager->getAvatar($user->getUID())->exists();
215
+        } catch (\Exception $e) {
216
+            //No avatar yet
217
+        }
218
+
219
+        return [
220
+            'name' => $user->getUID(),
221
+            'displayname' => $user->getDisplayName(),
222
+            'groups' => (empty($userGroups)) ? $this->groupManager->getUserGroupIds($user) : $userGroups,
223
+            'subadmin' => $subAdminGroups,
224
+            'quota' => $user->getQuota(),
225
+            'storageLocation' => $user->getHome(),
226
+            'lastLogin' => $user->getLastLogin() * 1000,
227
+            'backend' => $user->getBackendClassName(),
228
+            'email' => $displayName,
229
+            'isRestoreDisabled' => !$restorePossible,
230
+            'isAvatarAvailable' => $avatarAvailable,
231
+            'isEnabled' => $user->isEnabled(),
232
+        ];
233
+    }
234
+
235
+    /**
236
+     * @param array $userIDs Array with schema [$uid => $displayName]
237
+     * @return IUser[]
238
+     */
239
+    private function getUsersForUID(array $userIDs) {
240
+        $users = [];
241
+        foreach ($userIDs as $uid => $displayName) {
242
+            $users[$uid] = $this->userManager->get($uid);
243
+        }
244
+        return $users;
245
+    }
246
+
247
+    /**
248
+     * @NoAdminRequired
249
+     *
250
+     * @param int $offset
251
+     * @param int $limit
252
+     * @param string $gid GID to filter for
253
+     * @param string $pattern Pattern to search for in the username
254
+     * @param string $backend Backend to filter for (class-name)
255
+     * @return DataResponse
256
+     *
257
+     * TODO: Tidy up and write unit tests - code is mainly static method calls
258
+     */
259
+    public function index($offset = 0, $limit = 10, $gid = '', $pattern = '', $backend = '') {
260
+        // Remove backends
261
+        if(!empty($backend)) {
262
+            $activeBackends = $this->userManager->getBackends();
263
+            $this->userManager->clearBackends();
264
+            foreach($activeBackends as $singleActiveBackend) {
265
+                if($backend === get_class($singleActiveBackend)) {
266
+                    $this->userManager->registerBackend($singleActiveBackend);
267
+                    break;
268
+                }
269
+            }
270
+        }
271
+
272
+        $users = [];
273
+        if ($this->isAdmin) {
274
+            if($gid !== '' && $gid !== '_disabledUsers') {
275
+                $batch = $this->getUsersForUID($this->groupManager->displayNamesInGroup($gid, $pattern, $limit, $offset));
276
+            } else {
277
+                $batch = $this->userManager->search($pattern, $limit, $offset);
278
+            }
279
+
280
+            foreach ($batch as $user) {
281
+                if( ($gid !== '_disabledUsers' && $user->isEnabled()) ||
282
+                    ($gid === '_disabledUsers' && !$user->isEnabled())
283
+                ) {
284
+                    $users[] = $this->formatUserForIndex($user);
285
+                }
286
+            }
287
+
288
+        } else {
289
+            $subAdminOfGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($this->userSession->getUser());
290
+            // New class returns IGroup[] so convert back
291
+            $gids = [];
292
+            foreach ($subAdminOfGroups as $group) {
293
+                $gids[] = $group->getGID();
294
+            }
295
+            $subAdminOfGroups = $gids;
296
+
297
+            // Set the $gid parameter to an empty value if the subadmin has no rights to access a specific group
298
+            if($gid !== '' && $gid !== '_disabledUsers' && !in_array($gid, $subAdminOfGroups)) {
299
+                $gid = '';
300
+            }
301
+
302
+            // Batch all groups the user is subadmin of when a group is specified
303
+            $batch = [];
304
+            if($gid === '') {
305
+                foreach($subAdminOfGroups as $group) {
306
+                    $groupUsers = $this->groupManager->displayNamesInGroup($group, $pattern, $limit, $offset);
307
+
308
+                    foreach($groupUsers as $uid => $displayName) {
309
+                        $batch[$uid] = $displayName;
310
+                    }
311
+                }
312
+            } else {
313
+                $batch = $this->groupManager->displayNamesInGroup($gid, $pattern, $limit, $offset);
314
+            }
315
+            $batch = $this->getUsersForUID($batch);
316
+
317
+            foreach ($batch as $user) {
318
+                // Only add the groups, this user is a subadmin of
319
+                $userGroups = array_values(array_intersect(
320
+                    $this->groupManager->getUserGroupIds($user),
321
+                    $subAdminOfGroups
322
+                ));
323
+                if( ($gid !== '_disabledUsers' && $user->isEnabled()) ||
324
+                    ($gid === '_disabledUsers' && !$user->isEnabled())
325
+                ) {
326
+                    $users[] = $this->formatUserForIndex($user, $userGroups);
327
+                }
328
+            }
329
+        }
330
+
331
+        return new DataResponse($users);
332
+    }
333
+
334
+    /**
335
+     * @NoAdminRequired
336
+     * @PasswordConfirmationRequired
337
+     *
338
+     * @param string $username
339
+     * @param string $password
340
+     * @param array $groups
341
+     * @param string $email
342
+     * @return DataResponse
343
+     */
344
+    public function create($username, $password, array $groups=[], $email='') {
345
+        if($email !== '' && !$this->mailer->validateMailAddress($email)) {
346
+            return new DataResponse(
347
+                [
348
+                    'message' => (string)$this->l10n->t('Invalid mail address')
349
+                ],
350
+                Http::STATUS_UNPROCESSABLE_ENTITY
351
+            );
352
+        }
353
+
354
+        $currentUser = $this->userSession->getUser();
355
+
356
+        if (!$this->isAdmin) {
357
+            if (!empty($groups)) {
358
+                foreach ($groups as $key => $group) {
359
+                    $groupObject = $this->groupManager->get($group);
360
+                    if($groupObject === null) {
361
+                        unset($groups[$key]);
362
+                        continue;
363
+                    }
364
+
365
+                    if (!$this->groupManager->getSubAdmin()->isSubAdminofGroup($currentUser, $groupObject)) {
366
+                        unset($groups[$key]);
367
+                    }
368
+                }
369
+            }
370
+
371
+            if (empty($groups)) {
372
+                return new DataResponse(
373
+                    [
374
+                        'message' => $this->l10n->t('No valid group selected'),
375
+                    ],
376
+                    Http::STATUS_FORBIDDEN
377
+                );
378
+            }
379
+        }
380
+
381
+        if ($this->userManager->userExists($username)) {
382
+            return new DataResponse(
383
+                [
384
+                    'message' => (string)$this->l10n->t('A user with that name already exists.')
385
+                ],
386
+                Http::STATUS_CONFLICT
387
+            );
388
+        }
389
+
390
+        $generatePasswordResetToken = false;
391
+        if ($password === '') {
392
+            if ($email === '') {
393
+                return new DataResponse(
394
+                    [
395
+                        'message' => (string)$this->l10n->t('To send a password link to the user an email address is required.')
396
+                    ],
397
+                    Http::STATUS_UNPROCESSABLE_ENTITY
398
+                );
399
+            }
400
+
401
+            $password = $this->secureRandom->generate(32);
402
+            $generatePasswordResetToken = true;
403
+        }
404
+
405
+        try {
406
+            $user = $this->userManager->createUser($username, $password);
407
+        } catch (\Exception $exception) {
408
+            $message = $exception->getMessage();
409
+            if (!$message) {
410
+                $message = $this->l10n->t('Unable to create user.');
411
+            }
412
+            return new DataResponse(
413
+                [
414
+                    'message' => (string) $message,
415
+                ],
416
+                Http::STATUS_FORBIDDEN
417
+            );
418
+        }
419
+
420
+        if($user instanceof IUser) {
421
+            if($groups !== null) {
422
+                foreach($groups as $groupName) {
423
+                    $group = $this->groupManager->get($groupName);
424
+
425
+                    if(empty($group)) {
426
+                        $group = $this->groupManager->createGroup($groupName);
427
+                    }
428
+                    $group->addUser($user);
429
+                }
430
+            }
431
+            /**
432
+             * Send new user mail only if a mail is set
433
+             */
434
+            if($email !== '') {
435
+                $user->setEMailAddress($email);
436
+                try {
437
+                    $emailTemplate = $this->newUserMailHelper->generateTemplate($user, $generatePasswordResetToken);
438
+                    $this->newUserMailHelper->sendMail($user, $emailTemplate);
439
+                } catch(\Exception $e) {
440
+                    $this->log->error("Can't send new user mail to $email: " . $e->getMessage(), ['app' => 'settings']);
441
+                }
442
+            }
443
+            // fetch users groups
444
+            $userGroups = $this->groupManager->getUserGroupIds($user);
445
+
446
+            return new DataResponse(
447
+                $this->formatUserForIndex($user, $userGroups),
448
+                Http::STATUS_CREATED
449
+            );
450
+        }
451
+
452
+        return new DataResponse(
453
+            [
454
+                'message' => (string) $this->l10n->t('Unable to create user.')
455
+            ],
456
+            Http::STATUS_FORBIDDEN
457
+        );
458
+
459
+    }
460
+
461
+    /**
462
+     * @NoAdminRequired
463
+     * @PasswordConfirmationRequired
464
+     *
465
+     * @param string $id
466
+     * @return DataResponse
467
+     */
468
+    public function destroy($id) {
469
+        $userId = $this->userSession->getUser()->getUID();
470
+        $user = $this->userManager->get($id);
471
+
472
+        if($userId === $id) {
473
+            return new DataResponse(
474
+                [
475
+                    'status' => 'error',
476
+                    'data' => [
477
+                        'message' => (string) $this->l10n->t('Unable to delete user.')
478
+                    ]
479
+                ],
480
+                Http::STATUS_FORBIDDEN
481
+            );
482
+        }
483
+
484
+        if(!$this->isAdmin && !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)) {
485
+            return new DataResponse(
486
+                [
487
+                    'status' => 'error',
488
+                    'data' => [
489
+                        'message' => (string)$this->l10n->t('Authentication error')
490
+                    ]
491
+                ],
492
+                Http::STATUS_FORBIDDEN
493
+            );
494
+        }
495
+
496
+        if($user) {
497
+            if($user->delete()) {
498
+                return new DataResponse(
499
+                    [
500
+                        'status' => 'success',
501
+                        'data' => [
502
+                            'username' => $id
503
+                        ]
504
+                    ],
505
+                    Http::STATUS_NO_CONTENT
506
+                );
507
+            }
508
+        }
509
+
510
+        return new DataResponse(
511
+            [
512
+                'status' => 'error',
513
+                'data' => [
514
+                    'message' => (string)$this->l10n->t('Unable to delete user.')
515
+                ]
516
+            ],
517
+            Http::STATUS_FORBIDDEN
518
+        );
519
+    }
520
+
521
+    /**
522
+     * @NoAdminRequired
523
+     *
524
+     * @param string $id
525
+     * @param int $enabled
526
+     * @return DataResponse
527
+     */
528
+    public function setEnabled($id, $enabled) {
529
+        $enabled = (bool)$enabled;
530
+        if($enabled) {
531
+            $errorMsgGeneral = (string) $this->l10n->t('Error while enabling user.');
532
+        } else {
533
+            $errorMsgGeneral = (string) $this->l10n->t('Error while disabling user.');
534
+        }
535
+
536
+        $userId = $this->userSession->getUser()->getUID();
537
+        $user = $this->userManager->get($id);
538
+
539
+        if ($userId === $id) {
540
+            return new DataResponse(
541
+                [
542
+                    'status' => 'error',
543
+                    'data' => [
544
+                        'message' => $errorMsgGeneral
545
+                    ]
546
+                ], Http::STATUS_FORBIDDEN
547
+            );
548
+        }
549
+
550
+        if($user) {
551
+            if (!$this->isAdmin && !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)) {
552
+                return new DataResponse(
553
+                    [
554
+                        'status' => 'error',
555
+                        'data' => [
556
+                            'message' => (string) $this->l10n->t('Authentication error')
557
+                        ]
558
+                    ],
559
+                    Http::STATUS_FORBIDDEN
560
+                );
561
+            }
562
+
563
+            $user->setEnabled($enabled);
564
+            return new DataResponse(
565
+                [
566
+                    'status' => 'success',
567
+                    'data' => [
568
+                        'username' => $id,
569
+                        'enabled' => $enabled
570
+                    ]
571
+                ]
572
+            );
573
+        } else {
574
+            return new DataResponse(
575
+                [
576
+                    'status' => 'error',
577
+                    'data' => [
578
+                        'message' => $errorMsgGeneral
579
+                    ]
580
+                ],
581
+                Http::STATUS_FORBIDDEN
582
+            );
583
+        }
584
+
585
+    }
586
+
587
+    /**
588
+     * Set the mail address of a user
589
+     *
590
+     * @NoAdminRequired
591
+     * @NoSubadminRequired
592
+     * @PasswordConfirmationRequired
593
+     *
594
+     * @param string $account
595
+     * @param bool $onlyVerificationCode only return verification code without updating the data
596
+     * @return DataResponse
597
+     */
598
+    public function getVerificationCode($account, $onlyVerificationCode) {
599
+
600
+        $user = $this->userSession->getUser();
601
+
602
+        if ($user === null) {
603
+            return new DataResponse([], Http::STATUS_BAD_REQUEST);
604
+        }
605
+
606
+        $accountData = $this->accountManager->getUser($user);
607
+        $cloudId = $user->getCloudId();
608
+        $message = "Use my Federated Cloud ID to share with me: " . $cloudId;
609
+        $signature = $this->signMessage($user, $message);
610
+
611
+        $code = $message . ' ' . $signature;
612
+        $codeMd5 = $message . ' ' . md5($signature);
613
+
614
+        switch ($account) {
615
+            case 'verify-twitter':
616
+                $accountData[AccountManager::PROPERTY_TWITTER]['verified'] = AccountManager::VERIFICATION_IN_PROGRESS;
617
+                $msg = $this->l10n->t('In order to verify your Twitter account, post the following tweet on Twitter (please make sure to post it without any line breaks):');
618
+                $code = $codeMd5;
619
+                $type = AccountManager::PROPERTY_TWITTER;
620
+                $data = $accountData[AccountManager::PROPERTY_TWITTER]['value'];
621
+                $accountData[AccountManager::PROPERTY_TWITTER]['signature'] = $signature;
622
+                break;
623
+            case 'verify-website':
624
+                $accountData[AccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::VERIFICATION_IN_PROGRESS;
625
+                $msg = $this->l10n->t('In order to verify your Website, store the following content in your web-root at \'.well-known/CloudIdVerificationCode.txt\' (please make sure that the complete text is in one line):');
626
+                $type = AccountManager::PROPERTY_WEBSITE;
627
+                $data = $accountData[AccountManager::PROPERTY_WEBSITE]['value'];
628
+                $accountData[AccountManager::PROPERTY_WEBSITE]['signature'] = $signature;
629
+                break;
630
+            default:
631
+                return new DataResponse([], Http::STATUS_BAD_REQUEST);
632
+        }
633
+
634
+        if ($onlyVerificationCode === false) {
635
+            $this->accountManager->updateUser($user, $accountData);
636
+
637
+            $this->jobList->add('OC\Settings\BackgroundJobs\VerifyUserData',
638
+                [
639
+                    'verificationCode' => $code,
640
+                    'data' => $data,
641
+                    'type' => $type,
642
+                    'uid' => $user->getUID(),
643
+                    'try' => 0,
644
+                    'lastRun' => $this->getCurrentTime()
645
+                ]
646
+            );
647
+        }
648
+
649
+        return new DataResponse(['msg' => $msg, 'code' => $code]);
650
+    }
651
+
652
+    /**
653
+     * get current timestamp
654
+     *
655
+     * @return int
656
+     */
657
+    protected function getCurrentTime() {
658
+        return time();
659
+    }
660
+
661
+    /**
662
+     * sign message with users private key
663
+     *
664
+     * @param IUser $user
665
+     * @param string $message
666
+     *
667
+     * @return string base64 encoded signature
668
+     */
669
+    protected function signMessage(IUser $user, $message) {
670
+        $privateKey = $this->keyManager->getKey($user)->getPrivate();
671
+        openssl_sign(json_encode($message), $signature, $privateKey, OPENSSL_ALGO_SHA512);
672
+        $signatureBase64 = base64_encode($signature);
673
+
674
+        return $signatureBase64;
675
+    }
676
+
677
+    /**
678
+     * @NoAdminRequired
679
+     * @NoSubadminRequired
680
+     * @PasswordConfirmationRequired
681
+     *
682
+     * @param string $avatarScope
683
+     * @param string $displayname
684
+     * @param string $displaynameScope
685
+     * @param string $phone
686
+     * @param string $phoneScope
687
+     * @param string $email
688
+     * @param string $emailScope
689
+     * @param string $website
690
+     * @param string $websiteScope
691
+     * @param string $address
692
+     * @param string $addressScope
693
+     * @param string $twitter
694
+     * @param string $twitterScope
695
+     * @return DataResponse
696
+     */
697
+    public function setUserSettings($avatarScope,
698
+                                    $displayname,
699
+                                    $displaynameScope,
700
+                                    $phone,
701
+                                    $phoneScope,
702
+                                    $email,
703
+                                    $emailScope,
704
+                                    $website,
705
+                                    $websiteScope,
706
+                                    $address,
707
+                                    $addressScope,
708
+                                    $twitter,
709
+                                    $twitterScope
710
+    ) {
711
+
712
+        if (!empty($email) && !$this->mailer->validateMailAddress($email)) {
713
+            return new DataResponse(
714
+                [
715
+                    'status' => 'error',
716
+                    'data' => [
717
+                        'message' => (string) $this->l10n->t('Invalid mail address')
718
+                    ]
719
+                ],
720
+                Http::STATUS_UNPROCESSABLE_ENTITY
721
+            );
722
+        }
723
+
724
+        $user = $this->userSession->getUser();
725
+
726
+        $data = $this->accountManager->getUser($user);
727
+
728
+        $data[AccountManager::PROPERTY_AVATAR] =  ['scope' => $avatarScope];
729
+        if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) {
730
+            $data[AccountManager::PROPERTY_DISPLAYNAME] = ['value' => $displayname, 'scope' => $displaynameScope];
731
+            $data[AccountManager::PROPERTY_EMAIL] = ['value' => $email, 'scope' => $emailScope];
732
+        }
733
+
734
+        if ($this->appManager->isEnabledForUser('federatedfilesharing')) {
735
+            $federatedFileSharing = new \OCA\FederatedFileSharing\AppInfo\Application();
736
+            $shareProvider = $federatedFileSharing->getFederatedShareProvider();
737
+            if ($shareProvider->isLookupServerUploadEnabled()) {
738
+                $data[AccountManager::PROPERTY_WEBSITE] = ['value' => $website, 'scope' => $websiteScope];
739
+                $data[AccountManager::PROPERTY_ADDRESS] = ['value' => $address, 'scope' => $addressScope];
740
+                $data[AccountManager::PROPERTY_PHONE] = ['value' => $phone, 'scope' => $phoneScope];
741
+                $data[AccountManager::PROPERTY_TWITTER] = ['value' => $twitter, 'scope' => $twitterScope];
742
+            }
743
+        }
744
+
745
+        try {
746
+            $this->saveUserSettings($user, $data);
747
+            return new DataResponse(
748
+                [
749
+                    'status' => 'success',
750
+                    'data' => [
751
+                        'userId' => $user->getUID(),
752
+                        'avatarScope' => $data[AccountManager::PROPERTY_AVATAR]['scope'],
753
+                        'displayname' => $data[AccountManager::PROPERTY_DISPLAYNAME]['value'],
754
+                        'displaynameScope' => $data[AccountManager::PROPERTY_DISPLAYNAME]['scope'],
755
+                        'email' => $data[AccountManager::PROPERTY_EMAIL]['value'],
756
+                        'emailScope' => $data[AccountManager::PROPERTY_EMAIL]['scope'],
757
+                        'website' => $data[AccountManager::PROPERTY_WEBSITE]['value'],
758
+                        'websiteScope' => $data[AccountManager::PROPERTY_WEBSITE]['scope'],
759
+                        'address' => $data[AccountManager::PROPERTY_ADDRESS]['value'],
760
+                        'addressScope' => $data[AccountManager::PROPERTY_ADDRESS]['scope'],
761
+                        'message' => (string) $this->l10n->t('Settings saved')
762
+                    ]
763
+                ],
764
+                Http::STATUS_OK
765
+            );
766
+        } catch (ForbiddenException $e) {
767
+            return new DataResponse([
768
+                'status' => 'error',
769
+                'data' => [
770
+                    'message' => $e->getMessage()
771
+                ],
772
+            ]);
773
+        }
774
+
775
+    }
776
+
777
+
778
+    /**
779
+     * update account manager with new user data
780
+     *
781
+     * @param IUser $user
782
+     * @param array $data
783
+     * @throws ForbiddenException
784
+     */
785
+    protected function saveUserSettings(IUser $user, $data) {
786
+
787
+        // keep the user back-end up-to-date with the latest display name and email
788
+        // address
789
+        $oldDisplayName = $user->getDisplayName();
790
+        $oldDisplayName = is_null($oldDisplayName) ? '' : $oldDisplayName;
791
+        if (isset($data[AccountManager::PROPERTY_DISPLAYNAME]['value'])
792
+            && $oldDisplayName !== $data[AccountManager::PROPERTY_DISPLAYNAME]['value']
793
+        ) {
794
+            $result = $user->setDisplayName($data[AccountManager::PROPERTY_DISPLAYNAME]['value']);
795
+            if ($result === false) {
796
+                throw new ForbiddenException($this->l10n->t('Unable to change full name'));
797
+            }
798
+        }
799
+
800
+        $oldEmailAddress = $user->getEMailAddress();
801
+        $oldEmailAddress = is_null($oldEmailAddress) ? '' : $oldEmailAddress;
802
+        if (isset($data[AccountManager::PROPERTY_EMAIL]['value'])
803
+            && $oldEmailAddress !== $data[AccountManager::PROPERTY_EMAIL]['value']
804
+        ) {
805
+            // this is the only permission a backend provides and is also used
806
+            // for the permission of setting a email address
807
+            if (!$user->canChangeDisplayName()) {
808
+                throw new ForbiddenException($this->l10n->t('Unable to change email address'));
809
+            }
810
+            $user->setEMailAddress($data[AccountManager::PROPERTY_EMAIL]['value']);
811
+        }
812
+
813
+        $this->accountManager->updateUser($user, $data);
814
+    }
815
+
816
+    /**
817
+     * Count all unique users visible for the current admin/subadmin.
818
+     *
819
+     * @NoAdminRequired
820
+     *
821
+     * @return DataResponse
822
+     */
823
+    public function stats() {
824
+        $userCount = 0;
825
+        if ($this->isAdmin) {
826
+            $countByBackend = $this->userManager->countUsers();
827
+
828
+            if (!empty($countByBackend)) {
829
+                foreach ($countByBackend as $count) {
830
+                    $userCount += $count;
831
+                }
832
+            }
833
+        } else {
834
+            $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($this->userSession->getUser());
835
+
836
+            $uniqueUsers = [];
837
+            foreach ($groups as $group) {
838
+                foreach($group->getUsers() as $uid => $displayName) {
839
+                    $uniqueUsers[$uid] = true;
840
+                }
841
+            }
842
+
843
+            $userCount = count($uniqueUsers);
844
+        }
845
+
846
+        return new DataResponse(
847
+            [
848
+                'totalUsers' => $userCount
849
+            ]
850
+        );
851
+    }
852
+
853
+
854
+    /**
855
+     * Set the displayName of a user
856
+     *
857
+     * @NoAdminRequired
858
+     * @NoSubadminRequired
859
+     * @PasswordConfirmationRequired
860
+     * @todo merge into saveUserSettings
861
+     *
862
+     * @param string $username
863
+     * @param string $displayName
864
+     * @return DataResponse
865
+     */
866
+    public function setDisplayName($username, $displayName) {
867
+        $currentUser = $this->userSession->getUser();
868
+        $user = $this->userManager->get($username);
869
+
870
+        if ($user === null ||
871
+            !$user->canChangeDisplayName() ||
872
+            (
873
+                !$this->groupManager->isAdmin($currentUser->getUID()) &&
874
+                !$this->groupManager->getSubAdmin()->isUserAccessible($currentUser, $user) &&
875
+                $currentUser->getUID() !== $username
876
+
877
+            )
878
+        ) {
879
+            return new DataResponse([
880
+                'status' => 'error',
881
+                'data' => [
882
+                    'message' => $this->l10n->t('Authentication error'),
883
+                ],
884
+            ]);
885
+        }
886
+
887
+        $userData = $this->accountManager->getUser($user);
888
+        $userData[AccountManager::PROPERTY_DISPLAYNAME]['value'] = $displayName;
889
+
890
+
891
+        try {
892
+            $this->saveUserSettings($user, $userData);
893
+            return new DataResponse([
894
+                'status' => 'success',
895
+                'data' => [
896
+                    'message' => $this->l10n->t('Your full name has been changed.'),
897
+                    'username' => $username,
898
+                    'displayName' => $displayName,
899
+                ],
900
+            ]);
901
+        } catch (ForbiddenException $e) {
902
+            return new DataResponse([
903
+                'status' => 'error',
904
+                'data' => [
905
+                    'message' => $e->getMessage(),
906
+                    'displayName' => $user->getDisplayName(),
907
+                ],
908
+            ]);
909
+        }
910
+    }
911
+
912
+    /**
913
+     * Set the mail address of a user
914
+     *
915
+     * @NoAdminRequired
916
+     * @NoSubadminRequired
917
+     * @PasswordConfirmationRequired
918
+     *
919
+     * @param string $id
920
+     * @param string $mailAddress
921
+     * @return DataResponse
922
+     */
923
+    public function setEMailAddress($id, $mailAddress) {
924
+        $user = $this->userManager->get($id);
925
+        if (!$this->isAdmin
926
+            && !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)
927
+        ) {
928
+            return new DataResponse(
929
+                [
930
+                    'status' => 'error',
931
+                    'data' => [
932
+                        'message' => (string) $this->l10n->t('Forbidden')
933
+                    ]
934
+                ],
935
+                Http::STATUS_FORBIDDEN
936
+            );
937
+        }
938
+
939
+        if($mailAddress !== '' && !$this->mailer->validateMailAddress($mailAddress)) {
940
+            return new DataResponse(
941
+                [
942
+                    'status' => 'error',
943
+                    'data' => [
944
+                        'message' => (string) $this->l10n->t('Invalid mail address')
945
+                    ]
946
+                ],
947
+                Http::STATUS_UNPROCESSABLE_ENTITY
948
+            );
949
+        }
950
+
951
+        if (!$user) {
952
+            return new DataResponse(
953
+                [
954
+                    'status' => 'error',
955
+                    'data' => [
956
+                        'message' => (string) $this->l10n->t('Invalid user')
957
+                    ]
958
+                ],
959
+                Http::STATUS_UNPROCESSABLE_ENTITY
960
+            );
961
+        }
962
+        // this is the only permission a backend provides and is also used
963
+        // for the permission of setting a email address
964
+        if (!$user->canChangeDisplayName()) {
965
+            return new DataResponse(
966
+                [
967
+                    'status' => 'error',
968
+                    'data' => [
969
+                        'message' => (string) $this->l10n->t('Unable to change mail address')
970
+                    ]
971
+                ],
972
+                Http::STATUS_FORBIDDEN
973
+            );
974
+        }
975
+
976
+        $userData = $this->accountManager->getUser($user);
977
+        $userData[AccountManager::PROPERTY_EMAIL]['value'] = $mailAddress;
978
+
979
+        try {
980
+            $this->saveUserSettings($user, $userData);
981
+            return new DataResponse(
982
+                [
983
+                    'status' => 'success',
984
+                    'data' => [
985
+                        'username' => $id,
986
+                        'mailAddress' => $mailAddress,
987
+                        'message' => (string) $this->l10n->t('Email saved')
988
+                    ]
989
+                ],
990
+                Http::STATUS_OK
991
+            );
992
+        } catch (ForbiddenException $e) {
993
+            return new DataResponse([
994
+                'status' => 'error',
995
+                'data' => [
996
+                    'message' => $e->getMessage()
997
+                ],
998
+            ]);
999
+        }
1000
+    }
1001 1001
 
1002 1002
 }
Please login to merge, or discard this patch.
Spacing   +40 added lines, -40 removed lines patch added patch discarded remove patch
@@ -160,7 +160,7 @@  discard block
 block discarded – undo
160 160
 
161 161
 		// check for encryption state - TODO see formatUserForIndex
162 162
 		$this->isEncryptionAppEnabled = $appManager->isEnabledForUser('encryption');
163
-		if($this->isEncryptionAppEnabled) {
163
+		if ($this->isEncryptionAppEnabled) {
164 164
 			// putting this directly in empty is possible in PHP 5.5+
165 165
 			$result = $config->getAppValue('encryption', 'recoveryAdminEnabled', 0);
166 166
 			$this->isRestoreEnabled = !empty($result);
@@ -200,7 +200,7 @@  discard block
 block discarded – undo
200 200
 		}
201 201
 
202 202
 		$subAdminGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user);
203
-		foreach($subAdminGroups as $key => $subAdminGroup) {
203
+		foreach ($subAdminGroups as $key => $subAdminGroup) {
204 204
 			$subAdminGroups[$key] = $subAdminGroup->getGID();
205 205
 		}
206 206
 
@@ -258,11 +258,11 @@  discard block
 block discarded – undo
258 258
 	 */
259 259
 	public function index($offset = 0, $limit = 10, $gid = '', $pattern = '', $backend = '') {
260 260
 		// Remove backends
261
-		if(!empty($backend)) {
261
+		if (!empty($backend)) {
262 262
 			$activeBackends = $this->userManager->getBackends();
263 263
 			$this->userManager->clearBackends();
264
-			foreach($activeBackends as $singleActiveBackend) {
265
-				if($backend === get_class($singleActiveBackend)) {
264
+			foreach ($activeBackends as $singleActiveBackend) {
265
+				if ($backend === get_class($singleActiveBackend)) {
266 266
 					$this->userManager->registerBackend($singleActiveBackend);
267 267
 					break;
268 268
 				}
@@ -271,14 +271,14 @@  discard block
 block discarded – undo
271 271
 
272 272
 		$users = [];
273 273
 		if ($this->isAdmin) {
274
-			if($gid !== '' && $gid !== '_disabledUsers') {
274
+			if ($gid !== '' && $gid !== '_disabledUsers') {
275 275
 				$batch = $this->getUsersForUID($this->groupManager->displayNamesInGroup($gid, $pattern, $limit, $offset));
276 276
 			} else {
277 277
 				$batch = $this->userManager->search($pattern, $limit, $offset);
278 278
 			}
279 279
 
280 280
 			foreach ($batch as $user) {
281
-				if( ($gid !== '_disabledUsers' && $user->isEnabled()) ||
281
+				if (($gid !== '_disabledUsers' && $user->isEnabled()) ||
282 282
 					($gid === '_disabledUsers' && !$user->isEnabled())
283 283
 				) {
284 284
 					$users[] = $this->formatUserForIndex($user);
@@ -295,17 +295,17 @@  discard block
 block discarded – undo
295 295
 			$subAdminOfGroups = $gids;
296 296
 
297 297
 			// Set the $gid parameter to an empty value if the subadmin has no rights to access a specific group
298
-			if($gid !== '' && $gid !== '_disabledUsers' && !in_array($gid, $subAdminOfGroups)) {
298
+			if ($gid !== '' && $gid !== '_disabledUsers' && !in_array($gid, $subAdminOfGroups)) {
299 299
 				$gid = '';
300 300
 			}
301 301
 
302 302
 			// Batch all groups the user is subadmin of when a group is specified
303 303
 			$batch = [];
304
-			if($gid === '') {
305
-				foreach($subAdminOfGroups as $group) {
304
+			if ($gid === '') {
305
+				foreach ($subAdminOfGroups as $group) {
306 306
 					$groupUsers = $this->groupManager->displayNamesInGroup($group, $pattern, $limit, $offset);
307 307
 
308
-					foreach($groupUsers as $uid => $displayName) {
308
+					foreach ($groupUsers as $uid => $displayName) {
309 309
 						$batch[$uid] = $displayName;
310 310
 					}
311 311
 				}
@@ -320,7 +320,7 @@  discard block
 block discarded – undo
320 320
 					$this->groupManager->getUserGroupIds($user),
321 321
 					$subAdminOfGroups
322 322
 				));
323
-				if( ($gid !== '_disabledUsers' && $user->isEnabled()) ||
323
+				if (($gid !== '_disabledUsers' && $user->isEnabled()) ||
324 324
 					($gid === '_disabledUsers' && !$user->isEnabled())
325 325
 				) {
326 326
 					$users[] = $this->formatUserForIndex($user, $userGroups);
@@ -341,11 +341,11 @@  discard block
 block discarded – undo
341 341
 	 * @param string $email
342 342
 	 * @return DataResponse
343 343
 	 */
344
-	public function create($username, $password, array $groups=[], $email='') {
345
-		if($email !== '' && !$this->mailer->validateMailAddress($email)) {
344
+	public function create($username, $password, array $groups = [], $email = '') {
345
+		if ($email !== '' && !$this->mailer->validateMailAddress($email)) {
346 346
 			return new DataResponse(
347 347
 				[
348
-					'message' => (string)$this->l10n->t('Invalid mail address')
348
+					'message' => (string) $this->l10n->t('Invalid mail address')
349 349
 				],
350 350
 				Http::STATUS_UNPROCESSABLE_ENTITY
351 351
 			);
@@ -357,7 +357,7 @@  discard block
 block discarded – undo
357 357
 			if (!empty($groups)) {
358 358
 				foreach ($groups as $key => $group) {
359 359
 					$groupObject = $this->groupManager->get($group);
360
-					if($groupObject === null) {
360
+					if ($groupObject === null) {
361 361
 						unset($groups[$key]);
362 362
 						continue;
363 363
 					}
@@ -381,7 +381,7 @@  discard block
 block discarded – undo
381 381
 		if ($this->userManager->userExists($username)) {
382 382
 			return new DataResponse(
383 383
 				[
384
-					'message' => (string)$this->l10n->t('A user with that name already exists.')
384
+					'message' => (string) $this->l10n->t('A user with that name already exists.')
385 385
 				],
386 386
 				Http::STATUS_CONFLICT
387 387
 			);
@@ -392,7 +392,7 @@  discard block
 block discarded – undo
392 392
 			if ($email === '') {
393 393
 				return new DataResponse(
394 394
 					[
395
-						'message' => (string)$this->l10n->t('To send a password link to the user an email address is required.')
395
+						'message' => (string) $this->l10n->t('To send a password link to the user an email address is required.')
396 396
 					],
397 397
 					Http::STATUS_UNPROCESSABLE_ENTITY
398 398
 				);
@@ -417,12 +417,12 @@  discard block
 block discarded – undo
417 417
 			);
418 418
 		}
419 419
 
420
-		if($user instanceof IUser) {
421
-			if($groups !== null) {
422
-				foreach($groups as $groupName) {
420
+		if ($user instanceof IUser) {
421
+			if ($groups !== null) {
422
+				foreach ($groups as $groupName) {
423 423
 					$group = $this->groupManager->get($groupName);
424 424
 
425
-					if(empty($group)) {
425
+					if (empty($group)) {
426 426
 						$group = $this->groupManager->createGroup($groupName);
427 427
 					}
428 428
 					$group->addUser($user);
@@ -431,13 +431,13 @@  discard block
 block discarded – undo
431 431
 			/**
432 432
 			 * Send new user mail only if a mail is set
433 433
 			 */
434
-			if($email !== '') {
434
+			if ($email !== '') {
435 435
 				$user->setEMailAddress($email);
436 436
 				try {
437 437
 					$emailTemplate = $this->newUserMailHelper->generateTemplate($user, $generatePasswordResetToken);
438 438
 					$this->newUserMailHelper->sendMail($user, $emailTemplate);
439
-				} catch(\Exception $e) {
440
-					$this->log->error("Can't send new user mail to $email: " . $e->getMessage(), ['app' => 'settings']);
439
+				} catch (\Exception $e) {
440
+					$this->log->error("Can't send new user mail to $email: ".$e->getMessage(), ['app' => 'settings']);
441 441
 				}
442 442
 			}
443 443
 			// fetch users groups
@@ -469,7 +469,7 @@  discard block
 block discarded – undo
469 469
 		$userId = $this->userSession->getUser()->getUID();
470 470
 		$user = $this->userManager->get($id);
471 471
 
472
-		if($userId === $id) {
472
+		if ($userId === $id) {
473 473
 			return new DataResponse(
474 474
 				[
475 475
 					'status' => 'error',
@@ -481,20 +481,20 @@  discard block
 block discarded – undo
481 481
 			);
482 482
 		}
483 483
 
484
-		if(!$this->isAdmin && !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)) {
484
+		if (!$this->isAdmin && !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)) {
485 485
 			return new DataResponse(
486 486
 				[
487 487
 					'status' => 'error',
488 488
 					'data' => [
489
-						'message' => (string)$this->l10n->t('Authentication error')
489
+						'message' => (string) $this->l10n->t('Authentication error')
490 490
 					]
491 491
 				],
492 492
 				Http::STATUS_FORBIDDEN
493 493
 			);
494 494
 		}
495 495
 
496
-		if($user) {
497
-			if($user->delete()) {
496
+		if ($user) {
497
+			if ($user->delete()) {
498 498
 				return new DataResponse(
499 499
 					[
500 500
 						'status' => 'success',
@@ -511,7 +511,7 @@  discard block
 block discarded – undo
511 511
 			[
512 512
 				'status' => 'error',
513 513
 				'data' => [
514
-					'message' => (string)$this->l10n->t('Unable to delete user.')
514
+					'message' => (string) $this->l10n->t('Unable to delete user.')
515 515
 				]
516 516
 			],
517 517
 			Http::STATUS_FORBIDDEN
@@ -526,8 +526,8 @@  discard block
 block discarded – undo
526 526
 	 * @return DataResponse
527 527
 	 */
528 528
 	public function setEnabled($id, $enabled) {
529
-		$enabled = (bool)$enabled;
530
-		if($enabled) {
529
+		$enabled = (bool) $enabled;
530
+		if ($enabled) {
531 531
 			$errorMsgGeneral = (string) $this->l10n->t('Error while enabling user.');
532 532
 		} else {
533 533
 			$errorMsgGeneral = (string) $this->l10n->t('Error while disabling user.');
@@ -547,7 +547,7 @@  discard block
 block discarded – undo
547 547
 			);
548 548
 		}
549 549
 
550
-		if($user) {
550
+		if ($user) {
551 551
 			if (!$this->isAdmin && !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)) {
552 552
 				return new DataResponse(
553 553
 					[
@@ -605,11 +605,11 @@  discard block
 block discarded – undo
605 605
 
606 606
 		$accountData = $this->accountManager->getUser($user);
607 607
 		$cloudId = $user->getCloudId();
608
-		$message = "Use my Federated Cloud ID to share with me: " . $cloudId;
608
+		$message = "Use my Federated Cloud ID to share with me: ".$cloudId;
609 609
 		$signature = $this->signMessage($user, $message);
610 610
 
611
-		$code = $message . ' ' . $signature;
612
-		$codeMd5 = $message . ' ' . md5($signature);
611
+		$code = $message.' '.$signature;
612
+		$codeMd5 = $message.' '.md5($signature);
613 613
 
614 614
 		switch ($account) {
615 615
 			case 'verify-twitter':
@@ -725,7 +725,7 @@  discard block
 block discarded – undo
725 725
 
726 726
 		$data = $this->accountManager->getUser($user);
727 727
 
728
-		$data[AccountManager::PROPERTY_AVATAR] =  ['scope' => $avatarScope];
728
+		$data[AccountManager::PROPERTY_AVATAR] = ['scope' => $avatarScope];
729 729
 		if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) {
730 730
 			$data[AccountManager::PROPERTY_DISPLAYNAME] = ['value' => $displayname, 'scope' => $displaynameScope];
731 731
 			$data[AccountManager::PROPERTY_EMAIL] = ['value' => $email, 'scope' => $emailScope];
@@ -835,7 +835,7 @@  discard block
 block discarded – undo
835 835
 
836 836
 			$uniqueUsers = [];
837 837
 			foreach ($groups as $group) {
838
-				foreach($group->getUsers() as $uid => $displayName) {
838
+				foreach ($group->getUsers() as $uid => $displayName) {
839 839
 					$uniqueUsers[$uid] = true;
840 840
 				}
841 841
 			}
@@ -936,7 +936,7 @@  discard block
 block discarded – undo
936 936
 			);
937 937
 		}
938 938
 
939
-		if($mailAddress !== '' && !$this->mailer->validateMailAddress($mailAddress)) {
939
+		if ($mailAddress !== '' && !$this->mailer->validateMailAddress($mailAddress)) {
940 940
 			return new DataResponse(
941 941
 				[
942 942
 					'status' => 'error',
Please login to merge, or discard this patch.