|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace OCA\Files_Sharing\Listener; |
| 11 | + |
| 12 | +use OCA\Files_Sharing\AppInfo\Application; |
| 13 | +use OCP\Constants; |
| 14 | +use OCP\EventDispatcher\Event; |
| 15 | +use OCP\EventDispatcher\IEventListener; |
| 16 | +use OCP\Files\File; |
| 17 | +use OCP\Files\Folder; |
| 18 | +use OCP\Files\IRootFolder; |
| 19 | +use OCP\IL10N; |
| 20 | +use OCP\Interaction\Actions\ShareAction; |
| 21 | +use OCP\Interaction\InteractionRestrictedException; |
| 22 | +use OCP\Interaction\Receivers\EmailReceiver; |
| 23 | +use OCP\Interaction\Receivers\LinkReceiver; |
| 24 | +use OCP\Interaction\Resources\NodeResource; |
| 25 | +use OCP\Interaction\RestrictInteractionEvent; |
| 26 | +use OCP\L10N\IFactory; |
| 27 | +use OCP\Share\IManager; |
| 28 | + |
| 29 | +/** |
| 30 | + * @template-implements IEventListener<RestrictInteractionEvent> |
| 31 | + */ |
| 32 | +final class RestrictInteractionListener implements IEventListener { |
| 33 | + private readonly IL10N $l10n; |
| 34 | + |
| 35 | + public function __construct( |
| 36 | + private IRootFolder $rootFolder, |
| 37 | + private IManager $manager, |
| 38 | + IFactory $l10nFactory, |
| 39 | + ) { |
| 40 | + $this->l10n = $l10nFactory->get(Application::APP_ID); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @param RestrictInteractionEvent $event |
| 45 | + */ |
| 46 | + #[\Override] |
| 47 | + public function handle(Event $event): void { |
| 48 | + if ($event->resource instanceof NodeResource && $event->action instanceof ShareAction) { |
| 49 | + if (!$event->resource->getNode()->isShareable()) { |
| 50 | + throw new InteractionRestrictedException('Node is not shareable.', $this->l10n->t('You are not allowed to share "%s".', [$event->resource->getNode()->getName()])); |
| 51 | + } |
| 52 | + |
| 53 | + $userFolder = $this->rootFolder->getUserFolder($event->userId); |
| 54 | + if ($event->resource->nodeId === $userFolder->getId()) { |
| 55 | + throw new InteractionRestrictedException('Cannot share home folder node.', $this->l10n->t('You cannot share your home folder.')); |
| 56 | + } |
| 57 | + |
| 58 | + if ($event->action->filesSharingPermissions !== null) { |
| 59 | + if (($event->action->filesSharingPermissions & ~$event->resource->getNodePermissions()) !== 0) { |
| 60 | + $path = $userFolder->getRelativePath($event->resource->getNode()->getPath()); |
| 61 | + throw new InteractionRestrictedException('Cannot share node with more permissions than the node already has.', $this->l10n->t('You cannot share "%s" with more permission than you have yourself.', [$path])); |
| 62 | + } |
| 63 | + |
| 64 | + if ($event->resource->getNode() instanceof File) { |
| 65 | + if (($event->action->filesSharingPermissions & Constants::PERMISSION_DELETE) === Constants::PERMISSION_DELETE) { |
| 66 | + throw new InteractionRestrictedException('Cannot share file node with delete permission.', $this->l10n->t('File cannot be shared with delete permission.')); |
| 67 | + } |
| 68 | + |
| 69 | + if (($event->action->filesSharingPermissions & Constants::PERMISSION_CREATE) === Constants::PERMISSION_CREATE) { |
| 70 | + throw new InteractionRestrictedException('Cannot share file node with create permission.', $this->l10n->t('File cannot be shared with create permission.')); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if (!$event->receiver instanceof LinkReceiver |
| 75 | + && !$event->receiver instanceof EmailReceiver |
| 76 | + && ($event->action->filesSharingPermissions & Constants::PERMISSION_READ) !== Constants::PERMISSION_READ) { |
| 77 | + throw new InteractionRestrictedException('No read permission on the share.', $this->l10n->t('File share needs at least read permission.')); |
| 78 | + } |
| 79 | + |
| 80 | + if (($event->receiver instanceof LinkReceiver || $event->receiver instanceof EmailReceiver) |
| 81 | + && $event->resource->getNode() instanceof Folder |
| 82 | + && ($event->action->filesSharingPermissions & (Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE)) !== 0 |
| 83 | + && !$this->manager->shareApiLinkAllowPublicUpload()) { |
| 84 | + throw new InteractionRestrictedException('Public upload is not allowed.', $this->l10n->t('Public upload is not allowed.')); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments