You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MoveCalendar.php 8.3KB

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