Passed
Push — master ( 2b4b5d...79fc7e )
by Morris
11:24 queued 12s
created
lib/private/Group/Database.php 2 patches
Indentation   +441 added lines, -441 removed lines patch added patch discarded remove patch
@@ -61,445 +61,445 @@
 block discarded – undo
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('g.uid')
352
-			->from('group_user', 'g')
353
-			->where($query->expr()->eq('gid', $query->createNamedParameter($gid)))
354
-			->orderBy('g.uid', 'ASC');
355
-
356
-		if ($search !== '') {
357
-			$query->leftJoin('g', 'users', 'u', $query->expr()->eq('g.uid', 'u.uid'))
358
-				->leftJoin('u', 'preferences', 'p', $query->expr()->andX(
359
-					$query->expr()->eq('p.userid', 'u.uid'),
360
-					$query->expr()->eq('p.appid', $query->expr()->literal('settings')),
361
-					$query->expr()->eq('p.configkey', $query->expr()->literal('email')))
362
-				)
363
-				// sqlite doesn't like re-using a single named parameter here
364
-				->andWhere(
365
-					$query->expr()->orX(
366
-						$query->expr()->ilike('g.uid', $query->createNamedParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%')),
367
-						$query->expr()->ilike('u.displayname', $query->createNamedParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%')),
368
-						$query->expr()->ilike('p.configvalue', $query->createNamedParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%'))
369
-					)
370
-				)
371
-				->orderBy('u.uid_lower', 'ASC');
372
-		}
373
-
374
-		if ($limit !== -1) {
375
-			$query->setMaxResults($limit);
376
-		}
377
-		if ($offset !== 0) {
378
-			$query->setFirstResult($offset);
379
-		}
380
-
381
-		$result = $query->execute();
382
-
383
-		$users = [];
384
-		while ($row = $result->fetch()) {
385
-			$users[] = $row['uid'];
386
-		}
387
-		$result->closeCursor();
388
-
389
-		return $users;
390
-	}
391
-
392
-	/**
393
-	 * get the number of all users matching the search string in a group
394
-	 * @param string $gid
395
-	 * @param string $search
396
-	 * @return int
397
-	 */
398
-	public function countUsersInGroup(string $gid, string $search = ''): int {
399
-		$this->fixDI();
400
-
401
-		$query = $this->dbConn->getQueryBuilder();
402
-		$query->select($query->func()->count('*', 'num_users'))
403
-			->from('group_user')
404
-			->where($query->expr()->eq('gid', $query->createNamedParameter($gid)));
405
-
406
-		if ($search !== '') {
407
-			$query->andWhere($query->expr()->like('uid', $query->createNamedParameter(
408
-				'%' . $this->dbConn->escapeLikeParameter($search) . '%'
409
-			)));
410
-		}
411
-
412
-		$result = $query->execute();
413
-		$count = $result->fetchColumn();
414
-		$result->closeCursor();
415
-
416
-		if ($count !== false) {
417
-			$count = (int)$count;
418
-		} else {
419
-			$count = 0;
420
-		}
421
-
422
-		return $count;
423
-	}
424
-
425
-	/**
426
-	 * get the number of disabled users in a group
427
-	 *
428
-	 * @param string $search
429
-	 *
430
-	 * @return int
431
-	 */
432
-	public function countDisabledInGroup(string $gid): int {
433
-		$this->fixDI();
434
-
435
-		$query = $this->dbConn->getQueryBuilder();
436
-		$query->select($query->createFunction('COUNT(DISTINCT ' . $query->getColumnName('uid') . ')'))
437
-			->from('preferences', 'p')
438
-			->innerJoin('p', 'group_user', 'g', $query->expr()->eq('p.userid', 'g.uid'))
439
-			->where($query->expr()->eq('appid', $query->createNamedParameter('core')))
440
-			->andWhere($query->expr()->eq('configkey', $query->createNamedParameter('enabled')))
441
-			->andWhere($query->expr()->eq('configvalue', $query->createNamedParameter('false'), IQueryBuilder::PARAM_STR))
442
-			->andWhere($query->expr()->eq('gid', $query->createNamedParameter($gid), IQueryBuilder::PARAM_STR));
443
-
444
-		$result = $query->execute();
445
-		$count = $result->fetchColumn();
446
-		$result->closeCursor();
447
-
448
-		if ($count !== false) {
449
-			$count = (int)$count;
450
-		} else {
451
-			$count = 0;
452
-		}
453
-
454
-		return $count;
455
-	}
456
-
457
-	public function getDisplayName(string $gid): string {
458
-		if (isset($this->groupCache[$gid])) {
459
-			return $this->groupCache[$gid]['displayname'];
460
-		}
461
-
462
-		$this->fixDI();
463
-
464
-		$query = $this->dbConn->getQueryBuilder();
465
-		$query->select('displayname')
466
-			->from('groups')
467
-			->where($query->expr()->eq('gid', $query->createNamedParameter($gid)));
468
-
469
-		$result = $query->execute();
470
-		$displayName = $result->fetchColumn();
471
-		$result->closeCursor();
472
-
473
-		return (string) $displayName;
474
-	}
475
-
476
-	public function getGroupDetails(string $gid): array {
477
-		$displayName = $this->getDisplayName($gid);
478
-		if ($displayName !== '') {
479
-			return ['displayName' => $displayName];
480
-		}
481
-
482
-		return [];
483
-	}
484
-
485
-	public function setDisplayName(string $gid, string $displayName): bool {
486
-		if (!$this->groupExists($gid)) {
487
-			return false;
488
-		}
489
-
490
-		$this->fixDI();
491
-
492
-		$displayName = trim($displayName);
493
-		if ($displayName === '') {
494
-			$displayName = $gid;
495
-		}
496
-
497
-		$query = $this->dbConn->getQueryBuilder();
498
-		$query->update('groups')
499
-			->set('displayname', $query->createNamedParameter($displayName))
500
-			->where($query->expr()->eq('gid', $query->createNamedParameter($gid)));
501
-		$query->execute();
502
-
503
-		return true;
504
-	}
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('g.uid')
352
+            ->from('group_user', 'g')
353
+            ->where($query->expr()->eq('gid', $query->createNamedParameter($gid)))
354
+            ->orderBy('g.uid', 'ASC');
355
+
356
+        if ($search !== '') {
357
+            $query->leftJoin('g', 'users', 'u', $query->expr()->eq('g.uid', 'u.uid'))
358
+                ->leftJoin('u', 'preferences', 'p', $query->expr()->andX(
359
+                    $query->expr()->eq('p.userid', 'u.uid'),
360
+                    $query->expr()->eq('p.appid', $query->expr()->literal('settings')),
361
+                    $query->expr()->eq('p.configkey', $query->expr()->literal('email')))
362
+                )
363
+                // sqlite doesn't like re-using a single named parameter here
364
+                ->andWhere(
365
+                    $query->expr()->orX(
366
+                        $query->expr()->ilike('g.uid', $query->createNamedParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%')),
367
+                        $query->expr()->ilike('u.displayname', $query->createNamedParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%')),
368
+                        $query->expr()->ilike('p.configvalue', $query->createNamedParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%'))
369
+                    )
370
+                )
371
+                ->orderBy('u.uid_lower', 'ASC');
372
+        }
373
+
374
+        if ($limit !== -1) {
375
+            $query->setMaxResults($limit);
376
+        }
377
+        if ($offset !== 0) {
378
+            $query->setFirstResult($offset);
379
+        }
380
+
381
+        $result = $query->execute();
382
+
383
+        $users = [];
384
+        while ($row = $result->fetch()) {
385
+            $users[] = $row['uid'];
386
+        }
387
+        $result->closeCursor();
388
+
389
+        return $users;
390
+    }
391
+
392
+    /**
393
+     * get the number of all users matching the search string in a group
394
+     * @param string $gid
395
+     * @param string $search
396
+     * @return int
397
+     */
398
+    public function countUsersInGroup(string $gid, string $search = ''): int {
399
+        $this->fixDI();
400
+
401
+        $query = $this->dbConn->getQueryBuilder();
402
+        $query->select($query->func()->count('*', 'num_users'))
403
+            ->from('group_user')
404
+            ->where($query->expr()->eq('gid', $query->createNamedParameter($gid)));
405
+
406
+        if ($search !== '') {
407
+            $query->andWhere($query->expr()->like('uid', $query->createNamedParameter(
408
+                '%' . $this->dbConn->escapeLikeParameter($search) . '%'
409
+            )));
410
+        }
411
+
412
+        $result = $query->execute();
413
+        $count = $result->fetchColumn();
414
+        $result->closeCursor();
415
+
416
+        if ($count !== false) {
417
+            $count = (int)$count;
418
+        } else {
419
+            $count = 0;
420
+        }
421
+
422
+        return $count;
423
+    }
424
+
425
+    /**
426
+     * get the number of disabled users in a group
427
+     *
428
+     * @param string $search
429
+     *
430
+     * @return int
431
+     */
432
+    public function countDisabledInGroup(string $gid): int {
433
+        $this->fixDI();
434
+
435
+        $query = $this->dbConn->getQueryBuilder();
436
+        $query->select($query->createFunction('COUNT(DISTINCT ' . $query->getColumnName('uid') . ')'))
437
+            ->from('preferences', 'p')
438
+            ->innerJoin('p', 'group_user', 'g', $query->expr()->eq('p.userid', 'g.uid'))
439
+            ->where($query->expr()->eq('appid', $query->createNamedParameter('core')))
440
+            ->andWhere($query->expr()->eq('configkey', $query->createNamedParameter('enabled')))
441
+            ->andWhere($query->expr()->eq('configvalue', $query->createNamedParameter('false'), IQueryBuilder::PARAM_STR))
442
+            ->andWhere($query->expr()->eq('gid', $query->createNamedParameter($gid), IQueryBuilder::PARAM_STR));
443
+
444
+        $result = $query->execute();
445
+        $count = $result->fetchColumn();
446
+        $result->closeCursor();
447
+
448
+        if ($count !== false) {
449
+            $count = (int)$count;
450
+        } else {
451
+            $count = 0;
452
+        }
453
+
454
+        return $count;
455
+    }
456
+
457
+    public function getDisplayName(string $gid): string {
458
+        if (isset($this->groupCache[$gid])) {
459
+            return $this->groupCache[$gid]['displayname'];
460
+        }
461
+
462
+        $this->fixDI();
463
+
464
+        $query = $this->dbConn->getQueryBuilder();
465
+        $query->select('displayname')
466
+            ->from('groups')
467
+            ->where($query->expr()->eq('gid', $query->createNamedParameter($gid)));
468
+
469
+        $result = $query->execute();
470
+        $displayName = $result->fetchColumn();
471
+        $result->closeCursor();
472
+
473
+        return (string) $displayName;
474
+    }
475
+
476
+    public function getGroupDetails(string $gid): array {
477
+        $displayName = $this->getDisplayName($gid);
478
+        if ($displayName !== '') {
479
+            return ['displayName' => $displayName];
480
+        }
481
+
482
+        return [];
483
+    }
484
+
485
+    public function setDisplayName(string $gid, string $displayName): bool {
486
+        if (!$this->groupExists($gid)) {
487
+            return false;
488
+        }
489
+
490
+        $this->fixDI();
491
+
492
+        $displayName = trim($displayName);
493
+        if ($displayName === '') {
494
+            $displayName = $gid;
495
+        }
496
+
497
+        $query = $this->dbConn->getQueryBuilder();
498
+        $query->update('groups')
499
+            ->set('displayname', $query->createNamedParameter($displayName))
500
+            ->where($query->expr()->eq('gid', $query->createNamedParameter($gid)));
501
+        $query->execute();
502
+
503
+        return true;
504
+    }
505 505
 }
Please login to merge, or discard this patch.
Spacing   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -285,10 +285,10 @@  discard block
 block discarded – undo
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
 
@@ -363,9 +363,9 @@  discard block
 block discarded – undo
363 363
 				// sqlite doesn't like re-using a single named parameter here
364 364
 				->andWhere(
365 365
 					$query->expr()->orX(
366
-						$query->expr()->ilike('g.uid', $query->createNamedParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%')),
367
-						$query->expr()->ilike('u.displayname', $query->createNamedParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%')),
368
-						$query->expr()->ilike('p.configvalue', $query->createNamedParameter('%' . $this->dbConn->escapeLikeParameter($search) . '%'))
366
+						$query->expr()->ilike('g.uid', $query->createNamedParameter('%'.$this->dbConn->escapeLikeParameter($search).'%')),
367
+						$query->expr()->ilike('u.displayname', $query->createNamedParameter('%'.$this->dbConn->escapeLikeParameter($search).'%')),
368
+						$query->expr()->ilike('p.configvalue', $query->createNamedParameter('%'.$this->dbConn->escapeLikeParameter($search).'%'))
369 369
 					)
370 370
 				)
371 371
 				->orderBy('u.uid_lower', 'ASC');
@@ -405,7 +405,7 @@  discard block
 block discarded – undo
405 405
 
406 406
 		if ($search !== '') {
407 407
 			$query->andWhere($query->expr()->like('uid', $query->createNamedParameter(
408
-				'%' . $this->dbConn->escapeLikeParameter($search) . '%'
408
+				'%'.$this->dbConn->escapeLikeParameter($search).'%'
409 409
 			)));
410 410
 		}
411 411
 
@@ -414,7 +414,7 @@  discard block
 block discarded – undo
414 414
 		$result->closeCursor();
415 415
 
416 416
 		if ($count !== false) {
417
-			$count = (int)$count;
417
+			$count = (int) $count;
418 418
 		} else {
419 419
 			$count = 0;
420 420
 		}
@@ -433,7 +433,7 @@  discard block
 block discarded – undo
433 433
 		$this->fixDI();
434 434
 
435 435
 		$query = $this->dbConn->getQueryBuilder();
436
-		$query->select($query->createFunction('COUNT(DISTINCT ' . $query->getColumnName('uid') . ')'))
436
+		$query->select($query->createFunction('COUNT(DISTINCT '.$query->getColumnName('uid').')'))
437 437
 			->from('preferences', 'p')
438 438
 			->innerJoin('p', 'group_user', 'g', $query->expr()->eq('p.userid', 'g.uid'))
439 439
 			->where($query->expr()->eq('appid', $query->createNamedParameter('core')))
@@ -446,7 +446,7 @@  discard block
 block discarded – undo
446 446
 		$result->closeCursor();
447 447
 
448 448
 		if ($count !== false) {
449
-			$count = (int)$count;
449
+			$count = (int) $count;
450 450
 		} else {
451 451
 			$count = 0;
452 452
 		}
Please login to merge, or discard this patch.