|
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
|
|
|
* @author Vinicius Cubas Brand <[email protected]> |
|
11
|
|
|
* @author Daniel Tygel <[email protected]> |
|
12
|
|
|
* |
|
13
|
|
|
* @copyright 2017 |
|
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
|
|
|
namespace OCA\Circles\Db; |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
use Doctrine\DBAL\Query\QueryBuilder; |
|
35
|
|
|
use OCA\Circles\Model\Circle; |
|
36
|
|
|
use OCA\Circles\Model\Member; |
|
37
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder; |
|
38
|
|
|
use OCP\Share; |
|
39
|
|
|
use OCP\Share\IShare; |
|
40
|
|
|
|
|
41
|
|
|
class CircleProviderRequestBuilder extends CoreRequestBuilder { |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* returns the SQL request to get a specific share from the fileId and circleId |
|
46
|
|
|
* |
|
47
|
|
|
* @param int $fileId |
|
48
|
|
|
* @param int $circleId |
|
49
|
|
|
* |
|
50
|
|
|
* @return IQueryBuilder |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function findShareParentSql($fileId, $circleId) { |
|
53
|
|
|
|
|
54
|
|
|
$qb = $this->getBaseSelectSql(); |
|
55
|
|
|
$this->limitToShareParent($qb); |
|
56
|
|
|
$this->limitToCircles($qb, [$circleId]); |
|
57
|
|
|
$this->limitToFiles($qb, $fileId); |
|
58
|
|
|
|
|
59
|
|
|
return $qb; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Limit the request to the given Circles. |
|
65
|
|
|
* |
|
66
|
|
|
* @param IQueryBuilder $qb |
|
67
|
|
|
* @param array $circleUniqueIds |
|
68
|
|
|
*/ |
|
69
|
|
View Code Duplication |
protected function limitToCircles(IQueryBuilder &$qb, $circleUniqueIds) { |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
if (!is_array($circleUniqueIds)) { |
|
72
|
|
|
$circleUniqueIds = array($circleUniqueIds); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$expr = $qb->expr(); |
|
76
|
|
|
$pf = ($qb->getType() === QueryBuilder::SELECT) ? 's.' : ''; |
|
77
|
|
|
$qb->andWhere( |
|
78
|
|
|
$expr->in( |
|
79
|
|
|
$pf . 'share_with', |
|
80
|
|
|
$qb->createNamedParameter($circleUniqueIds, IQueryBuilder::PARAM_STR_ARRAY) |
|
81
|
|
|
) |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Limit the request to the Share by its Id. |
|
88
|
|
|
* |
|
89
|
|
|
* @param IQueryBuilder $qb |
|
90
|
|
|
* @param $shareId |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function limitToShare(IQueryBuilder &$qb, $shareId) { |
|
93
|
|
|
$expr = $qb->expr(); |
|
94
|
|
|
$pf = ($qb->getType() === QueryBuilder::SELECT) ? 's.' : ''; |
|
95
|
|
|
|
|
96
|
|
|
$qb->andWhere($expr->eq($pf . 'id', $qb->createNamedParameter($shareId))); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Limit the request to the top share (no children) |
|
102
|
|
|
* |
|
103
|
|
|
* @param IQueryBuilder $qb |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function limitToShareParent(IQueryBuilder &$qb) { |
|
106
|
|
|
$expr = $qb->expr(); |
|
107
|
|
|
|
|
108
|
|
|
$qb->andWhere($expr->isNull('parent')); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* limit the request to the children of a share |
|
114
|
|
|
* |
|
115
|
|
|
* @param IQueryBuilder $qb |
|
116
|
|
|
* @param $userId |
|
117
|
|
|
* @param int $parentId |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function limitToShareChildren(IQueryBuilder &$qb, $userId, $parentId = -1) { |
|
120
|
|
|
$expr = $qb->expr(); |
|
121
|
|
|
$qb->andWhere($expr->eq('share_with', $qb->createNamedParameter($userId))); |
|
122
|
|
|
|
|
123
|
|
|
if ($parentId > -1) { |
|
124
|
|
|
$qb->andWhere($expr->eq('parent', $qb->createNamedParameter($parentId))); |
|
125
|
|
|
} else { |
|
126
|
|
|
$qb->andWhere($expr->isNotNull('parent')); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* limit the request to the share itself AND its children. |
|
133
|
|
|
* perfect if you want to delete everything related to a share |
|
134
|
|
|
* |
|
135
|
|
|
* @param IQueryBuilder $qb |
|
136
|
|
|
* @param $circleId |
|
137
|
|
|
*/ |
|
138
|
|
|
protected function limitToShareAndChildren(IQueryBuilder &$qb, $circleId) { |
|
139
|
|
|
$expr = $qb->expr(); |
|
140
|
|
|
$pf = ($qb->getType() === QueryBuilder::SELECT) ? 's.' : ''; |
|
141
|
|
|
|
|
142
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
143
|
|
|
$qb->andWhere( |
|
144
|
|
|
$expr->orX( |
|
145
|
|
|
$expr->eq($pf . 'parent', $qb->createNamedParameter($circleId)), |
|
146
|
|
|
$expr->eq($pf . 'id', $qb->createNamedParameter($circleId)) |
|
147
|
|
|
) |
|
148
|
|
|
); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* limit the request to a fileId. |
|
154
|
|
|
* |
|
155
|
|
|
* @param IQueryBuilder $qb |
|
156
|
|
|
* @param $files |
|
157
|
|
|
*/ |
|
158
|
|
View Code Duplication |
protected function limitToFiles(IQueryBuilder &$qb, $files) { |
|
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
if (!is_array($files)) { |
|
161
|
|
|
$files = array($files); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
$expr = $qb->expr(); |
|
165
|
|
|
$pf = ($qb->getType() === QueryBuilder::SELECT) ? 's.' : ''; |
|
166
|
|
|
$qb->andWhere( |
|
167
|
|
|
$expr->in( |
|
168
|
|
|
$pf . 'file_source', |
|
169
|
|
|
$qb->createNamedParameter($files, IQueryBuilder::PARAM_INT_ARRAY) |
|
170
|
|
|
) |
|
171
|
|
|
); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @param IQueryBuilder $qb |
|
177
|
|
|
* @param int $limit |
|
178
|
|
|
* @param int $offset |
|
179
|
|
|
*/ |
|
180
|
|
|
protected function limitToPage(IQueryBuilder &$qb, $limit = -1, $offset = 0) { |
|
181
|
|
|
if ($limit !== -1) { |
|
182
|
|
|
$qb->setMaxResults($limit); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
$qb->setFirstResult($offset); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* limit the request to a userId |
|
191
|
|
|
* |
|
192
|
|
|
* @param IQueryBuilder $qb |
|
193
|
|
|
* @param string $userId |
|
194
|
|
|
* @param bool $reShares |
|
195
|
|
|
*/ |
|
196
|
|
|
protected function limitToShareOwner(IQueryBuilder &$qb, $userId, $reShares = false) { |
|
197
|
|
|
$expr = $qb->expr(); |
|
198
|
|
|
$pf = ($qb->getType() === QueryBuilder::SELECT) ? 's.' : ''; |
|
199
|
|
|
|
|
200
|
|
|
if ($reShares === false) { |
|
201
|
|
|
$qb->andWhere($expr->eq($pf . 'uid_initiator', $qb->createNamedParameter($userId))); |
|
202
|
|
|
} else { |
|
203
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
204
|
|
|
$qb->andWhere( |
|
205
|
|
|
$expr->orX( |
|
206
|
|
|
$expr->eq($pf . 'uid_owner', $qb->createNamedParameter($userId)), |
|
207
|
|
|
$expr->eq($pf . 'uid_initiator', $qb->createNamedParameter($userId)) |
|
208
|
|
|
) |
|
209
|
|
|
); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* link circle field |
|
216
|
|
|
* |
|
217
|
|
|
* @deprecated |
|
218
|
|
|
* |
|
219
|
|
|
* @param IQueryBuilder $qb |
|
220
|
|
|
* @param int $shareId |
|
221
|
|
|
*/ |
|
222
|
|
|
protected function linkCircleField(IQueryBuilder &$qb, $shareId = -1) { |
|
223
|
|
|
$expr = $qb->expr(); |
|
224
|
|
|
|
|
225
|
|
|
$qb->from(CoreRequestBuilder::TABLE_CIRCLES, 'c'); |
|
226
|
|
|
|
|
227
|
|
|
$tmpOrX = $expr->eq( |
|
228
|
|
|
's.share_with', |
|
229
|
|
|
$qb->createFunction('SUBSTR(`c`.`unique_id`, 1, ' . Circle::SHORT_UNIQUE_ID_LENGTH . ')') |
|
230
|
|
|
); |
|
231
|
|
|
|
|
232
|
|
|
if ($shareId === -1) { |
|
233
|
|
|
$qb->andWhere($tmpOrX); |
|
234
|
|
|
|
|
235
|
|
|
return; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
239
|
|
|
$qb->andWhere( |
|
240
|
|
|
$expr->orX( |
|
241
|
|
|
$tmpOrX, |
|
242
|
|
|
$expr->eq('s.parent', $qb->createNamedParameter($shareId)) |
|
243
|
|
|
) |
|
244
|
|
|
); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* @param IQueryBuilder $qb |
|
250
|
|
|
*/ |
|
251
|
|
|
protected function linkToCircleOwner(IQueryBuilder &$qb) { |
|
252
|
|
|
$expr = $qb->expr(); |
|
253
|
|
|
|
|
254
|
|
|
$qb->selectAlias('mo.user_id', 'circle_owner'); |
|
255
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
256
|
|
|
$qb->leftJoin( |
|
257
|
|
|
'c', CoreRequestBuilder::TABLE_MEMBERS, 'mo', $expr->andX( |
|
258
|
|
|
$expr->eq( |
|
259
|
|
|
'mo.circle_id', |
|
260
|
|
|
$qb->createFunction('SUBSTR(`c`.`unique_id`, 1, ' . Circle::SHORT_UNIQUE_ID_LENGTH . ')') |
|
261
|
|
|
), $expr->eq('mo.user_type', $qb->createNamedParameter(Member::TYPE_USER)), |
|
262
|
|
|
$expr->eq('mo.level', $qb->createNamedParameter(Member::LEVEL_OWNER)) |
|
263
|
|
|
) |
|
264
|
|
|
); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* Link to member (userId) of circle |
|
270
|
|
|
* |
|
271
|
|
|
* @param IQueryBuilder $qb |
|
272
|
|
|
* @param string $userId |
|
273
|
|
|
* @param bool $groupMemberAllowed |
|
274
|
|
|
*/ |
|
275
|
|
|
protected function linkToMember(IQueryBuilder &$qb, $userId, $groupMemberAllowed) { |
|
276
|
|
|
$expr = $qb->expr(); |
|
277
|
|
|
|
|
278
|
|
|
$qb->from(CoreRequestBuilder::TABLE_MEMBERS, 'm'); |
|
279
|
|
|
|
|
280
|
|
|
$orX = $expr->orX(); |
|
281
|
|
|
$orX->add($this->exprLinkToMemberAsCircleMember($qb, $userId)); |
|
282
|
|
|
if ($groupMemberAllowed === true) { |
|
283
|
|
|
$orX->add($this->exprLinkToMemberAsGroupMember($qb, $userId)); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
$qb->andWhere($orX); |
|
287
|
|
|
|
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
|
|
291
|
|
|
/** |
|
292
|
|
|
* generate CompositeExpression to link to a Member as a Real Circle Member |
|
293
|
|
|
* |
|
294
|
|
|
* @param IQueryBuilder $qb |
|
295
|
|
|
* @param string $userId |
|
296
|
|
|
* |
|
297
|
|
|
* @return \OCP\DB\QueryBuilder\ICompositeExpression |
|
298
|
|
|
*/ |
|
299
|
|
|
private function exprLinkToMemberAsCircleMember(IQueryBuilder &$qb, $userId) { |
|
300
|
|
|
|
|
301
|
|
|
$expr = $qb->expr(); |
|
302
|
|
|
$andX = $expr->andX(); |
|
303
|
|
|
|
|
304
|
|
|
$andX->add($expr->eq('m.user_id', $qb->createNamedParameter($userId))); |
|
305
|
|
|
$andX->add($expr->eq('m.user_type', $qb->createNamedParameter(Member::TYPE_USER))); |
|
306
|
|
|
$andX->add( |
|
307
|
|
|
$expr->eq( |
|
308
|
|
|
'm.circle_id', |
|
309
|
|
|
$qb->createFunction( |
|
310
|
|
|
'SUBSTR(`c`.`unique_id`, 1, ' . Circle::SHORT_UNIQUE_ID_LENGTH . ')' |
|
311
|
|
|
) |
|
312
|
|
|
) |
|
313
|
|
|
); |
|
314
|
|
|
$andX->add($expr->gte('m.level', $qb->createNamedParameter(Member::LEVEL_MEMBER))); |
|
315
|
|
|
|
|
316
|
|
|
return $andX; |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
|
|
320
|
|
|
/** |
|
321
|
|
|
* generate CompositeExpression to link to a Member as a Group Member (core NC) |
|
322
|
|
|
* |
|
323
|
|
|
* @param IQueryBuilder $qb |
|
324
|
|
|
* @param string $userId |
|
325
|
|
|
* |
|
326
|
|
|
* @return \OCP\DB\QueryBuilder\ICompositeExpression |
|
327
|
|
|
*/ |
|
328
|
|
|
private function exprLinkToMemberAsGroupMember(IQueryBuilder &$qb, $userId) { |
|
329
|
|
|
$expr = $qb->expr(); |
|
330
|
|
|
|
|
331
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
332
|
|
|
$qb->leftJoin( |
|
333
|
|
|
'c', CoreRequestBuilder::TABLE_GROUPS, 'g', |
|
334
|
|
|
$expr->andX( |
|
335
|
|
|
$expr->eq( |
|
336
|
|
|
'g.circle_id', |
|
337
|
|
|
$qb->createFunction('SUBSTR(`c`.`unique_id`, 1, ' . Circle::SHORT_UNIQUE_ID_LENGTH . ')') |
|
338
|
|
|
), |
|
339
|
|
|
$expr->gte('g.level', $qb->createNamedParameter(Member::LEVEL_MEMBER)) |
|
340
|
|
|
) |
|
341
|
|
|
); |
|
342
|
|
|
|
|
343
|
|
|
$qb->leftJoin( |
|
344
|
|
|
'g', CoreRequestBuilder::NC_TABLE_GROUP_USER, 'ncgu', |
|
345
|
|
|
$expr->eq('ncgu.gid', 'g.group_id') |
|
346
|
|
|
); |
|
347
|
|
|
|
|
348
|
|
|
return $expr->andX($expr->eq('ncgu.uid', $qb->createNamedParameter($userId))); |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
|
|
352
|
|
|
/** |
|
353
|
|
|
* left join to get more data about the initiator of the share |
|
354
|
|
|
* |
|
355
|
|
|
* @param IQueryBuilder $qb |
|
356
|
|
|
*/ |
|
357
|
|
|
protected function leftJoinShareInitiator(IQueryBuilder &$qb) { |
|
358
|
|
|
$expr = $qb->expr(); |
|
359
|
|
|
|
|
360
|
|
|
$qb->selectAlias('src_m.level', 'initiator_circle_level'); |
|
361
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
362
|
|
|
$qb->leftJoin( |
|
363
|
|
|
's', CoreRequestBuilder::TABLE_MEMBERS, 'src_m', |
|
364
|
|
|
$expr->andX( |
|
365
|
|
|
$expr->eq('s.uid_initiator', 'src_m.user_id'), |
|
366
|
|
|
$expr->eq('src_m.user_type', $qb->createNamedParameter(Member::TYPE_USER)), |
|
367
|
|
|
$expr->eq('s.share_with', 'src_m.circle_id') |
|
368
|
|
|
) |
|
369
|
|
|
); |
|
370
|
|
|
|
|
371
|
|
|
$qb->selectAlias('src_g.level', 'initiator_group_level'); |
|
372
|
|
|
$qb->leftJoin( |
|
373
|
|
|
's', CoreRequestBuilder::NC_TABLE_GROUP_USER, 'src_ncgu', |
|
374
|
|
|
$expr->eq('s.uid_initiator', 'src_ncgu.uid') |
|
375
|
|
|
); |
|
376
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
377
|
|
|
$qb->leftJoin( |
|
378
|
|
|
's', 'circles_groups', 'src_g', |
|
379
|
|
|
$expr->andX( |
|
380
|
|
|
$expr->gte('src_g.level', $qb->createNamedParameter(Member::LEVEL_MEMBER)), |
|
381
|
|
|
$expr->eq('src_ncgu.gid', 'src_g.group_id'), |
|
382
|
|
|
$req = $expr->eq('s.share_with', 'src_g.circle_id') |
|
383
|
|
|
) |
|
384
|
|
|
); |
|
385
|
|
|
} |
|
386
|
|
|
|
|
387
|
|
|
|
|
388
|
|
|
/** |
|
389
|
|
|
* Link to all members of circle |
|
390
|
|
|
* |
|
391
|
|
|
* @param IQueryBuilder $qb |
|
392
|
|
|
*/ |
|
393
|
|
|
protected function joinCircleMembers(IQueryBuilder &$qb) { |
|
394
|
|
|
$expr = $qb->expr(); |
|
395
|
|
|
|
|
396
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
397
|
|
|
$qb->addSelect('m.user_id') |
|
398
|
|
|
->from(CoreRequestBuilder::TABLE_MEMBERS, 'm') |
|
399
|
|
|
->andWhere( |
|
400
|
|
|
$expr->andX( |
|
401
|
|
|
$expr->eq('s.share_with', 'm.circle_id'), |
|
402
|
|
|
$expr->eq('m.user_type', $qb->createNamedParameter(Member::TYPE_USER)) |
|
403
|
|
|
) |
|
404
|
|
|
); |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
|
|
408
|
|
|
/** |
|
409
|
|
|
* Link to storage/filecache |
|
410
|
|
|
* |
|
411
|
|
|
* @param IQueryBuilder $qb |
|
412
|
|
|
* @param string $userId |
|
413
|
|
|
*/ |
|
414
|
|
|
protected function linkToFileCache(IQueryBuilder &$qb, $userId) { |
|
415
|
|
|
$expr = $qb->expr(); |
|
416
|
|
|
|
|
417
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
418
|
|
|
$qb->leftJoin('s', 'filecache', 'f', $expr->eq('s.file_source', 'f.fileid')) |
|
419
|
|
|
->leftJoin('f', 'storages', 'st', $expr->eq('f.storage', 'st.numeric_id')) |
|
420
|
|
|
->leftJoin( |
|
421
|
|
|
's', 'share', 's2', $expr->andX( |
|
422
|
|
|
$expr->eq('s.id', 's2.parent'), |
|
423
|
|
|
$expr->eq('s2.share_with', $qb->createNamedParameter($userId)) |
|
424
|
|
|
) |
|
425
|
|
|
); |
|
426
|
|
|
|
|
427
|
|
|
$qb->selectAlias('s2.id', 'parent_id'); |
|
428
|
|
|
$qb->selectAlias('s2.file_target', 'parent_target'); |
|
429
|
|
|
$qb->selectAlias('s2.permissions', 'parent_perms'); |
|
430
|
|
|
|
|
431
|
|
|
} |
|
432
|
|
|
|
|
433
|
|
|
|
|
434
|
|
|
/** |
|
435
|
|
|
* add share to the database and return the ID |
|
436
|
|
|
* |
|
437
|
|
|
* @param IShare $share |
|
438
|
|
|
* |
|
439
|
|
|
* @return IQueryBuilder |
|
440
|
|
|
*/ |
|
441
|
|
|
protected function getBaseInsertSql($share) { |
|
442
|
|
|
$qb = $this->dbConnection->getQueryBuilder(); |
|
443
|
|
|
$qb->insert('share') |
|
444
|
|
|
->setValue('share_type', $qb->createNamedParameter(Share::SHARE_TYPE_CIRCLE)) |
|
445
|
|
|
->setValue('item_type', $qb->createNamedParameter($share->getNodeType())) |
|
446
|
|
|
->setValue('item_source', $qb->createNamedParameter($share->getNodeId())) |
|
447
|
|
|
->setValue('file_source', $qb->createNamedParameter($share->getNodeId())) |
|
448
|
|
|
->setValue('file_target', $qb->createNamedParameter($share->getTarget())) |
|
449
|
|
|
->setValue('share_with', $qb->createNamedParameter($share->getSharedWith())) |
|
450
|
|
|
->setValue('uid_owner', $qb->createNamedParameter($share->getShareOwner())) |
|
451
|
|
|
->setValue('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) |
|
452
|
|
|
->setValue('permissions', $qb->createNamedParameter($share->getPermissions())) |
|
453
|
|
|
->setValue('token', $qb->createNamedParameter($share->getToken())) |
|
454
|
|
|
->setValue('stime', $qb->createFunction('UNIX_TIMESTAMP()')); |
|
455
|
|
|
|
|
456
|
|
|
return $qb; |
|
457
|
|
|
} |
|
458
|
|
|
|
|
459
|
|
|
|
|
460
|
|
|
/** |
|
461
|
|
|
* generate and return a base sql request. |
|
462
|
|
|
* |
|
463
|
|
|
* @param int $shareId |
|
464
|
|
|
* |
|
465
|
|
|
* @return IQueryBuilder |
|
466
|
|
|
*/ |
|
467
|
|
|
protected function getBaseSelectSql($shareId = -1) { |
|
468
|
|
|
$qb = $this->dbConnection->getQueryBuilder(); |
|
469
|
|
|
|
|
470
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
471
|
|
|
$qb->select( |
|
472
|
|
|
's.id', 's.share_type', 's.share_with', 's.uid_owner', 's.uid_initiator', |
|
473
|
|
|
's.parent', 's.item_type', 's.item_source', 's.item_target', 's.permissions', 's.stime', |
|
474
|
|
|
's.accepted', 's.expiration', |
|
475
|
|
|
's.token', 's.mail_send', 'c.type AS circle_type', 'c.name AS circle_name' |
|
476
|
|
|
); |
|
477
|
|
|
$this->linkToCircleOwner($qb); |
|
478
|
|
|
$this->joinShare($qb); |
|
479
|
|
|
|
|
480
|
|
|
// TODO: Left-join circle and REMOVE this line |
|
481
|
|
|
$this->linkCircleField($qb, $shareId); |
|
|
|
|
|
|
482
|
|
|
|
|
483
|
|
|
return $qb; |
|
484
|
|
|
} |
|
485
|
|
|
|
|
486
|
|
|
|
|
487
|
|
|
/** |
|
488
|
|
|
* Generate and return a base sql request |
|
489
|
|
|
* This one should be used to retrieve a complete list of users (ie. access list). |
|
490
|
|
|
* |
|
491
|
|
|
* @return IQueryBuilder |
|
492
|
|
|
*/ |
|
493
|
|
|
protected function getAccessListBaseSelectSql() { |
|
494
|
|
|
$qb = $this->dbConnection->getQueryBuilder(); |
|
495
|
|
|
|
|
496
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
497
|
|
|
$this->joinCircleMembers($qb); |
|
498
|
|
|
$this->joinShare($qb); |
|
499
|
|
|
|
|
500
|
|
|
return $qb; |
|
501
|
|
|
} |
|
502
|
|
|
|
|
503
|
|
|
|
|
504
|
|
|
/** |
|
505
|
|
|
* @return IQueryBuilder |
|
506
|
|
|
*/ |
|
507
|
|
|
protected function getCompleteSelectSql() { |
|
508
|
|
|
$qb = $this->dbConnection->getQueryBuilder(); |
|
509
|
|
|
|
|
510
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
511
|
|
|
$qb->selectDistinct('s.id') |
|
512
|
|
|
->addSelect( |
|
513
|
|
|
's.*', 'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', |
|
514
|
|
|
'f.path_hash', 'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', |
|
515
|
|
|
'f.size', 'f.mtime', 'f.storage_mtime', 'f.encrypted', 'f.unencrypted_size', |
|
516
|
|
|
'f.etag', 'f.checksum', 'c.type AS circle_type', 'c.name AS circle_name' |
|
517
|
|
|
) |
|
518
|
|
|
->selectAlias('st.id', 'storage_string_id'); |
|
519
|
|
|
|
|
520
|
|
|
|
|
521
|
|
|
$this->linkToCircleOwner($qb); |
|
522
|
|
|
$this->joinShare($qb); |
|
523
|
|
|
$this->linkCircleField($qb); |
|
|
|
|
|
|
524
|
|
|
|
|
525
|
|
|
|
|
526
|
|
|
return $qb; |
|
527
|
|
|
} |
|
528
|
|
|
|
|
529
|
|
|
|
|
530
|
|
|
/** |
|
531
|
|
|
* @param IQueryBuilder $qb |
|
532
|
|
|
*/ |
|
533
|
|
|
private function joinShare(IQueryBuilder &$qb) { |
|
534
|
|
|
$expr = $qb->expr(); |
|
535
|
|
|
|
|
536
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
537
|
|
|
$qb->addSelect('s.file_source', 's.file_target'); |
|
538
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
539
|
|
|
$qb->from('share', 's') |
|
540
|
|
|
->andWhere($expr->eq('s.share_type', $qb->createNamedParameter(Share::SHARE_TYPE_CIRCLE))) |
|
541
|
|
|
->andWhere( |
|
542
|
|
|
$expr->orX( |
|
543
|
|
|
$expr->eq('s.item_type', $qb->createNamedParameter('file')), |
|
544
|
|
|
$expr->eq('s.item_type', $qb->createNamedParameter('folder')) |
|
545
|
|
|
) |
|
546
|
|
|
); |
|
547
|
|
|
} |
|
548
|
|
|
|
|
549
|
|
|
|
|
550
|
|
|
/** |
|
551
|
|
|
* generate and return a base sql request. |
|
552
|
|
|
* |
|
553
|
|
|
* @return \OCP\DB\QueryBuilder\IQueryBuilder |
|
554
|
|
|
*/ |
|
555
|
|
View Code Duplication |
protected function getBaseDeleteSql() { |
|
|
|
|
|
|
556
|
|
|
$qb = $this->dbConnection->getQueryBuilder(); |
|
557
|
|
|
$expr = $qb->expr(); |
|
558
|
|
|
|
|
559
|
|
|
$qb->delete('share') |
|
560
|
|
|
->where($expr->eq('share_type', $qb->createNamedParameter(Share::SHARE_TYPE_CIRCLE))); |
|
561
|
|
|
|
|
562
|
|
|
return $qb; |
|
563
|
|
|
} |
|
564
|
|
|
|
|
565
|
|
|
|
|
566
|
|
|
/** |
|
567
|
|
|
* generate and return a base sql request. |
|
568
|
|
|
* |
|
569
|
|
|
* @return \OCP\DB\QueryBuilder\IQueryBuilder |
|
570
|
|
|
*/ |
|
571
|
|
View Code Duplication |
protected function getBaseUpdateSql() { |
|
|
|
|
|
|
572
|
|
|
$qb = $this->dbConnection->getQueryBuilder(); |
|
573
|
|
|
$expr = $qb->expr(); |
|
574
|
|
|
|
|
575
|
|
|
$qb->update('share') |
|
576
|
|
|
->where($expr->eq('share_type', $qb->createNamedParameter(Share::SHARE_TYPE_CIRCLE))); |
|
577
|
|
|
|
|
578
|
|
|
return $qb; |
|
579
|
|
|
} |
|
580
|
|
|
} |
|
581
|
|
|
|
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.