MoveCalendar.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Thomas Citharel <nextcloud@tcit.fr>
  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 John Molakvoæ <skjnldsv@protonmail.com>
  9. * @author Thomas Citharel <nextcloud@tcit.fr>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\DAV\Command;
  28. use OCA\DAV\CalDAV\CalDavBackend;
  29. use OCA\DAV\CalDAV\Calendar;
  30. use OCP\IConfig;
  31. use OCP\IGroupManager;
  32. use OCP\IL10N;
  33. use OCP\IUserManager;
  34. use OCP\Share\IManager as IShareManager;
  35. use Psr\Log\LoggerInterface;
  36. use Symfony\Component\Console\Command\Command;
  37. use Symfony\Component\Console\Input\InputArgument;
  38. use Symfony\Component\Console\Input\InputInterface;
  39. use Symfony\Component\Console\Input\InputOption;
  40. use Symfony\Component\Console\Output\OutputInterface;
  41. use Symfony\Component\Console\Style\SymfonyStyle;
  42. class MoveCalendar extends Command {
  43. private ?SymfonyStyle $io = null;
  44. public const URI_USERS = 'principals/users/';
  45. public function __construct(
  46. private IUserManager $userManager,
  47. private IGroupManager $groupManager,
  48. private IShareManager $shareManager,
  49. private IConfig $config,
  50. private IL10N $l10n,
  51. private CalDavBackend $calDav,
  52. private LoggerInterface $logger,
  53. ) {
  54. parent::__construct();
  55. }
  56. protected function configure(): void {
  57. $this
  58. ->setName('dav:move-calendar')
  59. ->setDescription('Move a calendar from an user to another')
  60. ->addArgument('name',
  61. InputArgument::REQUIRED,
  62. 'Name of the calendar to move')
  63. ->addArgument('sourceuid',
  64. InputArgument::REQUIRED,
  65. 'User who currently owns the calendar')
  66. ->addArgument('destinationuid',
  67. InputArgument::REQUIRED,
  68. 'User who will receive the calendar')
  69. ->addOption('force', 'f', InputOption::VALUE_NONE, "Force the migration by removing existing shares and renaming calendars in case of conflicts");
  70. }
  71. protected function execute(InputInterface $input, OutputInterface $output): int {
  72. $userOrigin = $input->getArgument('sourceuid');
  73. $userDestination = $input->getArgument('destinationuid');
  74. $this->io = new SymfonyStyle($input, $output);
  75. if (!$this->userManager->userExists($userOrigin)) {
  76. throw new \InvalidArgumentException("User <$userOrigin> is unknown.");
  77. }
  78. if (!$this->userManager->userExists($userDestination)) {
  79. throw new \InvalidArgumentException("User <$userDestination> is unknown.");
  80. }
  81. $name = $input->getArgument('name');
  82. $newName = null;
  83. $calendar = $this->calDav->getCalendarByUri(self::URI_USERS . $userOrigin, $name);
  84. if (null === $calendar) {
  85. throw new \InvalidArgumentException("User <$userOrigin> has no calendar named <$name>. You can run occ dav:list-calendars to list calendars URIs for this user.");
  86. }
  87. // Calendar already exists
  88. if ($this->calendarExists($userDestination, $name)) {
  89. if ($input->getOption('force')) {
  90. // Try to find a suitable name
  91. $newName = $this->getNewCalendarName($userDestination, $name);
  92. // If we didn't find a suitable value after all the iterations, give up
  93. if ($this->calendarExists($userDestination, $newName)) {
  94. throw new \InvalidArgumentException("Unable to find a suitable calendar name for <$userDestination> with initial name <$name>.");
  95. }
  96. } else {
  97. throw new \InvalidArgumentException("User <$userDestination> already has a calendar named <$name>.");
  98. }
  99. }
  100. $hadShares = $this->checkShares($calendar, $userOrigin, $userDestination, $input->getOption('force'));
  101. if ($hadShares) {
  102. /**
  103. * Warn that share links have changed if there are shares
  104. */
  105. $this->io->note([
  106. "Please note that moving calendar " . $calendar['uri'] . " from user <$userOrigin> to <$userDestination> has caused share links to change.",
  107. "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\""
  108. ]);
  109. }
  110. $this->calDav->moveCalendar($name, self::URI_USERS . $userOrigin, self::URI_USERS . $userDestination, $newName);
  111. $this->io->success("Calendar <$name> was moved from user <$userOrigin> to <$userDestination>" . ($newName ? " as <$newName>" : ''));
  112. return self::SUCCESS;
  113. }
  114. /**
  115. * Check if the calendar exists for user
  116. */
  117. protected function calendarExists(string $userDestination, string $name): bool {
  118. return null !== $this->calDav->getCalendarByUri(self::URI_USERS . $userDestination, $name);
  119. }
  120. /**
  121. * Try to find a suitable new calendar name that
  122. * doesn't exist for the provided user
  123. */
  124. protected function getNewCalendarName(string $userDestination, string $name): string {
  125. $increment = 1;
  126. $newName = $name . '-' . $increment;
  127. while ($increment <= 10) {
  128. $this->io->writeln("Trying calendar name <$newName>", OutputInterface::VERBOSITY_VERBOSE);
  129. if (!$this->calendarExists($userDestination, $newName)) {
  130. // New name is good to go
  131. $this->io->writeln("Found proper new calendar name <$newName>", OutputInterface::VERBOSITY_VERBOSE);
  132. break;
  133. }
  134. $newName = $name . '-' . $increment;
  135. $increment++;
  136. }
  137. return $newName;
  138. }
  139. /**
  140. * Check that moving the calendar won't break shares
  141. *
  142. * @return bool had any shares or not
  143. * @throws \InvalidArgumentException
  144. */
  145. private function checkShares(array $calendar, string $userOrigin, string $userDestination, bool $force = false): bool {
  146. $shares = $this->calDav->getShares($calendar['id']);
  147. foreach ($shares as $share) {
  148. [, $prefix, $userOrGroup] = explode('/', $share['href'], 3);
  149. /**
  150. * Check that user destination is member of the groups which whom the calendar was shared
  151. * If we ask to force the migration, the share with the group is dropped
  152. */
  153. if ($this->shareManager->shareWithGroupMembersOnly() === true && 'groups' === $prefix && !$this->groupManager->isInGroup($userDestination, $userOrGroup)) {
  154. if ($force) {
  155. $this->calDav->updateShares(new Calendar($this->calDav, $calendar, $this->l10n, $this->config, $this->logger), [], ['principal:principals/groups/' . $userOrGroup]);
  156. } else {
  157. 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.");
  158. }
  159. }
  160. /**
  161. * Check that calendar isn't already shared with user destination
  162. */
  163. if ($userOrGroup === $userDestination) {
  164. if ($force) {
  165. $this->calDav->updateShares(new Calendar($this->calDav, $calendar, $this->l10n, $this->config, $this->logger), [], ['principal:principals/users/' . $userOrGroup]);
  166. } else {
  167. 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.");
  168. }
  169. }
  170. }
  171. return count($shares) > 0;
  172. }
  173. }