Completed
Pull Request — master (#326)
by Maxence
01:30
created

CoreRequestBuilder::limitToCircleId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
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
34
	const NC_TABLE_GROUP_USER = 'group_user';
35
36
	/** @var IDBConnection */
37
	protected $dbConnection;
38
39
	/** @var IL10N */
40
	protected $l10n;
41
42
	/** @var ConfigService */
43
	protected $configService;
44
45
	/** @var TimezoneService */
46
	protected $timezoneService;
47
48
	/** @var MiscService */
49
	protected $miscService;
50
51
	/** @var string */
52
	protected $default_select_alias;
53
54
	/** @var bool */
55
	protected $leftJoinedNCGroupAndUser = false;
56
57
	/**
58
	 * RequestBuilder constructor.
59
	 *
60
	 * @param IL10N $l10n
61
	 * @param IDBConnection $connection
62
	 * @param ConfigService $configService
63
	 * @param TimezoneService $timezoneService
64
	 * @param MiscService $miscService
65
	 */
66
	public function __construct(
67
		IL10N $l10n, IDBConnection $connection, ConfigService $configService,
68
		TimezoneService $timezoneService, MiscService $miscService
69
	) {
70
		$this->l10n = $l10n;
71
		$this->dbConnection = $connection;
72
		$this->configService = $configService;
73
		$this->timezoneService = $timezoneService;
74
		$this->miscService = $miscService;
75
	}
76
77
78
	/**
79
	 * Limit the request by its Id.
80
	 *
81
	 * @param IQueryBuilder $qb
82
	 * @param int $id
83
	 */
84
	protected function limitToId(IQueryBuilder &$qb, $id) {
85
		$this->limitToDBField($qb, 'id', $id);
86
	}
87
88
89
	/**
90
	 * Limit the request by its UniqueId.
91
	 *
92
	 * @param IQueryBuilder $qb
93
	 * @param int $uniqueId
94
	 */
95
	protected function limitToUniqueId(IQueryBuilder &$qb, $uniqueId) {
96
		$this->limitToDBField($qb, 'unique_id', $uniqueId);
97
	}
98
99
100
	/**
101
	 * Limit the request by its Token.
102
	 *
103
	 * @param IQueryBuilder $qb
104
	 * @param string $token
105
	 */
106
	protected function limitToToken(IQueryBuilder &$qb, $token) {
107
		$this->limitToDBField($qb, 'token', $token);
108
	}
109
110
111
	/**
112
	 * Limit the request to the User by its Id.
113
	 *
114
	 * @param IQueryBuilder $qb
115
	 * @param $userId
116
	 */
117
	protected function limitToUserId(IQueryBuilder &$qb, $userId) {
118
		$this->limitToDBField($qb, 'user_id', $userId);
119
	}
120
121
122
	/**
123
	 * Limit the request to the Type entry.
124
	 *
125
	 * @param IQueryBuilder $qb
126
	 * @param int $type
127
	 */
128
	protected function limitToUserType(IQueryBuilder &$qb, $type) {
129
		$this->limitToDBField($qb, 'user_type', $type);
130
	}
131
132
133
	/**
134
	 * Limit the request to the Circle by its Id.
135
	 *
136
	 * @param IQueryBuilder $qb
137
	 * @param string $circleUniqueId
138
	 */
139
	protected function limitToCircleId(IQueryBuilder &$qb, $circleUniqueId) {
140
		$this->limitToDBField($qb, 'circle_id', $circleUniqueId);
141
	}
142
143
144
	/**
145
	 * Limit the request to the Circle by its Id.
146
	 *
147
	 * @param IQueryBuilder $qb
148
	 * @param int $shareId
149
	 */
150
	protected function limitToShareId(IQueryBuilder &$qb, int $shareId) {
151
		$this->limitToDBField($qb, 'circle_id', $shareId);
152
	}
153
154
155
	/**
156
	 * Limit the request to the Circle by its Shorten Unique Id.
157
	 *
158
	 * @param IQueryBuilder $qb
159
	 * @param string $circleUniqueId
160
	 * @param $length
161
	 */
162
	protected function limitToShortenUniqueId(IQueryBuilder &$qb, $circleUniqueId, $length) {
163
		$expr = $qb->expr();
164
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? '`' . $this->default_select_alias
165
														  . '`.' : '';
166
167
		$qb->andWhere(
168
			$expr->eq(
169
				$qb->createNamedParameter($circleUniqueId),
170
				$qb->createFunction('SUBSTR(' . $pf . '`unique_id`' . ', 1, ' . $length . ')')
171
			)
172
		);
173
174
	}
175
176
177
	/**
178
	 * Limit the request to the Group by its Id.
179
	 *
180
	 * @param IQueryBuilder $qb
181
	 * @param int $groupId
182
	 */
183
	protected function limitToGroupId(IQueryBuilder &$qb, $groupId) {
184
		$this->limitToDBField($qb, 'group_id', $groupId);
185
	}
186
187
188
	/**
189
	 * Limit the search by its Name
190
	 *
191
	 * @param IQueryBuilder $qb
192
	 * @param string $name
193
	 */
194
	protected function limitToName(IQueryBuilder &$qb, $name) {
195
		$this->limitToDBField($qb, 'name', $name);
196
	}
197
198
199
	/**
200
	 * Limit the search by its Status (or greater)
201
	 *
202
	 * @param IQueryBuilder $qb
203
	 * @param string $name
204
	 */
205
	protected function limitToStatus(IQueryBuilder &$qb, $name) {
206
		$this->limitToDBFieldOrGreater($qb, 'status', $name);
207
	}
208
209
210
	/**
211
	 * Limit the request by its Id.
212
	 *
213
	 * @param IQueryBuilder $qb
214
	 * @param string $type
215
	 */
216
	protected function limitToShareType(IQueryBuilder &$qb, string $type) {
217
		$this->limitToDBField($qb, 'share_type', $type);
218
	}
219
220
221
	/**
222
	 * Limit the request by its Id.
223
	 *
224
	 * @param IQueryBuilder $qb
225
	 * @param string $with
226
	 */
227
	protected function limitToShareWith(IQueryBuilder &$qb, string $with) {
228
		$this->limitToDBField($qb, 'share_with', $with);
229
	}
230
231
232
	/**
233
	 * Limit the request to a minimum member level.
234
	 *
235
	 * if $pf is an array, will generate an SQL OR request to limit level in multiple tables
236
	 *
237
	 * @param IQueryBuilder $qb
238
	 * @param int $level
239
	 * @param string|array $pf
240
	 */
241
	protected function limitToLevel(IQueryBuilder &$qb, $level, $pf = '') {
242
		$expr = $qb->expr();
243
244
		if ($pf === '') {
245
			$p = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
246
			$qb->andWhere($expr->gte($p . 'level', $qb->createNamedParameter($level)));
247
248
			return;
249
		}
250
251
		if (!is_array($pf)) {
252
			$pf = [$pf];
253
		}
254
255
		$orX = $this->generateLimitToLevelMultipleTableRequest($qb, $level, $pf);
256
		$qb->andWhere($orX);
257
	}
258
259
260
	/**
261
	 * @param IQueryBuilder $qb
262
	 * @param array $pf
263
	 *
264
	 * @return mixed
265
	 */
266
	private function generateLimitToLevelMultipleTableRequest(IQueryBuilder $qb, $level, $pf) {
267
		$expr = $qb->expr();
268
		$orX = $expr->orX();
269
270
		foreach ($pf as $p) {
271
			if ($p === 'g' && !$this->leftJoinedNCGroupAndUser) {
272
				continue;
273
			}
274
			$orX->add($expr->gte($p . '.level', $qb->createNamedParameter($level)));
275
		}
276
277
		return $orX;
278
	}
279
280
281
	/**
282
	 * Limit the search to Members and Almost members
283
	 *
284
	 * @param IQueryBuilder $qb
285
	 */
286
	protected function limitToMembersAndAlmost(IQueryBuilder &$qb) {
287
		$expr = $qb->expr();
288
289
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
290
291
		$orX = $expr->orX();
292
		$orX->add($expr->eq($pf . 'status', $qb->createNamedParameter(Member::STATUS_MEMBER)));
293
		$orX->add($expr->eq($pf . 'status', $qb->createNamedParameter(Member::STATUS_INVITED)));
294
		$orX->add($expr->eq($pf . 'status', $qb->createNamedParameter(Member::STATUS_REQUEST)));
295
296
		$qb->andWhere($orX);
297
	}
298
299
300
	/**
301
	 * @param IQueryBuilder $qb
302
	 * @param string $field
303
	 * @param string|integer $value
304
	 */
305
	private function limitToDBField(IQueryBuilder &$qb, $field, $value) {
306
		$expr = $qb->expr();
307
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
308
		$qb->andWhere($expr->eq($pf . $field, $qb->createNamedParameter($value)));
309
	}
310
311
312
	/**
313
	 * @param IQueryBuilder $qb
314
	 * @param string $field
315
	 * @param string|integer $value
316
	 */
317
	private function limitToDBFieldOrGreater(IQueryBuilder &$qb, $field, $value) {
318
		$expr = $qb->expr();
319
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
320
		$qb->andWhere($expr->gte($pf . $field, $qb->createNamedParameter($value)));
321
	}
322
323
324
	/**
325
	 * link to the groupId/UserId of the NC DB.
326
	 * If userId is empty, we add the uid of the NCGroup Table in the select list with 'user_id'
327
	 * alias
328
	 *
329
	 * @param IQueryBuilder $qb
330
	 * @param string $userId
331
	 */
332
	protected function limitToNCGroupUser(IQueryBuilder $qb, $userId = '') {
333
		$expr = $qb->expr();
334
335
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
336
337
		$and = $expr->andX($expr->eq($pf . 'group_id', 'ncgu.gid'));
338
		if ($userId !== '') {
339
			$and->add($expr->eq('ncgu.uid', $qb->createNamedParameter($userId)));
340
		} else {
341
			$qb->selectAlias('ncgu.uid', 'user_id');
342
		}
343
344
		$qb->from(self::NC_TABLE_GROUP_USER, 'ncgu');
345
		$qb->andWhere($and);
346
	}
347
348
349
	/**
350
	 * Right Join the Circles table
351
	 *
352
	 * @param IQueryBuilder $qb
353
	 *
354
	 * @deprecated not used (14/07/17)
355
	 */
356
	protected function rightJoinCircles(IQueryBuilder &$qb) {
357
		$expr = $qb->expr();
358
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
359
360
		$qb->from(self::TABLE_CIRCLES, 'c')
361
		   ->andWhere(
362
			   $expr->eq(
363
				   $pf . 'circle_id',
364
				   $qb->createFunction(
365
					   'SUBSTR(`c`.`unique_id`, 1, ' . Circle::SHORT_UNIQUE_ID_LENGTH . ')'
366
				   )
367
			   )
368
		   );
369
	}
370
371
372
	/**
373
	 * Left Join circle table to get more information about the circle.
374
	 *
375
	 * @param IQueryBuilder $qb
376
	 */
377
	protected function leftJoinCircle(IQueryBuilder &$qb) {
378
379
		if ($qb->getType() !== QueryBuilder::SELECT) {
380
			return;
381
		}
382
383
		$expr = $qb->expr();
384
		$pf = $this->default_select_alias . '.';
385
386
		/** @noinspection PhpMethodParametersCountMismatchInspection */
387
		$qb->selectAlias('lc.type', 'circle_type')
388
		   ->selectAlias('lc.name', 'circle_name')
389
		   ->leftJoin(
390
			   $this->default_select_alias, CoreRequestBuilder::TABLE_CIRCLES, 'lc',
391
			   $expr->eq(
392
				   $pf . 'circle_id',
393
				   $qb->createFunction(
394
					   'SUBSTR(`lc`.`unique_id`, 1, ' . Circle::SHORT_UNIQUE_ID_LENGTH . ')'
395
				   )
396
			   )
397
		   );
398
	}
399
400
401
	/**
402
	 * link to the groupId/UserId of the NC DB.
403
	 *
404
	 * @param IQueryBuilder $qb
405
	 * @param string $userId
406
	 * @param string $field
407
	 */
408
	protected function leftJoinNCGroupAndUser(IQueryBuilder $qb, $userId, $field) {
409
		if (!$this->configService->isLinkedGroupsAllowed()) {
410
			return;
411
		}
412
413
		$expr = $qb->expr();
414
		$qb->leftJoin(
415
			$this->default_select_alias, self::NC_TABLE_GROUP_USER, 'ncgu',
416
			$expr->eq('ncgu.uid', $qb->createNamedParameter($userId))
417
		);
418
419
		/** @noinspection PhpMethodParametersCountMismatchInspection */
420
		$qb->leftJoin(
421
			$this->default_select_alias, CoreRequestBuilder::TABLE_GROUPS, 'g',
422
			$expr->andX(
423
				$expr->eq('ncgu.gid', 'g.group_id'),
424
				$expr->eq(
425
					'g.circle_id', $qb->createFunction(
426
					'SUBSTR(' . $field . ', 1, ' . Circle::SHORT_UNIQUE_ID_LENGTH . ')'
427
				)
428
				)
429
			)
430
		);
431
432
		$this->leftJoinedNCGroupAndUser = true;
433
	}
434
}
435
436
437
438