Bugfixes, Postgres improved support

This commit is contained in:
2020-06-25 16:54:58 +02:00
parent 2bbc895496
commit a0b935c082
19 changed files with 350 additions and 125 deletions

View File

@@ -1,3 +1 @@
Mail\.class\.php
JWT\.class\.php
Database\.class\.php

View File

@@ -2,53 +2,33 @@
namespace Configuration;
use Error;
use Objects\ConnectionData;
class Configuration {
private ?ConnectionData $database;
private ?ConnectionData $mail;
private ?KeyData $jwt;
private Settings $settings;
function __construct() {
}
$this->database = null;
$this->settings = Settings::loadDefaults();
public function load() {
try {
$classes = array(
\Configuration\Database::class => &$this->database,
\Configuration\Mail::class => &$this->mail,
\Configuration\JWT::class => &$this->jwt
);
$success = true;
foreach($classes as $class => &$ref) {
$path = getClassPath($class);
if(!file_exists($path)) {
$success = false;
} else {
include_once $path;
if(class_exists($class)) {
$ref = new $class();
}
}
$class = \Configuration\Database::class;
$path = getClassPath($class, true);
if(file_exists($path) && is_readable($path)) {
include_once $path;
if(class_exists($class)) {
$this->database = new \Configuration\Database();
}
return $success;
} catch(Error $e) {
die($e);
}
}
public function getDatabase() { return $this->database; }
public function getJWT() { return $this->jwt; }
public function getMail() { return $this->mail; }
public function getDatabase() : ?ConnectionData {
return $this->database;
}
public function isFilePresent($className) {
$path = getClassPath("\\Configuration\\$className");
return file_exists($path);
public function getSettings() : Settings {
return $this->settings;
}
public function create(string $className, $data) {

View File

@@ -8,6 +8,9 @@ use \Driver\SQL\Strategy\CascadeStrategy;
class CreateDatabase {
// NOTE:
// explicit serial ids removed due to postgres' serial implementation
public static function createQueries(SQL $sql) {
$queries = array();
@@ -21,8 +24,8 @@ class CreateDatabase {
->unique("name");
$queries[] = $sql->insert("Language", array("uid", "code", "name"))
->addRow(1, "en_US", 'American English')
->addRow(2, "de_DE", 'Deutsch Standard');
->addRow( "en_US", 'American English')
->addRow( "de_DE", 'Deutsch Standard');
$queries[] = $sql->createTable("User")
->addSerial("uid")
@@ -72,9 +75,9 @@ class CreateDatabase {
->unique("name");
$queries[] = $sql->insert("Group", array("uid", "name", "color"))
->addRow(USER_GROUP_MODERATOR, USER_GROUP_MODERATOR_NAME, "#007bff")
->addRow(USER_GROUP_SUPPORT, USER_GROUP_SUPPORT_NAME, "#28a745")
->addRow(USER_GROUP_ADMIN, USER_GROUP_ADMIN_NAME, "#dc3545");
->addRow(USER_GROUP_MODERATOR_NAME, "#007bff")
->addRow(USER_GROUP_SUPPORT_NAME, "#28a745")
->addRow(USER_GROUP_ADMIN_NAME, "#dc3545");
$queries[] = $sql->createTable("UserGroup")
->addInt("user_id")
@@ -137,6 +140,22 @@ class CreateDatabase {
->addRow("^/acceptInvite(/)?$", "dynamic", "\\Documents\\Account", "\\Views\\Account\\AcceptInvite")
->addRow("^/$", "static", "/static/welcome.html", NULL);
$queries[] = $sql->createTable("Settings")
->addString("name", 32)
->addString("value", 1024, true)
->primaryKey("name");
$settingsQuery = $sql->insert("Settings", array("name", "value"))
// ->addRow("mail_enabled", "0") # this key will be set during installation
->addRow("mail_host", "")
->addRow("mail_port", "")
->addRow("mail_username", "")
->addRow("mail_password", "")
->addRow("mail_from", "");
(Settings::loadDefaults())->addRows($settingsQuery);
$queries[] = $settingsQuery;
return $queries;
}
}

View File

@@ -1,17 +0,0 @@
<?php
namespace Configuration;
class KeyData {
protected string $key;
public function __construct(string $key) {
$this->key = $key;
}
public function getKey() {
return $this->key;
}
}

View File

@@ -0,0 +1,71 @@
<?php
/**
* Do not change settings here, they are dynamically loaded from database.
*/
namespace Configuration;
use Driver\SQL\Query\Insert;
use Objects\User;
class Settings {
private string $siteName;
private string $baseUrl;
private string $jwtSecret;
private bool $installationComplete;
private bool $registrationAllowed;
public function getJwtSecret(): string {
return $this->jwtSecret;
}
public function isInstalled() {
return $this->installationComplete;
}
public static function loadDefaults() : Settings {
$hostname = php_uname("n");
$protocol = getProtocol();
$jwt = generateRandomString(32);
$settings = new Settings();
$settings->siteName = "WebBase";
$settings->baseUrl = "$protocol://$hostname";
$settings->jwtSecret = $jwt;
$settings->installationComplete = false;
$settings->registrationAllowed = false;
return $settings;
}
public function loadFromDatabase(User $user) {
$req = new \Api\Settings\Get($user);
$success = $req->execute();
if ($success) {
$result = $req->getResult()["settings"];
$this->siteName = $result["site_name"] ?? $this->siteName;
$this->registrationAllowed = $result["user_registration_enabled"] ?? $this->registrationAllowed;
$this->installationComplete = $result["installation_completed"] ?? $this->installationComplete;
$this->jwtSecret = $result["jwt_secret"] ?? $this->jwtSecret;
if (!isset($result["jwt_secret"])) {
$req = new \Api\Settings\Set($user);
$req->execute(array("settings" => array(
"jwt_secret" => $this->jwtSecret
)));
}
}
return false;
}
public function addRows(Insert $query) {
$query->addRow("site_name", $this->siteName)
->addRow("base_url", $this->baseUrl)
->addRow("user_registration_enabled", $this->registrationAllowed ? "1" : "0")
->addRow("installation_completed", $this->installationComplete ? "1" : "0")
->addRow("jwt_secret", $this->jwtSecret);
}
}