Skip to content
Snippets Groups Projects
Commit bfa7456a authored by Rich Lott / Artful Robot's avatar Rich Lott / Artful Robot
Browse files

Add a validateConfig parameter on Inlay.get API for upgrader scripts

parent c08ba9f7
Branches
Tags
No related merge requests found
......@@ -5,7 +5,19 @@ use Civi\Api4\Generic\DAOGetAction;
use Civi\Api4\Generic\Result;
use Civi\Inlay\Config as InlayConfig;
class Get extends DAOGetAction {
/**
*
* @method Get setValidateConfig(bool $validateConfig)
*/
class Get extends DAOGetAction {
/**
* Whether to validate the config. For upgrader use only.
*
* @var bool
* @default TRUE;
*/
protected $validateConfig;
/**
* Override the _run method.
......@@ -15,34 +27,46 @@ class Get extends DAOGetAction {
$select = $this->getSelect();
$weCanAddBundleUrl = ($select === [] || (in_array('public_id', $select)));
$weCanAddConfig = ($select === [] || (count(array_intersect($select, ['class', 'public_id', 'config', 'name'])) === 4));
$configDataLoaded = ($select === [] || (in_array('public_id', $select)));
$weCanValidateConfig = ($select === [] || (count(array_intersect($select, ['class', 'public_id', 'config', 'name'])) === 4));
$inlayConfig = InlayConfig::singleton();
foreach ($result as &$item) {
if ($weCanAddBundleUrl) {
$item['scriptUrl'] = $inlayConfig->getBundleUrl($item['public_id']);
}
if ($weCanAddConfig) {
// Allow the class to validate the input.
$item['config'] = json_decode($item['config'], TRUE);
try {
$inlay = \Civi\Inlay\Type::fromArray($item);
$item['config'] = $inlay->config;
$item['scriptUrl'] = $inlay->getBundleUrl();
}
catch (\RuntimeException $e) {
if ($e->getCode() === \Civi\Inlay\Type::INVALID_SUBCLASS) {
$item['error'] = $e->getMessage();
if ($this->validateConfig) {
// Normally we only allow access to 'config' if we can validate it.
if ($weCanValidateConfig) {
// Allow the class to validate the input.
$item['config'] = json_decode($item['config'] ?? '[]', TRUE);
try {
$inlay = \Civi\Inlay\Type::fromArray($item);
$item['config'] = $inlay->config;
}
else {
throw $e;
catch (\RuntimeException $e) {
if ($e->getCode() === \Civi\Inlay\Type::INVALID_SUBCLASS) {
$item['error'] = $e->getMessage();
}
else {
throw $e;
}
}
}
else {
// Do not allow access to the config unless we have enough to validate it with the class.
unset($item['config']);
}
}
else {
// Do not allow access to the config unless we have enough to validate it with the class.
unset($item['config']);
// Special case: do NOT validate the config. This can be used by
// Upgrader scripts that need to munge config; if they only ever
// got valid config they might lose access to deprecated values.
if ($configDataLoaded) {
$item['config'] = json_decode($item['config'] ?? '[]', TRUE);
}
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment