Completed
Pull Request — master (#449)
by Maxence
01:59
created

CirclesRequestBuilder::generateLimitPersonal()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
nop 4
1
<?php
2
3
/**
4
 * Circles - Bring cloud-users closer together.
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Maxence Lange <[email protected]>
10
 * @copyright 2017
11
 * @license GNU AGPL version 3 or any later version
12
 *
13
 * This program is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License as
15
 * published by the Free Software Foundation, either version 3 of the
16
 * License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25
 *
26
 */
27
28
namespace OCA\Circles\Db;
29
30
31
use Doctrine\DBAL\Query\QueryBuilder;
32
use OCA\Circles\Exceptions\ConfigNoCircleAvailableException;
33
use OCA\Circles\Model\Circle;
34
use OCA\Circles\Model\Member;
35
use OCA\Circles\Service\ConfigService;
36
use OCA\Circles\Service\MiscService;
37
use OCA\Circles\Service\TimezoneService;
38
use OCP\DB\QueryBuilder\IQueryBuilder;
39
use OCP\IDBConnection;
40
use OCP\IL10N;
41
42
class CirclesRequestBuilder extends CoreRequestBuilder {
43
44
45
	/** @var MembersRequest */
46
	protected $membersRequest;
47
48
	/**
49
	 * CirclesRequestBuilder constructor.
50
	 *
51
	 * {@inheritdoc}
52
	 * @param MembersRequest $membersRequest
53
	 */
54 View Code Duplication
	public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
55
		IL10N $l10n, IDBConnection $connection, MembersRequest $membersRequest,
56
		ConfigService $configService, TimezoneService $timezoneService, MiscService $miscService
57
	) {
58
		parent::__construct($l10n, $connection, $configService, $timezoneService, $miscService);
59
		$this->membersRequest = $membersRequest;
60
	}
61
62
63
	/**
64
	 * Limit the search to a non-personal circle
65
	 *
66
	 * @param IQueryBuilder $qb
67
	 */
68
	protected function limitToNonPersonalCircle(IQueryBuilder &$qb) {
69
		$expr = $qb->expr();
70
71
		$qb->andWhere(
72
			$expr->neq('c.type', $qb->createNamedParameter(Circle::CIRCLES_PERSONAL))
73
		);
74
	}
75
76
77
	/**
78
	 * @param IQueryBuilder $qb
79
	 * @param string $userId
80
	 * @param string $circleUniqueId
81
	 * @param $type
82
	 * @param $name
83
	 * @param bool $forceAll
84
	 *
85
	 * @throws ConfigNoCircleAvailableException
86
	 */
87
	protected function limitRegardingCircleType(
88
		IQueryBuilder &$qb, string $userId, $circleUniqueId, int $type,
89
		string $name, bool $forceAll = false
90
	) {
91
		$orTypes = $this->generateLimit($qb, $circleUniqueId, $userId, $type, $name, $forceAll);
92
		if (sizeof($orTypes) === 0) {
93
			throw new ConfigNoCircleAvailableException(
94
				$this->l10n->t(
95
					'You cannot use the Circles Application until your administrator has allowed at least one type of circles'
96
				)
97
			);
98
		}
99
100
		$orXTypes = $qb->expr()
101
					   ->orX();
102
		foreach ($orTypes as $orType) {
103
			$orXTypes->add($orType);
104
		}
105
106
		$qb->andWhere($orXTypes);
107
	}
108
109
110
	/**
111
	 * @param IQueryBuilder $qb
112
	 * @param string $circleUniqueId
113
	 * @param $userId
114
	 * @param $type
115
	 * @param $name
116
	 * @param bool $forceAll
117
	 *
118
	 * @return array
119
	 */
120
	private function generateLimit(
121
		IQueryBuilder &$qb, $circleUniqueId, $userId, $type, $name, $forceAll = false
122
	) {
123
		$orTypes = [];
124
		array_push($orTypes, $this->generateLimitPersonal($qb, $userId, $type, $forceAll));
125
		array_push($orTypes, $this->generateLimitSecret($qb, $circleUniqueId, $type, $name));
126
		array_push($orTypes, $this->generateLimitClosed($qb, $type));
127
		array_push($orTypes, $this->generateLimitPublic($qb, $type));
128
129
		return array_filter($orTypes);
130
	}
131
132
133
	/**
134
	 * @param IQueryBuilder $qb
135
	 * @param int|string $userId
136
	 * @param int $type
137
	 * @param bool $forceAll
138
	 *
139
	 * @return \OCP\DB\QueryBuilder\ICompositeExpression
140
	 */
141
	private function generateLimitPersonal(IQueryBuilder $qb, $userId, $type, $forceAll = false) {
142
		if (!(Circle::CIRCLES_PERSONAL & (int)$type)) {
143
			return null;
144
		}
145
		$expr = $qb->expr();
146
147
		$andX = $expr->andX();
148
		$andX->add($expr->eq('c.type', $qb->createNamedParameter(Circle::CIRCLES_PERSONAL)));
149
		$andX->add($expr->eq('o.instance', $qb->createNamedParameter('')));
150
151
		if (!$forceAll) {
152
			$andX->add($expr->eq('o.user_id', $qb->createNamedParameter((string)$userId)));
153
		}
154
155
		return $andX;
156
	}
157
158
159
	/**
160
	 * @param IQueryBuilder $qb
161
	 * @param string $circleUniqueId
162
	 * @param int $type
163
	 * @param string $name
164
	 *
165
	 * @return string
166
	 */
167
	private function generateLimitSecret(IQueryBuilder $qb, $circleUniqueId, $type, $name) {
168
		if (!(Circle::CIRCLES_SECRET & (int)$type)) {
169
			return null;
170
		}
171
		$expr = $qb->expr();
172
173
		$orX = $expr->orX($expr->gte('u.level', $qb->createNamedParameter(Member::LEVEL_MEMBER)));
174
		$orX->add($expr->eq('c.name', $qb->createNamedParameter($name)))
175
			->add($expr->eq('c.unique_id', $qb->createNamedParameter($circleUniqueId)));
176
177
		if ($this->leftJoinedNCGroupAndUser) {
178
			$orX->add($expr->gte('g.level', $qb->createNamedParameter(Member::LEVEL_MEMBER)));
179
		}
180
181
		/** @noinspection PhpMethodParametersCountMismatchInspection */
182
		$sqb = $expr->andX(
183
			$expr->eq('c.type', $qb->createNamedParameter(Circle::CIRCLES_SECRET)),
184
			$expr->orX($orX)
185
		);
186
187
		return $sqb;
188
	}
189
190
191
	/**
192
	 * @param IQueryBuilder $qb
193
	 * @param int $type
194
	 *
195
	 * @return string
196
	 */
197 View Code Duplication
	private function generateLimitClosed(IQueryBuilder $qb, $type) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
198
		if (!(Circle::CIRCLES_CLOSED & (int)$type)) {
199
			return null;
200
		}
201
202
		return $qb->expr()
203
				  ->eq(
204
					  'c.type',
205
					  $qb->createNamedParameter(Circle::CIRCLES_CLOSED)
206
				  );
207
	}
208
209
210
	/**
211
	 * @param IQueryBuilder $qb
212
	 * @param int $type
213
	 *
214
	 * @return string
215
	 */
216 View Code Duplication
	private function generateLimitPublic(IQueryBuilder $qb, $type) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
217
		if (!(Circle::CIRCLES_PUBLIC & (int)$type)) {
218
			return null;
219
		}
220
221
		return $qb->expr()
222
				  ->eq(
223
					  'c.type',
224
					  $qb->createNamedParameter(Circle::CIRCLES_PUBLIC)
225
				  );
226
	}
227
228
229
	/**
230
	 * add a request to the members list, using the current user ID.
231
	 * will returns level and stuff.
232
	 *
233
	 * @param IQueryBuilder $qb
234
	 * @param string $userId
235
	 * @param int $type
236
	 * @param string $instanceId
237
	 */
238
	public function leftJoinUserIdAsViewer(IQueryBuilder &$qb, string $userId, int $type, string $instanceId
239
	) {
240
241
		if ($qb->getType() !== QueryBuilder::SELECT) {
242
			return;
243
		}
244
245
		$expr = $qb->expr();
246
		$pf = '' . $this->default_select_alias . '.';
247
248
		/** @noinspection PhpMethodParametersCountMismatchInspection */
249
		$qb->selectAlias('u.user_id', 'viewer_userid')
250
		   ->selectAlias('u.user_type', 'viewer_type')
251
		   ->selectAlias('u.instance', 'viewer_instance')
252
		   ->selectAlias('u.status', 'viewer_status')
253
		   ->selectAlias('u.member_id', 'viewer_member_id')
254
		   ->selectAlias('u.level', 'viewer_level')
255
		   ->leftJoin(
256
			   $this->default_select_alias, CoreRequestBuilder::TABLE_MEMBERS, 'u',
257
			   $expr->andX(
258
				   $expr->eq('u.circle_id', $pf . 'unique_id'),
259
				   $expr->eq('u.user_id', $qb->createNamedParameter($userId)),
260
				   $expr->eq('u.instance', $qb->createNamedParameter($instanceId)),
261
				   $expr->eq('u.user_type', $qb->createNamedParameter($type))
262
			   )
263
		   );
264
	}
265
266
267
	/**
268
	 * Left Join members table to get the owner of the circle.
269
	 *
270
	 * @param IQueryBuilder $qb
271
	 * @param string $ownerId
272
	 */
273
	public function leftJoinOwner(IQueryBuilder &$qb, string $ownerId = '') {
274
275
		if ($qb->getType() !== QueryBuilder::SELECT) {
276
			return;
277
		}
278
279
		$expr = $qb->expr();
280
		$pf = $this->default_select_alias . '.';
281
282
		/** @noinspection PhpMethodParametersCountMismatchInspection */
283
		$qb->selectAlias('o.user_id', 'owner_userid')
284
		   ->selectAlias('o.instance', 'owner_instance')
285
		   ->selectAlias('o.status', 'owner_status')
286
		   ->selectAlias('o.level', 'owner_level')
287
		   ->leftJoin(
288
			   $this->default_select_alias, CoreRequestBuilder::TABLE_MEMBERS, 'o',
289
			   $expr->andX(
290
				   $expr->eq('o.circle_id', $pf . 'unique_id'),
291
				   $expr->eq('o.level', $qb->createNamedParameter(Member::LEVEL_OWNER)),
292
				   $expr->eq('o.user_type', $qb->createNamedParameter(Member::TYPE_USER))
293
			   )
294
		   );
295
296
		if ($ownerId !== '') {
297
			$qb->andWhere($expr->eq('o.user_id', $qb->createNamedParameter($ownerId)));
298
		}
299
	}
300
301
302
	/**
303
	 * Base of the Sql Insert request for Shares
304
	 *
305
	 *
306
	 * @return IQueryBuilder
307
	 */
308 View Code Duplication
	protected function getCirclesInsertSql() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
309
		$qb = $this->dbConnection->getQueryBuilder();
310
		$qb->insert(self::TABLE_CIRCLES)
311
		   ->setValue('creation', $qb->createNamedParameter($this->timezoneService->getUTCDate()));
312
313
		return $qb;
314
	}
315
316
317
	/**
318
	 * Base of the Sql Update request for Shares
319
	 *
320
	 * @param int $uniqueId
321
	 *
322
	 * @return IQueryBuilder
323
	 */
324 View Code Duplication
	protected function getCirclesUpdateSql($uniqueId) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
325
		$qb = $this->dbConnection->getQueryBuilder();
326
		$qb->update(self::TABLE_CIRCLES)
327
		   ->where(
328
			   $qb->expr()
329
				  ->eq('unique_id', $qb->createNamedParameter($uniqueId))
330
		   );
331
332
		return $qb;
333
	}
334
335
336
	/**
337
	 * Base of the Sql Delete request
338
	 *
339
	 * @param string $circleUniqueId
340
	 *
341
	 * @return IQueryBuilder
342
	 */
343 View Code Duplication
	protected function getCirclesDeleteSql($circleUniqueId) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
344
		$qb = $this->dbConnection->getQueryBuilder();
345
		$qb->delete(self::TABLE_CIRCLES)
346
		   ->where(
347
			   $qb->expr()
348
				  ->eq('unique_id', $qb->createNamedParameter($circleUniqueId))
349
		   );
350
351
		return $qb;
352
	}
353
354
355
	/**
356
	 * @return IQueryBuilder
357
	 */
358
	protected function getCirclesSelectSql() {
359
		$qb = $this->dbConnection->getQueryBuilder();
360
361
		/** @noinspection PhpMethodParametersCountMismatchInspection */
362
		$qb->selectDistinct('c.unique_id')
363
		   ->addSelect(
364
			   'c.id', 'c.name', 'c.description', 'c.settings', 'c.type', 'contact_addressbook',
365
			   'contact_groupname', 'c.creation'
366
		   )
367
		   ->from(CoreRequestBuilder::TABLE_CIRCLES, 'c');
368
		$this->default_select_alias = 'c';
369
370
		return $qb;
371
	}
372
373
374
	/**
375
	 * @param array $data
376
	 *
377
	 * @return Circle
378
	 */
379
	protected function parseCirclesSelectSql($data) {
380
381
		$circle = new Circle();
382
		$circle->setId($data['id']);
383
		$circle->setUniqueId($data['unique_id']);
384
		$circle->setName($data['name']);
385
		$circle->setDescription($data['description']);
386
		if ($data['contact_addressbook'] !== null) {
387
			$circle->setContactAddressBook($data['contact_addressbook']);
388
		}
389
		if ($data['contact_groupname'] !== null) {
390
			$circle->setContactGroupName($data['contact_groupname']);
391
		}
392
		$circle->setSettings($data['settings']);
393
		$circle->setType($data['type']);
394
		$circle->setCreation($data['creation']);
395
396
		if (key_exists('viewer_level', $data)) {
397
			$user = new Member($data['viewer_userid'], Member::TYPE_USER, $circle->getUniqueId());
398
			$user->setStatus($data['viewer_status']);
399
			$user->setMemberId($data['viewer_member_id']);
400
			$user->setType($data['viewer_type']);
401
			$user->setInstance($data['viewer_instance']);
402
			$user->setLevel($data['viewer_level']);
403
			$circle->setViewer($user);
404
		}
405
406
		if (key_exists('owner_level', $data)) {
407
			$owner = new Member($data['owner_userid'], Member::TYPE_USER, $circle->getUniqueId());
408
			$owner->setStatus($data['owner_status']);
409
			$owner->setInstance($data['owner_instance']);
410
			$owner->setLevel($data['owner_level']);
411
			$circle->setOwner($owner);
412
		}
413
414
		return $circle;
415
	}
416
417
418
}
419