Completed
Push — master ( 3aeedd...be0cda )
by Maxence
03:35 queued 11s
created

CircleRequest::deleteFederatedUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 Circle $circle
127
	 */
128
	public function updateConfig(Circle $circle) {
129
		$qb = $this->getCircleUpdateSql();
130
		$qb->set('config', $qb->createNamedParameter($circle->getConfig()));
131
132
		$qb->limitToUniqueId($circle->getSingleId());
133
134
		$qb->execute();
135
	}
136
137
138
	/**
139
	 * @param Circle|null $circleFilter
140
	 * @param Member|null $memberFilter
141
	 * @param IFederatedUser|null $initiator
142
	 * @param RemoteInstance|null $remoteInstance
143
	 * @param SimpleDataStore $params
144
	 *
145
	 * @return Circle[]
146
	 * @throws RequestBuilderException
147
	 */
148
	public function getCircles(
149
		?Circle $circleFilter,
150
		?Member $memberFilter,
151
		?IFederatedUser $initiator,
152
		?RemoteInstance $remoteInstance,
153
		SimpleDataStore $params
154
	): array {
155
		$qb = $this->getCircleSelectSql();
156
		$qb->leftJoinOwner(CoreQueryBuilder::CIRCLE);
157
		$qb->setOptions(
158
			[CoreQueryBuilder::CIRCLE],
159
			[
160
				'getData'      => true,
161
				'mustBeMember' => $params->gBool('mustBeMember')
162
			]
163
		);
164
165
		if (!$params->gBool('includeSystemCircles')) {
166
			$qb->filterCircles(
167
				CoreQueryBuilder::CIRCLE,
168
				Circle::CFG_SINGLE | Circle::CFG_HIDDEN | Circle::CFG_BACKEND
169
			);
170
		}
171
		if (!is_null($initiator)) {
172
			$qb->limitToInitiator(CoreQueryBuilder::CIRCLE, $initiator);
173
		}
174
		if (!is_null($memberFilter)) {
175
			$qb->limitToDirectMembership(CoreQueryBuilder::CIRCLE, $memberFilter);
176
		}
177
		if (!is_null($circleFilter)) {
178
			$qb->filterCircle($circleFilter);
179
		}
180
		if (!is_null($remoteInstance)) {
181
			$qb->limitToRemoteInstance(CoreQueryBuilder::CIRCLE, $remoteInstance, false);
182
		}
183
184
		$qb->chunk($params->gInt('offset'), $params->gInt('limit'));
185
186
		return $this->getItemsFromRequest($qb);
187
	}
188
189
190
	/**
191
	 * @param array $circleIds
192
	 *
193
	 * @return array
194
	 * @throws RequestBuilderException
195
	 */
196
	public function getCirclesByIds(array $circleIds): array {
197
		$qb = $this->getCircleSelectSql();
198
		$qb->setOptions([CoreQueryBuilder::CIRCLE], ['getData' => true, 'canBeVisitor' => true]);
199
200
		$qb->limitInArray('unique_id', $circleIds);
201
//		$qb->filterCircles(CoreQueryBuilder::CIRCLE, $filter);
202
		$qb->leftJoinOwner(CoreQueryBuilder::CIRCLE);
203
204
		return $this->getItemsFromRequest($qb);
205
	}
206
207
	/**
208
	 * @param string $id
209
	 * @param IFederatedUser|null $initiator
210
	 * @param RemoteInstance|null $remoteInstance
211
	 * @param int $filter
212
	 *
213
	 * @return Circle
214
	 * @throws CircleNotFoundException
215
	 * @throws RequestBuilderException
216
	 */
217
	public function getCircle(
218
		string $id,
219
		?IFederatedUser $initiator = null,
220
		?RemoteInstance $remoteInstance = null,
221
		int $filter = Circle::CFG_BACKEND | Circle::CFG_SINGLE | Circle::CFG_HIDDEN
222
	): Circle {
223
		$qb = $this->getCircleSelectSql();
224
		$qb->setOptions([CoreQueryBuilder::CIRCLE], ['getData' => true, 'canBeVisitor' => true]);
225
226
		$qb->limitToUniqueId($id);
227
		$qb->filterCircles(CoreQueryBuilder::CIRCLE, $filter);
228
		$qb->leftJoinOwner(CoreQueryBuilder::CIRCLE);
229
//		$qb->setOptions(
230
//			[CoreRequestBuilder::CIRCLE, CoreRequestBuilder::INITIATOR], [
231
//																		   'mustBeMember' => false,
232
//																		   'canBeVisitor' => true
233
//																	   ]
234
//		);
235
236
		if (!is_null($initiator)) {
237
			$qb->limitToInitiator(CoreQueryBuilder::CIRCLE, $initiator);
238
		}
239
		if (!is_null($remoteInstance)) {
240
			$qb->limitToRemoteInstance(CoreQueryBuilder::CIRCLE, $remoteInstance, false);
241
		}
242
243
		return $this->getItemFromRequest($qb);
244
	}
245
246
247
	/**
248
	 * @param string $singleId
249
	 *
250
	 * @return FederatedUser
251
	 * @throws CircleNotFoundException
252
	 * @throws OwnerNotFoundException
253
	 * @throws RequestBuilderException
254
	 */
255
	public function getFederatedUserBySingleId(string $singleId): FederatedUser {
256
		$qb = $this->getCircleSelectSql();
257
		$qb->limitToUniqueId($singleId);
258
		$qb->leftJoinOwner(CoreQueryBuilder::CIRCLE);
259
260
		$circle = $this->getItemFromRequest($qb);
261
262
		$federatedUser = new FederatedUser();
263
		$federatedUser->importFromCircle($circle);
264
265
		return $federatedUser;
266
	}
267
268
269
	/**
270
	 * method that return the single-user Circle based on a FederatedUser.
271
	 *
272
	 * @param IFederatedUser $initiator
273
	 *
274
	 * @return Circle
275
	 * @throws SingleCircleNotFoundException
276
	 * @throws RequestBuilderException
277
	 */
278
	public function getSingleCircle(IFederatedUser $initiator): Circle {
279
		$qb = $this->getCircleSelectSql(CoreQueryBuilder::SINGLE);
280
281
		if ($initiator instanceof FederatedUser) {
282
			$member = new Member();
283
			$member->importFromIFederatedUser($initiator);
284
			$member->setLevel(Member::LEVEL_OWNER);
285
		} else {
286
			$member = clone $initiator;
287
		}
288
289
		$qb->limitToDirectMembership(CoreQueryBuilder::SINGLE, $member);
290
		$qb->limitToConfigFlag(Circle::CFG_SINGLE);
291
292
		try {
293
			return $this->getItemFromRequest($qb);
294
		} catch (CircleNotFoundException $e) {
295
			throw new SingleCircleNotFoundException();
296
		}
297
	}
298
299
300
	/**
301
	 * @param Circle $circle
302
	 * @param IFederatedUser|null $initiator
303
	 *
304
	 * @return Circle
305
	 * @throws CircleNotFoundException
306
	 * @throws RequestBuilderException
307
	 */
308
	public function searchCircle(Circle $circle, ?IFederatedUser $initiator = null): Circle {
309
		$qb = $this->getCircleSelectSql();
310
		$qb->leftJoinOwner(CoreQueryBuilder::CIRCLE);
311
312
		if ($circle->getName() !== '') {
313
			$qb->limitToName($circle->getName());
314
		}
315
		if ($circle->getDisplayName() !== '') {
316
			$qb->limitToDisplayName($circle->getDisplayName());
317
		}
318
		if ($circle->getSanitizedName() !== '') {
319
			$qb->limitToSanitizedName($circle->getSanitizedName());
320
		}
321
		if ($circle->getConfig() > 0) {
322
			$qb->limitToConfig($circle->getConfig());
323
		}
324
		if ($circle->getSource() > 0) {
325
			$qb->limitToSource($circle->getSource());
326
		}
327
328
		if ($circle->hasOwner()) {
329
			$aliasOwner = $qb->generateAlias(CoreQueryBuilder::CIRCLE, CoreQueryBuilder::OWNER);
330
			$qb->filterDirectMembership($aliasOwner, $circle->getOwner());
331
		}
332
333
		if (!is_null($initiator)) {
334
			$qb->setOptions([CoreQueryBuilder::CIRCLE], ['getData' => true]);
335
			$qb->limitToInitiator(CoreQueryBuilder::CIRCLE, $initiator);
336
		}
337
338
		return $this->getItemFromRequest($qb);
339
	}
340
341
342
	/**
343
	 * @return Circle[]
344
	 * @throws RequestBuilderException
345
	 */
346
	public function getFederated(): array {
347
		$qb = $this->getCircleSelectSql();
348
		$qb->limitToConfigFlag(Circle::CFG_FEDERATED, CoreQueryBuilder::CIRCLE);
349
350
		$qb->leftJoinOwner(CoreQueryBuilder::CIRCLE);
351
352
		return $this->getItemsFromRequest($qb);
353
	}
354
355
356
	/**
357
	 * @param Circle $circle
358
	 */
359
	public function delete(Circle $circle): void {
360
		$qb = $this->getCircleDeleteSql();
361
		$qb->limitToUniqueId($circle->getSingleId());
362
363
		$qb->execute();
364
	}
365
366
367
	/**
368
	 * @param IFederatedUser $federatedUser
369
	 */
370
	public function deleteFederatedUser(IFederatedUser $federatedUser): void {
371
		$qb = $this->getCircleDeleteSql();
372
		$qb->limitToUniqueId($federatedUser->getSingleId());
373
374
		$qb->execute();
375
	}
376
377
}
378
379