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