Completed
Pull Request — master (#362)
by Maxence
02:54
created

CoreRequestBuilder::limitToShareId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: maxence
5
 * Date: 7/4/17
6
 * Time: 5:01 PM
7
 */
8
9
namespace OCA\Circles\Db;
10
11
12
use Doctrine\DBAL\Query\QueryBuilder;
13
use OCA\Circles\Model\Circle;
14
use OCA\Circles\Model\Member;
15
use OCA\Circles\Service\ConfigService;
16
use OCA\Circles\Service\MiscService;
17
use OCA\Circles\Service\TimezoneService;
18
use OCP\DB\QueryBuilder\IQueryBuilder;
19
use OCP\IDBConnection;
20
use OCP\IL10N;
21
22
class CoreRequestBuilder {
23
24
	const TABLE_FILE_SHARES = 'share';
25
	const SHARE_TYPE = 7;
26
27
	const TABLE_CIRCLES = 'circles_circles';
28
	const TABLE_MEMBERS = 'circles_members';
29
	const TABLE_GROUPS = 'circles_groups';
30
	const TABLE_SHARES = 'circles_shares';
31
	const TABLE_LINKS = 'circles_links';
32
	const TABLE_TOKENS = 'circles_tokens';
33
	const TABLE_GSEVENTS = 'circles_gsevents';
34
	const TABLE_GSSHARES = 'circles_gsshares';
35
36
	const NC_TABLE_GROUP_USER = 'group_user';
37
38
	/** @var array */
39
	private $tables = [
40
		self::TABLE_CIRCLES,
41
		self::TABLE_MEMBERS,
42
		self::TABLE_GROUPS,
43
		self::TABLE_SHARES,
44
		self::TABLE_LINKS,
45
		self::TABLE_TOKENS,
46
		self::TABLE_GSEVENTS,
47
		self::TABLE_GSSHARES
48
	];
49
50
51
	/** @var IDBConnection */
52
	protected $dbConnection;
53
54
	/** @var IL10N */
55
	protected $l10n;
56
57
	/** @var ConfigService */
58
	protected $configService;
59
60
	/** @var TimezoneService */
61
	protected $timezoneService;
62
63
	/** @var MiscService */
64
	protected $miscService;
65
66
	/** @var string */
67
	protected $default_select_alias;
68
69
	/** @var bool */
70
	protected $leftJoinedNCGroupAndUser = false;
71
72
	/**
73
	 * RequestBuilder constructor.
74
	 *
75
	 * @param IL10N $l10n
76
	 * @param IDBConnection $connection
77
	 * @param ConfigService $configService
78
	 * @param TimezoneService $timezoneService
79
	 * @param MiscService $miscService
80
	 */
81
	public function __construct(
82
		IL10N $l10n, IDBConnection $connection, ConfigService $configService,
83
		TimezoneService $timezoneService, MiscService $miscService
84
	) {
85
		$this->l10n = $l10n;
86
		$this->dbConnection = $connection;
87
		$this->configService = $configService;
88
		$this->timezoneService = $timezoneService;
89
		$this->miscService = $miscService;
90
	}
91
92
93
	/**
94
	 * Limit the request by its Id.
95
	 *
96
	 * @param IQueryBuilder $qb
97
	 * @param int $id
98
	 */
99
	protected function limitToId(IQueryBuilder &$qb, $id) {
100
		$this->limitToDBField($qb, 'id', $id);
101
	}
102
103
104
	/**
105
	 * Limit the request by its UniqueId.
106
	 *
107
	 * @param IQueryBuilder $qb
108
	 * @param int $uniqueId
109
	 */
110
	protected function limitToUniqueId(IQueryBuilder &$qb, $uniqueId) {
111
		$this->limitToDBField($qb, 'unique_id', $uniqueId);
112
	}
113
114
115
	/**
116
	 * Limit the request by its addressbookId.
117
	 *
118
	 * @param IQueryBuilder $qb
119
	 * @param int $bookId
120
	 */
121
	protected function limitToAddressBookId(IQueryBuilder &$qb, $bookId) {
122
		$this->limitToDBField($qb, 'contact_addressbook', (string)$bookId);
123
	}
124
125
126
	/**
127
	 * Limit the request by its addressbookId.
128
	 *
129
	 * @param IQueryBuilder $qb
130
	 * @param string $groupName
131
	 */
132
	protected function limitToContactGroup(IQueryBuilder &$qb, $groupName) {
133
		$this->limitToDBField($qb, 'contact_groupname', $groupName);
134
	}
135
136
137
	/**
138
	 * Limit the request to the Circle by its Id.
139
	 *
140
	 * @param IQueryBuilder $qb
141
	 * @param string $contactId
142
	 */
143
	protected function limitToContactId(IQueryBuilder &$qb, $contactId) {
144
		$this->limitToDBField($qb, 'contact_id', $contactId);
145
	}
146
147
148
	/**
149
	 * Limit the request by its Token.
150
	 *
151
	 * @param IQueryBuilder $qb
152
	 * @param string $token
153
	 */
154
	protected function limitToToken(IQueryBuilder &$qb, $token) {
155
		$this->limitToDBField($qb, 'token', $token);
156
	}
157
158
159
	/**
160
	 * Limit the request to the User by its Id.
161
	 *
162
	 * @param IQueryBuilder $qb
163
	 * @param $userId
164
	 */
165
	protected function limitToUserId(IQueryBuilder &$qb, $userId) {
166
		$this->limitToDBField($qb, 'user_id', $userId);
167
	}
168
169
170
	/**
171
	 * Limit the request to the owner
172
	 *
173
	 * @param IQueryBuilder $qb
174
	 * @param $owner
175
	 */
176
	protected function limitToOwner(IQueryBuilder &$qb, $owner) {
177
		$this->limitToDBField($qb, 'owner', $owner);
178
	}
179
180
181
	/**
182
	 * Limit the request to the Member by its Id.
183
	 *
184
	 * @param IQueryBuilder $qb
185
	 * @param string $memberId
186
	 */
187
	protected function limitToMemberId(IQueryBuilder &$qb, string $memberId) {
188
		$this->limitToDBField($qb, 'member_id', $memberId);
189
	}
190
191
192
	/**
193
	 * Limit the request to the Type entry.
194
	 *
195
	 * @param IQueryBuilder $qb
196
	 * @param int $type
197
	 */
198
	protected function limitToUserType(IQueryBuilder &$qb, $type) {
199
		$this->limitToDBField($qb, 'user_type', $type);
200
	}
201
202
203
	/**
204
	 * Limit the request to the Instance.
205
	 *
206
	 * @param IQueryBuilder $qb
207
	 * @param string $instance
208
	 */
209
	protected function limitToInstance(IQueryBuilder &$qb, string $instance) {
210
		$this->limitToDBField($qb, 'instance', $instance);
211
	}
212
213
214
	/**
215
	 * Limit the request to the Circle by its Id.
216
	 *
217
	 * @param IQueryBuilder $qb
218
	 * @param string $circleUniqueId
219
	 */
220
	protected function limitToCircleId(IQueryBuilder &$qb, $circleUniqueId) {
221
		$this->limitToDBField($qb, 'circle_id', $circleUniqueId);
222
	}
223
224
225
	/**
226
	 * Limit the request to the Circle by its Id.
227
	 *
228
	 * @param IQueryBuilder $qb
229
	 * @param int $shareId
230
	 */
231
	protected function limitToShareId(IQueryBuilder &$qb, int $shareId) {
232
		$this->limitToDBField($qb, 'share_id', $shareId);
233
	}
234
235
236
	/**
237
	 * Limit the request to the Circle by its Shorten Unique Id.
238
	 *
239
	 * @param IQueryBuilder $qb
240
	 * @param string $circleUniqueId
241
	 * @param $length
242
	 */
243
	protected function limitToShortenUniqueId(IQueryBuilder &$qb, $circleUniqueId, $length) {
244
		$expr = $qb->expr();
245
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? '`' . $this->default_select_alias . '`.' : '';
246
247
		$qb->andWhere(
248
			$expr->eq(
249
				$qb->createNamedParameter($circleUniqueId),
250
				$qb->createFunction('SUBSTR(' . $pf . '`unique_id`' . ', 1, ' . $length . ')')
251
			)
252
		);
253
254
	}
255
256
257
	/**
258
	 * Limit the request to the Group by its Id.
259
	 *
260
	 * @param IQueryBuilder $qb
261
	 * @param int $groupId
262
	 */
263
	protected function limitToGroupId(IQueryBuilder &$qb, $groupId) {
264
		$this->limitToDBField($qb, 'group_id', $groupId);
265
	}
266
267
268
	/**
269
	 * Limit the search by its Name
270
	 *
271
	 * @param IQueryBuilder $qb
272
	 * @param string $name
273
	 */
274
	protected function limitToName(IQueryBuilder &$qb, $name) {
275
		$this->limitToDBField($qb, 'name', $name);
276
	}
277
278
279
	/**
280
	 * Limit the search by its Status (or greater)
281
	 *
282
	 * @param IQueryBuilder $qb
283
	 * @param string $name
284
	 */
285
	protected function limitToStatus(IQueryBuilder &$qb, $name) {
286
		$this->limitToDBFieldOrGreater($qb, 'status', $name);
287
	}
288
289
290
	/**
291
	 * Limit the request by its Id.
292
	 *
293
	 * @param IQueryBuilder $qb
294
	 * @param string $type
295
	 */
296
	protected function limitToShareType(IQueryBuilder &$qb, string $type) {
297
		$this->limitToDBField($qb, 'share_type', $type);
298
	}
299
300
301
	/**
302
	 * Limit the request by its Id.
303
	 *
304
	 * @param IQueryBuilder $qb
305
	 * @param string $with
306
	 */
307
	protected function limitToShareWith(IQueryBuilder &$qb, string $with) {
308
		$this->limitToDBField($qb, 'share_with', $with);
309
	}
310
311
312
	/**
313
	 * Limit the request to a minimum member level.
314
	 *
315
	 * if $pf is an array, will generate an SQL OR request to limit level in multiple tables
316
	 *
317
	 * @param IQueryBuilder $qb
318
	 * @param int $level
319
	 * @param string|array $pf
320
	 */
321
	protected function limitToLevel(IQueryBuilder &$qb, $level, $pf = '') {
322
		$expr = $qb->expr();
323
324
		if ($pf === '') {
325
			$p = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
326
			$qb->andWhere($expr->gte($p . 'level', $qb->createNamedParameter($level)));
327
328
			return;
329
		}
330
331
		if (!is_array($pf)) {
332
			$pf = [$pf];
333
		}
334
335
		$orX = $this->generateLimitToLevelMultipleTableRequest($qb, $level, $pf);
336
		$qb->andWhere($orX);
337
	}
338
339
340
	/**
341
	 * @param IQueryBuilder $qb
342
	 * @param array $pf
343
	 *
344
	 * @return mixed
345
	 */
346
	private function generateLimitToLevelMultipleTableRequest(IQueryBuilder $qb, $level, $pf) {
347
		$expr = $qb->expr();
348
		$orX = $expr->orX();
349
350
		foreach ($pf as $p) {
351
			if ($p === 'g' && !$this->leftJoinedNCGroupAndUser) {
352
				continue;
353
			}
354
			$orX->add($expr->gte($p . '.level', $qb->createNamedParameter($level)));
355
		}
356
357
		return $orX;
358
	}
359
360
361
	/**
362
	 * Limit the search to Members and Almost members
363
	 *
364
	 * @param IQueryBuilder $qb
365
	 */
366
	protected function limitToMembersAndAlmost(IQueryBuilder &$qb) {
367
		$expr = $qb->expr();
368
369
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
370
371
		$orX = $expr->orX();
372
		$orX->add($expr->eq($pf . 'status', $qb->createNamedParameter(Member::STATUS_MEMBER)));
373
		$orX->add($expr->eq($pf . 'status', $qb->createNamedParameter(Member::STATUS_INVITED)));
374
		$orX->add($expr->eq($pf . 'status', $qb->createNamedParameter(Member::STATUS_REQUEST)));
375
376
		$qb->andWhere($orX);
377
	}
378
379
380
	/**
381
	 * @param IQueryBuilder $qb
382
	 * @param string $field
383
	 * @param string|integer $value
384
	 */
385
	private function limitToDBField(IQueryBuilder &$qb, $field, $value) {
386
		$expr = $qb->expr();
387
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
388
		$qb->andWhere($expr->eq($pf . $field, $qb->createNamedParameter($value)));
389
	}
390
391
392
	/**
393
	 * @param IQueryBuilder $qb
394
	 * @param string $field
395
	 * @param string|integer $value
396
	 */
397
	private function limitToDBFieldOrGreater(IQueryBuilder &$qb, $field, $value) {
398
		$expr = $qb->expr();
399
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
400
		$qb->andWhere($expr->gte($pf . $field, $qb->createNamedParameter($value)));
401
	}
402
403
404
	/**
405
	 * link to the groupId/UserId of the NC DB.
406
	 * If userId is empty, we add the uid of the NCGroup Table in the select list with 'user_id'
407
	 * alias
408
	 *
409
	 * @param IQueryBuilder $qb
410
	 * @param string $userId
411
	 */
412
	protected function limitToNCGroupUser(IQueryBuilder $qb, $userId = '') {
413
		$expr = $qb->expr();
414
415
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
416
417
		$and = $expr->andX($expr->eq($pf . 'group_id', 'ncgu.gid'));
418
		if ($userId !== '') {
419
			$and->add($expr->eq('ncgu.uid', $qb->createNamedParameter($userId)));
420
		} else {
421
			$qb->selectAlias('ncgu.uid', 'user_id');
422
		}
423
424
		$qb->from(self::NC_TABLE_GROUP_USER, 'ncgu');
425
		$qb->andWhere($and);
426
	}
427
428
429
	/**
430
	 * Right Join the Circles table
431
	 *
432
	 * @param IQueryBuilder $qb
433
	 *
434
	 * @deprecated not used (14/07/17)
435
	 */
436
	protected function rightJoinCircles(IQueryBuilder &$qb) {
437
		$expr = $qb->expr();
438
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
439
440
		$qb->from(self::TABLE_CIRCLES, 'c')
441
		   ->andWhere(
442
			   $expr->eq(
443
				   $pf . 'circle_id',
444
				   $qb->createFunction(
445
					   'SUBSTR(`c`.`unique_id`, 1, ' . Circle::SHORT_UNIQUE_ID_LENGTH . ')'
446
				   )
447
			   )
448
		   );
449
	}
450
451
452
	/**
453
	 * Left Join circle table to get more information about the circle.
454
	 *
455
	 * @param IQueryBuilder $qb
456
	 */
457
	protected function leftJoinCircle(IQueryBuilder &$qb) {
458
		if ($qb->getType() !== QueryBuilder::SELECT) {
459
			return;
460
		}
461
462
		$expr = $qb->expr();
463
		$pf = $this->default_select_alias . '.';
464
465
		/** @noinspection PhpMethodParametersCountMismatchInspection */
466
		$qb->selectAlias('lc.type', 'circle_type')
467
		   ->selectAlias('lc.name', 'circle_name')
468
		   ->leftJoin(
469
			   $this->default_select_alias, CoreRequestBuilder::TABLE_CIRCLES, 'lc',
470
			   $expr->eq(
471
				   $pf . 'circle_id',
472
				   $qb->createFunction(
473
					   'SUBSTR(`lc`.`unique_id`, 1, ' . Circle::SHORT_UNIQUE_ID_LENGTH . ')'
474
				   )
475
			   )
476
		   );
477
	}
478
479
480
	/**
481
	 * link to the groupId/UserId of the NC DB.
482
	 *
483
	 * @param IQueryBuilder $qb
484
	 * @param string $userId
485
	 * @param string $field
486
	 */
487
	protected function leftJoinNCGroupAndUser(IQueryBuilder $qb, $userId, $field) {
488
		if (!$this->configService->isLinkedGroupsAllowed()) {
489
			return;
490
		}
491
492
		$expr = $qb->expr();
493
		$qb->leftJoin(
494
			$this->default_select_alias, self::NC_TABLE_GROUP_USER, 'ncgu',
495
			$expr->eq('ncgu.uid', $qb->createNamedParameter($userId))
496
		);
497
498
		/** @noinspection PhpMethodParametersCountMismatchInspection */
499
		$qb->leftJoin(
500
			$this->default_select_alias, CoreRequestBuilder::TABLE_GROUPS, 'g',
501
			$expr->andX(
502
				$expr->eq('ncgu.gid', 'g.group_id'),
503
				$expr->eq(
504
					'g.circle_id', $qb->createFunction(
505
					'SUBSTR(' . $field . ', 1, ' . Circle::SHORT_UNIQUE_ID_LENGTH . ')'
506
				)
507
				)
508
			)
509
		);
510
511
		$this->leftJoinedNCGroupAndUser = true;
512
	}
513
514
515
	/**
516
	 *
517
	 */
518
	public function cleanDatabase(): void {
519
		foreach ($this->tables as $table) {
520
			$qb = $this->dbConnection->getQueryBuilder();
521
			$qb->delete($table);
522
			$qb->execute();
523
		}
524
525
		$qb = $this->dbConnection->getQueryBuilder();
526
		$expr = $qb->expr();
527
		$qb->delete(self::TABLE_FILE_SHARES);
528
		$qb->where($expr->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE)));
529
		$qb->execute();
530
	}
531
532
}
533
534
535
536