1
0

MoveCalendar.php 6.3 KB

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