DeleteCalendar.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. /** @var CalDavBackend */
  38. private $calDav;
  39. /** @var IConfig */
  40. private $config;
  41. /** @var IL10N */
  42. private $l10n;
  43. /** @var IUserManager */
  44. private $userManager;
  45. /** @var LoggerInterface */
  46. private $logger;
  47. /**
  48. * @param CalDavBackend $calDav
  49. * @param IConfig $config
  50. * @param IL10N $l10n
  51. * @param IUserManager $userManager
  52. */
  53. public function __construct(
  54. CalDavBackend $calDav,
  55. IConfig $config,
  56. IL10N $l10n,
  57. IUserManager $userManager,
  58. LoggerInterface $logger
  59. ) {
  60. parent::__construct();
  61. $this->calDav = $calDav;
  62. $this->config = $config;
  63. $this->l10n = $l10n;
  64. $this->userManager = $userManager;
  65. $this->logger = $logger;
  66. }
  67. protected function configure(): void {
  68. $this
  69. ->setName('dav:delete-calendar')
  70. ->setDescription('Delete a dav calendar')
  71. ->addArgument('uid',
  72. InputArgument::REQUIRED,
  73. 'User who owns the calendar')
  74. ->addArgument('name',
  75. InputArgument::OPTIONAL,
  76. 'Name of the calendar to delete')
  77. ->addOption('birthday',
  78. null,
  79. InputOption::VALUE_NONE,
  80. 'Delete the birthday calendar')
  81. ->addOption('force',
  82. 'f',
  83. InputOption::VALUE_NONE,
  84. 'Force delete skipping trashbin');
  85. }
  86. protected function execute(
  87. InputInterface $input,
  88. OutputInterface $output
  89. ): int {
  90. /** @var string $user **/
  91. $user = $input->getArgument('uid');
  92. if (!$this->userManager->userExists($user)) {
  93. throw new \InvalidArgumentException(
  94. 'User <' . $user . '> is unknown.');
  95. }
  96. $birthday = $input->getOption('birthday');
  97. if ($birthday !== false) {
  98. $name = BirthdayService::BIRTHDAY_CALENDAR_URI;
  99. } else {
  100. /** @var string $name **/
  101. $name = $input->getArgument('name');
  102. if (!$name) {
  103. throw new \InvalidArgumentException(
  104. 'Please specify a calendar name or --birthday');
  105. }
  106. }
  107. $calendarInfo = $this->calDav->getCalendarByUri(
  108. 'principals/users/' . $user,
  109. $name);
  110. if ($calendarInfo === null) {
  111. throw new \InvalidArgumentException(
  112. 'User <' . $user . '> has no calendar named <' . $name . '>. You can run occ dav:list-calendars to list calendars URIs for this user.');
  113. }
  114. $calendar = new Calendar(
  115. $this->calDav,
  116. $calendarInfo,
  117. $this->l10n,
  118. $this->config,
  119. $this->logger
  120. );
  121. $force = $input->getOption('force');
  122. if ($force) {
  123. $calendar->disableTrashbin();
  124. }
  125. $calendar->delete();
  126. return 0;
  127. }
  128. }