MoveCalendar.php 6.4 KB

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