Passed
Push — master ( 6d2c79...4d8b4e )
by Roeland
23:36 queued 11:49
created
apps/provisioning_api/lib/Controller/GroupsController.php 1 patch
Indentation   +266 added lines, -266 removed lines patch added patch discarded remove patch
@@ -51,270 +51,270 @@
 block discarded – undo
51 51
 
52 52
 class GroupsController extends AUserData {
53 53
 
54
-	/** @var ILogger */
55
-	private $logger;
56
-
57
-	public function __construct(string $appName,
58
-								IRequest $request,
59
-								IUserManager $userManager,
60
-								IConfig $config,
61
-								IGroupManager $groupManager,
62
-								IUserSession $userSession,
63
-								AccountManager $accountManager,
64
-								IFactory $l10nFactory,
65
-								ILogger $logger) {
66
-		parent::__construct($appName,
67
-			$request,
68
-			$userManager,
69
-			$config,
70
-			$groupManager,
71
-			$userSession,
72
-			$accountManager,
73
-			$l10nFactory
74
-		);
75
-
76
-		$this->logger = $logger;
77
-	}
78
-
79
-	/**
80
-	 * returns a list of groups
81
-	 *
82
-	 * @NoAdminRequired
83
-	 *
84
-	 * @param string $search
85
-	 * @param int $limit
86
-	 * @param int $offset
87
-	 * @return DataResponse
88
-	 */
89
-	public function getGroups(string $search = '', int $limit = null, int $offset = 0): DataResponse {
90
-		$groups = $this->groupManager->search($search, $limit, $offset);
91
-		$groups = array_map(function ($group) {
92
-			/** @var IGroup $group */
93
-			return $group->getGID();
94
-		}, $groups);
95
-
96
-		return new DataResponse(['groups' => $groups]);
97
-	}
98
-
99
-	/**
100
-	 * returns a list of groups details with ids and displaynames
101
-	 *
102
-	 * @NoAdminRequired
103
-	 *
104
-	 * @param string $search
105
-	 * @param int $limit
106
-	 * @param int $offset
107
-	 * @return DataResponse
108
-	 */
109
-	public function getGroupsDetails(string $search = '', int $limit = null, int $offset = 0): DataResponse {
110
-		$groups = $this->groupManager->search($search, $limit, $offset);
111
-		$groups = array_map(function ($group) {
112
-			/** @var IGroup $group */
113
-			return [
114
-				'id' => $group->getGID(),
115
-				'displayname' => $group->getDisplayName(),
116
-				'usercount' => $group->count(),
117
-				'disabled' => $group->countDisabled(),
118
-				'canAdd' => $group->canAddUser(),
119
-				'canRemove' => $group->canRemoveUser(),
120
-			];
121
-		}, $groups);
122
-
123
-		return new DataResponse(['groups' => $groups]);
124
-	}
125
-
126
-	/**
127
-	 * @NoAdminRequired
128
-	 *
129
-	 * @param string $groupId
130
-	 * @return DataResponse
131
-	 * @throws OCSException
132
-	 *
133
-	 * @deprecated 14 Use getGroupUsers
134
-	 */
135
-	public function getGroup(string $groupId): DataResponse {
136
-		return $this->getGroupUsers($groupId);
137
-	}
138
-
139
-	/**
140
-	 * returns an array of users in the specified group
141
-	 *
142
-	 * @NoAdminRequired
143
-	 *
144
-	 * @param string $groupId
145
-	 * @return DataResponse
146
-	 * @throws OCSException
147
-	 */
148
-	public function getGroupUsers(string $groupId): DataResponse {
149
-		$groupId = urldecode($groupId);
150
-
151
-		$user = $this->userSession->getUser();
152
-		$isSubadminOfGroup = false;
153
-
154
-		// Check the group exists
155
-		$group = $this->groupManager->get($groupId);
156
-		if ($group !== null) {
157
-			$isSubadminOfGroup =$this->groupManager->getSubAdmin()->isSubAdminOfGroup($user, $group);
158
-		} else {
159
-			throw new OCSNotFoundException('The requested group could not be found');
160
-		}
161
-
162
-		// Check subadmin has access to this group
163
-		if ($this->groupManager->isAdmin($user->getUID())
164
-		   || $isSubadminOfGroup) {
165
-			$users = $this->groupManager->get($groupId)->getUsers();
166
-			$users =  array_map(function ($user) {
167
-				/** @var IUser $user */
168
-				return $user->getUID();
169
-			}, $users);
170
-			$users = array_values($users);
171
-			return new DataResponse(['users' => $users]);
172
-		}
173
-
174
-		throw new OCSForbiddenException();
175
-	}
176
-
177
-	/**
178
-	 * returns an array of users details in the specified group
179
-	 *
180
-	 * @NoAdminRequired
181
-	 *
182
-	 * @param string $groupId
183
-	 * @param string $search
184
-	 * @param int $limit
185
-	 * @param int $offset
186
-	 * @return DataResponse
187
-	 * @throws OCSException
188
-	 */
189
-	public function getGroupUsersDetails(string $groupId, string $search = '', int $limit = null, int $offset = 0): DataResponse {
190
-		$groupId = urldecode($groupId);
191
-		$currentUser = $this->userSession->getUser();
192
-
193
-		// Check the group exists
194
-		$group = $this->groupManager->get($groupId);
195
-		if ($group !== null) {
196
-			$isSubadminOfGroup = $this->groupManager->getSubAdmin()->isSubAdminOfGroup($currentUser, $group);
197
-		} else {
198
-			throw new OCSException('The requested group could not be found', \OCP\API::RESPOND_NOT_FOUND);
199
-		}
200
-
201
-		// Check subadmin has access to this group
202
-		if ($this->groupManager->isAdmin($currentUser->getUID()) || $isSubadminOfGroup) {
203
-			$users = $group->searchUsers($search, $limit, $offset);
204
-
205
-			// Extract required number
206
-			$usersDetails = [];
207
-			foreach ($users as $user) {
208
-				try {
209
-					/** @var IUser $user */
210
-					$userId = (string)$user->getUID();
211
-					$userData = $this->getUserData($userId);
212
-					// Do not insert empty entry
213
-					if (!empty($userData)) {
214
-						$usersDetails[$userId] = $userData;
215
-					} else {
216
-						// Logged user does not have permissions to see this user
217
-						// only showing its id
218
-						$usersDetails[$userId] = ['id' => $userId];
219
-					}
220
-				} catch (OCSNotFoundException $e) {
221
-					// continue if a users ceased to exist.
222
-				}
223
-			}
224
-			return new DataResponse(['users' => $usersDetails]);
225
-		}
226
-
227
-		throw new OCSException('User does not have access to specified group', \OCP\API::RESPOND_UNAUTHORISED);
228
-	}
229
-
230
-	/**
231
-	 * creates a new group
232
-	 *
233
-	 * @PasswordConfirmationRequired
234
-	 *
235
-	 * @param string $groupid
236
-	 * @return DataResponse
237
-	 * @throws OCSException
238
-	 */
239
-	public function addGroup(string $groupid): DataResponse {
240
-		// Validate name
241
-		if (empty($groupid)) {
242
-			$this->logger->error('Group name not supplied', ['app' => 'provisioning_api']);
243
-			throw new OCSException('Invalid group name', 101);
244
-		}
245
-		// Check if it exists
246
-		if ($this->groupManager->groupExists($groupid)) {
247
-			throw new OCSException('group exists', 102);
248
-		}
249
-		$this->groupManager->createGroup($groupid);
250
-		return new DataResponse();
251
-	}
252
-
253
-	/**
254
-	 * @PasswordConfirmationRequired
255
-	 *
256
-	 * @param string $groupId
257
-	 * @param string $key
258
-	 * @param string $value
259
-	 * @return DataResponse
260
-	 * @throws OCSException
261
-	 */
262
-	public function updateGroup(string $groupId, string $key, string $value): DataResponse {
263
-		$groupId = urldecode($groupId);
264
-
265
-		if ($key === 'displayname') {
266
-			$group = $this->groupManager->get($groupId);
267
-			if ($group->setDisplayName($value)) {
268
-				return new DataResponse();
269
-			}
270
-
271
-			throw new OCSException('Not supported by backend', 101);
272
-		} else {
273
-			throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
274
-		}
275
-	}
276
-
277
-	/**
278
-	 * @PasswordConfirmationRequired
279
-	 *
280
-	 * @param string $groupId
281
-	 * @return DataResponse
282
-	 * @throws OCSException
283
-	 */
284
-	public function deleteGroup(string $groupId): DataResponse {
285
-		$groupId = urldecode($groupId);
286
-
287
-		// Check it exists
288
-		if (!$this->groupManager->groupExists($groupId)) {
289
-			throw new OCSException('', 101);
290
-		} elseif ($groupId === 'admin' || !$this->groupManager->get($groupId)->delete()) {
291
-			// Cannot delete admin group
292
-			throw new OCSException('', 102);
293
-		}
294
-
295
-		return new DataResponse();
296
-	}
297
-
298
-	/**
299
-	 * @param string $groupId
300
-	 * @return DataResponse
301
-	 * @throws OCSException
302
-	 */
303
-	public function getSubAdminsOfGroup(string $groupId): DataResponse {
304
-		// Check group exists
305
-		$targetGroup = $this->groupManager->get($groupId);
306
-		if ($targetGroup === null) {
307
-			throw new OCSException('Group does not exist', 101);
308
-		}
309
-
310
-		/** @var IUser[] $subadmins */
311
-		$subadmins = $this->groupManager->getSubAdmin()->getGroupsSubAdmins($targetGroup);
312
-		// New class returns IUser[] so convert back
313
-		$uids = [];
314
-		foreach ($subadmins as $user) {
315
-			$uids[] = $user->getUID();
316
-		}
317
-
318
-		return new DataResponse($uids);
319
-	}
54
+    /** @var ILogger */
55
+    private $logger;
56
+
57
+    public function __construct(string $appName,
58
+                                IRequest $request,
59
+                                IUserManager $userManager,
60
+                                IConfig $config,
61
+                                IGroupManager $groupManager,
62
+                                IUserSession $userSession,
63
+                                AccountManager $accountManager,
64
+                                IFactory $l10nFactory,
65
+                                ILogger $logger) {
66
+        parent::__construct($appName,
67
+            $request,
68
+            $userManager,
69
+            $config,
70
+            $groupManager,
71
+            $userSession,
72
+            $accountManager,
73
+            $l10nFactory
74
+        );
75
+
76
+        $this->logger = $logger;
77
+    }
78
+
79
+    /**
80
+     * returns a list of groups
81
+     *
82
+     * @NoAdminRequired
83
+     *
84
+     * @param string $search
85
+     * @param int $limit
86
+     * @param int $offset
87
+     * @return DataResponse
88
+     */
89
+    public function getGroups(string $search = '', int $limit = null, int $offset = 0): DataResponse {
90
+        $groups = $this->groupManager->search($search, $limit, $offset);
91
+        $groups = array_map(function ($group) {
92
+            /** @var IGroup $group */
93
+            return $group->getGID();
94
+        }, $groups);
95
+
96
+        return new DataResponse(['groups' => $groups]);
97
+    }
98
+
99
+    /**
100
+     * returns a list of groups details with ids and displaynames
101
+     *
102
+     * @NoAdminRequired
103
+     *
104
+     * @param string $search
105
+     * @param int $limit
106
+     * @param int $offset
107
+     * @return DataResponse
108
+     */
109
+    public function getGroupsDetails(string $search = '', int $limit = null, int $offset = 0): DataResponse {
110
+        $groups = $this->groupManager->search($search, $limit, $offset);
111
+        $groups = array_map(function ($group) {
112
+            /** @var IGroup $group */
113
+            return [
114
+                'id' => $group->getGID(),
115
+                'displayname' => $group->getDisplayName(),
116
+                'usercount' => $group->count(),
117
+                'disabled' => $group->countDisabled(),
118
+                'canAdd' => $group->canAddUser(),
119
+                'canRemove' => $group->canRemoveUser(),
120
+            ];
121
+        }, $groups);
122
+
123
+        return new DataResponse(['groups' => $groups]);
124
+    }
125
+
126
+    /**
127
+     * @NoAdminRequired
128
+     *
129
+     * @param string $groupId
130
+     * @return DataResponse
131
+     * @throws OCSException
132
+     *
133
+     * @deprecated 14 Use getGroupUsers
134
+     */
135
+    public function getGroup(string $groupId): DataResponse {
136
+        return $this->getGroupUsers($groupId);
137
+    }
138
+
139
+    /**
140
+     * returns an array of users in the specified group
141
+     *
142
+     * @NoAdminRequired
143
+     *
144
+     * @param string $groupId
145
+     * @return DataResponse
146
+     * @throws OCSException
147
+     */
148
+    public function getGroupUsers(string $groupId): DataResponse {
149
+        $groupId = urldecode($groupId);
150
+
151
+        $user = $this->userSession->getUser();
152
+        $isSubadminOfGroup = false;
153
+
154
+        // Check the group exists
155
+        $group = $this->groupManager->get($groupId);
156
+        if ($group !== null) {
157
+            $isSubadminOfGroup =$this->groupManager->getSubAdmin()->isSubAdminOfGroup($user, $group);
158
+        } else {
159
+            throw new OCSNotFoundException('The requested group could not be found');
160
+        }
161
+
162
+        // Check subadmin has access to this group
163
+        if ($this->groupManager->isAdmin($user->getUID())
164
+           || $isSubadminOfGroup) {
165
+            $users = $this->groupManager->get($groupId)->getUsers();
166
+            $users =  array_map(function ($user) {
167
+                /** @var IUser $user */
168
+                return $user->getUID();
169
+            }, $users);
170
+            $users = array_values($users);
171
+            return new DataResponse(['users' => $users]);
172
+        }
173
+
174
+        throw new OCSForbiddenException();
175
+    }
176
+
177
+    /**
178
+     * returns an array of users details in the specified group
179
+     *
180
+     * @NoAdminRequired
181
+     *
182
+     * @param string $groupId
183
+     * @param string $search
184
+     * @param int $limit
185
+     * @param int $offset
186
+     * @return DataResponse
187
+     * @throws OCSException
188
+     */
189
+    public function getGroupUsersDetails(string $groupId, string $search = '', int $limit = null, int $offset = 0): DataResponse {
190
+        $groupId = urldecode($groupId);
191
+        $currentUser = $this->userSession->getUser();
192
+
193
+        // Check the group exists
194
+        $group = $this->groupManager->get($groupId);
195
+        if ($group !== null) {
196
+            $isSubadminOfGroup = $this->groupManager->getSubAdmin()->isSubAdminOfGroup($currentUser, $group);
197
+        } else {
198
+            throw new OCSException('The requested group could not be found', \OCP\API::RESPOND_NOT_FOUND);
199
+        }
200
+
201
+        // Check subadmin has access to this group
202
+        if ($this->groupManager->isAdmin($currentUser->getUID()) || $isSubadminOfGroup) {
203
+            $users = $group->searchUsers($search, $limit, $offset);
204
+
205
+            // Extract required number
206
+            $usersDetails = [];
207
+            foreach ($users as $user) {
208
+                try {
209
+                    /** @var IUser $user */
210
+                    $userId = (string)$user->getUID();
211
+                    $userData = $this->getUserData($userId);
212
+                    // Do not insert empty entry
213
+                    if (!empty($userData)) {
214
+                        $usersDetails[$userId] = $userData;
215
+                    } else {
216
+                        // Logged user does not have permissions to see this user
217
+                        // only showing its id
218
+                        $usersDetails[$userId] = ['id' => $userId];
219
+                    }
220
+                } catch (OCSNotFoundException $e) {
221
+                    // continue if a users ceased to exist.
222
+                }
223
+            }
224
+            return new DataResponse(['users' => $usersDetails]);
225
+        }
226
+
227
+        throw new OCSException('User does not have access to specified group', \OCP\API::RESPOND_UNAUTHORISED);
228
+    }
229
+
230
+    /**
231
+     * creates a new group
232
+     *
233
+     * @PasswordConfirmationRequired
234
+     *
235
+     * @param string $groupid
236
+     * @return DataResponse
237
+     * @throws OCSException
238
+     */
239
+    public function addGroup(string $groupid): DataResponse {
240
+        // Validate name
241
+        if (empty($groupid)) {
242
+            $this->logger->error('Group name not supplied', ['app' => 'provisioning_api']);
243
+            throw new OCSException('Invalid group name', 101);
244
+        }
245
+        // Check if it exists
246
+        if ($this->groupManager->groupExists($groupid)) {
247
+            throw new OCSException('group exists', 102);
248
+        }
249
+        $this->groupManager->createGroup($groupid);
250
+        return new DataResponse();
251
+    }
252
+
253
+    /**
254
+     * @PasswordConfirmationRequired
255
+     *
256
+     * @param string $groupId
257
+     * @param string $key
258
+     * @param string $value
259
+     * @return DataResponse
260
+     * @throws OCSException
261
+     */
262
+    public function updateGroup(string $groupId, string $key, string $value): DataResponse {
263
+        $groupId = urldecode($groupId);
264
+
265
+        if ($key === 'displayname') {
266
+            $group = $this->groupManager->get($groupId);
267
+            if ($group->setDisplayName($value)) {
268
+                return new DataResponse();
269
+            }
270
+
271
+            throw new OCSException('Not supported by backend', 101);
272
+        } else {
273
+            throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
274
+        }
275
+    }
276
+
277
+    /**
278
+     * @PasswordConfirmationRequired
279
+     *
280
+     * @param string $groupId
281
+     * @return DataResponse
282
+     * @throws OCSException
283
+     */
284
+    public function deleteGroup(string $groupId): DataResponse {
285
+        $groupId = urldecode($groupId);
286
+
287
+        // Check it exists
288
+        if (!$this->groupManager->groupExists($groupId)) {
289
+            throw new OCSException('', 101);
290
+        } elseif ($groupId === 'admin' || !$this->groupManager->get($groupId)->delete()) {
291
+            // Cannot delete admin group
292
+            throw new OCSException('', 102);
293
+        }
294
+
295
+        return new DataResponse();
296
+    }
297
+
298
+    /**
299
+     * @param string $groupId
300
+     * @return DataResponse
301
+     * @throws OCSException
302
+     */
303
+    public function getSubAdminsOfGroup(string $groupId): DataResponse {
304
+        // Check group exists
305
+        $targetGroup = $this->groupManager->get($groupId);
306
+        if ($targetGroup === null) {
307
+            throw new OCSException('Group does not exist', 101);
308
+        }
309
+
310
+        /** @var IUser[] $subadmins */
311
+        $subadmins = $this->groupManager->getSubAdmin()->getGroupsSubAdmins($targetGroup);
312
+        // New class returns IUser[] so convert back
313
+        $uids = [];
314
+        foreach ($subadmins as $user) {
315
+            $uids[] = $user->getUID();
316
+        }
317
+
318
+        return new DataResponse($uids);
319
+    }
320 320
 }
Please login to merge, or discard this patch.