Completed
Push — master ( 8dabac...269796 )
by Maxence
02:53 queued 11s
created

ShareWrapperRequest::searchShare()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
6
/**
7
 * Circles - Bring cloud-users closer together.
8
 *
9
 * This file is licensed under the Affero General Public License version 3 or
10
 * later. See the COPYING file.
11
 *
12
 * @author Maxence Lange <[email protected]>
13
 * @copyright 2021
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
32
namespace OCA\Circles\Db;
33
34
35
use OCA\Circles\Exceptions\RequestBuilderException;
36
use OCA\Circles\Exceptions\ShareWrapperNotFoundException;
37
use OCA\Circles\Model\FederatedUser;
38
use OCA\Circles\Model\Membership;
39
use OCA\Circles\Model\ShareWrapper;
40
use OCP\Files\NotFoundException;
41
use OCP\Share\Exceptions\IllegalIDChangeException;
42
use OCP\Share\IShare;
43
44
45
/**
46
 * Class ShareWrapperRequest
47
 *
48
 * @package OCA\Circles\Db
49
 */
50
class ShareWrapperRequest extends ShareWrapperRequestBuilder {
51
52
53
	/**
54
	 * @param IShare $share
55
	 * @param int $parentId
56
	 *
57
	 * @return int
58
	 * @throws NotFoundException
59
	 */
60
	public function save(IShare $share, int $parentId = 0): int {
61
//		$hasher = \OC::$server->getHasher();
62
//		$password = ($share->getPassword() !== null) ? $hasher->hash($share->getPassword()) : '';
63
		$password = '';
64
65
		$qb = $this->getShareInsertSql();
66
		$qb->setValue('share_type', $qb->createNamedParameter($share->getShareType()))
67
		   ->setValue('item_type', $qb->createNamedParameter($share->getNodeType()))
68
		   ->setValue('item_source', $qb->createNamedParameter($share->getNodeId()))
69
		   ->setValue('file_source', $qb->createNamedParameter($share->getNodeId()))
70
		   ->setValue('file_target', $qb->createNamedParameter($share->getTarget()))
71
		   ->setValue('share_with', $qb->createNamedParameter($share->getSharedWith()))
72
		   ->setValue('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
73
		   ->setValue('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
74
		   ->setValue('accepted', $qb->createNamedParameter(IShare::STATUS_ACCEPTED))
75
		   ->setValue('password', $qb->createNamedParameter($password))
76
		   ->setValue('permissions', $qb->createNamedParameter($share->getPermissions()))
77
		   ->setValue('token', $qb->createNamedParameter($share->getToken()))
78
		   ->setValue('stime', $qb->createFunction('UNIX_TIMESTAMP()'));
79
80
		if ($parentId > 0) {
81
			$qb->setValue('parent', $qb->createNamedParameter($parentId));
82
		}
83
84
		$qb->execute();
85
		$id = $qb->getLastInsertId();
86
		try {
87
			$share->setId($id);
88
		} catch (IllegalIDChangeException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
Bug introduced by
The class OCP\Share\Exceptions\IllegalIDChangeException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
89
		}
90
91
		return $id;
92
	}
93
94
95
	/**
96
	 * @param ShareWrapper $shareWrapper
97
	 */
98
	public function update(ShareWrapper $shareWrapper): void {
99
		$qb = $this->getShareUpdateSql();
100
		$qb->set('file_target', $qb->createNamedParameter($shareWrapper->getFileTarget()))
101
		   ->set('share_with', $qb->createNamedParameter($shareWrapper->getSharedWith()))
102
		   ->set('uid_owner', $qb->createNamedParameter($shareWrapper->getShareOwner()))
103
		   ->set('uid_initiator', $qb->createNamedParameter($shareWrapper->getSharedBy()))
104
		   ->set('accepted', $qb->createNamedParameter(IShare::STATUS_ACCEPTED))
105
		   ->set('permissions', $qb->createNamedParameter($shareWrapper->getPermissions()));
106
107
		$qb->limitToId((int)$shareWrapper->getId());
108
109
		$qb->execute();
110
	}
111
112
113
	/**
114
	 * @param Membership $membership
115
	 */
116
	public function removeByMembership(Membership $membership) {
117
		$qb = $this->getShareDeleteSql();
118
		$qb->limitToShareWith($membership->getCircleId());
119
		$qb->limit('uid_initiator', $membership->getSingleId());
120
121
		$qb->execute();
122
	}
123
124
125
	/**
126
	 * @param string $initiator
127
	 * @param string $shareWith
128
	 *
129
	 * @deprecated in NC30 when initiator uses FederatedUser - use removeByMembership()
130
	 */
131
	public function removeByInitiatorAndShareWith(string $initiator, string $shareWith) {
132
		$qb = $this->getShareDeleteSql();
133
		$qb->limitToShareWith($shareWith);
134
		$qb->limit('uid_initiator', $initiator);
135
136
		$qb->execute();
137
	}
138
139
140
	/**
141
	 * @return array
142
	 */
143
	public function getShares(): array {
144
		$qb = $this->getShareSelectSql();
145
146
		return $this->getItemsFromRequest($qb);
147
	}
148
149
150
	/**
151
	 * @param string $circleId
152
	 * @param FederatedUser|null $shareRecipient
153
	 * @param FederatedUser|null $shareInitiator
154
	 * @param bool $completeDetails
155
	 *
156
	 * @return ShareWrapper[]
157
	 * @throws RequestBuilderException
158
	 */
159
	public function getSharesToCircle(
160
		string $circleId,
161
		?FederatedUser $shareRecipient = null,
0 ignored issues
show
Unused Code introduced by
The parameter $shareRecipient 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...
162
		?FederatedUser $shareInitiator = null,
163
		bool $completeDetails = false
164
	): array {
165
		$qb = $this->getShareSelectSql();
166
		$qb->limitToDBFieldEmpty('parent', true);
167
		$qb->setOptions([CoreQueryBuilder::SHARE], ['getData' => true]);
168
169
		$qb->leftJoinCircle(CoreQueryBuilder::SHARE, null, 'share_with');
170
171
		$aliasUpstreamMembership =
172
			$qb->generateAlias(CoreQueryBuilder::SHARE, CoreQueryBuilder::UPSTREAM_MEMBERSHIPS);
173
		$qb->limitToInheritedMemberships(CoreQueryBuilder::SHARE, $circleId, 'share_with');
174
175
//		if (!is_null($shareRecipient)) {
176
//			$qb->limitToInitiator(CoreRequestBuilder::SHARE, $shareRecipient, 'share_with');
177
//		}
178
179
		$qb->leftJoinInheritedMembers(
180
			$aliasUpstreamMembership,
181
			'circle_id',
182
			$qb->generateAlias(CoreQueryBuilder::SHARE, CoreQueryBuilder::INHERITED_BY)
183
		);
184
185
		$aliasMembership = $qb->generateAlias($aliasUpstreamMembership, CoreQueryBuilder::MEMBERSHIPS);
186
		$qb->leftJoinFileCache(CoreQueryBuilder::SHARE);
187
		$qb->leftJoinShareChild(CoreQueryBuilder::SHARE, $aliasMembership);
188
189
		if (!is_null($shareInitiator)) {
190
		}
191
192
		if ($completeDetails) {
193
			$qb->generateGroupBy(
194
				self::$tables[self::TABLE_MEMBERSHIP],
0 ignored issues
show
Bug introduced by
The property tables cannot be accessed from this context as it is declared private in class OCA\Circles\Db\CoreRequestBuilder.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
195
				$aliasMembership,
196
				true
197
			);
198
		}
199
200
		return $this->getItemsFromRequest($qb);
201
	}
202
203
204
	/**
205
	 * @param int $shareId
206
	 * @param FederatedUser|null $federatedUser
207
	 *
208
	 * @return ShareWrapper
209
	 * @throws ShareWrapperNotFoundException
210
	 * @throws RequestBuilderException
211
	 */
212
	public function getShareById(int $shareId, ?FederatedUser $federatedUser = null): ShareWrapper {
213
		$qb = $this->getShareSelectSql();
214
215
		$qb->setOptions([CoreQueryBuilder::SHARE], ['getData' => true]);
216
		$qb->leftJoinCircle(CoreQueryBuilder::SHARE, null, 'share_with');
217
		$qb->limitToId($shareId);
218
219
		if (!is_null($federatedUser)) {
220
			$qb->limitToInitiator(CoreQueryBuilder::SHARE, $federatedUser, 'share_with');
221
			$qb->leftJoinShareChild(CoreQueryBuilder::SHARE);
222
		}
223
224
		return $this->getItemFromRequest($qb);
225
	}
226
227
228
	/**
229
	 * @param FederatedUser $federatedUser
230
	 * @param int $shareId
231
	 *
232
	 * @return ShareWrapper
233
	 * @throws ShareWrapperNotFoundException
234
	 */
235
	public function getChild(FederatedUser $federatedUser, int $shareId): ShareWrapper {
236
		$qb = $this->getShareSelectSql();
237
		$qb->limitToShareParent($shareId);
238
		$qb->limitToShareWith($federatedUser->getSingleId());
239
240
		return $this->getItemFromRequest($qb);
241
	}
242
243
244
	/**
245
	 * @param int $fileId
246
	 * @param bool $getData
247
	 *
248
	 * @return ShareWrapper[]
249
	 * @throws RequestBuilderException
250
	 */
251
	public function getSharesByFileId(int $fileId, bool $getData = false): array {
252
		$qb = $this->getShareSelectSql();
253
		$qb->limitToFileSource($fileId);
254
255
		if ($getData) {
256
			$qb->setOptions([CoreQueryBuilder::SHARE], ['getData' => $getData]);
257
			$qb->leftJoinCircle(CoreQueryBuilder::SHARE, null, 'share_with');
258
//			$qb->leftJoinFileCache(CoreRequestBuilder::SHARE);
259
			$qb->limitToDBFieldEmpty('parent', true);
260
261
			$aliasMembership = $qb->generateAlias(CoreQueryBuilder::SHARE, CoreQueryBuilder::MEMBERSHIPS);
262
			$qb->leftJoinInheritedMembers(CoreQueryBuilder::SHARE, 'share_with');
263
			$qb->leftJoinShareChild(CoreQueryBuilder::SHARE);
264
265
			$qb->generateGroupBy(
266
				self::$tables[self::TABLE_MEMBERSHIP],
0 ignored issues
show
Bug introduced by
The property tables cannot be accessed from this context as it is declared private in class OCA\Circles\Db\CoreRequestBuilder.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
267
				$aliasMembership,
268
				true
269
			);
270
		}
271
272
		return $this->getItemsFromRequest($qb);
273
	}
274
275
276
	/**
277
	 * @param FederatedUser $federatedUser
278
	 * @param int $nodeId
279
	 * @param int $offset
280
	 * @param int $limit
281
	 * @param bool $getData
282
	 *
283
	 * @return ShareWrapper[]
284
	 * @throws RequestBuilderException
285
	 */
286
	public function getSharedWith(
287
		FederatedUser $federatedUser,
288
		int $nodeId,
289
		int $offset,
290
		int $limit,
291
		bool $getData = false
292
	): array {
293
		$qb = $this->getShareSelectSql();
294
		$qb->setOptions([CoreQueryBuilder::SHARE], ['getData' => $getData]);
295
		if ($getData) {
296
			$qb->leftJoinCircle(CoreQueryBuilder::SHARE, null, 'share_with');
297
		}
298
299
		$qb->limitToInitiator(CoreQueryBuilder::SHARE, $federatedUser, 'share_with');
300
301
		$qb->leftJoinFileCache(CoreQueryBuilder::SHARE);
302
		$qb->limitToDBFieldEmpty('parent', true);
303
		$qb->leftJoinShareChild(CoreQueryBuilder::SHARE);
304
305
		if ($nodeId > 0) {
306
			$qb->limitToFileSource($nodeId);
307
		}
308
309
		$qb->chunk($offset, $limit);
310
311
		return $this->getItemsFromRequest($qb);
312
	}
313
314
315
	/**
316
	 * @param FederatedUser $federatedUser
317
	 * @param int $nodeId
318
	 * @param bool $reshares
319
	 * @param int $offset
320
	 * @param int $limit
321
	 * @param bool $getData
322
	 * @param bool $completeDetails
323
	 *
324
	 * @return ShareWrapper[]
325
	 * @throws RequestBuilderException
326
	 */
327
	public function getSharesBy(
328
		FederatedUser $federatedUser,
329
		int $nodeId,
330
		bool $reshares,
331
		int $offset,
332
		int $limit,
333
		bool $getData = false,
334
		bool $completeDetails = false
335
	): array {
336
		$qb = $this->getShareSelectSql();
337
		$qb->setOptions([CoreQueryBuilder::SHARE], ['getData' => $getData]);
338
		$qb->leftJoinCircle(CoreQueryBuilder::SHARE, null, 'share_with');
339
340
		$qb->limitToShareOwner(CoreQueryBuilder::SHARE, $federatedUser, $reshares);
341
		$qb->limitToDBFieldEmpty('parent', true);
342
343
		if ($nodeId > 0) {
344
			$qb->limitToFileSource($nodeId);
345
		}
346
347
		if ($completeDetails) {
348
			$aliasMembership = $qb->generateAlias(CoreQueryBuilder::SHARE, CoreQueryBuilder::MEMBERSHIPS);
349
			$qb->leftJoinInheritedMembers(CoreQueryBuilder::SHARE, 'share_with');
350
			$qb->leftJoinFileCache(CoreQueryBuilder::SHARE);
351
			$qb->leftJoinShareChild(CoreQueryBuilder::SHARE, $aliasMembership);
352
			$qb->generateGroupBy(
353
				self::$tables[self::TABLE_MEMBERSHIP],
0 ignored issues
show
Bug introduced by
The property tables cannot be accessed from this context as it is declared private in class OCA\Circles\Db\CoreRequestBuilder.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
354
				$aliasMembership,
355
				true
356
			);
357
		}
358
359
		$qb->chunk($offset, $limit);
360
361
		return $this->getItemsFromRequest($qb);
362
	}
363
364
365
	/**
366
	 * @param FederatedUser $federatedUser
367
	 * @param int $nodeId
368
	 * @param bool $reshares
369
	 *
370
	 * @return ShareWrapper[]
371
	 * @throws RequestBuilderException
372
	 */
373
	public function getSharesInFolder(
374
		FederatedUser $federatedUser,
375
		int $nodeId,
376
		bool $reshares
377
	): array {
378
		$qb = $this->getShareSelectSql();
379
380
		$qb->leftJoinCircle(CoreQueryBuilder::SHARE, null, 'share_with');
381
		$qb->limitToShareOwner(CoreQueryBuilder::SHARE, $federatedUser, $reshares);
382
		$qb->leftJoinFileCache(CoreQueryBuilder::SHARE);
383
		if ($nodeId > 0) {
384
			$aliasFileCache = $qb->generateAlias(CoreQueryBuilder::SHARE, CoreQueryBuilder::FILE_CACHE);
385
			$qb->limitToDBFieldInt('parent', $nodeId, $aliasFileCache);
386
		}
387
		$qb->limitToDBFieldEmpty('parent', true);
388
389
		return $this->getItemsFromRequest($qb);
390
	}
391
392
393
	/**
394
	 * returns the SQL request to get a specific share from the fileId and circleId
395
	 *
396
	 * @param string $singleId
397
	 * @param int $fileId
398
	 *
399
	 * @return ShareWrapper
400
	 * @throws ShareWrapperNotFoundException
401
	 * @throws RequestBuilderException
402
	 */
403
	public function searchShare(string $singleId, int $fileId): ShareWrapper {
404
		$qb = $this->getShareSelectSql();
405
406
		$qb->setOptions([CoreQueryBuilder::SHARE], ['getData' => true]);
407
		$qb->leftJoinCircle(CoreQueryBuilder::SHARE, null, 'share_with');
408
409
		$qb->limitToDBFieldEmpty('parent', true);
410
		$qb->limitToShareWith($singleId);
411
		$qb->limitToFileSource($fileId);
412
413
		return $this->getItemFromRequest($qb);
414
	}
415
416
}
417
418