ListCalendars.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018, Georg Ehrke
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Thomas Citharel <nextcloud@tcit.fr>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\DAV\Command;
  27. use OCA\DAV\CalDAV\BirthdayService;
  28. use OCA\DAV\CalDAV\CalDavBackend;
  29. use OCP\IUserManager;
  30. use Symfony\Component\Console\Command\Command;
  31. use Symfony\Component\Console\Helper\Table;
  32. use Symfony\Component\Console\Input\InputArgument;
  33. use Symfony\Component\Console\Input\InputInterface;
  34. use Symfony\Component\Console\Output\OutputInterface;
  35. class ListCalendars extends Command {
  36. public function __construct(
  37. protected IUserManager $userManager,
  38. private CalDavBackend $caldav,
  39. ) {
  40. parent::__construct();
  41. }
  42. protected function configure(): void {
  43. $this
  44. ->setName('dav:list-calendars')
  45. ->setDescription('List all calendars of a user')
  46. ->addArgument('uid',
  47. InputArgument::REQUIRED,
  48. 'User for whom all calendars will be listed');
  49. }
  50. protected function execute(InputInterface $input, OutputInterface $output): int {
  51. $user = $input->getArgument('uid');
  52. if (!$this->userManager->userExists($user)) {
  53. throw new \InvalidArgumentException("User <$user> is unknown.");
  54. }
  55. $calendars = $this->caldav->getCalendarsForUser("principals/users/$user");
  56. $calendarTableData = [];
  57. foreach ($calendars as $calendar) {
  58. // skip birthday calendar
  59. if ($calendar['uri'] === BirthdayService::BIRTHDAY_CALENDAR_URI) {
  60. continue;
  61. }
  62. $readOnly = false;
  63. $readOnlyIndex = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only';
  64. if (isset($calendar[$readOnlyIndex])) {
  65. $readOnly = $calendar[$readOnlyIndex];
  66. }
  67. $calendarTableData[] = [
  68. $calendar['uri'],
  69. $calendar['{DAV:}displayname'],
  70. $calendar['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'],
  71. $calendar['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'],
  72. $readOnly ? ' x ' : ' ✓ ',
  73. ];
  74. }
  75. if (count($calendarTableData) > 0) {
  76. $table = new Table($output);
  77. $table->setHeaders(['uri', 'displayname', 'owner\'s userid', 'owner\'s displayname', 'writable'])
  78. ->setRows($calendarTableData);
  79. $table->render();
  80. } else {
  81. $output->writeln("<info>User <$user> has no calendars</info>");
  82. }
  83. return self::SUCCESS;
  84. }
  85. }