|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\DAV\Command; |
| 11 | + |
| 12 | +use OCA\DAV\CalDAV\CalDavBackend; |
| 13 | +use OCA\DAV\CalDAV\Sharing\Backend; |
| 14 | +use OCA\DAV\Connector\Sabre\Principal; |
| 15 | +use OCA\DAV\DAV\Sharing\SharingMapper; |
| 16 | +use OCP\IUserManager; |
| 17 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 18 | +use Symfony\Component\Console\Command\Command; |
| 19 | +use Symfony\Component\Console\Helper\Table; |
| 20 | +use Symfony\Component\Console\Input\InputArgument; |
| 21 | +use Symfony\Component\Console\Input\InputInterface; |
| 22 | +use Symfony\Component\Console\Input\InputOption; |
| 23 | +use Symfony\Component\Console\Output\OutputInterface; |
| 24 | + |
| 25 | +#[AsCommand( |
| 26 | + name: 'dav:list-calendar-shares', |
| 27 | + description: 'List all calendar shares for a user', |
| 28 | + hidden: false, |
| 29 | +)] |
| 30 | +class ListCalendarShares extends Command { |
| 31 | + public function __construct( |
| 32 | + private IUserManager $userManager, |
| 33 | + private Principal $principal, |
| 34 | + private CalDavBackend $caldav, |
| 35 | + private SharingMapper $mapper, |
| 36 | + ) { |
| 37 | + parent::__construct(); |
| 38 | + } |
| 39 | + |
| 40 | + protected function configure(): void { |
| 41 | + $this->addArgument( |
| 42 | + 'uid', |
| 43 | + InputArgument::REQUIRED, |
| 44 | + 'User whose calendar shares will be listed' |
| 45 | + ); |
| 46 | + $this->addOption( |
| 47 | + 'calendar-id', |
| 48 | + '', |
| 49 | + InputOption::VALUE_REQUIRED, |
| 50 | + 'List only shares for the given calendar id id', |
| 51 | + null, |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 56 | + $user = (string)$input->getArgument('uid'); |
| 57 | + if (!$this->userManager->userExists($user)) { |
| 58 | + throw new \InvalidArgumentException("User $user is unknown"); |
| 59 | + } |
| 60 | + |
| 61 | + $principal = $this->principal->getPrincipalByPath('principals/users/' . $user); |
| 62 | + if ($principal === null) { |
| 63 | + throw new \InvalidArgumentException("Unable to fetch principal for user $user"); |
| 64 | + } |
| 65 | + |
| 66 | + $memberships = array_merge( |
| 67 | + [$principal['uri']], |
| 68 | + $this->principal->getGroupMembership($principal['uri']), |
| 69 | + $this->principal->getCircleMembership($principal['uri']), |
| 70 | + ); |
| 71 | + |
| 72 | + $shares = $this->mapper->getSharesByPrincipals($memberships, 'calendar'); |
| 73 | + |
| 74 | + $calendarId = $input->getOption('calendar-id'); |
| 75 | + if ($calendarId !== null) { |
| 76 | + $shares = array_filter($shares, fn ($share) => $share['resourceid'] === (int)$calendarId); |
| 77 | + } |
| 78 | + |
| 79 | + $rows = array_map(fn ($share) => $this->formatCalendarShare($share), $shares); |
| 80 | + |
| 81 | + if (count($rows) > 0) { |
| 82 | + $table = new Table($output); |
| 83 | + $table |
| 84 | + ->setHeaders(['Share Id', 'Calendar Id', 'Calendar URI', 'Calendar Name', 'Calendar Owner', 'Access By', 'Permissions']) |
| 85 | + ->setRows($rows) |
| 86 | + ->render(); |
| 87 | + } else { |
| 88 | + $output->writeln("User $user has no calendar shares"); |
| 89 | + } |
| 90 | + |
| 91 | + return self::SUCCESS; |
| 92 | + } |
| 93 | + |
| 94 | + private function formatCalendarShare(array $share): array { |
| 95 | + $calendarInfo = $this->caldav->getCalendarById($share['resourceid']); |
| 96 | + |
| 97 | + $calendarUri = 'Resource not found'; |
| 98 | + $calendarName = ''; |
| 99 | + $calendarOwner = ''; |
| 100 | + |
| 101 | + if ($calendarInfo !== null) { |
| 102 | + $calendarUri = $calendarInfo['uri']; |
| 103 | + $calendarName = $calendarInfo['{DAV:}displayname']; |
| 104 | + $calendarOwner = $calendarInfo['{http://nextcloud.com/ns}owner-displayname'] . ' (' . $calendarInfo['principaluri'] . ')'; |
| 105 | + } |
| 106 | + |
| 107 | + $accessBy = match (true) { |
| 108 | + str_starts_with($share['principaluri'], 'principals/users/') => 'Individual', |
| 109 | + str_starts_with($share['principaluri'], 'principals/groups/') => 'Group (' . $share['principaluri'] . ')', |
| 110 | + str_starts_with($share['principaluri'], 'principals/circles/') => 'Team (' . $share['principaluri'] . ')', |
| 111 | + default => $share['principaluri'], |
| 112 | + }; |
| 113 | + |
| 114 | + $permissions = match ($share['access']) { |
| 115 | + Backend::ACCESS_READ => 'Read', |
| 116 | + Backend::ACCESS_READ_WRITE => 'Read/Write', |
| 117 | + Backend::ACCESS_UNSHARED => 'Unshare', |
| 118 | + default => $share['access'], |
| 119 | + }; |
| 120 | + |
| 121 | + return [ |
| 122 | + $share['id'], |
| 123 | + $share['resourceid'], |
| 124 | + $calendarUri, |
| 125 | + $calendarName, |
| 126 | + $calendarOwner, |
| 127 | + $accessBy, |
| 128 | + $permissions, |
| 129 | + ]; |
| 130 | + } |
| 131 | +} |
0 commit comments