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

ShareByCircleProvider::getAllShares()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
 * @author Vinicius Cubas Brand <[email protected]>
14
 * @author Daniel Tygel <[email protected]>
15
 *
16
 * @copyright 2017
17
 * @license GNU AGPL version 3 or any later version
18
 *
19
 * This program is free software: you can redistribute it and/or modify
20
 * it under the terms of the GNU Affero General Public License as
21
 * published by the Free Software Foundation, either version 3 of the
22
 * License, or (at your option) any later version.
23
 *
24
 * This program is distributed in the hope that it will be useful,
25
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27
 * GNU Affero General Public License for more details.
28
 *
29
 * You should have received a copy of the GNU Affero General Public License
30
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31
 *
32
 */
33
34
35
namespace OCA\Circles;
36
37
38
use daita\MySmallPhpTools\Traits\TArrayTools;
39
use daita\MySmallPhpTools\Traits\TStringTools;
40
use OC;
41
use OCA\Circles\Exceptions\CircleNotFoundException;
42
use OCA\Circles\Exceptions\FederatedEventException;
43
use OCA\Circles\Exceptions\FederatedItemException;
44
use OCA\Circles\Exceptions\FederatedUserException;
45
use OCA\Circles\Exceptions\FederatedUserNotFoundException;
46
use OCA\Circles\Exceptions\InitiatorNotConfirmedException;
47
use OCA\Circles\Exceptions\InitiatorNotFoundException;
48
use OCA\Circles\Exceptions\InvalidIdException;
49
use OCA\Circles\Exceptions\OwnerNotFoundException;
50
use OCA\Circles\Exceptions\RemoteInstanceException;
51
use OCA\Circles\Exceptions\RemoteNotFoundException;
52
use OCA\Circles\Exceptions\RemoteResourceNotFoundException;
53
use OCA\Circles\Exceptions\RequestBuilderException;
54
use OCA\Circles\Exceptions\ShareWrapperNotFoundException;
55
use OCA\Circles\Exceptions\SingleCircleNotFoundException;
56
use OCA\Circles\Exceptions\UnknownRemoteException;
57
use OCA\Circles\FederatedItems\FileShare;
58
use OCA\Circles\Model\Federated\FederatedEvent;
59
use OCA\Circles\Model\Helpers\MemberHelper;
60
use OCA\Circles\Model\ShareWrapper;
61
use OCA\Circles\Service\CircleService;
62
use OCA\Circles\Service\EventService;
63
use OCA\Circles\Service\FederatedEventService;
64
use OCA\Circles\Service\FederatedUserService;
65
use OCA\Circles\Service\ShareWrapperService;
66
use OCP\Files\Folder;
67
use OCP\Files\InvalidPathException;
68
use OCP\Files\IRootFolder;
69
use OCP\Files\Node;
70
use OCP\Files\NotFoundException;
71
use OCP\IDBConnection;
72
use OCP\IL10N;
73
use OCP\ILogger;
74
use OCP\IURLGenerator;
75
use OCP\IUserManager;
76
use OCP\Security\ISecureRandom;
77
use OCP\Share\Exceptions\AlreadySharedException;
78
use OCP\Share\Exceptions\IllegalIDChangeException;
79
use OCP\Share\Exceptions\ShareNotFound;
80
use OCP\Share\IShare;
81
use OCP\Share\IShareProvider;
82
83
84
/**
85
 * Class ShareByCircleProvider
86
 *
87
 * @package OCA\Circles
88
 */
89
class ShareByCircleProvider implements IShareProvider {
90
91
92
	use TArrayTools;
93
	use TStringTools;
94
95
96
	const IDENTIFIER = 'ocCircleShare';
97
98
99
	/** @var IUserManager */
100
	private $userManager;
101
102
	/** @var IRootFolder */
103
	private $rootFolder;
104
105
	/** @var IL10N */
106
	private $l10n;
107
108
	/** @var ILogger */
109
	private $logger;
110
111
	/** @var IURLGenerator */
112
	private $urlGenerator;
113
114
115
	/** @var ShareWrapperService */
116
	private $shareWrapperService;
117
118
	/** @var FederatedUserService */
119
	private $federatedUserService;
120
121
	/** @var FederatedEventService */
122
	private $federatedEventService;
123
124
	/** @var CircleService */
125
	private $circleService;
126
127
	/** @var EventService */
128
	private $eventService;
129
130
131
	/**
132
	 * ShareByCircleProvider constructor.
133
	 *
134
	 * @param IDBConnection $connection
135
	 * @param ISecureRandom $secureRandom
136
	 * @param IUserManager $userManager
137
	 * @param IRootFolder $rootFolder
138
	 * @param IL10N $l10n
139
	 * @param ILogger $logger
140
	 * @param IURLGenerator $urlGenerator
141
	 */
142
	public function __construct(
143
		IDBConnection $connection, ISecureRandom $secureRandom, IUserManager $userManager,
144
		IRootFolder $rootFolder, IL10N $l10n, ILogger $logger, IURLGenerator $urlGenerator
145
	) {
146
		$this->userManager = $userManager;
147
		$this->rootFolder = $rootFolder;
148
		$this->l10n = $l10n;
149
		$this->logger = $logger;
150
		$this->urlGenerator = $urlGenerator;
151
152
		$this->federatedUserService = OC::$server->get(FederatedUserService::class);
153
		$this->federatedEventService = OC::$server->get(FederatedEventService::class);
154
		$this->shareWrapperService = OC::$server->get(ShareWrapperService::class);
155
		$this->circleService = OC::$server->get(CircleService::class);
156
		$this->eventService = OC::$server->get(EventService::class);
157
	}
158
159
160
	/**
161
	 * @return string
162
	 */
163
	public function identifier(): string {
164
		return self::IDENTIFIER;
165
	}
166
167
168
	/**
169
	 * @param IShare $share
170
	 *
171
	 * @return IShare
172
	 * @throws AlreadySharedException
173
	 * @throws CircleNotFoundException
174
	 * @throws FederatedEventException
175
	 * @throws FederatedItemException
176
	 * @throws InitiatorNotConfirmedException
177
	 * @throws OwnerNotFoundException
178
	 * @throws RemoteInstanceException
179
	 * @throws RemoteNotFoundException
180
	 * @throws RemoteResourceNotFoundException
181
	 * @throws UnknownRemoteException
182
	 * @throws FederatedUserException
183
	 * @throws FederatedUserNotFoundException
184
	 * @throws IllegalIDChangeException
185
	 * @throws InitiatorNotFoundException
186
	 * @throws InvalidIdException
187
	 * @throws InvalidPathException
188
	 * @throws NotFoundException
189
	 * @throws RequestBuilderException
190
	 * @throws ShareNotFound
191
	 * @throws SingleCircleNotFoundException
192
	 */
193
	public function create(IShare $share): IShare {
194
		if ($share->getShareType() !== IShare::TYPE_CIRCLE) {
195
			return $share;
196
		}
197
198
		$nodeId = $share->getNode()
199
						->getId();
200
201
		try {
202
			$knowShareWrapper = $this->shareWrapperService->searchShare($share->getSharedWith(), $nodeId);
203
			throw new AlreadySharedException(
204
				$this->l10n->t('This item is already shared with this circle'),
205
				$knowShareWrapper->getShare($this->rootFolder, $this->userManager, $this->urlGenerator)
206
			);
207
		} catch (ShareWrapperNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
208
		}
209
210
		$this->federatedUserService->initCurrentUser();
211
		$circle = $this->circleService->getCircle($share->getSharedWith());
212
		$owner = $circle->getInitiator();
213
214
		$initiatorHelper = new MemberHelper($circle->getInitiator());
215
		$initiatorHelper->mustBeMember();
216
217
		$share->setToken($this->token(15));
218
		$this->shareWrapperService->save($share);
219
220
		try {
221
			$wrappedShare = $this->shareWrapperService->getShareById(
222
				(int)$share->getId(),
223
				$this->federatedUserService->getCurrentUser()
224
			);
225
226
			// TODO: include setOwner in the SQL request directly (leftJoin)
227
			$wrappedShare->setOwner($owner);
228
		} catch (ShareWrapperNotFoundException $e) {
229
			throw new ShareNotFound();
230
		}
231
232
		// TODO: Move that into a Dispatched Event
233
		$event = new FederatedEvent(FileShare::class);
234
		$event->setSeverity(FederatedEvent::SEVERITY_HIGH)
235
			  ->setCircle($circle)
236
			  ->getData()->sObj('wrappedShare', $wrappedShare);
237
238
		$this->federatedEventService->newEvent($event);
239
240
		$this->eventService->shareCreated($wrappedShare);
241
242
		return $wrappedShare->getShare($this->rootFolder, $this->userManager, $this->urlGenerator);
243
	}
244
245
246
	/**
247
	 * @param IShare $share
248
	 *
249
	 * @return IShare
250
	 */
251
	public function update(IShare $share): IShare {
252
		OC::$server->getLogger()->log(3, 'CSP > update');
253
254
		return $share;
255
	}
256
257
	/**
258
	 * @param IShare $share
259
	 */
260
	public function delete(IShare $share): void {
261
		OC::$server->getLogger()->log(3, 'CSP > delete');
262
	}
263
264
	/**
265
	 * @param IShare $share
266
	 * @param string $recipient
267
	 */
268
	public function deleteFromSelf(IShare $share, $recipient): void {
269
		OC::$server->getLogger()->log(3, 'CSP > deleteFromSelf');
270
	}
271
272
	/**
273
	 * @param IShare $share
274
	 * @param string $recipient
275
	 *
276
	 * @return IShare
277
	 */
278
	public function restore(IShare $share, string $recipient): IShare {
279
		OC::$server->getLogger()->log(3, 'CSP > restore');
280
281
		return $share;
282
	}
283
284
	/**
285
	 * @param IShare $share
286
	 * @param string $recipient
287
	 *
288
	 * @return IShare
289
	 * @throws FederatedUserException
290
	 * @throws FederatedUserNotFoundException
291
	 * @throws InvalidIdException
292
	 * @throws ShareWrapperNotFoundException
293
	 * @throws SingleCircleNotFoundException
294
	 * @throws IllegalIDChangeException
295
	 * @throws NotFoundException
296
	 * @throws RequestBuilderException
297
	 */
298
	public function move(IShare $share, $recipient): IShare {
299
		OC::$server->getLogger()->log(3, 'CSP > move' . $share->getId() . ' ' . $recipient);
300
301
		$federatedUser = $this->federatedUserService->getLocalFederatedUser($recipient);
302
		$child = $this->shareWrapperService->getChild($share, $federatedUser);
303
		if ($child->getFileTarget() !== $share->getTarget()) {
304
			$child->setFileTarget($share->getTarget());
305
			$this->shareWrapperService->update($child);
306
		}
307
308
		$wrappedShare = $this->shareWrapperService->getShareById((int)$share->getId(), $federatedUser);
309
310
		return $wrappedShare->getShare($this->rootFolder, $this->userManager, $this->urlGenerator);
311
	}
312
313
314
	/**
315
	 * @param string $userId
316
	 * @param Folder $node
317
	 * @param bool $reshares
318
	 *
319
	 * @return array
320
	 * @throws FederatedUserException
321
	 * @throws FederatedUserNotFoundException
322
	 * @throws IllegalIDChangeException
323
	 * @throws InvalidIdException
324
	 * @throws InvalidPathException
325
	 * @throws NotFoundException
326
	 * @throws SingleCircleNotFoundException
327
	 * @throws RequestBuilderException
328
	 */
329
	public function getSharesInFolder($userId, Folder $node, $reshares): array {
330
		OC::$server->getLogger()->log(3, 'CSP > getSharesInFolder');
331
332
		$federatedUser = $this->federatedUserService->getLocalFederatedUser($userId);
333
		$wrappedShares = $this->shareWrapperService->getSharesInFolder(
334
			$federatedUser,
335
			(!is_null($node)) ? $node->getId() : 0,
336
			$reshares
337
		);
338
339
		$result = [];
340
		foreach ($wrappedShares as $wrappedShare) {
341
			if (!array_key_exists($wrappedShare->getFileSource(), $result)) {
342
				$result[$wrappedShare->getFileSource()] = [];
343
			}
344
			if ($wrappedShare->getFileCache()->isAccessible()) {
345
				$result[$wrappedShare->getFileSource()][] =
346
					$wrappedShare->getShare($this->rootFolder, $this->userManager, $this->urlGenerator);
347
			}
348
		}
349
350
		return $result;
351
	}
352
353
354
	/**
355
	 * @param string $userId
356
	 * @param int $shareType
357
	 * @param Node|null $node
358
	 * @param bool $reshares
359
	 * @param int $limit
360
	 * @param int $offset
361
	 *
362
	 * @return IShare[]
363
	 * @throws FederatedUserException
364
	 * @throws FederatedUserNotFoundException
365
	 * @throws IllegalIDChangeException
366
	 * @throws InvalidIdException
367
	 * @throws InvalidPathException
368
	 * @throws NotFoundException
369
	 * @throws RequestBuilderException
370
	 * @throws SingleCircleNotFoundException
371
	 */
372
	public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset): array {
373
		if ($shareType !== IShare::TYPE_CIRCLE) {
374
			return [];
375
		}
376
377
		OC::$server->getLogger()->log(3, 'CSP > getSharesBy');
378
379
		$federatedUser = $this->federatedUserService->getLocalFederatedUser($userId);
380
		$wrappedShares = $this->shareWrapperService->getSharesBy(
381
			$federatedUser,
382
			(!is_null($node)) ? $node->getId() : 0,
383
			$reshares,
384
			$limit,
385
			$offset,
386
			true
387
		);
388
389
		return array_filter(
390
			array_map(
391
				function(ShareWrapper $wrapper) {
392
					return $wrapper->getShare($this->rootFolder, $this->userManager, $this->urlGenerator);
393
				}, $wrappedShares
394
			)
395
		);
396
	}
397
398
399
	/**
400
	 * @param string $shareId
401
	 * @param string|null $recipientId
402
	 *
403
	 * @return IShare
404
	 * @throws FederatedUserException
405
	 * @throws FederatedUserNotFoundException
406
	 * @throws IllegalIDChangeException
407
	 * @throws InvalidIdException
408
	 * @throws ShareNotFound
409
	 * @throws SingleCircleNotFoundException
410
	 */
411
	public function getShareById($shareId, $recipientId = null): IShare {
412
		OC::$server->getLogger()->log(3, 'CSP > getShareById');
413
414
		if ($recipientId === null) {
415
			$federatedUser = null;
416
		} else {
417
			$federatedUser = $this->federatedUserService->getLocalFederatedUser($recipientId);
418
		}
419
420
		try {
421
			$wrappedShare = $this->shareWrapperService->getShareById((int)$shareId, $federatedUser);
422
		} catch (ShareWrapperNotFoundException $e) {
423
			throw new  ShareNotFound();
424
		}
425
426
		return $wrappedShare->getShare($this->rootFolder, $this->userManager, $this->urlGenerator);
427
	}
428
429
430
	/**
431
	 * @param Node $path
432
	 *
433
	 * @return IShare[]
434
	 * @throws InvalidPathException
435
	 * @throws NotFoundException
436
	 * @throws IllegalIDChangeException
437
	 */
438
	public function getSharesByPath(Node $path): array {
439
		OC::$server->getLogger()->log(3, 'CSP > getSharesByPath');
440
		$wrappedShares = $this->shareWrapperService->getSharesByFileId($path->getId());
441
442
		return array_filter(
443
			array_map(
444
				function(ShareWrapper $wrapper) {
445
					return $wrapper->getShare($this->rootFolder, $this->userManager, $this->urlGenerator);
446
				}, $wrappedShares
447
			)
448
		);
449
	}
450
451
452
	/**
453
	 * @param string $userId
454
	 * @param int $shareType
455
	 * @param Node|null $node
456
	 * @param int $limit
457
	 * @param int $offset
458
	 *
459
	 * @return IShare[]
460
	 * @throws FederatedUserException
461
	 * @throws FederatedUserNotFoundException
462
	 * @throws IllegalIDChangeException
463
	 * @throws InvalidIdException
464
	 * @throws InvalidPathException
465
	 * @throws NotFoundException
466
	 * @throws RequestBuilderException
467
	 * @throws SingleCircleNotFoundException
468
	 */
469
	public function getSharedWith($userId, $shareType, $node, $limit, $offset): array {
470
		if ($shareType !== IShare::TYPE_CIRCLE) {
471
			return [];
472
		}
473
474
		$federatedUser = $this->federatedUserService->getLocalFederatedUser($userId);
475
		$wrappedShares = $this->shareWrapperService->getSharedWith(
476
			$federatedUser,
477
			(!is_null($node)) ? $node->getId() : 0,
478
			$limit,
479
			$offset
480
		);
481
482
		return array_filter(
483
			array_map(
484
				function(ShareWrapper $wrapper) {
485
					return $wrapper->getShare($this->rootFolder, $this->userManager, $this->urlGenerator);
486
				}, $wrappedShares
487
			)
488
		);
489
	}
490
491
492
	/**
493
	 * @param string $token
494
	 *
495
	 * @return IShare
496
	 */
497
	public function getShareByToken($token): IShare {
498
		OC::$server->getLogger()->log(3, 'CSP > getShareByToken');
499
	}
500
501
502
	/**
503
	 * @param string $uid
504
	 * @param int $shareType
505
	 */
506
	public function userDeleted($uid, $shareType): void {
507
		OC::$server->getLogger()->log(3, 'CSP > userDeleted');
508
	}
509
510
511
	/**
512
	 * @param string $gid
513
	 */
514
	public function groupDeleted($gid): void {
515
		OC::$server->getLogger()->log(3, 'CSP > groupDeleted');
516
	}
517
518
519
	/**
520
	 * @param string $uid
521
	 * @param string $gid
522
	 */
523
	public function userDeletedFromGroup($uid, $gid): void {
524
		OC::$server->getLogger()->log(3, 'CSP > userDeletedFromGroup');
525
	}
526
527
528
	/**
529
	 * @param Node[] $nodes
530
	 * @param bool $currentAccess
531
	 *
532
	 * @return array
533
	 */
534
	public function getAccessList($nodes, $currentAccess): array {
535
		OC::$server->getLogger()->log(3, 'CSP > getAccessList');
536
537
		return [];
538
	}
539
540
541
	/**
542
	 * @return iterable
543
	 */
544
	public function getAllShares(): iterable {
545
		OC::$server->getLogger()->log(3, 'CSP > getAllShares');
546
547
//		$qb = $this->dbConnection->getQueryBuilder();
548
//
549
//		$qb->select(' * ')
550
//		   ->from('share')
551
//		   ->where(
552
//			   $qb->expr()
553
//				  ->orX(
554
//					  $qb->expr()
555
//						 ->eq('share_type', $qb->createNamedParameter(IShare::TYPE_CIRCLE))
556
//				  )
557
//		   );
558
//
559
//		$cursor = $qb->execute();
560
//		while ($data = $cursor->fetch()) {
561
//			try {
562
//				yield $this->createShareObject($data);
563
//			} catch (IllegalIDChangeException $e) {
564
//			};
565
//		}
566
//		$cursor->closeCursor();
567
	}
568
569
}
570
571