|
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\Model\Member; |
|
33
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder; |
|
34
|
|
|
use OCP\IDBConnection; |
|
35
|
|
|
use OCP\Share; |
|
36
|
|
|
use OCP\Share\IShare; |
|
37
|
|
|
|
|
38
|
|
|
class CircleProviderRequestBuilder { |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
/** @var IDBConnection */ |
|
42
|
|
|
protected $dbConnection; |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* returns the SQL request to get a specific share from the fileId and circleId |
|
47
|
|
|
* |
|
48
|
|
|
* @param int $fileId |
|
49
|
|
|
* @param int $circleId |
|
50
|
|
|
* |
|
51
|
|
|
* @return \OCP\DB\QueryBuilder\IQueryBuilder |
|
52
|
|
|
* @internal param $share |
|
53
|
|
|
* |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function findShareParentSql($fileId, $circleId) { |
|
56
|
|
|
|
|
57
|
|
|
$qb = $this->getBaseSelectSql(); |
|
58
|
|
|
$this->limitToShareParent($qb); |
|
59
|
|
|
$this->limitToCircle($qb, $circleId); |
|
60
|
|
|
$this->limitToFiles($qb, $fileId); |
|
61
|
|
|
|
|
62
|
|
|
return $qb; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Limit the request to a Circle. |
|
68
|
|
|
* |
|
69
|
|
|
* @param IQueryBuilder $qb |
|
70
|
|
|
* @param integer $circleId |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function limitToCircle(& $qb, $circleId) { |
|
73
|
|
|
$expr = $qb->expr(); |
|
74
|
|
|
$pf = ($qb->getType() === QueryBuilder::SELECT) ? 's.' : ''; |
|
75
|
|
|
|
|
76
|
|
|
$qb->andWhere($expr->eq($pf . 'share_with', $qb->createNamedParameter($circleId))); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Limit the request to the Share by its Id. |
|
82
|
|
|
* |
|
83
|
|
|
* @param IQueryBuilder $qb |
|
84
|
|
|
* @param $shareId |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function limitToShare(& $qb, $shareId) { |
|
87
|
|
|
$expr = $qb->expr(); |
|
88
|
|
|
$pf = ($qb->getType() === QueryBuilder::SELECT) ? 's.' : ''; |
|
89
|
|
|
|
|
90
|
|
|
$qb->andWhere($expr->eq($pf . 'id', $qb->createNamedParameter($shareId))); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Limit the request to the top share (no children) |
|
96
|
|
|
* |
|
97
|
|
|
* @param IQueryBuilder $qb |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function limitToShareParent(& $qb) { |
|
100
|
|
|
$expr = $qb->expr(); |
|
101
|
|
|
|
|
102
|
|
|
$qb->andWhere($expr->isNull('parent')); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* limit the request to the children of a share |
|
108
|
|
|
* |
|
109
|
|
|
* @param IQueryBuilder $qb |
|
110
|
|
|
* @param $userId |
|
111
|
|
|
* @param int $parentId |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function limitToShareChildren(& $qb, $userId, $parentId = -1) { |
|
114
|
|
|
$expr = $qb->expr(); |
|
115
|
|
|
$qb->andWhere($expr->eq('share_with', $qb->createNamedParameter($userId))); |
|
116
|
|
|
|
|
117
|
|
|
if ($parentId > -1) { |
|
118
|
|
|
$qb->andWhere($expr->eq('parent', $qb->createNamedParameter($parentId))); |
|
119
|
|
|
} else { |
|
120
|
|
|
$qb->andWhere($expr->isNotNull('parent')); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* limit the request to the share itself AND its children. |
|
127
|
|
|
* perfect if you want to delete everything related to a share |
|
128
|
|
|
* |
|
129
|
|
|
* @param IQueryBuilder $qb |
|
130
|
|
|
* @param $circleId |
|
131
|
|
|
*/ |
|
132
|
|
|
protected function limitToShareAndChildren(& $qb, $circleId) { |
|
133
|
|
|
$expr = $qb->expr(); |
|
134
|
|
|
$pf = ($qb->getType() === QueryBuilder::SELECT) ? 's.' : ''; |
|
135
|
|
|
|
|
136
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
137
|
|
|
$qb->andWhere( |
|
138
|
|
|
$expr->orX( |
|
139
|
|
|
$expr->eq($pf . 'parent', $qb->createNamedParameter($circleId)), |
|
140
|
|
|
$expr->eq($pf . 'id', $qb->createNamedParameter($circleId)) |
|
141
|
|
|
) |
|
142
|
|
|
); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* limit the request to a fileId. |
|
148
|
|
|
* |
|
149
|
|
|
* @param IQueryBuilder $qb |
|
150
|
|
|
* @param $fileId |
|
151
|
|
|
*/ |
|
152
|
|
|
protected function limitToFiles(& $qb, $files) { |
|
153
|
|
|
|
|
154
|
|
|
if (!is_array($files)) { |
|
155
|
|
|
$files = array($files); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
$expr = $qb->expr(); |
|
159
|
|
|
$pf = ($qb->getType() === QueryBuilder::SELECT) ? 's.' : ''; |
|
160
|
|
|
$qb->andWhere( |
|
161
|
|
|
$expr->in( |
|
162
|
|
|
$pf . 'file_source', |
|
163
|
|
|
$qb->createNamedParameter($files, IQueryBuilder::PARAM_INT_ARRAY) |
|
164
|
|
|
) |
|
165
|
|
|
); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @param IQueryBuilder $qb |
|
171
|
|
|
* @param int $limit |
|
172
|
|
|
* @param int $offset |
|
173
|
|
|
*/ |
|
174
|
|
|
protected function limitToPage(& $qb, $limit = -1, $offset = 0) { |
|
175
|
|
|
if ($limit !== -1) { |
|
176
|
|
|
$qb->setMaxResults($limit); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
$qb->setFirstResult($offset); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* limit the request to a userId |
|
184
|
|
|
* |
|
185
|
|
|
* @param IQueryBuilder $qb |
|
186
|
|
|
* @param string $userId |
|
187
|
|
|
* @param bool $reShares |
|
188
|
|
|
*/ |
|
189
|
|
|
protected function limitToShareOwner(& $qb, $userId, $reShares = false) { |
|
190
|
|
|
$expr = $qb->expr(); |
|
191
|
|
|
$pf = ($qb->getType() === QueryBuilder::SELECT) ? 's.' : ''; |
|
192
|
|
|
|
|
193
|
|
|
if ($reShares === false) { |
|
194
|
|
|
$qb->andWhere($expr->eq($pf . 'uid_initiator', $qb->createNamedParameter($userId))); |
|
195
|
|
|
} else { |
|
196
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
197
|
|
|
$qb->andWhere( |
|
198
|
|
|
$expr->orX( |
|
199
|
|
|
$expr->eq($pf . 'uid_owner', $qb->createNamedParameter($userId)), |
|
200
|
|
|
$expr->eq($pf . 'uid_initiator', $qb->createNamedParameter($userId)) |
|
201
|
|
|
) |
|
202
|
|
|
); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* link circle field |
|
209
|
|
|
* |
|
210
|
|
|
* @deprecated |
|
211
|
|
|
* |
|
212
|
|
|
* @param IQueryBuilder $qb |
|
213
|
|
|
* @param integer $shareId |
|
214
|
|
|
*/ |
|
215
|
|
|
// TODO - put this as a leftjoin |
|
216
|
|
|
protected function linkCircleField(& $qb, $shareId) { |
|
217
|
|
|
$expr = $qb->expr(); |
|
218
|
|
|
|
|
219
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
220
|
|
|
$qb->from(CirclesMapper::TABLENAME, 'c') |
|
221
|
|
|
->andWhere( |
|
222
|
|
|
$expr->orX( |
|
223
|
|
|
$expr->eq('s.share_with', 'c.id'), |
|
224
|
|
|
$expr->eq('s.parent', $qb->createNamedParameter($shareId)) |
|
225
|
|
|
) |
|
226
|
|
|
); |
|
227
|
|
|
//->orderBy('c.circle_name'); |
|
|
|
|
|
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @param IQueryBuilder $qb |
|
233
|
|
|
*/ |
|
234
|
|
|
protected function linkToCircleOwner(& $qb) { |
|
235
|
|
|
$expr = $qb->expr(); |
|
236
|
|
|
|
|
237
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
238
|
|
|
$qb->leftJoin( |
|
239
|
|
|
'c', 'circles_members', 'mo', $expr->andX( |
|
240
|
|
|
$expr->eq('c.id', 'mo.circle_id'), |
|
241
|
|
|
$expr->eq('mo.level', $qb->createNamedParameter(Member::LEVEL_OWNER)) |
|
242
|
|
|
) |
|
243
|
|
|
); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Link to member (userId) of circle |
|
249
|
|
|
* |
|
250
|
|
|
* @param IQueryBuilder $qb |
|
251
|
|
|
* @param string $userId |
|
252
|
|
|
*/ |
|
253
|
|
|
protected function linkToMember(& $qb, $userId) { |
|
254
|
|
|
$expr = $qb->expr(); |
|
255
|
|
|
|
|
256
|
|
|
$qb->from(MembersMapper::TABLENAME, 'm') |
|
257
|
|
|
->andWhere($expr->eq('s.share_with', 'm.circle_id')) |
|
258
|
|
|
->andWhere($expr->eq('m.user_id', $qb->createNamedParameter($userId))) |
|
259
|
|
|
->andWhere($expr->gte('m.level', $qb->createNamedParameter(Member::LEVEL_MEMBER))); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Link to all members of circle |
|
265
|
|
|
* |
|
266
|
|
|
* @param IQueryBuilder $qb |
|
267
|
|
|
*/ |
|
268
|
|
|
protected function joinCircleMembers(& $qb) { |
|
269
|
|
|
$expr = $qb->expr(); |
|
270
|
|
|
|
|
271
|
|
|
$qb->from(MembersMapper::TABLENAME, 'm') |
|
272
|
|
|
->andWhere($expr->eq('s.share_with', 'm.circle_id')); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Link to storage/filecache |
|
278
|
|
|
* |
|
279
|
|
|
* @param IQueryBuilder $qb |
|
280
|
|
|
* @param string $userId |
|
281
|
|
|
*/ |
|
282
|
|
|
protected function linkToFileCache(& $qb, $userId) { |
|
283
|
|
|
$expr = $qb->expr(); |
|
284
|
|
|
|
|
285
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
286
|
|
|
$qb->leftJoin('s', 'filecache', 'f', $expr->eq('s.file_source', 'f.fileid')) |
|
287
|
|
|
->leftJoin('f', 'storages', 'st', $expr->eq('f.storage', 'st.numeric_id')) |
|
288
|
|
|
->leftJoin( |
|
289
|
|
|
's', 'share', 's2', $expr->andX( |
|
290
|
|
|
$expr->eq('s.id', 's2.parent'), |
|
291
|
|
|
$expr->eq('s2.share_with', $qb->createNamedParameter($userId)) |
|
292
|
|
|
) |
|
293
|
|
|
); |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* add share to the database and return the ID |
|
299
|
|
|
* |
|
300
|
|
|
* @param IShare $share |
|
301
|
|
|
* |
|
302
|
|
|
* @return IQueryBuilder |
|
303
|
|
|
*/ |
|
304
|
|
|
protected function getBaseInsertSql($share) { |
|
305
|
|
|
$qb = $this->dbConnection->getQueryBuilder(); |
|
306
|
|
|
$qb->insert('share') |
|
307
|
|
|
->setValue('share_type', $qb->createNamedParameter(Share::SHARE_TYPE_CIRCLE)) |
|
308
|
|
|
->setValue('item_type', $qb->createNamedParameter($share->getNodeType())) |
|
309
|
|
|
->setValue('item_source', $qb->createNamedParameter($share->getNodeId())) |
|
310
|
|
|
->setValue('file_source', $qb->createNamedParameter($share->getNodeId())) |
|
311
|
|
|
->setValue('file_target', $qb->createNamedParameter($share->getTarget())) |
|
312
|
|
|
->setValue('share_with', $qb->createNamedParameter($share->getSharedWith())) |
|
313
|
|
|
->setValue('uid_owner', $qb->createNamedParameter($share->getShareOwner())) |
|
314
|
|
|
->setValue('uid_initiator', $qb->createNamedParameter($share->getSharedBy())) |
|
315
|
|
|
->setValue('permissions', $qb->createNamedParameter($share->getPermissions())) |
|
316
|
|
|
->setValue('token', $qb->createNamedParameter($share->getToken())) |
|
317
|
|
|
->setValue('stime', $qb->createNamedParameter(time())); |
|
318
|
|
|
|
|
319
|
|
|
return $qb; |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* generate and return a base sql request. |
|
325
|
|
|
* |
|
326
|
|
|
* @param int $shareId |
|
327
|
|
|
* |
|
328
|
|
|
* @return IQueryBuilder |
|
329
|
|
|
*/ |
|
330
|
|
|
protected function getBaseSelectSql($shareId = -1) { |
|
331
|
|
|
$qb = $this->dbConnection->getQueryBuilder(); |
|
332
|
|
|
|
|
333
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
334
|
|
|
$qb->select( |
|
335
|
|
|
's.id', 's.share_type', 's.share_with', 's.uid_owner', 's.uid_initiator', |
|
336
|
|
|
's.parent', 's.item_type', 's.item_source', 's.item_target', 's.file_source', |
|
337
|
|
|
's.file_target', 's.permissions', 's.stime', 's.accepted', 's.expiration', |
|
338
|
|
|
's.token', 's.mail_send', 'c.type AS circle_type', 'c.name AS circle_name', |
|
339
|
|
|
'mo.user_id AS circle_owner' |
|
340
|
|
|
); |
|
341
|
|
|
$this->linkToCircleOwner($qb); |
|
342
|
|
|
$this->joinShare($qb); |
|
343
|
|
|
|
|
344
|
|
|
// TODO: Left-join circle and REMOVE this line |
|
345
|
|
|
$this->linkCircleField($qb, $shareId); |
|
346
|
|
|
|
|
347
|
|
|
return $qb; |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
|
|
351
|
|
|
/** |
|
352
|
|
|
* Generate and return a base sql request |
|
353
|
|
|
* This one should be used to retrieve a complete list of users (ie. access list). |
|
354
|
|
|
* |
|
355
|
|
|
* @return IQueryBuilder |
|
356
|
|
|
*/ |
|
357
|
|
|
protected function getAccessListBaseSelectSql() { |
|
358
|
|
|
$qb = $this->dbConnection->getQueryBuilder(); |
|
359
|
|
|
|
|
360
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
361
|
|
|
$qb->select( |
|
362
|
|
|
'm.user_id', 's.file_source', 's.file_target' |
|
363
|
|
|
); |
|
364
|
|
|
$this->joinCircleMembers($qb); |
|
365
|
|
|
$this->joinShare($qb); |
|
366
|
|
|
|
|
367
|
|
|
return $qb; |
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
|
|
|
|
371
|
|
|
protected function getCompleteSelectSql() { |
|
372
|
|
|
$qb = $this->dbConnection->getQueryBuilder(); |
|
373
|
|
|
|
|
374
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
375
|
|
|
$qb->select( |
|
376
|
|
|
's.*', 'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', |
|
377
|
|
|
'f.path_hash', 'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', |
|
378
|
|
|
'f.size', 'f.mtime', 'f.storage_mtime', 'f.encrypted', 'f.unencrypted_size', |
|
379
|
|
|
'f.etag', 'f.checksum', 's2.id AS parent_id', 's2.file_target AS parent_target', |
|
380
|
|
|
's2.permissions AS parent_perms' |
|
381
|
|
|
) |
|
382
|
|
|
->selectAlias('st.id', 'storage_string_id'); |
|
383
|
|
|
|
|
384
|
|
|
$this->joinShare($qb); |
|
385
|
|
|
|
|
386
|
|
|
return $qb; |
|
387
|
|
|
} |
|
388
|
|
|
|
|
389
|
|
|
|
|
390
|
|
|
/** |
|
391
|
|
|
* @param IQueryBuilder $qb |
|
392
|
|
|
*/ |
|
393
|
|
|
private function joinShare(& $qb) { |
|
394
|
|
|
$expr = $qb->expr(); |
|
395
|
|
|
|
|
396
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
397
|
|
|
$qb->from('share', 's') |
|
398
|
|
|
->where($expr->eq('s.share_type', $qb->createNamedParameter(Share::SHARE_TYPE_CIRCLE))) |
|
399
|
|
|
->andWhere( |
|
400
|
|
|
$expr->orX( |
|
401
|
|
|
$expr->eq('s.item_type', $qb->createNamedParameter('file')), |
|
402
|
|
|
$expr->eq('s.item_type', $qb->createNamedParameter('folder')) |
|
403
|
|
|
) |
|
404
|
|
|
); |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
|
|
408
|
|
|
/** |
|
409
|
|
|
* generate and return a base sql request. |
|
410
|
|
|
* |
|
411
|
|
|
* @return \OCP\DB\QueryBuilder\IQueryBuilder |
|
412
|
|
|
*/ |
|
413
|
|
View Code Duplication |
protected function getBaseDeleteSql() { |
|
414
|
|
|
$qb = $this->dbConnection->getQueryBuilder(); |
|
415
|
|
|
$expr = $qb->expr(); |
|
416
|
|
|
|
|
417
|
|
|
$qb->delete('share') |
|
418
|
|
|
->where($expr->eq('share_type', $qb->createNamedParameter(Share::SHARE_TYPE_CIRCLE))); |
|
419
|
|
|
|
|
420
|
|
|
return $qb; |
|
421
|
|
|
} |
|
422
|
|
|
|
|
423
|
|
|
|
|
424
|
|
|
/** |
|
425
|
|
|
* generate and return a base sql request. |
|
426
|
|
|
* |
|
427
|
|
|
* @return \OCP\DB\QueryBuilder\IQueryBuilder |
|
428
|
|
|
*/ |
|
429
|
|
View Code Duplication |
protected function getBaseUpdateSql() { |
|
430
|
|
|
$qb = $this->dbConnection->getQueryBuilder(); |
|
431
|
|
|
$expr = $qb->expr(); |
|
432
|
|
|
|
|
433
|
|
|
$qb->update('share') |
|
434
|
|
|
->where($expr->eq('share_type', $qb->createNamedParameter(Share::SHARE_TYPE_CIRCLE))); |
|
435
|
|
|
|
|
436
|
|
|
return $qb; |
|
437
|
|
|
} |
|
438
|
|
|
} |
|
439
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.