Fixed configuration save location to point to existing config folder [#2176]

This commit is contained in:
Matias Griese
2021-09-14 17:59:12 +03:00
parent ab68cbdd59
commit 1091e7ab26
3 changed files with 46 additions and 135 deletions

View File

@@ -2,7 +2,9 @@
## mm/dd/2021
1. [](#improved)
* Composer update
* Improved `Admin:data()` and `Admin::getConfigurationData()` to be more strict
2. [](#bugfix)
* Fixed configuration save location to point to existing config folder [#2176](https://github.com/getgrav/grav-plugin-admin/issues/2176)
# v1.10.20
## 09/01/2021

View File

@@ -867,115 +867,20 @@ class Admin
*
* @param string $type
* @param array $post
* @return mixed
* @return object
* @throws \RuntimeException
*/
public function data($type, array $post = [])
{
static $data = [];
if (isset($data[$type])) {
return $data[$type];
}
if (!$post) {
$post = $this->grav['uri']->post();
$post = $post['data'] ?? [];
$post = $this->grav['uri']->post()['data'] ?? [];
}
// Check to see if a data type is plugin-provided, before looking into core ones
$event = $this->grav->fireEvent('onAdminData', new Event(['type' => &$type]));
if ($event) {
if (isset($event['data_type'])) {
return $event['data_type'];
try {
return $this->getConfigurationData($type, $post);
} catch (\RuntimeException $e) {
return new Data\Data();
}
if (is_string($event['type'])) {
$type = $event['type'];
}
}
/** @var UniformResourceLocator $locator */
$locator = $this->grav['locator'];
$filename = $locator->findResource("config://{$type}.yaml", true, true);
$file = CompiledYamlFile::instance($filename);
if (preg_match('|plugins/|', $type)) {
/** @var Plugins $plugins */
$plugins = $this->grav['plugins'];
$obj = $plugins->get(preg_replace('|plugins/|', '', $type));
if (!$obj) {
return [];
}
$obj->merge($post);
$obj->file($file);
$data[$type] = $obj;
} elseif (preg_match('|themes/|', $type)) {
/** @var Themes $themes */
$themes = $this->grav['themes'];
$obj = $themes->get(preg_replace('|themes/|', '', $type));
if (!$obj) {
return [];
}
$obj->merge($post);
$obj->file($file);
$data[$type] = $obj;
} elseif (preg_match('|users?/|', $type)) {
/** @var UserCollectionInterface $users */
$users = $this->grav['accounts'];
$obj = $users->load(preg_replace('|users?/|', '', $type));
$obj->update($this->cleanUserPost($post));
$data[$type] = $obj;
} elseif (preg_match('|config/|', $type)) {
$type = preg_replace('|config/|', '', $type);
$blueprints = $this->blueprints("config/{$type}");
$config = $this->grav['config'];
$obj = new Data\Data($config->get($type, []), $blueprints);
$obj->merge($post);
// FIXME: We shouldn't allow user to change configuration files in system folder!
$filename = $this->grav['locator']->findResource("config://{$type}.yaml")
?: $this->grav['locator']->findResource("config://{$type}.yaml", true, true);
$file = CompiledYamlFile::instance($filename);
$obj->file($file);
$data[$type] = $obj;
} elseif (preg_match('|media-manager/|', $type)) {
$filename = base64_decode(preg_replace('|media-manager/|', '', $type));
$file = File::instance($filename);
$pages = static::enablePages();
$obj = new \stdClass();
$obj->title = $file->basename();
$obj->path = $file->filename();
$obj->file = $file;
$obj->page = $pages->get(dirname($obj->path));
$fileInfo = pathinfo($obj->title);
$filename = str_replace(['@3x', '@2x'], '', $fileInfo['filename']);
if (isset($fileInfo['extension'])) {
$filename .= '.' . $fileInfo['extension'];
}
if ($obj->page && isset($obj->page->media()[$filename])) {
$obj->metadata = new Data\Data($obj->page->media()[$filename]->metadata());
}
$data[$type] = $obj;
} else {
throw new \RuntimeException("Data type '{$type}' doesn't exist!");
}
return $data[$type];
}
/**
@@ -993,7 +898,16 @@ class Admin
static $data = [];
if (isset($data[$type])) {
return $data[$type];
$obj = $data[$type];
if ($post) {
if ($obj instanceof Data\Data) {
$obj = $this->mergePost($obj, $post);
} elseif ($obj instanceof UserInterface) {
$obj->update($this->cleanUserPost($post));
}
}
return $obj;
}
// Check to see if a data type is plugin-provided, before looking into core ones
@@ -1010,61 +924,48 @@ class Admin
/** @var UniformResourceLocator $locator */
$locator = $this->grav['locator'];
$filename = $locator->findResource("config://{$type}.yaml", true, true);
// Configuration file will be saved to the existing config stream.
$filename = $locator->findResource('config://') . "/{$type}.yaml";
$file = CompiledYamlFile::instance($filename);
if (preg_match('|plugins/|', $type)) {
$obj = Plugins::get(preg_replace('|plugins/|', '', $type));
if (null === $obj) {
return new \stdClass();
throw new \RuntimeException("Plugin '{$type}' doesn't exist!");
}
if ($post) {
$obj = $this->mergePost($obj, $post);
}
$obj->file($file);
$data[$type] = $obj;
} elseif (preg_match('|themes/|', $type)) {
/** @var Themes $themes */
$themes = $this->grav['themes'];
$obj = $themes->get(preg_replace('|themes/|', '', $type));
if (null === $obj) {
return new \stdClass();
throw new \RuntimeException("Theme '{$type}' doesn't exist!");
}
if ($post) {
$obj = $this->mergePost($obj, $post);
}
$obj->file($file);
$data[$type] = $obj;
} elseif (preg_match('|users?/|', $type)) {
/** @var UserCollectionInterface $users */
$users = $this->grav['accounts'];
$obj = $users->load(preg_replace('|users?/|', '', $type));
$obj->update($this->cleanUserPost($post));
$data[$type] = $obj;
} elseif (preg_match('|config/|', $type)) {
$type = preg_replace('|config/|', '', $type);
$blueprints = $this->blueprints("config/{$type}");
if (!$blueprints->form()) {
throw new \RuntimeException("Configuration type '{$type}' doesn't exist!");
}
// Configuration file will be saved to the existing config stream.
$filename = $locator->findResource('config://') . "/{$type}.yaml";
$file = CompiledYamlFile::instance($filename);
$config = $this->grav['config'];
$obj = new Data\Data($config->get($type, []), $blueprints);
if ($post) {
$obj = $this->mergePost($obj, $post);
}
// FIXME: We shouldn't allow user to change configuration files in system folder!
$filename = $this->grav['locator']->findResource("config://{$type}.yaml")
?: $this->grav['locator']->findResource("config://{$type}.yaml", true, true);
$file = CompiledYamlFile::instance($filename);
$obj->file($file);
$data[$type] = $obj;
} elseif (preg_match('|media-manager/|', $type)) {
$filename = base64_decode(preg_replace('|media-manager/|', '', $type));
@@ -1088,12 +989,20 @@ class Admin
$obj->metadata = new Data\Data($obj->page->media()[$filename]->metadata());
}
$data[$type] = $obj;
} else {
throw new \RuntimeException("Data type '{$type}' doesn't exist!");
}
return $data[$type];
$data[$type] = $obj;
if ($post) {
if ($obj instanceof Data\Data) {
$obj = $this->mergePost($obj, $post);
} elseif ($obj instanceof UserInterface) {
$obj->update($this->cleanUserPost($post));
}
}
return $obj;
}
/**

View File

@@ -174,11 +174,11 @@ class AdminController extends AdminBaseController
*/
protected function saveDefault()
{
try {
// Handle standard data types.
$type = $this->getDataType();
$obj = $this->admin->getConfigurationData($type, $this->data);
try {
$obj = $this->admin->getConfigurationData($type, $this->data);
$obj->validate();
} catch (\Exception $e) {
/** @var Debugger $debugger */