Improve move page into itself check

This commit is contained in:
Matias Griese 2021-03-23 09:45:31 +02:00
parent eb89c00bd5
commit fa819064ef
2 changed files with 9 additions and 5 deletions

View File

@ -206,10 +206,6 @@ class PageObject extends FlexPageObject
// Make sure page isn't being moved under itself.
$key = $this->getStorageKey();
if ($key === $parentKey || strpos($parentKey, $key . '/') === 0) {
throw new RuntimeException(sprintf('Page /%s cannot be moved to %s', $this->getKey(), $parentRoute));
}
/** @var PageObject|null $parent */
$parent = $parentKey !== false ? $this->getFlexDirectory()->getObject($parentKey, 'storage_key') : null;
if (!$parent) {

View File

@ -373,7 +373,7 @@ class PageStorage extends FolderStorage
try {
if ($key === '' && empty($row['root'])) {
throw new RuntimeException('No storage key given');
throw new RuntimeException('Page has no path');
}
$grav = Grav::instance();
@ -394,9 +394,17 @@ class PageStorage extends FolderStorage
if ($oldFolder !== $newFolder && file_exists($oldFolder)) {
$isCopy = $row['__META']['copy'] ?? false;
if ($isCopy) {
if (strpos($newFolder, $oldFolder . '/') === 0) {
throw new RuntimeException(sprintf('Page /%s cannot be copied to itself', $oldKey));
}
$this->copyRow($oldKey, $newKey);
$debugger->addMessage("Page copied: {$oldFolder} => {$newFolder}", 'debug');
} else {
if (strpos($newFolder, $oldFolder . '/') === 0) {
throw new RuntimeException(sprintf('Page /%s cannot be moved to itself', $oldKey));
}
$this->renameRow($oldKey, $newKey);
$debugger->addMessage("Page moved: {$oldFolder} => {$newFolder}", 'debug');
}