@@ -61,433 +61,433 @@ |
||
61 | 61 | * Class for group management in a SQL Database (e.g. MySQL, SQLite) |
62 | 62 | */ |
63 | 63 | class Database extends ABackend implements |
64 | - IAddToGroupBackend, |
|
65 | - ICountDisabledInGroup, |
|
66 | - ICountUsersBackend, |
|
67 | - ICreateGroupBackend, |
|
68 | - IDeleteGroupBackend, |
|
69 | - IGetDisplayNameBackend, |
|
70 | - IGroupDetailsBackend, |
|
71 | - IRemoveFromGroupBackend, |
|
72 | - ISetDisplayNameBackend { |
|
73 | - |
|
74 | - /** @var string[] */ |
|
75 | - private $groupCache = []; |
|
76 | - |
|
77 | - /** @var IDBConnection */ |
|
78 | - private $dbConn; |
|
79 | - |
|
80 | - /** |
|
81 | - * \OC\Group\Database constructor. |
|
82 | - * |
|
83 | - * @param IDBConnection|null $dbConn |
|
84 | - */ |
|
85 | - public function __construct(IDBConnection $dbConn = null) { |
|
86 | - $this->dbConn = $dbConn; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * FIXME: This function should not be required! |
|
91 | - */ |
|
92 | - private function fixDI() { |
|
93 | - if ($this->dbConn === null) { |
|
94 | - $this->dbConn = \OC::$server->getDatabaseConnection(); |
|
95 | - } |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Try to create a new group |
|
100 | - * @param string $gid The name of the group to create |
|
101 | - * @return bool |
|
102 | - * |
|
103 | - * Tries to create a new group. If the group name already exists, false will |
|
104 | - * be returned. |
|
105 | - */ |
|
106 | - public function createGroup(string $gid): bool { |
|
107 | - $this->fixDI(); |
|
108 | - |
|
109 | - try { |
|
110 | - // Add group |
|
111 | - $builder = $this->dbConn->getQueryBuilder(); |
|
112 | - $result = $builder->insert('groups') |
|
113 | - ->setValue('gid', $builder->createNamedParameter($gid)) |
|
114 | - ->setValue('displayname', $builder->createNamedParameter($gid)) |
|
115 | - ->execute(); |
|
116 | - } catch (UniqueConstraintViolationException $e) { |
|
117 | - $result = 0; |
|
118 | - } |
|
119 | - |
|
120 | - // Add to cache |
|
121 | - $this->groupCache[$gid] = [ |
|
122 | - 'gid' => $gid, |
|
123 | - 'displayname' => $gid |
|
124 | - ]; |
|
125 | - |
|
126 | - return $result === 1; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * delete a group |
|
131 | - * @param string $gid gid of the group to delete |
|
132 | - * @return bool |
|
133 | - * |
|
134 | - * Deletes a group and removes it from the group_user-table |
|
135 | - */ |
|
136 | - public function deleteGroup(string $gid): bool { |
|
137 | - $this->fixDI(); |
|
138 | - |
|
139 | - // Delete the group |
|
140 | - $qb = $this->dbConn->getQueryBuilder(); |
|
141 | - $qb->delete('groups') |
|
142 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
143 | - ->execute(); |
|
144 | - |
|
145 | - // Delete the group-user relation |
|
146 | - $qb = $this->dbConn->getQueryBuilder(); |
|
147 | - $qb->delete('group_user') |
|
148 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
149 | - ->execute(); |
|
150 | - |
|
151 | - // Delete the group-groupadmin relation |
|
152 | - $qb = $this->dbConn->getQueryBuilder(); |
|
153 | - $qb->delete('group_admin') |
|
154 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
155 | - ->execute(); |
|
156 | - |
|
157 | - // Delete from cache |
|
158 | - unset($this->groupCache[$gid]); |
|
159 | - |
|
160 | - return true; |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * is user in group? |
|
165 | - * @param string $uid uid of the user |
|
166 | - * @param string $gid gid of the group |
|
167 | - * @return bool |
|
168 | - * |
|
169 | - * Checks whether the user is member of a group or not. |
|
170 | - */ |
|
171 | - public function inGroup($uid, $gid) { |
|
172 | - $this->fixDI(); |
|
173 | - |
|
174 | - // check |
|
175 | - $qb = $this->dbConn->getQueryBuilder(); |
|
176 | - $cursor = $qb->select('uid') |
|
177 | - ->from('group_user') |
|
178 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
179 | - ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
180 | - ->execute(); |
|
181 | - |
|
182 | - $result = $cursor->fetch(); |
|
183 | - $cursor->closeCursor(); |
|
184 | - |
|
185 | - return $result ? true : false; |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * Add a user to a group |
|
190 | - * @param string $uid Name of the user to add to group |
|
191 | - * @param string $gid Name of the group in which add the user |
|
192 | - * @return bool |
|
193 | - * |
|
194 | - * Adds a user to a group. |
|
195 | - */ |
|
196 | - public function addToGroup(string $uid, string $gid): bool { |
|
197 | - $this->fixDI(); |
|
198 | - |
|
199 | - // No duplicate entries! |
|
200 | - if (!$this->inGroup($uid, $gid)) { |
|
201 | - $qb = $this->dbConn->getQueryBuilder(); |
|
202 | - $qb->insert('group_user') |
|
203 | - ->setValue('uid', $qb->createNamedParameter($uid)) |
|
204 | - ->setValue('gid', $qb->createNamedParameter($gid)) |
|
205 | - ->execute(); |
|
206 | - return true; |
|
207 | - } else { |
|
208 | - return false; |
|
209 | - } |
|
210 | - } |
|
211 | - |
|
212 | - /** |
|
213 | - * Removes a user from a group |
|
214 | - * @param string $uid Name of the user to remove from group |
|
215 | - * @param string $gid Name of the group from which remove the user |
|
216 | - * @return bool |
|
217 | - * |
|
218 | - * removes the user from a group. |
|
219 | - */ |
|
220 | - public function removeFromGroup(string $uid, string $gid): bool { |
|
221 | - $this->fixDI(); |
|
222 | - |
|
223 | - $qb = $this->dbConn->getQueryBuilder(); |
|
224 | - $qb->delete('group_user') |
|
225 | - ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
226 | - ->andWhere($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
227 | - ->execute(); |
|
228 | - |
|
229 | - return true; |
|
230 | - } |
|
231 | - |
|
232 | - /** |
|
233 | - * Get all groups a user belongs to |
|
234 | - * @param string $uid Name of the user |
|
235 | - * @return array an array of group names |
|
236 | - * |
|
237 | - * This function fetches all groups a user belongs to. It does not check |
|
238 | - * if the user exists at all. |
|
239 | - */ |
|
240 | - public function getUserGroups($uid) { |
|
241 | - //guests has empty or null $uid |
|
242 | - if ($uid === null || $uid === '') { |
|
243 | - return []; |
|
244 | - } |
|
245 | - |
|
246 | - $this->fixDI(); |
|
247 | - |
|
248 | - // No magic! |
|
249 | - $qb = $this->dbConn->getQueryBuilder(); |
|
250 | - $cursor = $qb->select('gu.gid', 'g.displayname') |
|
251 | - ->from('group_user', 'gu') |
|
252 | - ->leftJoin('gu', 'groups', 'g', $qb->expr()->eq('gu.gid', 'g.gid')) |
|
253 | - ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
254 | - ->execute(); |
|
255 | - |
|
256 | - $groups = []; |
|
257 | - while ($row = $cursor->fetch()) { |
|
258 | - $groups[] = $row['gid']; |
|
259 | - $this->groupCache[$row['gid']] = [ |
|
260 | - 'gid' => $row['gid'], |
|
261 | - 'displayname' => $row['displayname'], |
|
262 | - ]; |
|
263 | - } |
|
264 | - $cursor->closeCursor(); |
|
265 | - |
|
266 | - return $groups; |
|
267 | - } |
|
268 | - |
|
269 | - /** |
|
270 | - * get a list of all groups |
|
271 | - * @param string $search |
|
272 | - * @param int $limit |
|
273 | - * @param int $offset |
|
274 | - * @return array an array of group names |
|
275 | - * |
|
276 | - * Returns a list with all groups |
|
277 | - */ |
|
278 | - public function getGroups($search = '', $limit = null, $offset = null) { |
|
279 | - $this->fixDI(); |
|
280 | - |
|
281 | - $query = $this->dbConn->getQueryBuilder(); |
|
282 | - $query->select('gid') |
|
283 | - ->from('groups') |
|
284 | - ->orderBy('gid', 'ASC'); |
|
285 | - |
|
286 | - if ($search !== '') { |
|
287 | - $query->where($query->expr()->iLike('gid', $query->createNamedParameter( |
|
288 | - '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
289 | - ))); |
|
290 | - $query->orWhere($query->expr()->iLike('displayname', $query->createNamedParameter( |
|
291 | - '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
292 | - ))); |
|
293 | - } |
|
294 | - |
|
295 | - $query->setMaxResults($limit) |
|
296 | - ->setFirstResult($offset); |
|
297 | - $result = $query->execute(); |
|
298 | - |
|
299 | - $groups = []; |
|
300 | - while ($row = $result->fetch()) { |
|
301 | - $groups[] = $row['gid']; |
|
302 | - } |
|
303 | - $result->closeCursor(); |
|
304 | - |
|
305 | - return $groups; |
|
306 | - } |
|
307 | - |
|
308 | - /** |
|
309 | - * check if a group exists |
|
310 | - * @param string $gid |
|
311 | - * @return bool |
|
312 | - */ |
|
313 | - public function groupExists($gid) { |
|
314 | - $this->fixDI(); |
|
315 | - |
|
316 | - // Check cache first |
|
317 | - if (isset($this->groupCache[$gid])) { |
|
318 | - return true; |
|
319 | - } |
|
320 | - |
|
321 | - $qb = $this->dbConn->getQueryBuilder(); |
|
322 | - $cursor = $qb->select('gid', 'displayname') |
|
323 | - ->from('groups') |
|
324 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
325 | - ->execute(); |
|
326 | - $result = $cursor->fetch(); |
|
327 | - $cursor->closeCursor(); |
|
328 | - |
|
329 | - if ($result !== false) { |
|
330 | - $this->groupCache[$gid] = [ |
|
331 | - 'gid' => $gid, |
|
332 | - 'displayname' => $result['displayname'], |
|
333 | - ]; |
|
334 | - return true; |
|
335 | - } |
|
336 | - return false; |
|
337 | - } |
|
338 | - |
|
339 | - /** |
|
340 | - * get a list of all users in a group |
|
341 | - * @param string $gid |
|
342 | - * @param string $search |
|
343 | - * @param int $limit |
|
344 | - * @param int $offset |
|
345 | - * @return array an array of user ids |
|
346 | - */ |
|
347 | - public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
|
348 | - $this->fixDI(); |
|
349 | - |
|
350 | - $query = $this->dbConn->getQueryBuilder(); |
|
351 | - $query->select('uid') |
|
352 | - ->from('group_user') |
|
353 | - ->where($query->expr()->eq('gid', $query->createNamedParameter($gid))) |
|
354 | - ->orderBy('uid', 'ASC'); |
|
355 | - |
|
356 | - if ($search !== '') { |
|
357 | - $query->andWhere($query->expr()->like('uid', $query->createNamedParameter( |
|
358 | - '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
359 | - ))); |
|
360 | - } |
|
361 | - |
|
362 | - if ($limit !== -1) { |
|
363 | - $query->setMaxResults($limit); |
|
364 | - } |
|
365 | - if ($offset !== 0) { |
|
366 | - $query->setFirstResult($offset); |
|
367 | - } |
|
368 | - |
|
369 | - $result = $query->execute(); |
|
370 | - |
|
371 | - $users = []; |
|
372 | - while ($row = $result->fetch()) { |
|
373 | - $users[] = $row['uid']; |
|
374 | - } |
|
375 | - $result->closeCursor(); |
|
376 | - |
|
377 | - return $users; |
|
378 | - } |
|
379 | - |
|
380 | - /** |
|
381 | - * get the number of all users matching the search string in a group |
|
382 | - * @param string $gid |
|
383 | - * @param string $search |
|
384 | - * @return int |
|
385 | - */ |
|
386 | - public function countUsersInGroup(string $gid, string $search = ''): int { |
|
387 | - $this->fixDI(); |
|
388 | - |
|
389 | - $query = $this->dbConn->getQueryBuilder(); |
|
390 | - $query->select($query->func()->count('*', 'num_users')) |
|
391 | - ->from('group_user') |
|
392 | - ->where($query->expr()->eq('gid', $query->createNamedParameter($gid))); |
|
393 | - |
|
394 | - if ($search !== '') { |
|
395 | - $query->andWhere($query->expr()->like('uid', $query->createNamedParameter( |
|
396 | - '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
397 | - ))); |
|
398 | - } |
|
399 | - |
|
400 | - $result = $query->execute(); |
|
401 | - $count = $result->fetchColumn(); |
|
402 | - $result->closeCursor(); |
|
403 | - |
|
404 | - if ($count !== false) { |
|
405 | - $count = (int)$count; |
|
406 | - } else { |
|
407 | - $count = 0; |
|
408 | - } |
|
409 | - |
|
410 | - return $count; |
|
411 | - } |
|
412 | - |
|
413 | - /** |
|
414 | - * get the number of disabled users in a group |
|
415 | - * |
|
416 | - * @param string $search |
|
417 | - * |
|
418 | - * @return int |
|
419 | - */ |
|
420 | - public function countDisabledInGroup(string $gid): int { |
|
421 | - $this->fixDI(); |
|
422 | - |
|
423 | - $query = $this->dbConn->getQueryBuilder(); |
|
424 | - $query->select($query->createFunction('COUNT(DISTINCT ' . $query->getColumnName('uid') . ')')) |
|
425 | - ->from('preferences', 'p') |
|
426 | - ->innerJoin('p', 'group_user', 'g', $query->expr()->eq('p.userid', 'g.uid')) |
|
427 | - ->where($query->expr()->eq('appid', $query->createNamedParameter('core'))) |
|
428 | - ->andWhere($query->expr()->eq('configkey', $query->createNamedParameter('enabled'))) |
|
429 | - ->andWhere($query->expr()->eq('configvalue', $query->createNamedParameter('false'), IQueryBuilder::PARAM_STR)) |
|
430 | - ->andWhere($query->expr()->eq('gid', $query->createNamedParameter($gid), IQueryBuilder::PARAM_STR)); |
|
431 | - |
|
432 | - $result = $query->execute(); |
|
433 | - $count = $result->fetchColumn(); |
|
434 | - $result->closeCursor(); |
|
435 | - |
|
436 | - if ($count !== false) { |
|
437 | - $count = (int)$count; |
|
438 | - } else { |
|
439 | - $count = 0; |
|
440 | - } |
|
441 | - |
|
442 | - return $count; |
|
443 | - } |
|
444 | - |
|
445 | - public function getDisplayName(string $gid): string { |
|
446 | - if (isset($this->groupCache[$gid])) { |
|
447 | - return $this->groupCache[$gid]['displayname']; |
|
448 | - } |
|
449 | - |
|
450 | - $this->fixDI(); |
|
451 | - |
|
452 | - $query = $this->dbConn->getQueryBuilder(); |
|
453 | - $query->select('displayname') |
|
454 | - ->from('groups') |
|
455 | - ->where($query->expr()->eq('gid', $query->createNamedParameter($gid))); |
|
456 | - |
|
457 | - $result = $query->execute(); |
|
458 | - $displayName = $result->fetchColumn(); |
|
459 | - $result->closeCursor(); |
|
460 | - |
|
461 | - return (string) $displayName; |
|
462 | - } |
|
463 | - |
|
464 | - public function getGroupDetails(string $gid): array { |
|
465 | - $displayName = $this->getDisplayName($gid); |
|
466 | - if ($displayName !== '') { |
|
467 | - return ['displayName' => $displayName]; |
|
468 | - } |
|
469 | - |
|
470 | - return []; |
|
471 | - } |
|
472 | - |
|
473 | - public function setDisplayName(string $gid, string $displayName): bool { |
|
474 | - if (!$this->groupExists($gid)) { |
|
475 | - return false; |
|
476 | - } |
|
477 | - |
|
478 | - $this->fixDI(); |
|
479 | - |
|
480 | - $displayName = trim($displayName); |
|
481 | - if ($displayName === '') { |
|
482 | - $displayName = $gid; |
|
483 | - } |
|
484 | - |
|
485 | - $query = $this->dbConn->getQueryBuilder(); |
|
486 | - $query->update('groups') |
|
487 | - ->set('displayname', $query->createNamedParameter($displayName)) |
|
488 | - ->where($query->expr()->eq('gid', $query->createNamedParameter($gid))); |
|
489 | - $query->execute(); |
|
490 | - |
|
491 | - return true; |
|
492 | - } |
|
64 | + IAddToGroupBackend, |
|
65 | + ICountDisabledInGroup, |
|
66 | + ICountUsersBackend, |
|
67 | + ICreateGroupBackend, |
|
68 | + IDeleteGroupBackend, |
|
69 | + IGetDisplayNameBackend, |
|
70 | + IGroupDetailsBackend, |
|
71 | + IRemoveFromGroupBackend, |
|
72 | + ISetDisplayNameBackend { |
|
73 | + |
|
74 | + /** @var string[] */ |
|
75 | + private $groupCache = []; |
|
76 | + |
|
77 | + /** @var IDBConnection */ |
|
78 | + private $dbConn; |
|
79 | + |
|
80 | + /** |
|
81 | + * \OC\Group\Database constructor. |
|
82 | + * |
|
83 | + * @param IDBConnection|null $dbConn |
|
84 | + */ |
|
85 | + public function __construct(IDBConnection $dbConn = null) { |
|
86 | + $this->dbConn = $dbConn; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * FIXME: This function should not be required! |
|
91 | + */ |
|
92 | + private function fixDI() { |
|
93 | + if ($this->dbConn === null) { |
|
94 | + $this->dbConn = \OC::$server->getDatabaseConnection(); |
|
95 | + } |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Try to create a new group |
|
100 | + * @param string $gid The name of the group to create |
|
101 | + * @return bool |
|
102 | + * |
|
103 | + * Tries to create a new group. If the group name already exists, false will |
|
104 | + * be returned. |
|
105 | + */ |
|
106 | + public function createGroup(string $gid): bool { |
|
107 | + $this->fixDI(); |
|
108 | + |
|
109 | + try { |
|
110 | + // Add group |
|
111 | + $builder = $this->dbConn->getQueryBuilder(); |
|
112 | + $result = $builder->insert('groups') |
|
113 | + ->setValue('gid', $builder->createNamedParameter($gid)) |
|
114 | + ->setValue('displayname', $builder->createNamedParameter($gid)) |
|
115 | + ->execute(); |
|
116 | + } catch (UniqueConstraintViolationException $e) { |
|
117 | + $result = 0; |
|
118 | + } |
|
119 | + |
|
120 | + // Add to cache |
|
121 | + $this->groupCache[$gid] = [ |
|
122 | + 'gid' => $gid, |
|
123 | + 'displayname' => $gid |
|
124 | + ]; |
|
125 | + |
|
126 | + return $result === 1; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * delete a group |
|
131 | + * @param string $gid gid of the group to delete |
|
132 | + * @return bool |
|
133 | + * |
|
134 | + * Deletes a group and removes it from the group_user-table |
|
135 | + */ |
|
136 | + public function deleteGroup(string $gid): bool { |
|
137 | + $this->fixDI(); |
|
138 | + |
|
139 | + // Delete the group |
|
140 | + $qb = $this->dbConn->getQueryBuilder(); |
|
141 | + $qb->delete('groups') |
|
142 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
143 | + ->execute(); |
|
144 | + |
|
145 | + // Delete the group-user relation |
|
146 | + $qb = $this->dbConn->getQueryBuilder(); |
|
147 | + $qb->delete('group_user') |
|
148 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
149 | + ->execute(); |
|
150 | + |
|
151 | + // Delete the group-groupadmin relation |
|
152 | + $qb = $this->dbConn->getQueryBuilder(); |
|
153 | + $qb->delete('group_admin') |
|
154 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
155 | + ->execute(); |
|
156 | + |
|
157 | + // Delete from cache |
|
158 | + unset($this->groupCache[$gid]); |
|
159 | + |
|
160 | + return true; |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * is user in group? |
|
165 | + * @param string $uid uid of the user |
|
166 | + * @param string $gid gid of the group |
|
167 | + * @return bool |
|
168 | + * |
|
169 | + * Checks whether the user is member of a group or not. |
|
170 | + */ |
|
171 | + public function inGroup($uid, $gid) { |
|
172 | + $this->fixDI(); |
|
173 | + |
|
174 | + // check |
|
175 | + $qb = $this->dbConn->getQueryBuilder(); |
|
176 | + $cursor = $qb->select('uid') |
|
177 | + ->from('group_user') |
|
178 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
179 | + ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
180 | + ->execute(); |
|
181 | + |
|
182 | + $result = $cursor->fetch(); |
|
183 | + $cursor->closeCursor(); |
|
184 | + |
|
185 | + return $result ? true : false; |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * Add a user to a group |
|
190 | + * @param string $uid Name of the user to add to group |
|
191 | + * @param string $gid Name of the group in which add the user |
|
192 | + * @return bool |
|
193 | + * |
|
194 | + * Adds a user to a group. |
|
195 | + */ |
|
196 | + public function addToGroup(string $uid, string $gid): bool { |
|
197 | + $this->fixDI(); |
|
198 | + |
|
199 | + // No duplicate entries! |
|
200 | + if (!$this->inGroup($uid, $gid)) { |
|
201 | + $qb = $this->dbConn->getQueryBuilder(); |
|
202 | + $qb->insert('group_user') |
|
203 | + ->setValue('uid', $qb->createNamedParameter($uid)) |
|
204 | + ->setValue('gid', $qb->createNamedParameter($gid)) |
|
205 | + ->execute(); |
|
206 | + return true; |
|
207 | + } else { |
|
208 | + return false; |
|
209 | + } |
|
210 | + } |
|
211 | + |
|
212 | + /** |
|
213 | + * Removes a user from a group |
|
214 | + * @param string $uid Name of the user to remove from group |
|
215 | + * @param string $gid Name of the group from which remove the user |
|
216 | + * @return bool |
|
217 | + * |
|
218 | + * removes the user from a group. |
|
219 | + */ |
|
220 | + public function removeFromGroup(string $uid, string $gid): bool { |
|
221 | + $this->fixDI(); |
|
222 | + |
|
223 | + $qb = $this->dbConn->getQueryBuilder(); |
|
224 | + $qb->delete('group_user') |
|
225 | + ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
226 | + ->andWhere($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
227 | + ->execute(); |
|
228 | + |
|
229 | + return true; |
|
230 | + } |
|
231 | + |
|
232 | + /** |
|
233 | + * Get all groups a user belongs to |
|
234 | + * @param string $uid Name of the user |
|
235 | + * @return array an array of group names |
|
236 | + * |
|
237 | + * This function fetches all groups a user belongs to. It does not check |
|
238 | + * if the user exists at all. |
|
239 | + */ |
|
240 | + public function getUserGroups($uid) { |
|
241 | + //guests has empty or null $uid |
|
242 | + if ($uid === null || $uid === '') { |
|
243 | + return []; |
|
244 | + } |
|
245 | + |
|
246 | + $this->fixDI(); |
|
247 | + |
|
248 | + // No magic! |
|
249 | + $qb = $this->dbConn->getQueryBuilder(); |
|
250 | + $cursor = $qb->select('gu.gid', 'g.displayname') |
|
251 | + ->from('group_user', 'gu') |
|
252 | + ->leftJoin('gu', 'groups', 'g', $qb->expr()->eq('gu.gid', 'g.gid')) |
|
253 | + ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
254 | + ->execute(); |
|
255 | + |
|
256 | + $groups = []; |
|
257 | + while ($row = $cursor->fetch()) { |
|
258 | + $groups[] = $row['gid']; |
|
259 | + $this->groupCache[$row['gid']] = [ |
|
260 | + 'gid' => $row['gid'], |
|
261 | + 'displayname' => $row['displayname'], |
|
262 | + ]; |
|
263 | + } |
|
264 | + $cursor->closeCursor(); |
|
265 | + |
|
266 | + return $groups; |
|
267 | + } |
|
268 | + |
|
269 | + /** |
|
270 | + * get a list of all groups |
|
271 | + * @param string $search |
|
272 | + * @param int $limit |
|
273 | + * @param int $offset |
|
274 | + * @return array an array of group names |
|
275 | + * |
|
276 | + * Returns a list with all groups |
|
277 | + */ |
|
278 | + public function getGroups($search = '', $limit = null, $offset = null) { |
|
279 | + $this->fixDI(); |
|
280 | + |
|
281 | + $query = $this->dbConn->getQueryBuilder(); |
|
282 | + $query->select('gid') |
|
283 | + ->from('groups') |
|
284 | + ->orderBy('gid', 'ASC'); |
|
285 | + |
|
286 | + if ($search !== '') { |
|
287 | + $query->where($query->expr()->iLike('gid', $query->createNamedParameter( |
|
288 | + '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
289 | + ))); |
|
290 | + $query->orWhere($query->expr()->iLike('displayname', $query->createNamedParameter( |
|
291 | + '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
292 | + ))); |
|
293 | + } |
|
294 | + |
|
295 | + $query->setMaxResults($limit) |
|
296 | + ->setFirstResult($offset); |
|
297 | + $result = $query->execute(); |
|
298 | + |
|
299 | + $groups = []; |
|
300 | + while ($row = $result->fetch()) { |
|
301 | + $groups[] = $row['gid']; |
|
302 | + } |
|
303 | + $result->closeCursor(); |
|
304 | + |
|
305 | + return $groups; |
|
306 | + } |
|
307 | + |
|
308 | + /** |
|
309 | + * check if a group exists |
|
310 | + * @param string $gid |
|
311 | + * @return bool |
|
312 | + */ |
|
313 | + public function groupExists($gid) { |
|
314 | + $this->fixDI(); |
|
315 | + |
|
316 | + // Check cache first |
|
317 | + if (isset($this->groupCache[$gid])) { |
|
318 | + return true; |
|
319 | + } |
|
320 | + |
|
321 | + $qb = $this->dbConn->getQueryBuilder(); |
|
322 | + $cursor = $qb->select('gid', 'displayname') |
|
323 | + ->from('groups') |
|
324 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
325 | + ->execute(); |
|
326 | + $result = $cursor->fetch(); |
|
327 | + $cursor->closeCursor(); |
|
328 | + |
|
329 | + if ($result !== false) { |
|
330 | + $this->groupCache[$gid] = [ |
|
331 | + 'gid' => $gid, |
|
332 | + 'displayname' => $result['displayname'], |
|
333 | + ]; |
|
334 | + return true; |
|
335 | + } |
|
336 | + return false; |
|
337 | + } |
|
338 | + |
|
339 | + /** |
|
340 | + * get a list of all users in a group |
|
341 | + * @param string $gid |
|
342 | + * @param string $search |
|
343 | + * @param int $limit |
|
344 | + * @param int $offset |
|
345 | + * @return array an array of user ids |
|
346 | + */ |
|
347 | + public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
|
348 | + $this->fixDI(); |
|
349 | + |
|
350 | + $query = $this->dbConn->getQueryBuilder(); |
|
351 | + $query->select('uid') |
|
352 | + ->from('group_user') |
|
353 | + ->where($query->expr()->eq('gid', $query->createNamedParameter($gid))) |
|
354 | + ->orderBy('uid', 'ASC'); |
|
355 | + |
|
356 | + if ($search !== '') { |
|
357 | + $query->andWhere($query->expr()->like('uid', $query->createNamedParameter( |
|
358 | + '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
359 | + ))); |
|
360 | + } |
|
361 | + |
|
362 | + if ($limit !== -1) { |
|
363 | + $query->setMaxResults($limit); |
|
364 | + } |
|
365 | + if ($offset !== 0) { |
|
366 | + $query->setFirstResult($offset); |
|
367 | + } |
|
368 | + |
|
369 | + $result = $query->execute(); |
|
370 | + |
|
371 | + $users = []; |
|
372 | + while ($row = $result->fetch()) { |
|
373 | + $users[] = $row['uid']; |
|
374 | + } |
|
375 | + $result->closeCursor(); |
|
376 | + |
|
377 | + return $users; |
|
378 | + } |
|
379 | + |
|
380 | + /** |
|
381 | + * get the number of all users matching the search string in a group |
|
382 | + * @param string $gid |
|
383 | + * @param string $search |
|
384 | + * @return int |
|
385 | + */ |
|
386 | + public function countUsersInGroup(string $gid, string $search = ''): int { |
|
387 | + $this->fixDI(); |
|
388 | + |
|
389 | + $query = $this->dbConn->getQueryBuilder(); |
|
390 | + $query->select($query->func()->count('*', 'num_users')) |
|
391 | + ->from('group_user') |
|
392 | + ->where($query->expr()->eq('gid', $query->createNamedParameter($gid))); |
|
393 | + |
|
394 | + if ($search !== '') { |
|
395 | + $query->andWhere($query->expr()->like('uid', $query->createNamedParameter( |
|
396 | + '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
397 | + ))); |
|
398 | + } |
|
399 | + |
|
400 | + $result = $query->execute(); |
|
401 | + $count = $result->fetchColumn(); |
|
402 | + $result->closeCursor(); |
|
403 | + |
|
404 | + if ($count !== false) { |
|
405 | + $count = (int)$count; |
|
406 | + } else { |
|
407 | + $count = 0; |
|
408 | + } |
|
409 | + |
|
410 | + return $count; |
|
411 | + } |
|
412 | + |
|
413 | + /** |
|
414 | + * get the number of disabled users in a group |
|
415 | + * |
|
416 | + * @param string $search |
|
417 | + * |
|
418 | + * @return int |
|
419 | + */ |
|
420 | + public function countDisabledInGroup(string $gid): int { |
|
421 | + $this->fixDI(); |
|
422 | + |
|
423 | + $query = $this->dbConn->getQueryBuilder(); |
|
424 | + $query->select($query->createFunction('COUNT(DISTINCT ' . $query->getColumnName('uid') . ')')) |
|
425 | + ->from('preferences', 'p') |
|
426 | + ->innerJoin('p', 'group_user', 'g', $query->expr()->eq('p.userid', 'g.uid')) |
|
427 | + ->where($query->expr()->eq('appid', $query->createNamedParameter('core'))) |
|
428 | + ->andWhere($query->expr()->eq('configkey', $query->createNamedParameter('enabled'))) |
|
429 | + ->andWhere($query->expr()->eq('configvalue', $query->createNamedParameter('false'), IQueryBuilder::PARAM_STR)) |
|
430 | + ->andWhere($query->expr()->eq('gid', $query->createNamedParameter($gid), IQueryBuilder::PARAM_STR)); |
|
431 | + |
|
432 | + $result = $query->execute(); |
|
433 | + $count = $result->fetchColumn(); |
|
434 | + $result->closeCursor(); |
|
435 | + |
|
436 | + if ($count !== false) { |
|
437 | + $count = (int)$count; |
|
438 | + } else { |
|
439 | + $count = 0; |
|
440 | + } |
|
441 | + |
|
442 | + return $count; |
|
443 | + } |
|
444 | + |
|
445 | + public function getDisplayName(string $gid): string { |
|
446 | + if (isset($this->groupCache[$gid])) { |
|
447 | + return $this->groupCache[$gid]['displayname']; |
|
448 | + } |
|
449 | + |
|
450 | + $this->fixDI(); |
|
451 | + |
|
452 | + $query = $this->dbConn->getQueryBuilder(); |
|
453 | + $query->select('displayname') |
|
454 | + ->from('groups') |
|
455 | + ->where($query->expr()->eq('gid', $query->createNamedParameter($gid))); |
|
456 | + |
|
457 | + $result = $query->execute(); |
|
458 | + $displayName = $result->fetchColumn(); |
|
459 | + $result->closeCursor(); |
|
460 | + |
|
461 | + return (string) $displayName; |
|
462 | + } |
|
463 | + |
|
464 | + public function getGroupDetails(string $gid): array { |
|
465 | + $displayName = $this->getDisplayName($gid); |
|
466 | + if ($displayName !== '') { |
|
467 | + return ['displayName' => $displayName]; |
|
468 | + } |
|
469 | + |
|
470 | + return []; |
|
471 | + } |
|
472 | + |
|
473 | + public function setDisplayName(string $gid, string $displayName): bool { |
|
474 | + if (!$this->groupExists($gid)) { |
|
475 | + return false; |
|
476 | + } |
|
477 | + |
|
478 | + $this->fixDI(); |
|
479 | + |
|
480 | + $displayName = trim($displayName); |
|
481 | + if ($displayName === '') { |
|
482 | + $displayName = $gid; |
|
483 | + } |
|
484 | + |
|
485 | + $query = $this->dbConn->getQueryBuilder(); |
|
486 | + $query->update('groups') |
|
487 | + ->set('displayname', $query->createNamedParameter($displayName)) |
|
488 | + ->where($query->expr()->eq('gid', $query->createNamedParameter($gid))); |
|
489 | + $query->execute(); |
|
490 | + |
|
491 | + return true; |
|
492 | + } |
|
493 | 493 | } |
@@ -285,10 +285,10 @@ discard block |
||
285 | 285 | |
286 | 286 | if ($search !== '') { |
287 | 287 | $query->where($query->expr()->iLike('gid', $query->createNamedParameter( |
288 | - '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
288 | + '%'.$this->dbConn->escapeLikeParameter($search).'%' |
|
289 | 289 | ))); |
290 | 290 | $query->orWhere($query->expr()->iLike('displayname', $query->createNamedParameter( |
291 | - '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
291 | + '%'.$this->dbConn->escapeLikeParameter($search).'%' |
|
292 | 292 | ))); |
293 | 293 | } |
294 | 294 | |
@@ -355,7 +355,7 @@ discard block |
||
355 | 355 | |
356 | 356 | if ($search !== '') { |
357 | 357 | $query->andWhere($query->expr()->like('uid', $query->createNamedParameter( |
358 | - '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
358 | + '%'.$this->dbConn->escapeLikeParameter($search).'%' |
|
359 | 359 | ))); |
360 | 360 | } |
361 | 361 | |
@@ -393,7 +393,7 @@ discard block |
||
393 | 393 | |
394 | 394 | if ($search !== '') { |
395 | 395 | $query->andWhere($query->expr()->like('uid', $query->createNamedParameter( |
396 | - '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
396 | + '%'.$this->dbConn->escapeLikeParameter($search).'%' |
|
397 | 397 | ))); |
398 | 398 | } |
399 | 399 | |
@@ -402,7 +402,7 @@ discard block |
||
402 | 402 | $result->closeCursor(); |
403 | 403 | |
404 | 404 | if ($count !== false) { |
405 | - $count = (int)$count; |
|
405 | + $count = (int) $count; |
|
406 | 406 | } else { |
407 | 407 | $count = 0; |
408 | 408 | } |
@@ -421,7 +421,7 @@ discard block |
||
421 | 421 | $this->fixDI(); |
422 | 422 | |
423 | 423 | $query = $this->dbConn->getQueryBuilder(); |
424 | - $query->select($query->createFunction('COUNT(DISTINCT ' . $query->getColumnName('uid') . ')')) |
|
424 | + $query->select($query->createFunction('COUNT(DISTINCT '.$query->getColumnName('uid').')')) |
|
425 | 425 | ->from('preferences', 'p') |
426 | 426 | ->innerJoin('p', 'group_user', 'g', $query->expr()->eq('p.userid', 'g.uid')) |
427 | 427 | ->where($query->expr()->eq('appid', $query->createNamedParameter('core'))) |
@@ -434,7 +434,7 @@ discard block |
||
434 | 434 | $result->closeCursor(); |
435 | 435 | |
436 | 436 | if ($count !== false) { |
437 | - $count = (int)$count; |
|
437 | + $count = (int) $count; |
|
438 | 438 | } else { |
439 | 439 | $count = 0; |
440 | 440 | } |