-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Introduce own method for calendar unsharing #52046
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\DAV\Command; | ||
|
|
||
| use OCA\DAV\CalDAV\CalDavBackend; | ||
| use OCA\DAV\CalDAV\Sharing\Backend; | ||
| use OCA\DAV\CalDAV\Sharing\Service; | ||
| use OCA\DAV\Connector\Sabre\Principal; | ||
| use OCA\DAV\DAV\Sharing\Backend as BackendAlias; | ||
| use OCA\DAV\DAV\Sharing\SharingMapper; | ||
| use OCP\IAppConfig; | ||
| use OCP\IUserManager; | ||
| use Symfony\Component\Console\Attribute\AsCommand; | ||
| use Symfony\Component\Console\Command\Command; | ||
| use Symfony\Component\Console\Helper\QuestionHelper; | ||
| use Symfony\Component\Console\Helper\Table; | ||
| use Symfony\Component\Console\Input\InputArgument; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
| use Symfony\Component\Console\Question\ConfirmationQuestion; | ||
|
|
||
| #[AsCommand( | ||
| name: 'dav:clear-calendar-unshares', | ||
| description: 'Clear calendar unshares for a user', | ||
| hidden: false, | ||
| )] | ||
| class ClearCalendarUnshares extends Command { | ||
| public function __construct( | ||
| private IUserManager $userManager, | ||
| private IAppConfig $appConfig, | ||
| private Principal $principal, | ||
| private CalDavBackend $caldav, | ||
| private Backend $sharingBackend, | ||
| private Service $sharingService, | ||
| private SharingMapper $mapper, | ||
| ) { | ||
| parent::__construct(); | ||
| } | ||
|
|
||
| protected function configure(): void { | ||
| $this->addArgument( | ||
| 'uid', | ||
| InputArgument::REQUIRED, | ||
| 'User whose unshares to clear' | ||
| ); | ||
| } | ||
|
|
||
| protected function execute(InputInterface $input, OutputInterface $output): int { | ||
| $user = (string)$input->getArgument('uid'); | ||
| if (!$this->userManager->userExists($user)) { | ||
| throw new \InvalidArgumentException("User $user is unknown"); | ||
| } | ||
|
|
||
| $principal = $this->principal->getPrincipalByPath('principals/users/' . $user); | ||
| if ($principal === null) { | ||
| throw new \InvalidArgumentException("Unable to fetch principal for user $user "); | ||
| } | ||
|
|
||
| $shares = $this->mapper->getSharesByPrincipals([$principal['uri']], 'calendar'); | ||
| $unshares = array_filter($shares, static fn ($share) => $share['access'] === BackendAlias::ACCESS_UNSHARED); | ||
|
|
||
| if (count($unshares) === 0) { | ||
| $output->writeln("User $user has no calendar unshares"); | ||
| return self::SUCCESS; | ||
| } | ||
|
|
||
| $rows = array_map(fn ($share) => $this->formatCalendarUnshare($share), $shares); | ||
|
|
||
| $table = new Table($output); | ||
| $table | ||
| ->setHeaders(['Share Id', 'Calendar Id', 'Calendar URI', 'Calendar Name']) | ||
| ->setRows($rows) | ||
| ->render(); | ||
|
|
||
| $output->writeln(''); | ||
|
|
||
| /** @var QuestionHelper $helper */ | ||
| $helper = $this->getHelper('question'); | ||
| $question = new ConfirmationQuestion('Please confirm to delete the above calendar unshare entries [y/n]', false); | ||
|
|
||
| if ($helper->ask($input, $output, $question)) { | ||
| $this->mapper->deleteUnsharesByPrincipal($principal['uri'], 'calendar'); | ||
| $output->writeln("Calendar unshares for user $user deleted"); | ||
| } | ||
|
|
||
| return self::SUCCESS; | ||
| } | ||
|
|
||
| private function formatCalendarUnshare(array $share): array { | ||
| $calendarInfo = $this->caldav->getCalendarById($share['resourceid']); | ||
|
|
||
| $resourceUri = 'Resource not found'; | ||
| $resourceName = ''; | ||
|
|
||
| if ($calendarInfo !== null) { | ||
| $resourceUri = $calendarInfo['uri']; | ||
| $resourceName = $calendarInfo['{DAV:}displayname']; | ||
| } | ||
|
|
||
| return [ | ||
| $share['id'], | ||
| $share['resourceid'], | ||
| $resourceUri, | ||
| $resourceName, | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\DAV\Command; | ||
|
|
||
| use OCA\DAV\CalDAV\CalDavBackend; | ||
| use OCA\DAV\CalDAV\Sharing\Backend; | ||
| use OCA\DAV\Connector\Sabre\Principal; | ||
| use OCA\DAV\DAV\Sharing\SharingMapper; | ||
| use OCP\IUserManager; | ||
| use Symfony\Component\Console\Attribute\AsCommand; | ||
| use Symfony\Component\Console\Command\Command; | ||
| use Symfony\Component\Console\Helper\Table; | ||
| use Symfony\Component\Console\Input\InputArgument; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Input\InputOption; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
|
|
||
| #[AsCommand( | ||
| name: 'dav:list-calendar-shares', | ||
| description: 'List all calendar shares for a user', | ||
| hidden: false, | ||
| )] | ||
| class ListCalendarShares extends Command { | ||
| public function __construct( | ||
| private IUserManager $userManager, | ||
| private Principal $principal, | ||
| private CalDavBackend $caldav, | ||
| private SharingMapper $mapper, | ||
| ) { | ||
| parent::__construct(); | ||
| } | ||
|
|
||
| protected function configure(): void { | ||
| $this->addArgument( | ||
| 'uid', | ||
| InputArgument::REQUIRED, | ||
| 'User whose calendar shares will be listed' | ||
| ); | ||
| $this->addOption( | ||
| 'calendar-id', | ||
| '', | ||
| InputOption::VALUE_REQUIRED, | ||
| 'List only shares for the given calendar id id', | ||
| null, | ||
| ); | ||
| } | ||
|
|
||
| protected function execute(InputInterface $input, OutputInterface $output): int { | ||
| $user = (string)$input->getArgument('uid'); | ||
| if (!$this->userManager->userExists($user)) { | ||
| throw new \InvalidArgumentException("User $user is unknown"); | ||
| } | ||
|
|
||
| $principal = $this->principal->getPrincipalByPath('principals/users/' . $user); | ||
| if ($principal === null) { | ||
| throw new \InvalidArgumentException("Unable to fetch principal for user $user"); | ||
| } | ||
|
|
||
| $memberships = array_merge( | ||
| [$principal['uri']], | ||
| $this->principal->getGroupMembership($principal['uri']), | ||
| $this->principal->getCircleMembership($principal['uri']), | ||
| ); | ||
|
|
||
| $shares = $this->mapper->getSharesByPrincipals($memberships, 'calendar'); | ||
|
|
||
| $calendarId = $input->getOption('calendar-id'); | ||
| if ($calendarId !== null) { | ||
| $shares = array_filter($shares, fn ($share) => $share['resourceid'] === (int)$calendarId); | ||
| } | ||
|
|
||
| $rows = array_map(fn ($share) => $this->formatCalendarShare($share), $shares); | ||
|
|
||
| if (count($rows) > 0) { | ||
| $table = new Table($output); | ||
| $table | ||
| ->setHeaders(['Share Id', 'Calendar Id', 'Calendar URI', 'Calendar Name', 'Calendar Owner', 'Access By', 'Permissions']) | ||
| ->setRows($rows) | ||
| ->render(); | ||
| } else { | ||
| $output->writeln("User $user has no calendar shares"); | ||
| } | ||
|
|
||
| return self::SUCCESS; | ||
| } | ||
|
|
||
| private function formatCalendarShare(array $share): array { | ||
| $calendarInfo = $this->caldav->getCalendarById($share['resourceid']); | ||
|
|
||
| $calendarUri = 'Resource not found'; | ||
| $calendarName = ''; | ||
| $calendarOwner = ''; | ||
|
|
||
| if ($calendarInfo !== null) { | ||
| $calendarUri = $calendarInfo['uri']; | ||
| $calendarName = $calendarInfo['{DAV:}displayname']; | ||
| $calendarOwner = $calendarInfo['{http://nextcloud.com/ns}owner-displayname'] . ' (' . $calendarInfo['principaluri'] . ')'; | ||
| } | ||
|
|
||
| $accessBy = match (true) { | ||
| str_starts_with($share['principaluri'], 'principals/users/') => 'Individual', | ||
| str_starts_with($share['principaluri'], 'principals/groups/') => 'Group (' . $share['principaluri'] . ')', | ||
| str_starts_with($share['principaluri'], 'principals/circles/') => 'Team (' . $share['principaluri'] . ')', | ||
| default => $share['principaluri'], | ||
| }; | ||
|
|
||
| $permissions = match ($share['access']) { | ||
| Backend::ACCESS_READ => 'Read', | ||
| Backend::ACCESS_READ_WRITE => 'Read/Write', | ||
| Backend::ACCESS_UNSHARED => 'Unshare', | ||
| default => $share['access'], | ||
| }; | ||
|
|
||
| return [ | ||
| $share['id'], | ||
| $share['resourceid'], | ||
| $calendarUri, | ||
| $calendarName, | ||
| $calendarOwner, | ||
| $accessBy, | ||
| $permissions, | ||
| ]; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
owner-displaynameis only set if we have displayname:server/apps/dav/lib/CalDAV/CalDavBackend.php
Lines 3522 to 3535 in 2ea3491
But we use it without an isset before and thus, marking it as optional, will trigger some new psalm warnings.