1
0

DeleteCalendar.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\Command;
  8. use OCA\DAV\CalDAV\BirthdayService;
  9. use OCA\DAV\CalDAV\CalDavBackend;
  10. use OCA\DAV\CalDAV\Calendar;
  11. use OCP\IConfig;
  12. use OCP\IL10N;
  13. use OCP\IUserManager;
  14. use Psr\Log\LoggerInterface;
  15. use Symfony\Component\Console\Command\Command;
  16. use Symfony\Component\Console\Input\InputArgument;
  17. use Symfony\Component\Console\Input\InputInterface;
  18. use Symfony\Component\Console\Input\InputOption;
  19. use Symfony\Component\Console\Output\OutputInterface;
  20. class DeleteCalendar extends Command {
  21. public function __construct(
  22. private CalDavBackend $calDav,
  23. private IConfig $config,
  24. private IL10N $l10n,
  25. private IUserManager $userManager,
  26. private LoggerInterface $logger,
  27. ) {
  28. parent::__construct();
  29. }
  30. protected function configure(): void {
  31. $this
  32. ->setName('dav:delete-calendar')
  33. ->setDescription('Delete a dav calendar')
  34. ->addArgument('uid',
  35. InputArgument::REQUIRED,
  36. 'User who owns the calendar')
  37. ->addArgument('name',
  38. InputArgument::OPTIONAL,
  39. 'Name of the calendar to delete')
  40. ->addOption('birthday',
  41. null,
  42. InputOption::VALUE_NONE,
  43. 'Delete the birthday calendar')
  44. ->addOption('force',
  45. 'f',
  46. InputOption::VALUE_NONE,
  47. 'Force delete skipping trashbin');
  48. }
  49. protected function execute(
  50. InputInterface $input,
  51. OutputInterface $output
  52. ): int {
  53. /** @var string $user **/
  54. $user = $input->getArgument('uid');
  55. if (!$this->userManager->userExists($user)) {
  56. throw new \InvalidArgumentException(
  57. 'User <' . $user . '> is unknown.');
  58. }
  59. $birthday = $input->getOption('birthday');
  60. if ($birthday !== false) {
  61. $name = BirthdayService::BIRTHDAY_CALENDAR_URI;
  62. } else {
  63. /** @var string $name **/
  64. $name = $input->getArgument('name');
  65. if (!$name) {
  66. throw new \InvalidArgumentException(
  67. 'Please specify a calendar name or --birthday');
  68. }
  69. }
  70. $calendarInfo = $this->calDav->getCalendarByUri(
  71. 'principals/users/' . $user,
  72. $name);
  73. if ($calendarInfo === null) {
  74. throw new \InvalidArgumentException(
  75. 'User <' . $user . '> has no calendar named <' . $name . '>. You can run occ dav:list-calendars to list calendars URIs for this user.');
  76. }
  77. $calendar = new Calendar(
  78. $this->calDav,
  79. $calendarInfo,
  80. $this->l10n,
  81. $this->config,
  82. $this->logger
  83. );
  84. $force = $input->getOption('force');
  85. if ($force) {
  86. $calendar->disableTrashbin();
  87. }
  88. $calendar->delete();
  89. return self::SUCCESS;
  90. }
  91. }