MoveCalendar.php 6.5 KB

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