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