ListCalendars.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\Command;
  7. use OCA\DAV\CalDAV\BirthdayService;
  8. use OCA\DAV\CalDAV\CalDavBackend;
  9. use OCP\IUserManager;
  10. use Symfony\Component\Console\Command\Command;
  11. use Symfony\Component\Console\Helper\Table;
  12. use Symfony\Component\Console\Input\InputArgument;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. class ListCalendars extends Command {
  16. public function __construct(
  17. protected IUserManager $userManager,
  18. private CalDavBackend $caldav,
  19. ) {
  20. parent::__construct();
  21. }
  22. protected function configure(): void {
  23. $this
  24. ->setName('dav:list-calendars')
  25. ->setDescription('List all calendars of a user')
  26. ->addArgument('uid',
  27. InputArgument::REQUIRED,
  28. 'User for whom all calendars will be listed');
  29. }
  30. protected function execute(InputInterface $input, OutputInterface $output): int {
  31. $user = $input->getArgument('uid');
  32. if (!$this->userManager->userExists($user)) {
  33. throw new \InvalidArgumentException("User <$user> is unknown.");
  34. }
  35. $calendars = $this->caldav->getCalendarsForUser("principals/users/$user");
  36. $calendarTableData = [];
  37. foreach ($calendars as $calendar) {
  38. // skip birthday calendar
  39. if ($calendar['uri'] === BirthdayService::BIRTHDAY_CALENDAR_URI) {
  40. continue;
  41. }
  42. $readOnly = false;
  43. $readOnlyIndex = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only';
  44. if (isset($calendar[$readOnlyIndex])) {
  45. $readOnly = $calendar[$readOnlyIndex];
  46. }
  47. $calendarTableData[] = [
  48. $calendar['uri'],
  49. $calendar['{DAV:}displayname'],
  50. $calendar['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'],
  51. $calendar['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'],
  52. $readOnly ? ' x ' : ' ✓ ',
  53. ];
  54. }
  55. if (count($calendarTableData) > 0) {
  56. $table = new Table($output);
  57. $table->setHeaders(['URI', 'Displayname', 'Owner principal', 'Owner displayname', 'Writable'])
  58. ->setRows($calendarTableData);
  59. $table->render();
  60. } else {
  61. $output->writeln("<info>User <$user> has no calendars</info>");
  62. }
  63. return self::SUCCESS;
  64. }
  65. }