MoveCalendar.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 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\CalDavBackend;
  8. use OCA\DAV\CalDAV\Calendar;
  9. use OCP\IConfig;
  10. use OCP\IGroupManager;
  11. use OCP\IL10N;
  12. use OCP\IUserManager;
  13. use OCP\Share\IManager as IShareManager;
  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. use Symfony\Component\Console\Style\SymfonyStyle;
  21. class MoveCalendar extends Command {
  22. private ?SymfonyStyle $io = null;
  23. public const URI_USERS = 'principals/users/';
  24. public function __construct(
  25. private IUserManager $userManager,
  26. private IGroupManager $groupManager,
  27. private IShareManager $shareManager,
  28. private IConfig $config,
  29. private IL10N $l10n,
  30. private CalDavBackend $calDav,
  31. private LoggerInterface $logger,
  32. ) {
  33. parent::__construct();
  34. }
  35. protected function configure(): void {
  36. $this
  37. ->setName('dav:move-calendar')
  38. ->setDescription('Move a calendar from an user to another')
  39. ->addArgument('name',
  40. InputArgument::REQUIRED,
  41. 'Name of the calendar to move')
  42. ->addArgument('sourceuid',
  43. InputArgument::REQUIRED,
  44. 'User who currently owns the calendar')
  45. ->addArgument('destinationuid',
  46. InputArgument::REQUIRED,
  47. 'User who will receive the calendar')
  48. ->addOption('force', 'f', InputOption::VALUE_NONE, 'Force the migration by removing existing shares and renaming calendars in case of conflicts');
  49. }
  50. protected function execute(InputInterface $input, OutputInterface $output): int {
  51. $userOrigin = $input->getArgument('sourceuid');
  52. $userDestination = $input->getArgument('destinationuid');
  53. $this->io = new SymfonyStyle($input, $output);
  54. if (!$this->userManager->userExists($userOrigin)) {
  55. throw new \InvalidArgumentException("User <$userOrigin> is unknown.");
  56. }
  57. if (!$this->userManager->userExists($userDestination)) {
  58. throw new \InvalidArgumentException("User <$userDestination> is unknown.");
  59. }
  60. $name = $input->getArgument('name');
  61. $newName = null;
  62. $calendar = $this->calDav->getCalendarByUri(self::URI_USERS . $userOrigin, $name);
  63. if ($calendar === null) {
  64. throw new \InvalidArgumentException("User <$userOrigin> has no calendar named <$name>. You can run occ dav:list-calendars to list calendars URIs for this user.");
  65. }
  66. // Calendar already exists
  67. if ($this->calendarExists($userDestination, $name)) {
  68. if ($input->getOption('force')) {
  69. // Try to find a suitable name
  70. $newName = $this->getNewCalendarName($userDestination, $name);
  71. // If we didn't find a suitable value after all the iterations, give up
  72. if ($this->calendarExists($userDestination, $newName)) {
  73. throw new \InvalidArgumentException("Unable to find a suitable calendar name for <$userDestination> with initial name <$name>.");
  74. }
  75. } else {
  76. throw new \InvalidArgumentException("User <$userDestination> already has a calendar named <$name>.");
  77. }
  78. }
  79. $hadShares = $this->checkShares($calendar, $userOrigin, $userDestination, $input->getOption('force'));
  80. if ($hadShares) {
  81. /**
  82. * Warn that share links have changed if there are shares
  83. */
  84. $this->io->note([
  85. 'Please note that moving calendar ' . $calendar['uri'] . " from user <$userOrigin> to <$userDestination> has caused share links to change.",
  86. 'Sharees will need to change "example.com/remote.php/dav/calendars/uid/' . $calendar['uri'] . "_shared_by_$userOrigin\" to \"example.com/remote.php/dav/calendars/uid/" . $newName ?: $calendar['uri'] . "_shared_by_$userDestination\""
  87. ]);
  88. }
  89. $this->calDav->moveCalendar($name, self::URI_USERS . $userOrigin, self::URI_USERS . $userDestination, $newName);
  90. $this->io->success("Calendar <$name> was moved from user <$userOrigin> to <$userDestination>" . ($newName ? " as <$newName>" : ''));
  91. return self::SUCCESS;
  92. }
  93. /**
  94. * Check if the calendar exists for user
  95. */
  96. protected function calendarExists(string $userDestination, string $name): bool {
  97. return $this->calDav->getCalendarByUri(self::URI_USERS . $userDestination, $name) !== null;
  98. }
  99. /**
  100. * Try to find a suitable new calendar name that
  101. * doesn't exist for the provided user
  102. */
  103. protected function getNewCalendarName(string $userDestination, string $name): string {
  104. $increment = 1;
  105. $newName = $name . '-' . $increment;
  106. while ($increment <= 10) {
  107. $this->io->writeln("Trying calendar name <$newName>", OutputInterface::VERBOSITY_VERBOSE);
  108. if (!$this->calendarExists($userDestination, $newName)) {
  109. // New name is good to go
  110. $this->io->writeln("Found proper new calendar name <$newName>", OutputInterface::VERBOSITY_VERBOSE);
  111. break;
  112. }
  113. $newName = $name . '-' . $increment;
  114. $increment++;
  115. }
  116. return $newName;
  117. }
  118. /**
  119. * Check that moving the calendar won't break shares
  120. *
  121. * @return bool had any shares or not
  122. * @throws \InvalidArgumentException
  123. */
  124. private function checkShares(array $calendar, string $userOrigin, string $userDestination, bool $force = false): bool {
  125. $shares = $this->calDav->getShares($calendar['id']);
  126. foreach ($shares as $share) {
  127. [, $prefix, $userOrGroup] = explode('/', $share['href'], 3);
  128. /**
  129. * Check that user destination is member of the groups which whom the calendar was shared
  130. * If we ask to force the migration, the share with the group is dropped
  131. */
  132. if ($this->shareManager->shareWithGroupMembersOnly() === true && $prefix === 'groups' && !$this->groupManager->isInGroup($userDestination, $userOrGroup)) {
  133. if ($force) {
  134. $this->calDav->updateShares(new Calendar($this->calDav, $calendar, $this->l10n, $this->config, $this->logger), [], ['principal:principals/groups/' . $userOrGroup]);
  135. } else {
  136. throw new \InvalidArgumentException("User <$userDestination> is not part of the group <$userOrGroup> with whom the calendar <" . $calendar['uri'] . '> was shared. You may use -f to move the calendar while deleting this share.');
  137. }
  138. }
  139. /**
  140. * Check that calendar isn't already shared with user destination
  141. */
  142. if ($userOrGroup === $userDestination) {
  143. if ($force) {
  144. $this->calDav->updateShares(new Calendar($this->calDav, $calendar, $this->l10n, $this->config, $this->logger), [], ['principal:principals/users/' . $userOrGroup]);
  145. } else {
  146. throw new \InvalidArgumentException('The calendar <' . $calendar['uri'] . "> is already shared to user <$userDestination>.You may use -f to move the calendar while deleting this share.");
  147. }
  148. }
  149. }
  150. return count($shares) > 0;
  151. }
  152. }