MoveCalendar.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 IUserManager $userManager;
  44. private IGroupManager $groupManager;
  45. private IShareManager $shareManager;
  46. private IConfig $config;
  47. private IL10N $l10n;
  48. private ?SymfonyStyle $io = null;
  49. private CalDavBackend $calDav;
  50. private LoggerInterface $logger;
  51. public const URI_USERS = 'principals/users/';
  52. public function __construct(
  53. IUserManager $userManager,
  54. IGroupManager $groupManager,
  55. IShareManager $shareManager,
  56. IConfig $config,
  57. IL10N $l10n,
  58. CalDavBackend $calDav,
  59. LoggerInterface $logger
  60. ) {
  61. parent::__construct();
  62. $this->userManager = $userManager;
  63. $this->groupManager = $groupManager;
  64. $this->shareManager = $shareManager;
  65. $this->config = $config;
  66. $this->l10n = $l10n;
  67. $this->calDav = $calDav;
  68. $this->logger = $logger;
  69. }
  70. protected function configure() {
  71. $this
  72. ->setName('dav:move-calendar')
  73. ->setDescription('Move a calendar from an user to another')
  74. ->addArgument('name',
  75. InputArgument::REQUIRED,
  76. 'Name of the calendar to move')
  77. ->addArgument('sourceuid',
  78. InputArgument::REQUIRED,
  79. 'User who currently owns the calendar')
  80. ->addArgument('destinationuid',
  81. InputArgument::REQUIRED,
  82. 'User who will receive the calendar')
  83. ->addOption('force', 'f', InputOption::VALUE_NONE, "Force the migration by removing existing shares and renaming calendars in case of conflicts");
  84. }
  85. protected function execute(InputInterface $input, OutputInterface $output): int {
  86. $userOrigin = $input->getArgument('sourceuid');
  87. $userDestination = $input->getArgument('destinationuid');
  88. $this->io = new SymfonyStyle($input, $output);
  89. if (!$this->userManager->userExists($userOrigin)) {
  90. throw new \InvalidArgumentException("User <$userOrigin> is unknown.");
  91. }
  92. if (!$this->userManager->userExists($userDestination)) {
  93. throw new \InvalidArgumentException("User <$userDestination> is unknown.");
  94. }
  95. $name = $input->getArgument('name');
  96. $newName = null;
  97. $calendar = $this->calDav->getCalendarByUri(self::URI_USERS . $userOrigin, $name);
  98. if (null === $calendar) {
  99. throw new \InvalidArgumentException("User <$userOrigin> has no calendar named <$name>. You can run occ dav:list-calendars to list calendars URIs for this user.");
  100. }
  101. // Calendar already exists
  102. if ($this->calendarExists($userDestination, $name)) {
  103. if ($input->getOption('force')) {
  104. // Try to find a suitable name
  105. $newName = $this->getNewCalendarName($userDestination, $name);
  106. // If we didn't find a suitable value after all the iterations, give up
  107. if ($this->calendarExists($userDestination, $newName)) {
  108. throw new \InvalidArgumentException("Unable to find a suitable calendar name for <$userDestination> with initial name <$name>.");
  109. }
  110. } else {
  111. throw new \InvalidArgumentException("User <$userDestination> already has a calendar named <$name>.");
  112. }
  113. }
  114. $hadShares = $this->checkShares($calendar, $userOrigin, $userDestination, $input->getOption('force'));
  115. if ($hadShares) {
  116. /**
  117. * Warn that share links have changed if there are shares
  118. */
  119. $this->io->note([
  120. "Please note that moving calendar " . $calendar['uri'] . " from user <$userOrigin> to <$userDestination> has caused share links to change.",
  121. "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\""
  122. ]);
  123. }
  124. $this->calDav->moveCalendar($name, self::URI_USERS . $userOrigin, self::URI_USERS . $userDestination, $newName);
  125. $this->io->success("Calendar <$name> was moved from user <$userOrigin> to <$userDestination>" . ($newName ? " as <$newName>" : ''));
  126. return 0;
  127. }
  128. /**
  129. * Check if the calendar exists for user
  130. *
  131. * @param string $userDestination
  132. * @param string $name
  133. * @return bool
  134. */
  135. protected function calendarExists(string $userDestination, string $name): bool {
  136. return null !== $this->calDav->getCalendarByUri(self::URI_USERS . $userDestination, $name);
  137. }
  138. /**
  139. * Try to find a suitable new calendar name that
  140. * doesn't exists for the provided user
  141. *
  142. * @param string $userDestination
  143. * @param string $name
  144. * @return string
  145. */
  146. protected function getNewCalendarName(string $userDestination, string $name): string {
  147. $increment = 1;
  148. $newName = $name . '-' . $increment;
  149. while ($increment <= 10) {
  150. $this->io->writeln("Trying calendar name <$newName>", OutputInterface::VERBOSITY_VERBOSE);
  151. if (!$this->calendarExists($userDestination, $newName)) {
  152. // New name is good to go
  153. $this->io->writeln("Found proper new calendar name <$newName>", OutputInterface::VERBOSITY_VERBOSE);
  154. break;
  155. }
  156. $newName = $name . '-' . $increment;
  157. $increment++;
  158. }
  159. return $newName;
  160. }
  161. /**
  162. * Check that moving the calendar won't break shares
  163. *
  164. * @param array $calendar
  165. * @param string $userOrigin
  166. * @param string $userDestination
  167. * @param bool $force
  168. * @return bool had any shares or not
  169. * @throws \InvalidArgumentException
  170. */
  171. private function checkShares(array $calendar, string $userOrigin, string $userDestination, bool $force = false): bool {
  172. $shares = $this->calDav->getShares($calendar['id']);
  173. foreach ($shares as $share) {
  174. [, $prefix, $userOrGroup] = explode('/', $share['href'], 3);
  175. /**
  176. * Check that user destination is member of the groups which whom the calendar was shared
  177. * If we ask to force the migration, the share with the group is dropped
  178. */
  179. if ($this->shareManager->shareWithGroupMembersOnly() === true && 'groups' === $prefix && !$this->groupManager->isInGroup($userDestination, $userOrGroup)) {
  180. if ($force) {
  181. $this->calDav->updateShares(new Calendar($this->calDav, $calendar, $this->l10n, $this->config, $this->logger), [], ['principal:principals/groups/' . $userOrGroup]);
  182. } else {
  183. 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.");
  184. }
  185. }
  186. /**
  187. * Check that calendar isn't already shared with user destination
  188. */
  189. if ($userOrGroup === $userDestination) {
  190. if ($force) {
  191. $this->calDav->updateShares(new Calendar($this->calDav, $calendar, $this->l10n, $this->config, $this->logger), [], ['principal:principals/users/' . $userOrGroup]);
  192. } else {
  193. 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.");
  194. }
  195. }
  196. }
  197. return count($shares) > 0;
  198. }
  199. }