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