1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Circles - Bring cloud-users closer together. |
8
|
|
|
* |
9
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
10
|
|
|
* later. See the COPYING file. |
11
|
|
|
* |
12
|
|
|
* @author Maxence Lange <[email protected]> |
13
|
|
|
* @copyright 2021 |
14
|
|
|
* @license GNU AGPL version 3 or any later version |
15
|
|
|
* |
16
|
|
|
* This program is free software: you can redistribute it and/or modify |
17
|
|
|
* it under the terms of the GNU Affero General Public License as |
18
|
|
|
* published by the Free Software Foundation, either version 3 of the |
19
|
|
|
* License, or (at your option) any later version. |
20
|
|
|
* |
21
|
|
|
* This program is distributed in the hope that it will be useful, |
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24
|
|
|
* GNU Affero General Public License for more details. |
25
|
|
|
* |
26
|
|
|
* You should have received a copy of the GNU Affero General Public License |
27
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
28
|
|
|
* |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
namespace OCA\Circles\Db; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
use daita\MySmallPhpTools\Model\SimpleDataStore; |
36
|
|
|
use OCA\Circles\Exceptions\CircleNotFoundException; |
37
|
|
|
use OCA\Circles\Exceptions\InvalidIdException; |
38
|
|
|
use OCA\Circles\Exceptions\OwnerNotFoundException; |
39
|
|
|
use OCA\Circles\Exceptions\RequestBuilderException; |
40
|
|
|
use OCA\Circles\Exceptions\SingleCircleNotFoundException; |
41
|
|
|
use OCA\Circles\IFederatedUser; |
42
|
|
|
use OCA\Circles\Model\Circle; |
43
|
|
|
use OCA\Circles\Model\Federated\RemoteInstance; |
44
|
|
|
use OCA\Circles\Model\FederatedUser; |
45
|
|
|
use OCA\Circles\Model\Member; |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Class CircleRequest |
50
|
|
|
* |
51
|
|
|
* @package OCA\Circles\Db |
52
|
|
|
*/ |
53
|
|
|
class CircleRequest extends CircleRequestBuilder { |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param Circle $circle |
58
|
|
|
* |
59
|
|
|
* @throws InvalidIdException |
60
|
|
|
*/ |
61
|
|
|
public function save(Circle $circle): void { |
62
|
|
|
$this->confirmValidId($circle->getSingleId()); |
63
|
|
|
|
64
|
|
|
$qb = $this->getCircleInsertSql(); |
65
|
|
|
$qb->setValue('unique_id', $qb->createNamedParameter($circle->getSingleId())) |
66
|
|
|
->setValue('name', $qb->createNamedParameter($circle->getName())) |
67
|
|
|
->setValue('source', $qb->createNamedParameter($circle->getSource())) |
68
|
|
|
->setValue('display_name', $qb->createNamedParameter($circle->getDisplayName())) |
69
|
|
|
->setValue('sanitized_name', $qb->createNamedParameter($circle->getSanitizedName())) |
70
|
|
|
->setValue('description', $qb->createNamedParameter($circle->getDescription())) |
71
|
|
|
->setValue('contact_addressbook', $qb->createNamedParameter($circle->getContactAddressBook())) |
72
|
|
|
->setValue('contact_groupname', $qb->createNamedParameter($circle->getContactGroupName())) |
73
|
|
|
->setValue('settings', $qb->createNamedParameter(json_encode($circle->getSettings()))) |
74
|
|
|
->setValue('config', $qb->createNamedParameter($circle->getConfig())); |
75
|
|
|
|
76
|
|
|
$qb->execute(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param Circle $circle |
82
|
|
|
*/ |
83
|
|
|
public function edit(Circle $circle): void { |
84
|
|
|
$qb = $this->getCircleUpdateSql(); |
85
|
|
|
$qb->set('display_name', $qb->createNamedParameter($circle->getDisplayName())) |
86
|
|
|
->set('description', $qb->createNamedParameter($circle->getDescription())); |
87
|
|
|
|
88
|
|
|
$qb->limitToUniqueId($circle->getSingleId()); |
89
|
|
|
|
90
|
|
|
$qb->execute(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param Circle $circle |
95
|
|
|
*/ |
96
|
|
|
public function update(Circle $circle) { |
97
|
|
|
$qb = $this->getCircleUpdateSql(); |
98
|
|
|
$qb->set('name', $qb->createNamedParameter($circle->getName())) |
99
|
|
|
->set('display_name', $qb->createNamedParameter($circle->getDisplayName())) |
100
|
|
|
->set('description', $qb->createNamedParameter($circle->getDescription())) |
101
|
|
|
->set('settings', $qb->createNamedParameter(json_encode($circle->getSettings()))) |
102
|
|
|
->set('config', $qb->createNamedParameter($circle->getConfig())); |
103
|
|
|
|
104
|
|
|
$qb->limitToUniqueId($circle->getSingleId()); |
105
|
|
|
|
106
|
|
|
$qb->execute(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param Circle $circle |
112
|
|
|
* |
113
|
|
|
* @throws InvalidIdException |
114
|
|
|
*/ |
115
|
|
|
public function insertOrUpdate(Circle $circle): void { |
116
|
|
|
try { |
117
|
|
|
$this->getCircle($circle->getSingleId()); |
118
|
|
|
$this->update($circle); |
119
|
|
|
} catch (CircleNotFoundException $e) { |
120
|
|
|
$this->save($circle); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $singleId |
127
|
|
|
* @param string $displayName |
128
|
|
|
*/ |
129
|
|
|
public function updateDisplayName(string $singleId, string $displayName): void { |
130
|
|
|
$qb = $this->getCircleUpdateSql(); |
131
|
|
|
$qb->set('display_name', $qb->createNamedParameter($displayName)); |
132
|
|
|
|
133
|
|
|
$qb->limitToUniqueId($singleId); |
134
|
|
|
|
135
|
|
|
$qb->execute(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param Circle $circle |
141
|
|
|
*/ |
142
|
|
|
public function updateConfig(Circle $circle) { |
143
|
|
|
$qb = $this->getCircleUpdateSql(); |
144
|
|
|
$qb->set('config', $qb->createNamedParameter($circle->getConfig())); |
145
|
|
|
|
146
|
|
|
$qb->limitToUniqueId($circle->getSingleId()); |
147
|
|
|
|
148
|
|
|
$qb->execute(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param Circle|null $circleFilter |
154
|
|
|
* @param Member|null $memberFilter |
155
|
|
|
* @param IFederatedUser|null $initiator |
156
|
|
|
* @param RemoteInstance|null $remoteInstance |
157
|
|
|
* @param SimpleDataStore $params |
158
|
|
|
* |
159
|
|
|
* @return Circle[] |
160
|
|
|
* @throws RequestBuilderException |
161
|
|
|
*/ |
162
|
|
|
public function getCircles( |
163
|
|
|
?Circle $circleFilter, |
164
|
|
|
?Member $memberFilter, |
165
|
|
|
?IFederatedUser $initiator, |
166
|
|
|
?RemoteInstance $remoteInstance, |
167
|
|
|
SimpleDataStore $params |
168
|
|
|
): array { |
169
|
|
|
$qb = $this->getCircleSelectSql(); |
170
|
|
|
$qb->leftJoinOwner(CoreQueryBuilder::CIRCLE); |
171
|
|
|
$qb->setOptions( |
172
|
|
|
[CoreQueryBuilder::CIRCLE], |
173
|
|
|
[ |
174
|
|
|
'getData' => true, |
175
|
|
|
'mustBeMember' => $params->gBool('mustBeMember') |
176
|
|
|
] |
177
|
|
|
); |
178
|
|
|
|
179
|
|
|
if (!$params->gBool('includeSystemCircles')) { |
180
|
|
|
$qb->filterCircles( |
181
|
|
|
CoreQueryBuilder::CIRCLE, |
182
|
|
|
Circle::CFG_SINGLE | Circle::CFG_HIDDEN | Circle::CFG_BACKEND |
183
|
|
|
); |
184
|
|
|
} |
185
|
|
|
if (!is_null($initiator)) { |
186
|
|
|
$qb->limitToInitiator(CoreQueryBuilder::CIRCLE, $initiator); |
187
|
|
|
} |
188
|
|
|
if (!is_null($memberFilter)) { |
189
|
|
|
$qb->limitToDirectMembership(CoreQueryBuilder::CIRCLE, $memberFilter); |
190
|
|
|
} |
191
|
|
|
if (!is_null($circleFilter)) { |
192
|
|
|
$qb->filterCircle($circleFilter); |
193
|
|
|
} |
194
|
|
|
if (!is_null($remoteInstance)) { |
195
|
|
|
$qb->limitToRemoteInstance(CoreQueryBuilder::CIRCLE, $remoteInstance, false); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$qb->chunk($params->gInt('offset'), $params->gInt('limit')); |
199
|
|
|
|
200
|
|
|
return $this->getItemsFromRequest($qb); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param array $circleIds |
206
|
|
|
* |
207
|
|
|
* @return array |
208
|
|
|
* @throws RequestBuilderException |
209
|
|
|
*/ |
210
|
|
|
public function getCirclesByIds(array $circleIds): array { |
211
|
|
|
$qb = $this->getCircleSelectSql(); |
212
|
|
|
$qb->setOptions([CoreQueryBuilder::CIRCLE], ['getData' => true, 'canBeVisitor' => true]); |
213
|
|
|
|
214
|
|
|
$qb->limitInArray('unique_id', $circleIds); |
215
|
|
|
// $qb->filterCircles(CoreQueryBuilder::CIRCLE, $filter); |
216
|
|
|
$qb->leftJoinOwner(CoreQueryBuilder::CIRCLE); |
217
|
|
|
|
218
|
|
|
return $this->getItemsFromRequest($qb); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param string $id |
223
|
|
|
* @param IFederatedUser|null $initiator |
224
|
|
|
* @param RemoteInstance|null $remoteInstance |
225
|
|
|
* @param int $filter |
226
|
|
|
* |
227
|
|
|
* @return Circle |
228
|
|
|
* @throws CircleNotFoundException |
229
|
|
|
* @throws RequestBuilderException |
230
|
|
|
*/ |
231
|
|
|
public function getCircle( |
232
|
|
|
string $id, |
233
|
|
|
?IFederatedUser $initiator = null, |
234
|
|
|
?RemoteInstance $remoteInstance = null, |
235
|
|
|
int $filter = Circle::CFG_BACKEND | Circle::CFG_SINGLE | Circle::CFG_HIDDEN |
236
|
|
|
): Circle { |
237
|
|
|
$qb = $this->getCircleSelectSql(); |
238
|
|
|
$qb->setOptions([CoreQueryBuilder::CIRCLE], ['getData' => true, 'canBeVisitor' => true]); |
239
|
|
|
|
240
|
|
|
$qb->limitToUniqueId($id); |
241
|
|
|
$qb->filterCircles(CoreQueryBuilder::CIRCLE, $filter); |
242
|
|
|
$qb->leftJoinOwner(CoreQueryBuilder::CIRCLE); |
243
|
|
|
// $qb->setOptions( |
244
|
|
|
// [CoreRequestBuilder::CIRCLE, CoreRequestBuilder::INITIATOR], [ |
245
|
|
|
// 'mustBeMember' => false, |
246
|
|
|
// 'canBeVisitor' => true |
247
|
|
|
// ] |
248
|
|
|
// ); |
249
|
|
|
|
250
|
|
|
if (!is_null($initiator)) { |
251
|
|
|
$qb->limitToInitiator(CoreQueryBuilder::CIRCLE, $initiator); |
252
|
|
|
} |
253
|
|
|
if (!is_null($remoteInstance)) { |
254
|
|
|
$qb->limitToRemoteInstance(CoreQueryBuilder::CIRCLE, $remoteInstance, false); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return $this->getItemFromRequest($qb); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @param string $singleId |
263
|
|
|
* |
264
|
|
|
* @return FederatedUser |
265
|
|
|
* @throws CircleNotFoundException |
266
|
|
|
* @throws OwnerNotFoundException |
267
|
|
|
* @throws RequestBuilderException |
268
|
|
|
*/ |
269
|
|
|
public function getFederatedUserBySingleId(string $singleId): FederatedUser { |
270
|
|
|
$qb = $this->getCircleSelectSql(); |
271
|
|
|
$qb->limitToUniqueId($singleId); |
272
|
|
|
$qb->leftJoinOwner(CoreQueryBuilder::CIRCLE); |
273
|
|
|
|
274
|
|
|
$circle = $this->getItemFromRequest($qb); |
275
|
|
|
|
276
|
|
|
$federatedUser = new FederatedUser(); |
277
|
|
|
$federatedUser->importFromCircle($circle); |
278
|
|
|
|
279
|
|
|
return $federatedUser; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* method that return the single-user Circle based on a FederatedUser. |
285
|
|
|
* |
286
|
|
|
* @param IFederatedUser $initiator |
287
|
|
|
* |
288
|
|
|
* @return Circle |
289
|
|
|
* @throws SingleCircleNotFoundException |
290
|
|
|
* @throws RequestBuilderException |
291
|
|
|
*/ |
292
|
|
|
public function getSingleCircle(IFederatedUser $initiator): Circle { |
293
|
|
|
$qb = $this->getCircleSelectSql(CoreQueryBuilder::SINGLE); |
294
|
|
|
|
295
|
|
|
if ($initiator instanceof FederatedUser) { |
296
|
|
|
$member = new Member(); |
297
|
|
|
$member->importFromIFederatedUser($initiator); |
298
|
|
|
$member->setLevel(Member::LEVEL_OWNER); |
299
|
|
|
} else { |
300
|
|
|
$member = clone $initiator; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
$qb->limitToDirectMembership(CoreQueryBuilder::SINGLE, $member); |
304
|
|
|
$qb->limitToConfigFlag(Circle::CFG_SINGLE); |
305
|
|
|
|
306
|
|
|
try { |
307
|
|
|
return $this->getItemFromRequest($qb); |
308
|
|
|
} catch (CircleNotFoundException $e) { |
309
|
|
|
throw new SingleCircleNotFoundException(); |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @param Circle $circle |
316
|
|
|
* @param IFederatedUser|null $initiator |
317
|
|
|
* |
318
|
|
|
* @return Circle |
319
|
|
|
* @throws CircleNotFoundException |
320
|
|
|
* @throws RequestBuilderException |
321
|
|
|
*/ |
322
|
|
|
public function searchCircle(Circle $circle, ?IFederatedUser $initiator = null): Circle { |
323
|
|
|
$qb = $this->getCircleSelectSql(); |
324
|
|
|
$qb->leftJoinOwner(CoreQueryBuilder::CIRCLE); |
325
|
|
|
|
326
|
|
|
if ($circle->getName() !== '') { |
327
|
|
|
$qb->limitToName($circle->getName()); |
328
|
|
|
} |
329
|
|
|
if ($circle->getDisplayName() !== '') { |
330
|
|
|
$qb->limitToDisplayName($circle->getDisplayName()); |
331
|
|
|
} |
332
|
|
|
if ($circle->getSanitizedName() !== '') { |
333
|
|
|
$qb->limitToSanitizedName($circle->getSanitizedName()); |
334
|
|
|
} |
335
|
|
|
if ($circle->getConfig() > 0) { |
336
|
|
|
$qb->limitToConfig($circle->getConfig()); |
337
|
|
|
} |
338
|
|
|
if ($circle->getSource() > 0) { |
339
|
|
|
$qb->limitToSource($circle->getSource()); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
if ($circle->hasOwner()) { |
343
|
|
|
$aliasOwner = $qb->generateAlias(CoreQueryBuilder::CIRCLE, CoreQueryBuilder::OWNER); |
344
|
|
|
$qb->filterDirectMembership($aliasOwner, $circle->getOwner()); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
if (!is_null($initiator)) { |
348
|
|
|
$qb->setOptions([CoreQueryBuilder::CIRCLE], ['getData' => true]); |
349
|
|
|
$qb->limitToInitiator(CoreQueryBuilder::CIRCLE, $initiator); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
return $this->getItemFromRequest($qb); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* @return Circle[] |
358
|
|
|
* @throws RequestBuilderException |
359
|
|
|
*/ |
360
|
|
|
public function getFederated(): array { |
361
|
|
|
$qb = $this->getCircleSelectSql(); |
362
|
|
|
$qb->limitToConfigFlag(Circle::CFG_FEDERATED, CoreQueryBuilder::CIRCLE); |
363
|
|
|
|
364
|
|
|
$qb->leftJoinOwner(CoreQueryBuilder::CIRCLE); |
365
|
|
|
|
366
|
|
|
return $this->getItemsFromRequest($qb); |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* @param Circle $circle |
372
|
|
|
*/ |
373
|
|
|
public function delete(Circle $circle): void { |
374
|
|
|
$qb = $this->getCircleDeleteSql(); |
375
|
|
|
$qb->limitToUniqueId($circle->getSingleId()); |
376
|
|
|
|
377
|
|
|
$qb->execute(); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* @param IFederatedUser $federatedUser |
383
|
|
|
*/ |
384
|
|
|
public function deleteFederatedUser(IFederatedUser $federatedUser): void { |
385
|
|
|
$qb = $this->getCircleDeleteSql(); |
386
|
|
|
$qb->limitToUniqueId($federatedUser->getSingleId()); |
387
|
|
|
|
388
|
|
|
$qb->execute(); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
|