1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Aaron Wood <aaronjwood@gmail.com>
* @author Loki3000 <github@labcms.ru>
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
/*
*
* The following SQL statement is just a help for developers and will not be
* executed!
*
* CREATE TABLE `groups` (
* `gid` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
* PRIMARY KEY (`gid`)
* ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
*
* CREATE TABLE `group_user` (
* `gid` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
* `uid` varchar(64) COLLATE utf8_unicode_ci NOT NULL
* ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
*
*/
namespace OC\Group;
use OCP\IDBConnection;
/**
* Class for group management in a SQL Database (e.g. MySQL, SQLite)
*/
class Database extends Backend {
/** @var string[] */
private $groupCache = [];
/** @var IDBConnection */
private $dbConn;
/**
* \OC\Group\Database constructor.
*
* @param IDBConnection|null $dbConn
*/
public function __construct(IDBConnection $dbConn = null) {
$this->dbConn = $dbConn;
}
/**
* FIXME: This function should not be required!
*/
private function fixDI() {
if ($this->dbConn === null) {
$this->dbConn = \OC::$server->getDatabaseConnection();
}
}
/**
* Try to create a new group
* @param string $gid The name of the group to create
* @return bool
*
* Tries to create a new group. If the group name already exists, false will
* be returned.
*/
public function createGroup( $gid ) {
$this->fixDI();
// Add group
$result = $this->dbConn->insertIfNotExist('*PREFIX*groups', [
'gid' => $gid,
]);
// Add to cache
$this->groupCache[$gid] = $gid;
return $result === 1;
}
/**
* delete a group
* @param string $gid gid of the group to delete
* @return bool
*
* Deletes a group and removes it from the group_user-table
*/
public function deleteGroup( $gid ) {
$this->fixDI();
// Delete the group
$qb = $this->dbConn->getQueryBuilder();
$qb->delete('groups')
->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
->execute();
// Delete the group-user relation
$qb = $this->dbConn->getQueryBuilder();
$qb->delete('group_user')
->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
->execute();
// Delete the group-groupadmin relation
$qb = $this->dbConn->getQueryBuilder();
$qb->delete('group_admin')
->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
->execute();
// Delete from cache
unset($this->groupCache[$gid]);
return true;
}
/**
* is user in group?
* @param string $uid uid of the user
* @param string $gid gid of the group
* @return bool
*
* Checks whether the user is member of a group or not.
*/
public function inGroup( $uid, $gid ) {
$this->fixDI();
// check
$qb = $this->dbConn->getQueryBuilder();
$cursor = $qb->select('uid')
->from('group_user')
->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
->execute();
$result = $cursor->fetch();
$cursor->closeCursor();
return $result ? true : false;
}
/**
* Add a user to a group
* @param string $uid Name of the user to add to group
* @param string $gid Name of the group in which add the user
* @return bool
*
* Adds a user to a group.
*/
public function addToGroup( $uid, $gid ) {
$this->fixDI();
// No duplicate entries!
if( !$this->inGroup( $uid, $gid )) {
$qb = $this->dbConn->getQueryBuilder();
$qb->insert('group_user')
->setValue('uid', $qb->createNamedParameter($uid))
->setValue('gid', $qb->createNamedParameter($gid))
->execute();
return true;
}else{
return false;
}
}
/**
* Removes a user from a group
* @param string $uid Name of the user to remove from group
* @param string $gid Name of the group from which remove the user
* @return bool
*
* removes the user from a group.
*/
public function removeFromGroup( $uid, $gid ) {
$this->fixDI();
$qb = $this->dbConn->getQueryBuilder();
$qb->delete('group_user')
->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
->andWhere($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
->execute();
return true;
}
/**
* Get all groups a user belongs to
* @param string $uid Name of the user
* @return array an array of group names
*
* This function fetches all groups a user belongs to. It does not check
* if the user exists at all.
*/
public function getUserGroups( $uid ) {
//guests has empty or null $uid
if ($uid === null || $uid === '') {
return [];
}
$this->fixDI();
// No magic!
$qb = $this->dbConn->getQueryBuilder();
$cursor = $qb->select('gid')
->from('group_user')
->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
->execute();
$groups = [];
while( $row = $cursor->fetch()) {
$groups[] = $row['gid'];
$this->groupCache[$row['gid']] = $row['gid'];
}
$cursor->closeCursor();
return $groups;
}
/**
* get a list of all groups
* @param string $search
* @param int $limit
* @param int $offset
* @return array an array of group names
*
* Returns a list with all groups
*/
public function getGroups($search = '', $limit = null, $offset = null) {
$query = $this->dbConn->getQueryBuilder();
$query->select('gid')
->from('groups')
->orderBy('gid', 'ASC');
if ($search !== '') {
$query->where($query->expr()->iLike('gid', $query->createNamedParameter(
'%' . $this->dbConn->escapeLikeParameter($search) . '%'
)));
}
$query->setMaxResults($limit)
->setFirstResult($offset);
$result = $query->execute();
$groups = [];
while ($row = $result->fetch()) {
$groups[] = $row['gid'];
}
$result->closeCursor();
return $groups;
}
/**
* check if a group exists
* @param string $gid
* @return bool
*/
public function groupExists($gid) {
$this->fixDI();
// Check cache first
if (isset($this->groupCache[$gid])) {
return true;
}
$qb = $this->dbConn->getQueryBuilder();
$cursor = $qb->select('gid')
->from('groups')
->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid)))
->execute();
$result = $cursor->fetch();
$cursor->closeCursor();
if ($result !== false) {
$this->groupCache[$gid] = $gid;
return true;
}
return false;
}
/**
* get a list of all users in a group
* @param string $gid
* @param string $search
* @param int $limit
* @param int $offset
* @return array an array of user ids
*/
public function usersInGroup($gid, $search = '', $limit = null, $offset = null) {
$query = $this->dbConn->getQueryBuilder();
$query->select('uid')
->from('group_user')
->where($query->expr()->eq('gid', $query->createNamedParameter($gid)))
->orderBy('uid', 'ASC');
if ($search !== '') {
$query->andWhere($query->expr()->like('uid', $query->createNamedParameter(
'%' . $this->dbConn->escapeLikeParameter($search) . '%'
)));
}
$query->setMaxResults($limit)
->setFirstResult($offset);
$result = $query->execute();
$users = [];
while ($row = $result->fetch()) {
$users[] = $row['uid'];
}
$result->closeCursor();
return $users;
}
/**
* get the number of all users matching the search string in a group
* @param string $gid
* @param string $search
* @return int|false
*/
public function countUsersInGroup($gid, $search = '') {
$query = $this->dbConn->getQueryBuilder();
$query->selectAlias($query->createFunction('COUNT(*)'), 'num_users')
->from('group_user')
->where($query->expr()->eq('gid', $query->createNamedParameter($gid)))
->orderBy('uid', 'ASC');
if ($search !== '') {
$query->andWhere($query->expr()->like('uid', $query->createNamedParameter(
'%' . $this->dbConn->escapeLikeParameter($search) . '%'
)));
}
$result = $query->execute();
$count = $result->fetchColumn();
$result->closeCursor();
if ($count !== false) {
$count = (int)$count;
}
return $count;
}
}
|