| Total Complexity | 252 |
| Total Lines | 1566 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Manager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Manager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 73 | class Manager implements IManager { |
||
| 74 | |||
| 75 | /** @var IProviderFactory */ |
||
| 76 | private $factory; |
||
| 77 | /** @var ILogger */ |
||
| 78 | private $logger; |
||
| 79 | /** @var IConfig */ |
||
| 80 | private $config; |
||
| 81 | /** @var ISecureRandom */ |
||
| 82 | private $secureRandom; |
||
| 83 | /** @var IHasher */ |
||
| 84 | private $hasher; |
||
| 85 | /** @var IMountManager */ |
||
| 86 | private $mountManager; |
||
| 87 | /** @var IGroupManager */ |
||
| 88 | private $groupManager; |
||
| 89 | /** @var IL10N */ |
||
| 90 | private $l; |
||
| 91 | /** @var IFactory */ |
||
| 92 | private $l10nFactory; |
||
| 93 | /** @var IUserManager */ |
||
| 94 | private $userManager; |
||
| 95 | /** @var IRootFolder */ |
||
| 96 | private $rootFolder; |
||
| 97 | /** @var CappedMemoryCache */ |
||
| 98 | private $sharingDisabledForUsersCache; |
||
| 99 | /** @var EventDispatcher */ |
||
| 100 | private $eventDispatcher; |
||
| 101 | /** @var LegacyHooks */ |
||
| 102 | private $legacyHooks; |
||
| 103 | /** @var IMailer */ |
||
| 104 | private $mailer; |
||
| 105 | /** @var IURLGenerator */ |
||
| 106 | private $urlGenerator; |
||
| 107 | /** @var \OC_Defaults */ |
||
| 108 | private $defaults; |
||
| 109 | |||
| 110 | |||
| 111 | /** |
||
| 112 | * Manager constructor. |
||
| 113 | * |
||
| 114 | * @param ILogger $logger |
||
| 115 | * @param IConfig $config |
||
| 116 | * @param ISecureRandom $secureRandom |
||
| 117 | * @param IHasher $hasher |
||
| 118 | * @param IMountManager $mountManager |
||
| 119 | * @param IGroupManager $groupManager |
||
| 120 | * @param IL10N $l |
||
| 121 | * @param IFactory $l10nFactory |
||
| 122 | * @param IProviderFactory $factory |
||
| 123 | * @param IUserManager $userManager |
||
| 124 | * @param IRootFolder $rootFolder |
||
| 125 | * @param EventDispatcher $eventDispatcher |
||
| 126 | * @param IMailer $mailer |
||
| 127 | * @param IURLGenerator $urlGenerator |
||
| 128 | * @param \OC_Defaults $defaults |
||
| 129 | */ |
||
| 130 | public function __construct( |
||
| 131 | ILogger $logger, |
||
| 132 | IConfig $config, |
||
| 133 | ISecureRandom $secureRandom, |
||
| 134 | IHasher $hasher, |
||
| 135 | IMountManager $mountManager, |
||
| 136 | IGroupManager $groupManager, |
||
| 137 | IL10N $l, |
||
| 138 | IFactory $l10nFactory, |
||
| 139 | IProviderFactory $factory, |
||
| 140 | IUserManager $userManager, |
||
| 141 | IRootFolder $rootFolder, |
||
| 142 | EventDispatcher $eventDispatcher, |
||
| 143 | IMailer $mailer, |
||
| 144 | IURLGenerator $urlGenerator, |
||
| 145 | \OC_Defaults $defaults |
||
| 146 | ) { |
||
| 147 | $this->logger = $logger; |
||
| 148 | $this->config = $config; |
||
| 149 | $this->secureRandom = $secureRandom; |
||
| 150 | $this->hasher = $hasher; |
||
| 151 | $this->mountManager = $mountManager; |
||
| 152 | $this->groupManager = $groupManager; |
||
| 153 | $this->l = $l; |
||
| 154 | $this->l10nFactory = $l10nFactory; |
||
| 155 | $this->factory = $factory; |
||
| 156 | $this->userManager = $userManager; |
||
| 157 | $this->rootFolder = $rootFolder; |
||
| 158 | $this->eventDispatcher = $eventDispatcher; |
||
| 159 | $this->sharingDisabledForUsersCache = new CappedMemoryCache(); |
||
| 160 | $this->legacyHooks = new LegacyHooks($this->eventDispatcher); |
||
| 161 | $this->mailer = $mailer; |
||
| 162 | $this->urlGenerator = $urlGenerator; |
||
| 163 | $this->defaults = $defaults; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Convert from a full share id to a tuple (providerId, shareId) |
||
| 168 | * |
||
| 169 | * @param string $id |
||
| 170 | * @return string[] |
||
| 171 | */ |
||
| 172 | private function splitFullId($id) { |
||
| 173 | return explode(':', $id, 2); |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Verify if a password meets all requirements |
||
| 178 | * |
||
| 179 | * @param string $password |
||
| 180 | * @throws \Exception |
||
| 181 | */ |
||
| 182 | protected function verifyPassword($password) { |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Check for generic requirements before creating a share |
||
| 203 | * |
||
| 204 | * @param \OCP\Share\IShare $share |
||
| 205 | * @throws \InvalidArgumentException |
||
| 206 | * @throws GenericShareException |
||
| 207 | * |
||
| 208 | * @suppress PhanUndeclaredClassMethod |
||
| 209 | */ |
||
| 210 | protected function generalCreateChecks(\OCP\Share\IShare $share) { |
||
| 326 | } |
||
| 327 | } |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Validate if the expiration date fits the system settings |
||
| 332 | * |
||
| 333 | * @param \OCP\Share\IShare $share The share to validate the expiration date of |
||
| 334 | * @return \OCP\Share\IShare The modified share object |
||
| 335 | * @throws GenericShareException |
||
| 336 | * @throws \InvalidArgumentException |
||
| 337 | * @throws \Exception |
||
| 338 | */ |
||
| 339 | protected function validateExpirationDate(\OCP\Share\IShare $share) { |
||
| 340 | |||
| 341 | $expirationDate = $share->getExpirationDate(); |
||
| 342 | |||
| 343 | if ($expirationDate !== null) { |
||
| 344 | //Make sure the expiration date is a date |
||
| 345 | $expirationDate->setTime(0, 0, 0); |
||
| 346 | |||
| 347 | $date = new \DateTime(); |
||
| 348 | $date->setTime(0, 0, 0); |
||
| 349 | if ($date >= $expirationDate) { |
||
| 350 | $message = $this->l->t('Expiration date is in the past'); |
||
| 351 | throw new GenericShareException($message, $message, 404); |
||
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | // If expiredate is empty set a default one if there is a default |
||
| 356 | $fullId = null; |
||
| 357 | try { |
||
| 358 | $fullId = $share->getFullId(); |
||
| 359 | } catch (\UnexpectedValueException $e) { |
||
| 360 | // This is a new share |
||
| 361 | } |
||
| 362 | |||
| 363 | if ($fullId === null && $expirationDate === null && $this->shareApiLinkDefaultExpireDate()) { |
||
| 364 | $expirationDate = new \DateTime(); |
||
| 365 | $expirationDate->setTime(0,0,0); |
||
| 366 | $expirationDate->add(new \DateInterval('P'.$this->shareApiLinkDefaultExpireDays().'D')); |
||
| 367 | } |
||
| 368 | |||
| 369 | // If we enforce the expiration date check that is does not exceed |
||
| 370 | if ($this->shareApiLinkDefaultExpireDateEnforced()) { |
||
| 371 | if ($expirationDate === null) { |
||
| 372 | throw new \InvalidArgumentException('Expiration date is enforced'); |
||
| 373 | } |
||
| 374 | |||
| 375 | $date = new \DateTime(); |
||
| 376 | $date->setTime(0, 0, 0); |
||
| 377 | $date->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D')); |
||
| 378 | if ($date < $expirationDate) { |
||
| 379 | $message = $this->l->t('Can’t set expiration date more than %s days in the future', [$this->shareApiLinkDefaultExpireDays()]); |
||
| 380 | throw new GenericShareException($message, $message, 404); |
||
| 381 | } |
||
| 382 | } |
||
| 383 | |||
| 384 | $accepted = true; |
||
| 385 | $message = ''; |
||
| 386 | \OCP\Util::emitHook('\OC\Share', 'verifyExpirationDate', [ |
||
| 387 | 'expirationDate' => &$expirationDate, |
||
| 388 | 'accepted' => &$accepted, |
||
| 389 | 'message' => &$message, |
||
| 390 | 'passwordSet' => $share->getPassword() !== null, |
||
| 391 | ]); |
||
| 392 | |||
| 393 | if (!$accepted) { |
||
| 394 | throw new \Exception($message); |
||
| 395 | } |
||
| 396 | |||
| 397 | $share->setExpirationDate($expirationDate); |
||
| 398 | |||
| 399 | return $share; |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Check for pre share requirements for user shares |
||
| 404 | * |
||
| 405 | * @param \OCP\Share\IShare $share |
||
| 406 | * @throws \Exception |
||
| 407 | */ |
||
| 408 | protected function userCreateChecks(\OCP\Share\IShare $share) { |
||
| 453 | } |
||
| 454 | } |
||
| 455 | } |
||
| 456 | } |
||
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Check for pre share requirements for group shares |
||
| 461 | * |
||
| 462 | * @param \OCP\Share\IShare $share |
||
| 463 | * @throws \Exception |
||
| 464 | */ |
||
| 465 | protected function groupCreateChecks(\OCP\Share\IShare $share) { |
||
| 466 | // Verify group shares are allowed |
||
| 467 | if (!$this->allowGroupSharing()) { |
||
| 468 | throw new \Exception('Group sharing is now allowed'); |
||
| 469 | } |
||
| 470 | |||
| 471 | // Verify if the user can share with this group |
||
| 472 | if ($this->shareWithGroupMembersOnly()) { |
||
| 473 | $sharedBy = $this->userManager->get($share->getSharedBy()); |
||
| 474 | $sharedWith = $this->groupManager->get($share->getSharedWith()); |
||
| 475 | if (is_null($sharedWith) || !$sharedWith->inGroup($sharedBy)) { |
||
| 476 | throw new \Exception('Sharing is only allowed within your own groups'); |
||
| 477 | } |
||
| 478 | } |
||
| 479 | |||
| 480 | /* |
||
| 481 | * TODO: Could be costly, fix |
||
| 482 | * |
||
| 483 | * Also this is not what we want in the future.. then we want to squash identical shares. |
||
| 484 | */ |
||
| 485 | $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP); |
||
| 486 | $existingShares = $provider->getSharesByPath($share->getNode()); |
||
| 487 | foreach($existingShares as $existingShare) { |
||
| 488 | try { |
||
| 489 | if ($existingShare->getFullId() === $share->getFullId()) { |
||
| 490 | continue; |
||
| 491 | } |
||
| 492 | } catch (\UnexpectedValueException $e) { |
||
| 493 | //It is a new share so just continue |
||
| 494 | } |
||
| 495 | |||
| 496 | if ($existingShare->getSharedWith() === $share->getSharedWith()) { |
||
| 497 | throw new \Exception('Path is already shared with this group'); |
||
| 498 | } |
||
| 499 | } |
||
| 500 | } |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Check for pre share requirements for link shares |
||
| 504 | * |
||
| 505 | * @param \OCP\Share\IShare $share |
||
| 506 | * @throws \Exception |
||
| 507 | */ |
||
| 508 | protected function linkCreateChecks(\OCP\Share\IShare $share) { |
||
| 509 | // Are link shares allowed? |
||
| 510 | if (!$this->shareApiAllowLinks()) { |
||
| 511 | throw new \Exception('Link sharing is not allowed'); |
||
| 512 | } |
||
| 513 | |||
| 514 | // Link shares by definition can't have share permissions |
||
| 515 | if ($share->getPermissions() & \OCP\Constants::PERMISSION_SHARE) { |
||
| 516 | throw new \InvalidArgumentException('Link shares can’t have reshare permissions'); |
||
| 517 | } |
||
| 518 | |||
| 519 | // Check if public upload is allowed |
||
| 520 | if (!$this->shareApiLinkAllowPublicUpload() && |
||
| 521 | ($share->getPermissions() & (\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE))) { |
||
| 522 | throw new \InvalidArgumentException('Public upload is not allowed'); |
||
| 523 | } |
||
| 524 | } |
||
| 525 | |||
| 526 | /** |
||
| 527 | * To make sure we don't get invisible link shares we set the parent |
||
| 528 | * of a link if it is a reshare. This is a quick word around |
||
| 529 | * until we can properly display multiple link shares in the UI |
||
| 530 | * |
||
| 531 | * See: https://github.com/owncloud/core/issues/22295 |
||
| 532 | * |
||
| 533 | * FIXME: Remove once multiple link shares can be properly displayed |
||
| 534 | * |
||
| 535 | * @param \OCP\Share\IShare $share |
||
| 536 | */ |
||
| 537 | protected function setLinkParent(\OCP\Share\IShare $share) { |
||
| 538 | |||
| 539 | // No sense in checking if the method is not there. |
||
| 540 | if (method_exists($share, 'setParent')) { |
||
| 541 | $storage = $share->getNode()->getStorage(); |
||
| 542 | if ($storage->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) { |
||
| 543 | /** @var \OCA\Files_Sharing\SharedStorage $storage */ |
||
| 544 | $share->setParent($storage->getShareId()); |
||
| 545 | } |
||
| 546 | } |
||
| 547 | } |
||
| 548 | |||
| 549 | /** |
||
| 550 | * @param File|Folder $path |
||
| 551 | */ |
||
| 552 | protected function pathCreateChecks($path) { |
||
| 559 | } |
||
| 560 | } |
||
| 561 | } |
||
| 562 | } |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Check if the user that is sharing can actually share |
||
| 566 | * |
||
| 567 | * @param \OCP\Share\IShare $share |
||
| 568 | * @throws \Exception |
||
| 569 | */ |
||
| 570 | protected function canShare(\OCP\Share\IShare $share) { |
||
| 577 | } |
||
| 578 | } |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Share a path |
||
| 582 | * |
||
| 583 | * @param \OCP\Share\IShare $share |
||
| 584 | * @return Share The share object |
||
| 585 | * @throws \Exception |
||
| 586 | * |
||
| 587 | * TODO: handle link share permissions or check them |
||
| 588 | */ |
||
| 589 | public function createShare(\OCP\Share\IShare $share) { |
||
| 708 | } |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Send mail notifications |
||
| 712 | * |
||
| 713 | * This method will catch and log mail transmission errors |
||
| 714 | * |
||
| 715 | * @param IL10N $l Language of the recipient |
||
| 716 | * @param string $filename file/folder name |
||
| 717 | * @param string $link link to the file/folder |
||
| 718 | * @param string $initiator user ID of share sender |
||
| 719 | * @param string $shareWith email address of share receiver |
||
| 720 | * @param \DateTime|null $expiration |
||
| 721 | */ |
||
| 722 | protected function sendMailNotification(IL10N $l, |
||
| 723 | $filename, |
||
| 724 | $link, |
||
| 725 | $initiator, |
||
| 726 | $shareWith, |
||
| 727 | \DateTime $expiration = null) { |
||
| 728 | $initiatorUser = $this->userManager->get($initiator); |
||
| 729 | $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator; |
||
| 730 | |||
| 731 | $message = $this->mailer->createMessage(); |
||
| 732 | |||
| 733 | $emailTemplate = $this->mailer->createEMailTemplate('files_sharing.RecipientNotification', [ |
||
| 734 | 'filename' => $filename, |
||
| 735 | 'link' => $link, |
||
| 736 | 'initiator' => $initiatorDisplayName, |
||
| 737 | 'expiration' => $expiration, |
||
| 738 | 'shareWith' => $shareWith, |
||
| 739 | ]); |
||
| 740 | |||
| 741 | $emailTemplate->setSubject($l->t('%1$s shared »%2$s« with you', array($initiatorDisplayName, $filename))); |
||
| 742 | $emailTemplate->addHeader(); |
||
| 743 | $emailTemplate->addHeading($l->t('%1$s shared »%2$s« with you', [$initiatorDisplayName, $filename]), false); |
||
| 744 | $text = $l->t('%1$s shared »%2$s« with you.', [$initiatorDisplayName, $filename]); |
||
| 745 | |||
| 746 | $emailTemplate->addBodyText( |
||
| 747 | htmlspecialchars($text . ' ' . $l->t('Click the button below to open it.')), |
||
| 748 | $text |
||
| 749 | ); |
||
| 750 | $emailTemplate->addBodyButton( |
||
| 751 | $l->t('Open »%s«', [$filename]), |
||
| 752 | $link |
||
| 753 | ); |
||
| 754 | |||
| 755 | $message->setTo([$shareWith]); |
||
| 756 | |||
| 757 | // The "From" contains the sharers name |
||
| 758 | $instanceName = $this->defaults->getName(); |
||
| 759 | $senderName = $l->t( |
||
| 760 | '%1$s via %2$s', |
||
| 761 | [ |
||
| 762 | $initiatorDisplayName, |
||
| 763 | $instanceName |
||
| 764 | ] |
||
| 765 | ); |
||
| 766 | $message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]); |
||
| 767 | |||
| 768 | // The "Reply-To" is set to the sharer if an mail address is configured |
||
| 769 | // also the default footer contains a "Do not reply" which needs to be adjusted. |
||
| 770 | $initiatorEmail = $initiatorUser->getEMailAddress(); |
||
| 771 | if($initiatorEmail !== null) { |
||
| 772 | $message->setReplyTo([$initiatorEmail => $initiatorDisplayName]); |
||
| 773 | $emailTemplate->addFooter($instanceName . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : '')); |
||
| 774 | } else { |
||
| 775 | $emailTemplate->addFooter(); |
||
| 776 | } |
||
| 777 | |||
| 778 | $message->useTemplate($emailTemplate); |
||
| 779 | try { |
||
| 780 | $failedRecipients = $this->mailer->send($message); |
||
| 781 | if(!empty($failedRecipients)) { |
||
| 782 | $this->logger->error('Share notification mail could not be sent to: ' . implode(', ', $failedRecipients)); |
||
| 783 | return; |
||
| 784 | } |
||
| 785 | } catch (\Exception $e) { |
||
| 786 | $this->logger->logException($e, ['message' => 'Share notification mail could not be sent']); |
||
| 787 | } |
||
| 788 | } |
||
| 789 | |||
| 790 | /** |
||
| 791 | * Update a share |
||
| 792 | * |
||
| 793 | * @param \OCP\Share\IShare $share |
||
| 794 | * @return \OCP\Share\IShare The share object |
||
| 795 | * @throws \InvalidArgumentException |
||
| 796 | */ |
||
| 797 | public function updateShare(\OCP\Share\IShare $share) { |
||
| 798 | $expirationDateUpdated = false; |
||
| 799 | |||
| 800 | $this->canShare($share); |
||
| 801 | |||
| 802 | try { |
||
| 803 | $originalShare = $this->getShareById($share->getFullId()); |
||
| 804 | } catch (\UnexpectedValueException $e) { |
||
| 805 | throw new \InvalidArgumentException('Share does not have a full id'); |
||
| 806 | } |
||
| 807 | |||
| 808 | // We can't change the share type! |
||
| 809 | if ($share->getShareType() !== $originalShare->getShareType()) { |
||
| 810 | throw new \InvalidArgumentException('Can’t change share type'); |
||
| 811 | } |
||
| 812 | |||
| 813 | // We can only change the recipient on user shares |
||
| 814 | if ($share->getSharedWith() !== $originalShare->getSharedWith() && |
||
| 815 | $share->getShareType() !== \OCP\Share::SHARE_TYPE_USER) { |
||
| 816 | throw new \InvalidArgumentException('Can only update recipient on user shares'); |
||
| 817 | } |
||
| 818 | |||
| 819 | // Cannot share with the owner |
||
| 820 | if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && |
||
| 821 | $share->getSharedWith() === $share->getShareOwner()) { |
||
| 822 | throw new \InvalidArgumentException('Can’t share with the share owner'); |
||
| 823 | } |
||
| 824 | |||
| 825 | $this->generalCreateChecks($share); |
||
| 826 | |||
| 827 | if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
||
| 828 | $this->userCreateChecks($share); |
||
| 829 | } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
||
| 830 | $this->groupCreateChecks($share); |
||
| 831 | } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
||
| 832 | $this->linkCreateChecks($share); |
||
| 833 | |||
| 834 | $this->updateSharePasswordIfNeeded($share, $originalShare); |
||
| 835 | |||
| 836 | if ($share->getExpirationDate() != $originalShare->getExpirationDate()) { |
||
| 837 | //Verify the expiration date |
||
| 838 | $this->validateExpirationDate($share); |
||
| 839 | $expirationDateUpdated = true; |
||
| 840 | } |
||
| 841 | } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { |
||
| 842 | // The new password is not set again if it is the same as the old |
||
| 843 | // one, unless when switching from sending by Talk to sending by |
||
| 844 | // mail. |
||
| 845 | $plainTextPassword = $share->getPassword(); |
||
| 846 | if (!empty($plainTextPassword) && !$this->updateSharePasswordIfNeeded($share, $originalShare) && |
||
| 847 | !($originalShare->getSendPasswordByTalk() && !$share->getSendPasswordByTalk())) { |
||
| 848 | $plainTextPassword = null; |
||
| 849 | } |
||
| 850 | if (empty($plainTextPassword) && !$originalShare->getSendPasswordByTalk() && $share->getSendPasswordByTalk()) { |
||
| 851 | // If the same password was already sent by mail the recipient |
||
| 852 | // would already have access to the share without having to call |
||
| 853 | // the sharer to verify her identity |
||
| 854 | throw new \InvalidArgumentException('Can’t enable sending the password by Talk without setting a new password'); |
||
| 855 | } |
||
| 856 | } |
||
| 857 | |||
| 858 | $this->pathCreateChecks($share->getNode()); |
||
| 859 | |||
| 860 | // Now update the share! |
||
| 861 | $provider = $this->factory->getProviderForType($share->getShareType()); |
||
| 862 | if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { |
||
| 863 | $share = $provider->update($share, $plainTextPassword); |
||
| 864 | } else { |
||
| 865 | $share = $provider->update($share); |
||
| 866 | } |
||
| 867 | |||
| 868 | if ($expirationDateUpdated === true) { |
||
| 869 | \OC_Hook::emit(Share::class, 'post_set_expiration_date', [ |
||
| 870 | 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', |
||
| 871 | 'itemSource' => $share->getNode()->getId(), |
||
| 872 | 'date' => $share->getExpirationDate(), |
||
| 873 | 'uidOwner' => $share->getSharedBy(), |
||
| 874 | ]); |
||
| 875 | } |
||
| 876 | |||
| 877 | if ($share->getPassword() !== $originalShare->getPassword()) { |
||
| 878 | \OC_Hook::emit(Share::class, 'post_update_password', [ |
||
| 879 | 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', |
||
| 880 | 'itemSource' => $share->getNode()->getId(), |
||
| 881 | 'uidOwner' => $share->getSharedBy(), |
||
| 882 | 'token' => $share->getToken(), |
||
| 883 | 'disabled' => is_null($share->getPassword()), |
||
| 884 | ]); |
||
| 885 | } |
||
| 886 | |||
| 887 | if ($share->getPermissions() !== $originalShare->getPermissions()) { |
||
| 888 | if ($this->userManager->userExists($share->getShareOwner())) { |
||
| 889 | $userFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); |
||
| 890 | } else { |
||
| 891 | $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); |
||
| 892 | } |
||
| 893 | \OC_Hook::emit(Share::class, 'post_update_permissions', array( |
||
| 894 | 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', |
||
| 895 | 'itemSource' => $share->getNode()->getId(), |
||
| 896 | 'shareType' => $share->getShareType(), |
||
| 897 | 'shareWith' => $share->getSharedWith(), |
||
| 898 | 'uidOwner' => $share->getSharedBy(), |
||
| 899 | 'permissions' => $share->getPermissions(), |
||
| 900 | 'path' => $userFolder->getRelativePath($share->getNode()->getPath()), |
||
| 901 | )); |
||
| 902 | } |
||
| 903 | |||
| 904 | return $share; |
||
| 905 | } |
||
| 906 | |||
| 907 | /** |
||
| 908 | * Updates the password of the given share if it is not the same as the |
||
| 909 | * password of the original share. |
||
| 910 | * |
||
| 911 | * @param \OCP\Share\IShare $share the share to update its password. |
||
| 912 | * @param \OCP\Share\IShare $originalShare the original share to compare its |
||
| 913 | * password with. |
||
| 914 | * @return boolean whether the password was updated or not. |
||
| 915 | */ |
||
| 916 | private function updateSharePasswordIfNeeded(\OCP\Share\IShare $share, \OCP\Share\IShare $originalShare) { |
||
| 917 | // Password updated. |
||
| 918 | if ($share->getPassword() !== $originalShare->getPassword()) { |
||
| 919 | //Verify the password |
||
| 920 | $this->verifyPassword($share->getPassword()); |
||
| 921 | |||
| 922 | // If a password is set. Hash it! |
||
| 923 | if ($share->getPassword() !== null) { |
||
| 924 | $share->setPassword($this->hasher->hash($share->getPassword())); |
||
| 925 | |||
| 926 | return true; |
||
| 927 | } |
||
| 928 | } |
||
| 929 | |||
| 930 | return false; |
||
| 931 | } |
||
| 932 | |||
| 933 | /** |
||
| 934 | * Delete all the children of this share |
||
| 935 | * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in |
||
| 936 | * |
||
| 937 | * @param \OCP\Share\IShare $share |
||
| 938 | * @return \OCP\Share\IShare[] List of deleted shares |
||
| 939 | */ |
||
| 940 | protected function deleteChildren(\OCP\Share\IShare $share) { |
||
| 941 | $deletedShares = []; |
||
| 942 | |||
| 943 | $provider = $this->factory->getProviderForType($share->getShareType()); |
||
| 944 | |||
| 945 | foreach ($provider->getChildren($share) as $child) { |
||
| 946 | $deletedChildren = $this->deleteChildren($child); |
||
| 947 | $deletedShares = array_merge($deletedShares, $deletedChildren); |
||
| 948 | |||
| 949 | $provider->delete($child); |
||
| 950 | $deletedShares[] = $child; |
||
| 951 | } |
||
| 952 | |||
| 953 | return $deletedShares; |
||
| 954 | } |
||
| 955 | |||
| 956 | /** |
||
| 957 | * Delete a share |
||
| 958 | * |
||
| 959 | * @param \OCP\Share\IShare $share |
||
| 960 | * @throws ShareNotFound |
||
| 961 | * @throws \InvalidArgumentException |
||
| 962 | */ |
||
| 963 | public function deleteShare(\OCP\Share\IShare $share) { |
||
| 964 | |||
| 965 | try { |
||
| 966 | $share->getFullId(); |
||
| 967 | } catch (\UnexpectedValueException $e) { |
||
| 968 | throw new \InvalidArgumentException('Share does not have a full id'); |
||
| 969 | } |
||
| 970 | |||
| 971 | $event = new GenericEvent($share); |
||
| 972 | $this->eventDispatcher->dispatch('OCP\Share::preUnshare', $event); |
||
| 973 | |||
| 974 | // Get all children and delete them as well |
||
| 975 | $deletedShares = $this->deleteChildren($share); |
||
| 976 | |||
| 977 | // Do the actual delete |
||
| 978 | $provider = $this->factory->getProviderForType($share->getShareType()); |
||
| 979 | $provider->delete($share); |
||
| 980 | |||
| 981 | // All the deleted shares caused by this delete |
||
| 982 | $deletedShares[] = $share; |
||
| 983 | |||
| 984 | // Emit post hook |
||
| 985 | $event->setArgument('deletedShares', $deletedShares); |
||
| 986 | $this->eventDispatcher->dispatch('OCP\Share::postUnshare', $event); |
||
| 987 | } |
||
| 988 | |||
| 989 | |||
| 990 | /** |
||
| 991 | * Unshare a file as the recipient. |
||
| 992 | * This can be different from a regular delete for example when one of |
||
| 993 | * the users in a groups deletes that share. But the provider should |
||
| 994 | * handle this. |
||
| 995 | * |
||
| 996 | * @param \OCP\Share\IShare $share |
||
| 997 | * @param string $recipientId |
||
| 998 | */ |
||
| 999 | public function deleteFromSelf(\OCP\Share\IShare $share, $recipientId) { |
||
| 1000 | list($providerId, ) = $this->splitFullId($share->getFullId()); |
||
| 1001 | $provider = $this->factory->getProvider($providerId); |
||
| 1002 | |||
| 1003 | $provider->deleteFromSelf($share, $recipientId); |
||
| 1004 | $event = new GenericEvent($share); |
||
| 1005 | $this->eventDispatcher->dispatch('OCP\Share::postUnshareFromSelf', $event); |
||
| 1006 | } |
||
| 1007 | |||
| 1008 | public function restoreShare(IShare $share, string $recipientId): IShare { |
||
| 1009 | list($providerId, ) = $this->splitFullId($share->getFullId()); |
||
| 1010 | $provider = $this->factory->getProvider($providerId); |
||
| 1011 | |||
| 1012 | return $provider->restore($share, $recipientId); |
||
| 1013 | } |
||
| 1014 | |||
| 1015 | /** |
||
| 1016 | * @inheritdoc |
||
| 1017 | */ |
||
| 1018 | public function moveShare(\OCP\Share\IShare $share, $recipientId) { |
||
| 1019 | if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
||
| 1020 | throw new \InvalidArgumentException('Can’t change target of link share'); |
||
| 1021 | } |
||
| 1022 | |||
| 1023 | if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && $share->getSharedWith() !== $recipientId) { |
||
| 1024 | throw new \InvalidArgumentException('Invalid recipient'); |
||
| 1025 | } |
||
| 1026 | |||
| 1027 | if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
||
| 1028 | $sharedWith = $this->groupManager->get($share->getSharedWith()); |
||
| 1029 | if (is_null($sharedWith)) { |
||
| 1030 | throw new \InvalidArgumentException('Group "' . $share->getSharedWith() . '" does not exist'); |
||
| 1031 | } |
||
| 1032 | $recipient = $this->userManager->get($recipientId); |
||
| 1033 | if (!$sharedWith->inGroup($recipient)) { |
||
| 1034 | throw new \InvalidArgumentException('Invalid recipient'); |
||
| 1035 | } |
||
| 1036 | } |
||
| 1037 | |||
| 1038 | list($providerId, ) = $this->splitFullId($share->getFullId()); |
||
| 1039 | $provider = $this->factory->getProvider($providerId); |
||
| 1040 | |||
| 1041 | $provider->move($share, $recipientId); |
||
| 1042 | } |
||
| 1043 | |||
| 1044 | public function getSharesInFolder($userId, Folder $node, $reshares = false) { |
||
| 1045 | $providers = $this->factory->getAllProviders(); |
||
| 1046 | |||
| 1047 | return array_reduce($providers, function($shares, IShareProvider $provider) use ($userId, $node, $reshares) { |
||
| 1048 | $newShares = $provider->getSharesInFolder($userId, $node, $reshares); |
||
| 1049 | foreach ($newShares as $fid => $data) { |
||
| 1050 | if (!isset($shares[$fid])) { |
||
| 1051 | $shares[$fid] = []; |
||
| 1052 | } |
||
| 1053 | |||
| 1054 | $shares[$fid] = array_merge($shares[$fid], $data); |
||
| 1055 | } |
||
| 1056 | return $shares; |
||
| 1057 | }, []); |
||
| 1058 | } |
||
| 1059 | |||
| 1060 | /** |
||
| 1061 | * @inheritdoc |
||
| 1062 | */ |
||
| 1063 | public function getSharesBy($userId, $shareType, $path = null, $reshares = false, $limit = 50, $offset = 0) { |
||
| 1064 | if ($path !== null && |
||
| 1065 | !($path instanceof \OCP\Files\File) && |
||
| 1066 | !($path instanceof \OCP\Files\Folder)) { |
||
| 1067 | throw new \InvalidArgumentException('invalid path'); |
||
| 1068 | } |
||
| 1069 | |||
| 1070 | try { |
||
| 1071 | $provider = $this->factory->getProviderForType($shareType); |
||
| 1072 | } catch (ProviderException $e) { |
||
| 1073 | return []; |
||
| 1074 | } |
||
| 1075 | |||
| 1076 | $shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset); |
||
| 1077 | |||
| 1078 | /* |
||
| 1079 | * Work around so we don't return expired shares but still follow |
||
| 1080 | * proper pagination. |
||
| 1081 | */ |
||
| 1082 | |||
| 1083 | $shares2 = []; |
||
| 1084 | |||
| 1085 | while(true) { |
||
| 1086 | $added = 0; |
||
| 1087 | foreach ($shares as $share) { |
||
| 1088 | |||
| 1089 | try { |
||
| 1090 | $this->checkExpireDate($share); |
||
| 1091 | } catch (ShareNotFound $e) { |
||
| 1092 | //Ignore since this basically means the share is deleted |
||
| 1093 | continue; |
||
| 1094 | } |
||
| 1095 | |||
| 1096 | $added++; |
||
| 1097 | $shares2[] = $share; |
||
| 1098 | |||
| 1099 | if (count($shares2) === $limit) { |
||
| 1100 | break; |
||
| 1101 | } |
||
| 1102 | } |
||
| 1103 | |||
| 1104 | // If we did not fetch more shares than the limit then there are no more shares |
||
| 1105 | if (count($shares) < $limit) { |
||
| 1106 | break; |
||
| 1107 | } |
||
| 1108 | |||
| 1109 | if (count($shares2) === $limit) { |
||
| 1110 | break; |
||
| 1111 | } |
||
| 1112 | |||
| 1113 | // If there was no limit on the select we are done |
||
| 1114 | if ($limit === -1) { |
||
| 1115 | break; |
||
| 1116 | } |
||
| 1117 | |||
| 1118 | $offset += $added; |
||
| 1119 | |||
| 1120 | // Fetch again $limit shares |
||
| 1121 | $shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset); |
||
| 1122 | |||
| 1123 | // No more shares means we are done |
||
| 1124 | if (empty($shares)) { |
||
| 1125 | break; |
||
| 1126 | } |
||
| 1127 | } |
||
| 1128 | |||
| 1129 | $shares = $shares2; |
||
| 1130 | |||
| 1131 | return $shares; |
||
| 1132 | } |
||
| 1133 | |||
| 1134 | /** |
||
| 1135 | * @inheritdoc |
||
| 1136 | */ |
||
| 1137 | public function getSharedWith($userId, $shareType, $node = null, $limit = 50, $offset = 0) { |
||
| 1138 | try { |
||
| 1139 | $provider = $this->factory->getProviderForType($shareType); |
||
| 1140 | } catch (ProviderException $e) { |
||
| 1141 | return []; |
||
| 1142 | } |
||
| 1143 | |||
| 1144 | $shares = $provider->getSharedWith($userId, $shareType, $node, $limit, $offset); |
||
| 1145 | |||
| 1146 | // remove all shares which are already expired |
||
| 1147 | foreach ($shares as $key => $share) { |
||
| 1148 | try { |
||
| 1149 | $this->checkExpireDate($share); |
||
| 1150 | } catch (ShareNotFound $e) { |
||
| 1151 | unset($shares[$key]); |
||
| 1152 | } |
||
| 1153 | } |
||
| 1154 | |||
| 1155 | return $shares; |
||
| 1156 | } |
||
| 1157 | |||
| 1158 | /** |
||
| 1159 | * @inheritdoc |
||
| 1160 | */ |
||
| 1161 | public function getDeletedSharedWith($userId, $shareType, $node = null, $limit = 50, $offset = 0) { |
||
| 1162 | $shares = $this->getSharedWith($userId, $shareType, $node, $limit, $offset); |
||
| 1163 | |||
| 1164 | // Only get deleted shares |
||
| 1165 | $shares = array_filter($shares, function(IShare $share) { |
||
| 1166 | return $share->getPermissions() === 0; |
||
| 1167 | }); |
||
| 1168 | |||
| 1169 | // Only get shares where the owner still exists |
||
| 1170 | $shares = array_filter($shares, function (IShare $share) { |
||
| 1171 | return $this->userManager->userExists($share->getShareOwner()); |
||
| 1172 | }); |
||
| 1173 | |||
| 1174 | return $shares; |
||
| 1175 | } |
||
| 1176 | |||
| 1177 | /** |
||
| 1178 | * @inheritdoc |
||
| 1179 | */ |
||
| 1180 | public function getShareById($id, $recipient = null) { |
||
| 1181 | if ($id === null) { |
||
| 1182 | throw new ShareNotFound(); |
||
| 1183 | } |
||
| 1184 | |||
| 1185 | list($providerId, $id) = $this->splitFullId($id); |
||
| 1186 | |||
| 1187 | try { |
||
| 1188 | $provider = $this->factory->getProvider($providerId); |
||
| 1189 | } catch (ProviderException $e) { |
||
| 1190 | throw new ShareNotFound(); |
||
| 1191 | } |
||
| 1192 | |||
| 1193 | $share = $provider->getShareById($id, $recipient); |
||
| 1194 | |||
| 1195 | $this->checkExpireDate($share); |
||
| 1196 | |||
| 1197 | return $share; |
||
| 1198 | } |
||
| 1199 | |||
| 1200 | /** |
||
| 1201 | * Get all the shares for a given path |
||
| 1202 | * |
||
| 1203 | * @param \OCP\Files\Node $path |
||
| 1204 | * @param int $page |
||
| 1205 | * @param int $perPage |
||
| 1206 | * |
||
| 1207 | * @return Share[] |
||
| 1208 | */ |
||
| 1209 | public function getSharesByPath(\OCP\Files\Node $path, $page=0, $perPage=50) { |
||
| 1211 | } |
||
| 1212 | |||
| 1213 | /** |
||
| 1214 | * Get the share by token possible with password |
||
| 1215 | * |
||
| 1216 | * @param string $token |
||
| 1217 | * @return Share |
||
| 1218 | * |
||
| 1219 | * @throws ShareNotFound |
||
| 1220 | */ |
||
| 1221 | public function getShareByToken($token) { |
||
| 1222 | // tokens can't be valid local user names |
||
| 1223 | if ($this->userManager->userExists($token)) { |
||
| 1224 | throw new ShareNotFound(); |
||
| 1225 | } |
||
| 1226 | $share = null; |
||
| 1227 | try { |
||
| 1228 | if($this->shareApiAllowLinks()) { |
||
| 1229 | $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_LINK); |
||
| 1230 | $share = $provider->getShareByToken($token); |
||
| 1231 | } |
||
| 1232 | } catch (ProviderException $e) { |
||
| 1233 | } catch (ShareNotFound $e) { |
||
| 1234 | } |
||
| 1235 | |||
| 1236 | |||
| 1237 | // If it is not a link share try to fetch a federated share by token |
||
| 1238 | if ($share === null) { |
||
| 1239 | try { |
||
| 1240 | $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_REMOTE); |
||
| 1241 | $share = $provider->getShareByToken($token); |
||
| 1242 | } catch (ProviderException $e) { |
||
| 1243 | } catch (ShareNotFound $e) { |
||
| 1244 | } |
||
| 1245 | } |
||
| 1246 | |||
| 1247 | // If it is not a link share try to fetch a mail share by token |
||
| 1248 | if ($share === null && $this->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) { |
||
| 1249 | try { |
||
| 1250 | $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_EMAIL); |
||
| 1251 | $share = $provider->getShareByToken($token); |
||
| 1252 | } catch (ProviderException $e) { |
||
| 1253 | } catch (ShareNotFound $e) { |
||
| 1254 | } |
||
| 1255 | } |
||
| 1256 | |||
| 1257 | if ($share === null && $this->shareProviderExists(\OCP\Share::SHARE_TYPE_CIRCLE)) { |
||
| 1258 | try { |
||
| 1259 | $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_CIRCLE); |
||
| 1260 | $share = $provider->getShareByToken($token); |
||
| 1261 | } catch (ProviderException $e) { |
||
| 1262 | } catch (ShareNotFound $e) { |
||
| 1263 | } |
||
| 1264 | } |
||
| 1265 | |||
| 1266 | if ($share === null && $this->shareProviderExists(\OCP\Share::SHARE_TYPE_ROOM)) { |
||
| 1267 | try { |
||
| 1268 | $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_ROOM); |
||
| 1269 | $share = $provider->getShareByToken($token); |
||
| 1270 | } catch (ProviderException $e) { |
||
| 1271 | } catch (ShareNotFound $e) { |
||
| 1272 | } |
||
| 1273 | } |
||
| 1274 | |||
| 1275 | if ($share === null) { |
||
| 1276 | throw new ShareNotFound($this->l->t('The requested share does not exist anymore')); |
||
| 1277 | } |
||
| 1278 | |||
| 1279 | $this->checkExpireDate($share); |
||
| 1280 | |||
| 1281 | /* |
||
| 1282 | * Reduce the permissions for link shares if public upload is not enabled |
||
| 1283 | */ |
||
| 1284 | if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK && |
||
| 1285 | !$this->shareApiLinkAllowPublicUpload()) { |
||
| 1286 | $share->setPermissions($share->getPermissions() & ~(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE)); |
||
| 1287 | } |
||
| 1288 | |||
| 1289 | return $share; |
||
| 1290 | } |
||
| 1291 | |||
| 1292 | protected function checkExpireDate($share) { |
||
| 1293 | if ($share->getExpirationDate() !== null && |
||
| 1294 | $share->getExpirationDate() <= new \DateTime()) { |
||
| 1295 | $this->deleteShare($share); |
||
| 1296 | throw new ShareNotFound($this->l->t('The requested share does not exist anymore')); |
||
| 1297 | } |
||
| 1298 | |||
| 1299 | } |
||
| 1300 | |||
| 1301 | /** |
||
| 1302 | * Verify the password of a public share |
||
| 1303 | * |
||
| 1304 | * @param \OCP\Share\IShare $share |
||
| 1305 | * @param string $password |
||
| 1306 | * @return bool |
||
| 1307 | */ |
||
| 1308 | public function checkPassword(\OCP\Share\IShare $share, $password) { |
||
| 1309 | $passwordProtected = $share->getShareType() !== \OCP\Share::SHARE_TYPE_LINK |
||
| 1310 | || $share->getShareType() !== \OCP\Share::SHARE_TYPE_EMAIL; |
||
| 1311 | if (!$passwordProtected) { |
||
| 1312 | //TODO maybe exception? |
||
| 1313 | return false; |
||
| 1314 | } |
||
| 1315 | |||
| 1316 | if ($password === null || $share->getPassword() === null) { |
||
| 1317 | return false; |
||
| 1318 | } |
||
| 1319 | |||
| 1320 | $newHash = ''; |
||
| 1321 | if (!$this->hasher->verify($password, $share->getPassword(), $newHash)) { |
||
| 1322 | return false; |
||
| 1323 | } |
||
| 1324 | |||
| 1325 | if (!empty($newHash)) { |
||
| 1326 | $share->setPassword($newHash); |
||
| 1327 | $provider = $this->factory->getProviderForType($share->getShareType()); |
||
| 1328 | $provider->update($share); |
||
| 1329 | } |
||
| 1330 | |||
| 1331 | return true; |
||
| 1332 | } |
||
| 1333 | |||
| 1334 | /** |
||
| 1335 | * @inheritdoc |
||
| 1336 | */ |
||
| 1337 | public function userDeleted($uid) { |
||
| 1338 | $types = [\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_LINK, \OCP\Share::SHARE_TYPE_REMOTE, \OCP\Share::SHARE_TYPE_EMAIL]; |
||
| 1339 | |||
| 1340 | foreach ($types as $type) { |
||
| 1341 | try { |
||
| 1342 | $provider = $this->factory->getProviderForType($type); |
||
| 1343 | } catch (ProviderException $e) { |
||
| 1344 | continue; |
||
| 1345 | } |
||
| 1346 | $provider->userDeleted($uid, $type); |
||
| 1347 | } |
||
| 1348 | } |
||
| 1349 | |||
| 1350 | /** |
||
| 1351 | * @inheritdoc |
||
| 1352 | */ |
||
| 1353 | public function groupDeleted($gid) { |
||
| 1354 | $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP); |
||
| 1355 | $provider->groupDeleted($gid); |
||
| 1356 | } |
||
| 1357 | |||
| 1358 | /** |
||
| 1359 | * @inheritdoc |
||
| 1360 | */ |
||
| 1361 | public function userDeletedFromGroup($uid, $gid) { |
||
| 1362 | $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP); |
||
| 1363 | $provider->userDeletedFromGroup($uid, $gid); |
||
| 1364 | } |
||
| 1365 | |||
| 1366 | /** |
||
| 1367 | * Get access list to a path. This means |
||
| 1368 | * all the users that can access a given path. |
||
| 1369 | * |
||
| 1370 | * Consider: |
||
| 1371 | * -root |
||
| 1372 | * |-folder1 (23) |
||
| 1373 | * |-folder2 (32) |
||
| 1374 | * |-fileA (42) |
||
| 1375 | * |
||
| 1376 | * fileA is shared with user1 and user1@server1 |
||
| 1377 | * folder2 is shared with group2 (user4 is a member of group2) |
||
| 1378 | * folder1 is shared with user2 (renamed to "folder (1)") and user2@server2 |
||
| 1379 | * |
||
| 1380 | * Then the access list to '/folder1/folder2/fileA' with $currentAccess is: |
||
| 1381 | * [ |
||
| 1382 | * users => [ |
||
| 1383 | * 'user1' => ['node_id' => 42, 'node_path' => '/fileA'], |
||
| 1384 | * 'user4' => ['node_id' => 32, 'node_path' => '/folder2'], |
||
| 1385 | * 'user2' => ['node_id' => 23, 'node_path' => '/folder (1)'], |
||
| 1386 | * ], |
||
| 1387 | * remote => [ |
||
| 1388 | * 'user1@server1' => ['node_id' => 42, 'token' => 'SeCr3t'], |
||
| 1389 | * 'user2@server2' => ['node_id' => 23, 'token' => 'FooBaR'], |
||
| 1390 | * ], |
||
| 1391 | * public => bool |
||
| 1392 | * mail => bool |
||
| 1393 | * ] |
||
| 1394 | * |
||
| 1395 | * The access list to '/folder1/folder2/fileA' **without** $currentAccess is: |
||
| 1396 | * [ |
||
| 1397 | * users => ['user1', 'user2', 'user4'], |
||
| 1398 | * remote => bool, |
||
| 1399 | * public => bool |
||
| 1400 | * mail => bool |
||
| 1401 | * ] |
||
| 1402 | * |
||
| 1403 | * This is required for encryption/activity |
||
| 1404 | * |
||
| 1405 | * @param \OCP\Files\Node $path |
||
| 1406 | * @param bool $recursive Should we check all parent folders as well |
||
| 1407 | * @param bool $currentAccess Ensure the recipient has access to the file (e.g. did not unshare it) |
||
| 1408 | * @return array |
||
| 1409 | */ |
||
| 1410 | public function getAccessList(\OCP\Files\Node $path, $recursive = true, $currentAccess = false) { |
||
| 1411 | $owner = $path->getOwner()->getUID(); |
||
| 1412 | |||
| 1413 | if ($currentAccess) { |
||
| 1414 | $al = ['users' => [], 'remote' => [], 'public' => false]; |
||
| 1415 | } else { |
||
| 1416 | $al = ['users' => [], 'remote' => false, 'public' => false]; |
||
| 1417 | } |
||
| 1418 | if (!$this->userManager->userExists($owner)) { |
||
| 1419 | return $al; |
||
| 1420 | } |
||
| 1421 | |||
| 1422 | //Get node for the owner |
||
| 1423 | $userFolder = $this->rootFolder->getUserFolder($owner); |
||
| 1424 | if ($path->getId() !== $userFolder->getId() && !$userFolder->isSubNode($path)) { |
||
| 1425 | $path = $userFolder->getById($path->getId())[0]; |
||
| 1426 | } |
||
| 1427 | |||
| 1428 | $providers = $this->factory->getAllProviders(); |
||
| 1429 | |||
| 1430 | /** @var Node[] $nodes */ |
||
| 1431 | $nodes = []; |
||
| 1432 | |||
| 1433 | |||
| 1434 | if ($currentAccess) { |
||
| 1435 | $ownerPath = $path->getPath(); |
||
| 1436 | $ownerPath = explode('/', $ownerPath, 4); |
||
| 1437 | if (count($ownerPath) < 4) { |
||
| 1438 | $ownerPath = ''; |
||
| 1439 | } else { |
||
| 1440 | $ownerPath = $ownerPath[3]; |
||
| 1441 | } |
||
| 1442 | $al['users'][$owner] = [ |
||
| 1443 | 'node_id' => $path->getId(), |
||
| 1444 | 'node_path' => '/' . $ownerPath, |
||
| 1445 | ]; |
||
| 1446 | } else { |
||
| 1447 | $al['users'][] = $owner; |
||
| 1448 | } |
||
| 1449 | |||
| 1450 | // Collect all the shares |
||
| 1451 | while ($path->getPath() !== $userFolder->getPath()) { |
||
| 1452 | $nodes[] = $path; |
||
| 1453 | if (!$recursive) { |
||
| 1454 | break; |
||
| 1455 | } |
||
| 1456 | $path = $path->getParent(); |
||
| 1457 | } |
||
| 1458 | |||
| 1459 | foreach ($providers as $provider) { |
||
| 1460 | $tmp = $provider->getAccessList($nodes, $currentAccess); |
||
| 1461 | |||
| 1462 | foreach ($tmp as $k => $v) { |
||
| 1463 | if (isset($al[$k])) { |
||
| 1464 | if (is_array($al[$k])) { |
||
| 1465 | if ($currentAccess) { |
||
| 1466 | $al[$k] += $v; |
||
| 1467 | } else { |
||
| 1468 | $al[$k] = array_merge($al[$k], $v); |
||
| 1469 | $al[$k] = array_unique($al[$k]); |
||
| 1470 | $al[$k] = array_values($al[$k]); |
||
| 1471 | } |
||
| 1472 | } else { |
||
| 1473 | $al[$k] = $al[$k] || $v; |
||
| 1474 | } |
||
| 1475 | } else { |
||
| 1476 | $al[$k] = $v; |
||
| 1477 | } |
||
| 1478 | } |
||
| 1479 | } |
||
| 1480 | |||
| 1481 | return $al; |
||
| 1482 | } |
||
| 1483 | |||
| 1484 | /** |
||
| 1485 | * Create a new share |
||
| 1486 | * @return \OCP\Share\IShare |
||
| 1487 | */ |
||
| 1488 | public function newShare() { |
||
| 1489 | return new \OC\Share20\Share($this->rootFolder, $this->userManager); |
||
| 1490 | } |
||
| 1491 | |||
| 1492 | /** |
||
| 1493 | * Is the share API enabled |
||
| 1494 | * |
||
| 1495 | * @return bool |
||
| 1496 | */ |
||
| 1497 | public function shareApiEnabled() { |
||
| 1499 | } |
||
| 1500 | |||
| 1501 | /** |
||
| 1502 | * Is public link sharing enabled |
||
| 1503 | * |
||
| 1504 | * @return bool |
||
| 1505 | */ |
||
| 1506 | public function shareApiAllowLinks() { |
||
| 1508 | } |
||
| 1509 | |||
| 1510 | /** |
||
| 1511 | * Is password on public link requires |
||
| 1512 | * |
||
| 1513 | * @return bool |
||
| 1514 | */ |
||
| 1515 | public function shareApiLinkEnforcePassword() { |
||
| 1516 | return $this->config->getAppValue('core', 'shareapi_enforce_links_password', 'no') === 'yes'; |
||
| 1517 | } |
||
| 1518 | |||
| 1519 | /** |
||
| 1520 | * Is default expire date enabled |
||
| 1521 | * |
||
| 1522 | * @return bool |
||
| 1523 | */ |
||
| 1524 | public function shareApiLinkDefaultExpireDate() { |
||
| 1525 | return $this->config->getAppValue('core', 'shareapi_default_expire_date', 'no') === 'yes'; |
||
| 1526 | } |
||
| 1527 | |||
| 1528 | /** |
||
| 1529 | * Is default expire date enforced |
||
| 1530 | *` |
||
| 1531 | * @return bool |
||
| 1532 | */ |
||
| 1533 | public function shareApiLinkDefaultExpireDateEnforced() { |
||
| 1534 | return $this->shareApiLinkDefaultExpireDate() && |
||
| 1535 | $this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no') === 'yes'; |
||
| 1536 | } |
||
| 1537 | |||
| 1538 | /** |
||
| 1539 | * Number of default expire days |
||
| 1540 | *shareApiLinkAllowPublicUpload |
||
| 1541 | * @return int |
||
| 1542 | */ |
||
| 1543 | public function shareApiLinkDefaultExpireDays() { |
||
| 1544 | return (int)$this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7'); |
||
| 1545 | } |
||
| 1546 | |||
| 1547 | /** |
||
| 1548 | * Allow public upload on link shares |
||
| 1549 | * |
||
| 1550 | * @return bool |
||
| 1551 | */ |
||
| 1552 | public function shareApiLinkAllowPublicUpload() { |
||
| 1553 | return $this->config->getAppValue('core', 'shareapi_allow_public_upload', 'yes') === 'yes'; |
||
| 1554 | } |
||
| 1555 | |||
| 1556 | /** |
||
| 1557 | * check if user can only share with group members |
||
| 1558 | * @return bool |
||
| 1559 | */ |
||
| 1560 | public function shareWithGroupMembersOnly() { |
||
| 1561 | return $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes'; |
||
| 1562 | } |
||
| 1563 | |||
| 1564 | /** |
||
| 1565 | * Check if users can share with groups |
||
| 1566 | * @return bool |
||
| 1567 | */ |
||
| 1568 | public function allowGroupSharing() { |
||
| 1569 | return $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes') === 'yes'; |
||
| 1570 | } |
||
| 1571 | |||
| 1572 | /** |
||
| 1573 | * Copied from \OC_Util::isSharingDisabledForUser |
||
| 1574 | * |
||
| 1575 | * TODO: Deprecate fuction from OC_Util |
||
| 1576 | * |
||
| 1577 | * @param string $userId |
||
| 1578 | * @return bool |
||
| 1579 | */ |
||
| 1580 | public function sharingDisabledForUser($userId) { |
||
| 1581 | if ($userId === null) { |
||
| 1582 | return false; |
||
| 1583 | } |
||
| 1584 | |||
| 1585 | if (isset($this->sharingDisabledForUsersCache[$userId])) { |
||
| 1586 | return $this->sharingDisabledForUsersCache[$userId]; |
||
| 1587 | } |
||
| 1588 | |||
| 1589 | if ($this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes') { |
||
| 1590 | $groupsList = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', ''); |
||
| 1591 | $excludedGroups = json_decode($groupsList); |
||
| 1592 | if (is_null($excludedGroups)) { |
||
| 1593 | $excludedGroups = explode(',', $groupsList); |
||
| 1594 | $newValue = json_encode($excludedGroups); |
||
| 1595 | $this->config->setAppValue('core', 'shareapi_exclude_groups_list', $newValue); |
||
| 1596 | } |
||
| 1597 | $user = $this->userManager->get($userId); |
||
| 1598 | $usersGroups = $this->groupManager->getUserGroupIds($user); |
||
| 1599 | if (!empty($usersGroups)) { |
||
| 1600 | $remainingGroups = array_diff($usersGroups, $excludedGroups); |
||
| 1601 | // if the user is only in groups which are disabled for sharing then |
||
| 1602 | // sharing is also disabled for the user |
||
| 1603 | if (empty($remainingGroups)) { |
||
| 1604 | $this->sharingDisabledForUsersCache[$userId] = true; |
||
| 1605 | return true; |
||
| 1606 | } |
||
| 1607 | } |
||
| 1608 | } |
||
| 1609 | |||
| 1610 | $this->sharingDisabledForUsersCache[$userId] = false; |
||
| 1611 | return false; |
||
| 1612 | } |
||
| 1613 | |||
| 1614 | /** |
||
| 1615 | * @inheritdoc |
||
| 1616 | */ |
||
| 1617 | public function outgoingServer2ServerSharesAllowed() { |
||
| 1619 | } |
||
| 1620 | |||
| 1621 | /** |
||
| 1622 | * @inheritdoc |
||
| 1623 | */ |
||
| 1624 | public function outgoingServer2ServerGroupSharesAllowed() { |
||
| 1625 | return $this->config->getAppValue('files_sharing', 'outgoing_server2server_group_share_enabled', 'no') === 'yes'; |
||
| 1626 | } |
||
| 1627 | |||
| 1628 | /** |
||
| 1629 | * @inheritdoc |
||
| 1630 | */ |
||
| 1631 | public function shareProviderExists($shareType) { |
||
| 1639 | } |
||
| 1640 | |||
| 1641 | } |
||
| 1642 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: