1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Circles - Bring cloud-users closer together. |
4
|
|
|
* |
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
6
|
|
|
* later. See the COPYING file. |
7
|
|
|
* |
8
|
|
|
* @author Maxence Lange <[email protected]> |
9
|
|
|
* @copyright 2017 |
10
|
|
|
* @license GNU AGPL version 3 or any later version |
11
|
|
|
* |
12
|
|
|
* This program is free software: you can redistribute it and/or modify |
13
|
|
|
* it under the terms of the GNU Affero General Public License as |
14
|
|
|
* published by the Free Software Foundation, either version 3 of the |
15
|
|
|
* License, or (at your option) any later version. |
16
|
|
|
* |
17
|
|
|
* This program is distributed in the hope that it will be useful, |
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20
|
|
|
* GNU Affero General Public License for more details. |
21
|
|
|
* |
22
|
|
|
* You should have received a copy of the GNU Affero General Public License |
23
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
24
|
|
|
* |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
namespace OCA\Circles\Db; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
use OCA\Circles\Exceptions\CircleAlreadyExistsException; |
32
|
|
|
use OCA\Circles\Exceptions\CircleDoesNotExistException; |
33
|
|
|
use OCA\Circles\Exceptions\ConfigNoCircleAvailableException; |
34
|
|
|
use OCA\Circles\Model\Circle; |
35
|
|
|
use OCA\Circles\Model\Member; |
36
|
|
|
|
37
|
|
|
class CirclesRequest extends CirclesRequestBuilder { |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* forceGetCircle(); |
42
|
|
|
* |
43
|
|
|
* returns data of a circle from its Id. |
44
|
|
|
* |
45
|
|
|
* WARNING: This function does not filters data regarding the current user/viewer. |
46
|
|
|
* In case of interaction with users, Please use getCircle() instead. |
47
|
|
|
* |
48
|
|
|
* @param string $circleUniqueId |
49
|
|
|
* |
50
|
|
|
* @return Circle |
51
|
|
|
* @throws CircleDoesNotExistException |
52
|
|
|
*/ |
53
|
|
View Code Duplication |
public function forceGetCircle($circleUniqueId) { |
|
|
|
|
54
|
|
|
$qb = $this->getCirclesSelectSql(); |
55
|
|
|
|
56
|
|
|
$this->leftJoinOwner($qb, ''); |
57
|
|
|
$this->limitToShortenUniqueId($qb, $circleUniqueId, Circle::SHORT_UNIQUE_ID_LENGTH); |
58
|
|
|
|
59
|
|
|
$cursor = $qb->execute(); |
60
|
|
|
$data = $cursor->fetch(); |
61
|
|
|
$cursor->closeCursor(); |
62
|
|
|
|
63
|
|
|
if ($data === false) { |
64
|
|
|
throw new CircleDoesNotExistException($this->l10n->t('Circle not found')); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$entry = $this->parseCirclesSelectSql($data); |
68
|
|
|
|
69
|
|
|
return $entry; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* forceGetCircles(); |
75
|
|
|
* |
76
|
|
|
* returns data of a all circles. |
77
|
|
|
* |
78
|
|
|
* WARNING: This function does not filters data regarding the current user/viewer. |
79
|
|
|
* In case of interaction with users, Please use getCircles() instead. |
80
|
|
|
* |
81
|
|
|
* @param string $ownerId |
82
|
|
|
* |
83
|
|
|
* @return Circle[] |
84
|
|
|
*/ |
85
|
|
View Code Duplication |
public function forceGetCircles(string $ownerId = '') { |
|
|
|
|
86
|
|
|
$qb = $this->getCirclesSelectSql(); |
87
|
|
|
$this->leftJoinOwner($qb, $ownerId); |
88
|
|
|
|
89
|
|
|
$circles = []; |
90
|
|
|
$cursor = $qb->execute(); |
91
|
|
|
while ($data = $cursor->fetch()) { |
92
|
|
|
$circles[] = $this->parseCirclesSelectSql($data); |
93
|
|
|
} |
94
|
|
|
$cursor->closeCursor(); |
95
|
|
|
|
96
|
|
|
return $circles; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* forceGetCircleByName(); |
102
|
|
|
* |
103
|
|
|
* returns data of a circle from its Name. |
104
|
|
|
* |
105
|
|
|
* WARNING: This function does not filters data regarding the current user/viewer. |
106
|
|
|
* In case of interaction with users, do not use this method. |
107
|
|
|
* |
108
|
|
|
* @param $name |
109
|
|
|
* |
110
|
|
|
* @return null|Circle |
111
|
|
|
* @throws CircleDoesNotExistException |
112
|
|
|
*/ |
113
|
|
View Code Duplication |
public function forceGetCircleByName($name) { |
|
|
|
|
114
|
|
|
|
115
|
|
|
$qb = $this->getCirclesSelectSql(); |
116
|
|
|
|
117
|
|
|
$this->limitToName($qb, $name); |
118
|
|
|
|
119
|
|
|
$cursor = $qb->execute(); |
120
|
|
|
$data = $cursor->fetch(); |
121
|
|
|
$cursor->closeCursor(); |
122
|
|
|
|
123
|
|
|
if ($data === false) { |
124
|
|
|
throw new CircleDoesNotExistException($this->l10n->t('Circle not found')); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$entry = $this->parseCirclesSelectSql($data); |
128
|
|
|
|
129
|
|
|
return $entry; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* forceGetCircleByGroupId(); |
135
|
|
|
* |
136
|
|
|
* returns data of a circle from its Group ID. |
137
|
|
|
* |
138
|
|
|
* WARNING: This function does not filters data regarding the current user/viewer. |
139
|
|
|
* In case of interaction with users, do not use this method. |
140
|
|
|
* |
141
|
|
|
* @param $groupId |
142
|
|
|
* |
143
|
|
|
* @return null|Circle |
144
|
|
|
*/ |
145
|
|
|
public function forceGetCircleByGroupId($groupId) { |
146
|
|
|
$qb = $this->getCirclesSelectSql(); |
147
|
|
|
|
148
|
|
|
$this->limitToGroupId($qb, $groupId); |
149
|
|
|
|
150
|
|
|
$cursor = $qb->execute(); |
151
|
|
|
$data = $cursor->fetch(); |
152
|
|
|
$cursor->closeCursor(); |
153
|
|
|
|
154
|
|
|
if ($data === false) { |
155
|
|
|
return null; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$entry = $this->parseCirclesSelectSql($data); |
159
|
|
|
|
160
|
|
|
return $entry; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param string $userId |
165
|
|
|
* @param int $type |
166
|
|
|
* @param string $name |
167
|
|
|
* @param int $level |
168
|
|
|
* @param bool $forceAll |
169
|
|
|
* @param string $ownerId |
170
|
|
|
* |
171
|
|
|
* @return Circle[] |
172
|
|
|
* @throws ConfigNoCircleAvailableException |
173
|
|
|
*/ |
174
|
|
|
public function getCircles($userId, $type = 0, $name = '', $level = 0, $forceAll = false, string $ownerId = '') { |
175
|
|
|
if ($type === 0) { |
176
|
|
|
$type = Circle::CIRCLES_ALL; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$qb = $this->getCirclesSelectSql(); |
180
|
|
|
$this->leftJoinUserIdAsViewer($qb, $userId); |
181
|
|
|
$this->leftJoinOwner($qb, $ownerId); |
182
|
|
|
$this->leftJoinNCGroupAndUser($qb, $userId, '`c`.`unique_id`'); |
183
|
|
|
|
184
|
|
|
if ($level > 0) { |
185
|
|
|
$this->limitToLevel($qb, $level, ['u', 'g']); |
186
|
|
|
} |
187
|
|
|
$this->limitRegardingCircleType($qb, $userId, -1, $type, $name, $forceAll); |
188
|
|
|
|
189
|
|
|
$circles = []; |
190
|
|
|
$cursor = $qb->execute(); |
191
|
|
|
while ($data = $cursor->fetch()) { |
192
|
|
|
if ($name === '' || stripos(strtolower($data['name']), strtolower($name)) !== false) { |
193
|
|
|
$circles[] = $this->parseCirclesSelectSql($data); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
$cursor->closeCursor(); |
197
|
|
|
|
198
|
|
|
return $circles; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* |
204
|
|
|
* @param string $circleUniqueId |
205
|
|
|
* @param string $viewerId |
206
|
|
|
* @param bool $forceAll |
207
|
|
|
* |
208
|
|
|
* @return Circle |
209
|
|
|
* @throws CircleDoesNotExistException |
210
|
|
|
* @throws ConfigNoCircleAvailableException |
211
|
|
|
*/ |
212
|
|
|
public function getCircle($circleUniqueId, $viewerId, $forceAll = false) { |
213
|
|
|
$qb = $this->getCirclesSelectSql(); |
214
|
|
|
|
215
|
|
|
$this->limitToShortenUniqueId($qb, $circleUniqueId, Circle::SHORT_UNIQUE_ID_LENGTH); |
216
|
|
|
|
217
|
|
|
$this->leftJoinUserIdAsViewer($qb, $viewerId); |
218
|
|
|
$this->leftJoinOwner($qb); |
219
|
|
|
$this->leftJoinNCGroupAndUser($qb, $viewerId, '`c`.`unique_id`'); |
220
|
|
|
|
221
|
|
|
$this->limitRegardingCircleType($qb, $viewerId, $circleUniqueId, Circle::CIRCLES_ALL, '', $forceAll); |
222
|
|
|
|
223
|
|
|
$cursor = $qb->execute(); |
224
|
|
|
$data = $cursor->fetch(); |
225
|
|
|
$cursor->closeCursor(); |
226
|
|
|
|
227
|
|
|
if ($data === false) { |
228
|
|
|
throw new CircleDoesNotExistException($this->l10n->t('Circle not found')); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
$circle = $this->parseCirclesSelectSql($data); |
232
|
|
|
$circle->setGroupViewer( |
233
|
|
|
$this->membersRequest->forceGetHigherLevelGroupFromUser($circleUniqueId, $viewerId) |
234
|
|
|
); |
235
|
|
|
|
236
|
|
|
return $circle; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* createCircle(); |
242
|
|
|
* |
243
|
|
|
* Create a circle with $userId as its owner. |
244
|
|
|
* Will returns the circle |
245
|
|
|
* |
246
|
|
|
* @param Circle $circle |
247
|
|
|
* @param $userId |
248
|
|
|
* |
249
|
|
|
* @throws CircleAlreadyExistsException |
250
|
|
|
*/ |
251
|
|
|
public function createCircle(Circle &$circle, $userId) { |
252
|
|
|
|
253
|
|
|
if (!$this->isCircleUnique($circle, $userId)) { |
254
|
|
|
throw new CircleAlreadyExistsException( |
255
|
|
|
$this->l10n->t('A circle with that name exists') |
256
|
|
|
); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
$circle->generateUniqueId(); |
260
|
|
|
$qb = $this->getCirclesInsertSql(); |
261
|
|
|
$qb->setValue('unique_id', $qb->createNamedParameter($circle->getUniqueId(true))) |
262
|
|
|
->setValue('name', $qb->createNamedParameter($circle->getName())) |
263
|
|
|
->setValue('description', $qb->createNamedParameter($circle->getDescription())) |
264
|
|
|
->setValue('contact_addressbook', $qb->createNamedParameter($circle->getContactAddressBook())) |
265
|
|
|
->setValue('contact_groupname', $qb->createNamedParameter($circle->getContactGroupName())) |
266
|
|
|
->setValue('settings', $qb->createNamedParameter($circle->getSettings(true))) |
267
|
|
|
->setValue('type', $qb->createNamedParameter($circle->getType())); |
268
|
|
|
$qb->execute(); |
269
|
|
|
|
270
|
|
|
$owner = new Member($userId, Member::TYPE_USER); |
271
|
|
|
$owner->setCircleId($circle->getUniqueId()) |
272
|
|
|
->setLevel(Member::LEVEL_OWNER) |
273
|
|
|
->setStatus(Member::STATUS_MEMBER); |
274
|
|
|
$circle->setOwner($owner) |
275
|
|
|
->setViewer($owner); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* remove a circle |
281
|
|
|
* |
282
|
|
|
* @param string $circleUniqueId |
283
|
|
|
*/ |
284
|
|
|
public function destroyCircle($circleUniqueId) { |
285
|
|
|
$qb = $this->getCirclesDeleteSql($circleUniqueId); |
286
|
|
|
|
287
|
|
|
$qb->execute(); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* returns if the circle is already in database |
293
|
|
|
* |
294
|
|
|
* @param Circle $circle |
295
|
|
|
* @param string $userId |
296
|
|
|
* |
297
|
|
|
* @return bool |
298
|
|
|
*/ |
299
|
|
|
private function isCircleUnique(Circle $circle, $userId) { |
300
|
|
|
|
301
|
|
|
if ($circle->getType() === Circle::CIRCLES_PERSONAL) { |
302
|
|
|
return $this->isPersonalCircleUnique($circle, $userId); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
$qb = $this->getCirclesSelectSql(); |
306
|
|
|
$this->limitToNonPersonalCircle($qb); |
307
|
|
|
|
308
|
|
|
$cursor = $qb->execute(); |
309
|
|
|
while ($data = $cursor->fetch()) { |
310
|
|
|
if (strtolower($data['name']) === strtolower($circle->getName()) |
311
|
|
|
&& $circle->getUniqueId(true) !== $data['unique_id']) { |
312
|
|
|
return false; |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
$cursor->closeCursor(); |
316
|
|
|
|
317
|
|
|
return true; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* return if the personal circle is unique |
323
|
|
|
* |
324
|
|
|
* @param Circle $circle |
325
|
|
|
* @param string $userId |
326
|
|
|
* |
327
|
|
|
* @return bool |
328
|
|
|
*/ |
329
|
|
|
private function isPersonalCircleUnique(Circle $circle, $userId) { |
330
|
|
|
|
331
|
|
|
$list = $this->getCircles( |
332
|
|
|
$userId, Circle::CIRCLES_PERSONAL, $circle->getName(), |
333
|
|
|
Member::LEVEL_OWNER |
334
|
|
|
); |
335
|
|
|
|
336
|
|
|
foreach ($list as $test) { |
337
|
|
|
if (strtolower($test->getName()) === strtolower($circle->getName()) |
338
|
|
|
&& $circle->getUniqueId(true) !== $test->getUniqueId(true)) { |
339
|
|
|
return false; |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
return true; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* @param Circle $circle |
349
|
|
|
* @param string $userId |
350
|
|
|
* |
351
|
|
|
* @throws CircleAlreadyExistsException |
352
|
|
|
*/ |
353
|
|
|
public function updateCircle(Circle $circle, $userId) { |
354
|
|
|
|
355
|
|
|
if (!$this->isCircleUnique($circle, $userId)) { |
356
|
|
|
throw new CircleAlreadyExistsException( |
357
|
|
|
$this->l10n->t('A circle with that name exists') |
358
|
|
|
); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
$qb = $this->getCirclesUpdateSql($circle->getUniqueId(true)); |
362
|
|
|
$qb->set('name', $qb->createNamedParameter($circle->getName())) |
363
|
|
|
->set('description', $qb->createNamedParameter($circle->getDescription())) |
364
|
|
|
->set('settings', $qb->createNamedParameter($circle->getSettings(true))) |
365
|
|
|
->set('group_id', $qb->createNamedParameter($circle->getGroupId())); |
366
|
|
|
|
367
|
|
|
$qb->execute(); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* @param string $uniqueId |
373
|
|
|
* |
374
|
|
|
* @return Circle |
375
|
|
|
* @throws CircleDoesNotExistException |
376
|
|
|
*/ |
377
|
|
View Code Duplication |
public function getCircleFromUniqueId($uniqueId) { |
|
|
|
|
378
|
|
|
$qb = $this->getCirclesSelectSql(); |
379
|
|
|
$this->limitToUniqueId($qb, (string)$uniqueId); |
380
|
|
|
|
381
|
|
|
$cursor = $qb->execute(); |
382
|
|
|
$data = $cursor->fetch(); |
383
|
|
|
$cursor->closeCursor(); |
384
|
|
|
|
385
|
|
|
if ($data === false) { |
386
|
|
|
throw new CircleDoesNotExistException($this->l10n->t('Circle not found')); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
$entry = $this->parseCirclesSelectSql($data); |
390
|
|
|
|
391
|
|
|
return $entry; |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* @param int $addressBookId |
397
|
|
|
* |
398
|
|
|
* @return array |
399
|
|
|
*/ |
400
|
|
View Code Duplication |
public function getFromBook(int $addressBookId) { |
|
|
|
|
401
|
|
|
$qb = $this->getCirclesSelectSql(); |
402
|
|
|
$this->limitToAddressBookId($qb, $addressBookId); |
403
|
|
|
|
404
|
|
|
$circles = []; |
405
|
|
|
$cursor = $qb->execute(); |
406
|
|
|
while ($data = $cursor->fetch()) { |
407
|
|
|
$circles[] = $this->parseCirclesSelectSql($data); |
408
|
|
|
} |
409
|
|
|
$cursor->closeCursor(); |
410
|
|
|
|
411
|
|
|
return $circles; |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* @param int $addressBookId |
417
|
|
|
* |
418
|
|
|
* @return Circle[] |
419
|
|
|
*/ |
420
|
|
View Code Duplication |
public function getFromContactBook(int $addressBookId): array { |
|
|
|
|
421
|
|
|
$qb = $this->getCirclesSelectSql(); |
422
|
|
|
$this->limitToAddressBookId($qb, $addressBookId); |
423
|
|
|
|
424
|
|
|
$circles = []; |
425
|
|
|
$cursor = $qb->execute(); |
426
|
|
|
while ($data = $cursor->fetch()) { |
427
|
|
|
$circles[] = $this->parseCirclesSelectSql($data); |
428
|
|
|
} |
429
|
|
|
$cursor->closeCursor(); |
430
|
|
|
|
431
|
|
|
return $circles; |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* @param int $addressBookId |
437
|
|
|
* @param string $group |
438
|
|
|
* |
439
|
|
|
* @return Circle |
440
|
|
|
* @throws CircleDoesNotExistException |
441
|
|
|
*/ |
442
|
|
|
public function getFromContactGroup(int $addressBookId, string $group): Circle { |
443
|
|
|
$qb = $this->getCirclesSelectSql(); |
444
|
|
|
$this->limitToAddressBookId($qb, $addressBookId); |
445
|
|
|
$this->limitToContactGroup($qb, $group); |
446
|
|
|
|
447
|
|
|
$cursor = $qb->execute(); |
448
|
|
|
$data = $cursor->fetch(); |
449
|
|
|
$cursor->closeCursor(); |
450
|
|
|
|
451
|
|
|
if ($data === false) { |
452
|
|
|
throw new CircleDoesNotExistException($this->l10n->t('Circle not found')); |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
return $this->parseCirclesSelectSql($data); |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
} |
459
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.