|
| 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 OCP\Constants; |
| 13 | +use OCP\EventDispatcher\Event; |
| 14 | +use OCP\EventDispatcher\IEventListener; |
| 15 | +use OCP\Files\File; |
| 16 | +use OCP\Files\Folder; |
| 17 | +use OCP\Files\IRootFolder; |
| 18 | +use OCP\Files\Mount\IMovableMount; |
| 19 | +use OCP\Files\Node; |
| 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\Server; |
| 27 | +use OCP\Share\IManager; |
| 28 | + |
| 29 | +/** |
| 30 | + * @template-implements IEventListener<RestrictInteractionEvent> |
| 31 | + */ |
| 32 | +final class RestrictInteractionListener implements IEventListener { |
| 33 | + /** |
| 34 | + * @param RestrictInteractionEvent $event |
| 35 | + */ |
| 36 | + #[\Override] |
| 37 | + public function handle(Event $event): void { |
| 38 | + if ($event->resource instanceof NodeResource && $event->action instanceof ShareAction) { |
| 39 | + $nodePermissions = array_reduce( |
| 40 | + Server::get(IRootFolder::class)->getUserFolder($event->user->getUID())->getById($event->resource->getNode($event->user->getUID())->getId()), |
| 41 | + static fn (int $nodePermissions, Node $node): int => $nodePermissions | ($node->getInternalPath() === '' && !$node->getMountPoint() instanceof IMovableMount ? $node->getStorage()->getPermissions('') : $node->getPermissions()), |
| 42 | + 0, |
| 43 | + ); |
| 44 | + |
| 45 | + if (($nodePermissions & Constants::PERMISSION_SHARE) !== Constants::PERMISSION_SHARE) { |
| 46 | + throw new InteractionRestrictedException('No share permission on the node.'); |
| 47 | + } |
| 48 | + |
| 49 | + if ($event->action->filesSharingPermissions !== null) { |
| 50 | + if (($event->action->filesSharingPermissions & ~$nodePermissions) !== 0) { |
| 51 | + throw new InteractionRestrictedException('Cannot share node with more permissions than the node already has.'); |
| 52 | + } |
| 53 | + |
| 54 | + if ($event->resource->getNode($event->user->getUID()) instanceof File) { |
| 55 | + if (($event->action->filesSharingPermissions & Constants::PERMISSION_DELETE) === Constants::PERMISSION_DELETE) { |
| 56 | + throw new InteractionRestrictedException('Cannot share file node with delete permission.'); |
| 57 | + } |
| 58 | + |
| 59 | + if (($event->action->filesSharingPermissions & Constants::PERMISSION_CREATE) === Constants::PERMISSION_CREATE) { |
| 60 | + throw new InteractionRestrictedException('Cannot share file node with create permission.'); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + if (!$event->receiver instanceof LinkReceiver |
| 65 | + && !$event->receiver instanceof EmailReceiver |
| 66 | + && ($event->action->filesSharingPermissions & Constants::PERMISSION_READ) !== Constants::PERMISSION_READ) { |
| 67 | + throw new InteractionRestrictedException('No read permission on the share.'); |
| 68 | + } |
| 69 | + |
| 70 | + if (($event->receiver instanceof LinkReceiver || $event->receiver instanceof EmailReceiver) |
| 71 | + && $event->resource->getNode($event->user->getUID()) instanceof Folder |
| 72 | + && ($event->action->filesSharingPermissions & (Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE)) !== 0 |
| 73 | + && !Server::get(IManager::class)->shareApiLinkAllowPublicUpload()) { |
| 74 | + throw new InteractionRestrictedException('Public upload is not allowed.'); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments