settings values json instead of strings

This commit is contained in:
2024-04-11 14:41:03 -04:00
parent 3851b7f289
commit 3888e7fcde
9 changed files with 111 additions and 47 deletions

View File

@@ -19,7 +19,7 @@ namespace Core\API {
if ($this->success) {
$settings = $req->getResult()["settings"];
if (!isset($settings["mail_enabled"]) || $settings["mail_enabled"] !== "1") {
if (!isset($settings["mail_enabled"]) || !$settings["mail_enabled"]) {
$this->createError("Mailing is not configured on this server yet.");
return null;
}

View File

@@ -171,6 +171,8 @@ abstract class Request {
if ($this->externalCall) {
$trustedDomains = $this->getCORS();
if (!empty($trustedDomains)) {
// TODO: origins require a protocol, e.g. https:// or http:// as prefix.
// should we force https for all origins? or make exceptions for localhost?
header("Access-Control-Allow-Origin: " . implode(", ", $trustedDomains));
}
}

View File

@@ -3,10 +3,27 @@
namespace Core\API {
use Core\Objects\Context;
use Core\API\Parameter\ArrayType;
use Core\API\Parameter\Parameter;
use Core\API\Parameter\StringType;
abstract class SettingsAPI extends Request {
protected array $predefinedKeys;
public function __construct(Context $context, bool $externalCall = false, array $params = array()) {
parent::__construct($context, $externalCall, $params);
// TODO: improve this, additional validation for allowed chars etc.
// API parameters should be more configurable, e.g. allow regexes, min/max values for numbers, etc.
$this->predefinedKeys = [
"allowed_extensions" => new ArrayType("allowed_extensions", Parameter::TYPE_STRING),
"trusted_domains" => new ArrayType("allowed_extensions", Parameter::TYPE_STRING),
"user_registration_enabled" => new Parameter("user_registration_enabled", Parameter::TYPE_BOOLEAN),
"recaptcha_enabled" => new Parameter("recaptcha_enabled", Parameter::TYPE_BOOLEAN),
"mail_enabled" => new Parameter("mail_enabled", Parameter::TYPE_BOOLEAN),
"mail_port" => new Parameter("mail_port", Parameter::TYPE_INT)
];
}
}
}
@@ -53,7 +70,6 @@ namespace Core\API\Settings {
}
}
// TODO: we need additional validation for built-in settings here, e.g. csv-values, bool values, etc.
class Set extends SettingsAPI {
public function __construct(Context $context, bool $externalCall = false) {
parent::__construct($context, $externalCall, array(
@@ -68,14 +84,16 @@ namespace Core\API\Settings {
}
$paramKey = new StringType('key', 32);
$paramValue = new StringType('value', 1024, true, NULL);
$paramValueDefault = new StringType('value', 1024, true, NULL);
$sql = $this->context->getSQL();
$query = $sql->insert("Settings", array("name", "value"));
$query = $sql->insert("Settings", ["name", "value"]);
$keys = array();
$deleteKeys = array();
foreach ($values as $key => $value) {
$paramValue = $this->predefinedKeys[$key] ?? $paramValueDefault;
if (!$paramKey->parseParam($key)) {
$key = print_r($key, true);
return $this->createError("Invalid Type for key in parameter settings: '$key' (Required: " . $paramKey->getTypeName() . ")");
@@ -86,7 +104,7 @@ namespace Core\API\Settings {
return $this->createError("The property key should only contain alphanumeric characters, underscores and dashes");
} else {
if (!is_null($paramValue->value)) {
$query->addRow($paramKey->value, $paramValue->value);
$query->addRow($paramKey->value, json_encode($paramValue->value));
} else {
$deleteKeys[] = $paramKey->value;
}
@@ -111,8 +129,8 @@ namespace Core\API\Settings {
if (count($deleteKeys) !== count($keys)) {
$query->onDuplicateKeyStrategy(new UpdateStrategy(
array("name"),
array("value" => new Column("value")))
["name"],
["value" => new Column("value")])
);

View File

@@ -29,8 +29,8 @@ class Stats extends Request {
if ($this->success) {
$settings = $req->getResult()["settings"];
$this->mailConfigured = ($settings["mail_enabled"] ?? "0") === "1";
$this->recaptchaConfigured = ($settings["recaptcha_enabled"] ?? "0") === "1";
$this->mailConfigured = $settings["mail_enabled"];
$this->recaptchaConfigured = $settings["recaptcha_enabled"];
}
return $this->success;