Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to force rename a conflicting calendar #24604

Merged
merged 1 commit into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/dav/lib/CalDAV/CalDavBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -2744,11 +2744,13 @@ public function purgeAllCachedEventsForSubscription($subscriptionId) {
* @param string $uriName
* @param string $uriOrigin
* @param string $uriDestination
* @param string $newUriName (optional) the new uriName
*/
public function moveCalendar($uriName, $uriOrigin, $uriDestination) {
public function moveCalendar($uriName, $uriOrigin, $uriDestination, $newUriName = null) {
$query = $this->db->getQueryBuilder();
$query->update('calendars')
->set('principaluri', $query->createNamedParameter($uriDestination))
->set('uri', $query->createNamedParameter($newUriName ?: $uriName))
skjnldsv marked this conversation as resolved.
Show resolved Hide resolved
->where($query->expr()->eq('principaluri', $query->createNamedParameter($uriOrigin)))
->andWhere($query->expr()->eq('uri', $query->createNamedParameter($uriName)))
->execute();
Expand Down
84 changes: 68 additions & 16 deletions apps/dav/lib/Command/MoveCalendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected function configure() {
->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 {
Expand All @@ -122,34 +122,93 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$name = $input->getArgument('name');
$newName = null;

$calendar = $this->calDav->getCalendarByUri(self::URI_USERS . $userOrigin, $name);

if (null === $calendar) {
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);
skjnldsv marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* 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);
Expand Down Expand Up @@ -177,14 +236,7 @@ private function checkShares(array $calendar, string $userOrigin, string $userDe
}
}
}
/**
* 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;
}
}