1
0

DeleteCalendar.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. *
  5. * @copyright Copyright (c) 2021, Mattia Narducci (mattianarducci1@gmail.com)
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\DAV\Command;
  24. use OCA\DAV\CalDAV\BirthdayService;
  25. use OCA\DAV\CalDAV\CalDavBackend;
  26. use OCA\DAV\CalDAV\Calendar;
  27. use OCP\IConfig;
  28. use OCP\IL10N;
  29. use OCP\IUserManager;
  30. use Psr\Log\LoggerInterface;
  31. use Symfony\Component\Console\Command\Command;
  32. use Symfony\Component\Console\Input\InputArgument;
  33. use Symfony\Component\Console\Input\InputInterface;
  34. use Symfony\Component\Console\Input\InputOption;
  35. use Symfony\Component\Console\Output\OutputInterface;
  36. class DeleteCalendar extends Command {
  37. public function __construct(
  38. private CalDavBackend $calDav,
  39. private IConfig $config,
  40. private IL10N $l10n,
  41. private IUserManager $userManager,
  42. private LoggerInterface $logger,
  43. ) {
  44. parent::__construct();
  45. }
  46. protected function configure(): void {
  47. $this
  48. ->setName('dav:delete-calendar')
  49. ->setDescription('Delete a dav calendar')
  50. ->addArgument('uid',
  51. InputArgument::REQUIRED,
  52. 'User who owns the calendar')
  53. ->addArgument('name',
  54. InputArgument::OPTIONAL,
  55. 'Name of the calendar to delete')
  56. ->addOption('birthday',
  57. null,
  58. InputOption::VALUE_NONE,
  59. 'Delete the birthday calendar')
  60. ->addOption('force',
  61. 'f',
  62. InputOption::VALUE_NONE,
  63. 'Force delete skipping trashbin');
  64. }
  65. protected function execute(
  66. InputInterface $input,
  67. OutputInterface $output
  68. ): int {
  69. /** @var string $user **/
  70. $user = $input->getArgument('uid');
  71. if (!$this->userManager->userExists($user)) {
  72. throw new \InvalidArgumentException(
  73. 'User <' . $user . '> is unknown.');
  74. }
  75. $birthday = $input->getOption('birthday');
  76. if ($birthday !== false) {
  77. $name = BirthdayService::BIRTHDAY_CALENDAR_URI;
  78. } else {
  79. /** @var string $name **/
  80. $name = $input->getArgument('name');
  81. if (!$name) {
  82. throw new \InvalidArgumentException(
  83. 'Please specify a calendar name or --birthday');
  84. }
  85. }
  86. $calendarInfo = $this->calDav->getCalendarByUri(
  87. 'principals/users/' . $user,
  88. $name);
  89. if ($calendarInfo === null) {
  90. throw new \InvalidArgumentException(
  91. 'User <' . $user . '> has no calendar named <' . $name . '>. You can run occ dav:list-calendars to list calendars URIs for this user.');
  92. }
  93. $calendar = new Calendar(
  94. $this->calDav,
  95. $calendarInfo,
  96. $this->l10n,
  97. $this->config,
  98. $this->logger
  99. );
  100. $force = $input->getOption('force');
  101. if ($force) {
  102. $calendar->disableTrashbin();
  103. }
  104. $calendar->delete();
  105. return self::SUCCESS;
  106. }
  107. }