aboutsummaryrefslogtreecommitdiffstats
path: root/apps/dav/lib/Command
diff options
context:
space:
mode:
authorJohn Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>2020-12-08 10:22:46 +0100
committerJohn Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>2020-12-10 09:12:28 +0100
commitfd01d2440268f97cb4c2365498688bb29044437d (patch)
tree9608f16ba2afcbacc13d4e60b16b033ee59a224f /apps/dav/lib/Command
parente02c9ec1bfc7eaabafe640dce87adb5efb013ad0 (diff)
downloadnextcloud-server-fd01d2440268f97cb4c2365498688bb29044437d.tar.gz
nextcloud-server-fd01d2440268f97cb4c2365498688bb29044437d.zip
Allow to force rename a conflicting calendar
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
Diffstat (limited to 'apps/dav/lib/Command')
-rw-r--r--apps/dav/lib/Command/MoveCalendar.php84
1 files changed, 68 insertions, 16 deletions
diff --git a/apps/dav/lib/Command/MoveCalendar.php b/apps/dav/lib/Command/MoveCalendar.php
index 04786e64780..00bf5966832 100644
--- a/apps/dav/lib/Command/MoveCalendar.php
+++ b/apps/dav/lib/Command/MoveCalendar.php
@@ -104,7 +104,7 @@ class MoveCalendar extends Command {
->addArgument('destinationuid',
InputArgument::REQUIRED,
'User who will receive the calendar')
- ->addOption('force', 'f', InputOption::VALUE_NONE, "Force the migration by removing existing shares");
+ ->addOption('force', 'f', InputOption::VALUE_NONE, "Force the migration by removing existing shares and renaming calendars in case of conflicts");
}
protected function execute(InputInterface $input, OutputInterface $output): int {
@@ -122,6 +122,7 @@ class MoveCalendar extends Command {
}
$name = $input->getArgument('name');
+ $newName = null;
$calendar = $this->calDav->getCalendarByUri(self::URI_USERS . $userOrigin, $name);
@@ -129,27 +130,85 @@ class MoveCalendar extends Command {
throw new \InvalidArgumentException("User <$userOrigin> has no calendar named <$name>. You can run occ dav:list-calendars to list calendars URIs for this user.");
}
- if (null !== $this->calDav->getCalendarByUri(self::URI_USERS . $userDestination, $name)) {
- throw new \InvalidArgumentException("User <$userDestination> already has a calendar named <$name>.");
+ // Calendar already exists
+ if ($this->calendarExists($userDestination, $name)) {
+ if ($input->getOption('force')) {
+ // Try to find a suitable name
+ $newName = $this->getNewCalendarName($userDestination, $name);
+
+ // If we didn't find a suitable value after all the iterations, give up
+ if ($this->calendarExists($userDestination, $newName)) {
+ throw new \InvalidArgumentException("Unable to find a suitable calendar name for <$userDestination> with initial name <$name>.");
+ }
+ } else {
+ throw new \InvalidArgumentException("User <$userDestination> already has a calendar named <$name>.");
+ }
}
- $this->checkShares($calendar, $userOrigin, $userDestination, $input->getOption('force'));
+ $hadShares = $this->checkShares($calendar, $userOrigin, $userDestination, $input->getOption('force'));
+ if ($hadShares) {
+ /**
+ * Warn that share links have changed if there are shares
+ */
+ $this->io->note([
+ "Please note that moving calendar " . $calendar['uri'] . " from user <$userOrigin> to <$userDestination> has caused share links to change.",
+ "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\""
+ ]);
+ }
- $this->calDav->moveCalendar($name, self::URI_USERS . $userOrigin, self::URI_USERS . $userDestination);
+ $this->calDav->moveCalendar($name, self::URI_USERS . $userOrigin, self::URI_USERS . $userDestination, $newName);
- $this->io->success("Calendar <$name> was moved from user <$userOrigin> to <$userDestination>");
+ $this->io->success("Calendar <$name> was moved from user <$userOrigin> to <$userDestination>" . ($newName ? " as <$newName>" : ''));
return 0;
}
/**
+ * Check if the calendar exists for user
+ *
+ * @param string $userDestination
+ * @param string $name
+ * @return bool
+ */
+ protected function calendarExists(string $userDestination, string $name): bool {
+ return null !== $this->calDav->getCalendarByUri(self::URI_USERS . $userDestination, $name);
+ }
+
+ /**
+ * Try to find a suitable new calendar name that
+ * doesn't exists for the provided user
+ *
+ * @param string $userDestination
+ * @param string $name
+ * @return string
+ */
+ protected function getNewCalendarName(string $userDestination, string $name): string {
+ $increment = 1;
+ $newName = $name . '-' . $increment;
+ while ($increment <= 10) {
+ $this->io->writeln("Trying calendar name <$newName>", OutputInterface::VERBOSITY_VERBOSE);
+ if (!$this->calendarExists($userDestination, $newName)) {
+ // New name is good to go
+ $this->io->writeln("Found proper new calendar name <$newName>", OutputInterface::VERBOSITY_VERBOSE);
+ break;
+ }
+ $newName = $name . '-' . $increment;
+ $increment++;
+ }
+
+ return $newName;
+ }
+
+ /**
* Check that moving the calendar won't break shares
*
* @param array $calendar
* @param string $userOrigin
* @param string $userDestination
* @param bool $force
+ * @return bool had any shares or not
+ * @throws \InvalidArgumentException
*/
- private function checkShares(array $calendar, string $userOrigin, string $userDestination, bool $force = false) {
+ private function checkShares(array $calendar, string $userOrigin, string $userDestination, bool $force = false): bool {
$shares = $this->calDav->getShares($calendar['id']);
foreach ($shares as $share) {
list(, $prefix, $userOrGroup) = explode('/', $share['href'], 3);
@@ -177,14 +236,7 @@ class MoveCalendar extends Command {
}
}
}
- /**
- * Warn that share links have changed if there are shares
- */
- if (count($shares) > 0) {
- $this->io->note([
- "Please note that moving calendar " . $calendar['uri'] . " from user <$userOrigin> to <$userDestination> has caused share links to change.",
- "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\""
- ]);
- }
+
+ return count($shares) > 0;
}
}