Completed
Push — master ( c57b95...1d3e11 )
by Maxence
30s queued 14s
created

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

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $userId is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $field is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
491
		return;
492
		if (!$this->configService->isLinkedGroupsAllowed()) {
0 ignored issues
show
Unused Code introduced by
if (!$this->configServic...owed()) { return; } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
Bug introduced by
The method isLinkedGroupsAllowed() does not seem to exist on object<OCA\Circles\Service\ConfigService>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
493
			return;
494
		}
495
496
		$expr = $qb->expr();
497
		$qb->leftJoin(
498
			$this->default_select_alias, self::NC_TABLE_GROUP_USER, 'ncgu',
499
			$expr->eq('ncgu.uid', $qb->createNamedParameter($userId))
500
		);
501
502
		/** @noinspection PhpMethodParametersCountMismatchInspection */
503
		$qb->leftJoin(
504
			$this->default_select_alias, DeprecatedRequestBuilder::TABLE_MEMBERS, 'g',
505
			$expr->andX(
506
				$expr->eq('g.user_id', 'ncgu.gid'),
507
				$expr->eq('g.user_type', $qb->createNamedParameter(DeprecatedMember::TYPE_GROUP)),
508
				$expr->eq('g.instance', $qb->createNamedParameter('')),
509
				$expr->eq('g.circle_id', $field)
510
			)
511
		);
512
513
		$this->leftJoinedNCGroupAndUser = true;
514
	}
515
516
}
517
518
519
520