Files
fewo-stommer/app/Support/SystemSettings.php
T

164 lines
7.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Support;
use App\Models\SystemSetting;
use Illuminate\Support\Facades\Cache;
use Throwable;
/**
* Settings of the whole installation, owned by the operator. They are laid over the configuration at boot,
* before any landlord is known, so `config('fewo.*')` keeps being the one place the rest of the code reads.
*
* This is not the old `settings` table in a new coat: that one held a single landlord's operating data,
* which now lives on `tenants`. What is kept here belongs to no landlord and is never reachable from a
* landlord's admin.
*
* Database credentials and APP_KEY cannot be kept here. They are needed to open the database and to decrypt
* what is in it, so they have to be readable before any of this exists – they stay in the server's .env.
*/
class SystemSettings
{
/**
* Editable keys: config key => [label, kind, section, help]. The kind decides the form field and
* whether the value is stored encrypted.
*
* @var array<string, array{0: string, 1: string, 2: string, 3: string}>
*/
public const EDITABLE = [
'fewo.guest_retention_years' => ['Gästedaten löschen nach (Jahre)', 'int', 'Löschfristen', 'Obergrenze für alle Mandanten; ein Mandant kann kürzer löschen.'],
'fewo.retention.inquiry_months' => ['Nicht gebuchte Anfragen löschen nach (Monate)', 'int', 'Löschfristen', ''],
'fewo.retention.portal_email_excerpt_days' => ['Portal-Mail-Auszug löschen nach (Tage)', 'int', 'Löschfristen', ''],
'fewo.retention.attribution_months' => ['Herkunftsangaben löschen nach (Monate)', 'int', 'Löschfristen', ''],
'fewo.retention.process_test_run_days' => ['Berichte des Prozess-Testers löschen nach (Tage)', 'int', 'Löschfristen', 'Nur synthetische Testdaten; Grundlage 30 Tage (Entscheidung O3).'],
'fewo.spam.min_seconds' => ['Mindestzeit im Formular (Sekunden)', 'int', 'Bot-Schutz', 'Untergrenze; ein Mandant kann länger warten lassen.'],
'fewo.spam.per_ip_per_hour' => ['Anfragen je IP und Stunde', 'int', 'Bot-Schutz', ''],
'fewo.spam.per_email_per_day' => ['Anfragen je E-Mail und Tag', 'int', 'Bot-Schutz', ''],
'fewo.spam.open_requests_per_email' => ['Gleichzeitig offene Anfragen je E-Mail', 'int', 'Bot-Schutz', ''],
'fewo.spam.max_links' => ['Erlaubte Links im Text', 'int', 'Bot-Schutz', ''],
'fewo.spam.total_per_hour' => ['Anfragen aller Websites zusammen, je Stunde', 'int', 'Bot-Schutz', 'Flutgrenze über alle Mandanten. Kein Mandant kann sie anheben.'],
'fewo.channels.stale_after_minutes' => ['Portal-Abgleich gilt als veraltet nach (Minuten)', 'int', 'Portale', ''],
'fewo.channels.task_reminder_after_minutes' => ['Erinnerung an Portal-Aufgaben nach (Minuten)', 'int', 'Portale', ''],
'fewo.backup.keep' => ['Aufbewahrte Datenbank-Sicherungen', 'int', 'Sicherung', 'Eine Sicherung der ganzen Datenbank, über alle Mandanten.'],
'mail.default' => ['Versandweg', 'mailer', 'E-Mail-Versand', '„log“ schreibt jede E-Mail nur in die Logdatei und versendet nichts.'],
'mail.mailers.smtp.host' => ['SMTP-Server', 'text', 'E-Mail-Versand', ''],
'mail.mailers.smtp.port' => ['SMTP-Port', 'int', 'E-Mail-Versand', ''],
'mail.mailers.smtp.username' => ['SMTP-Benutzer', 'text', 'E-Mail-Versand', 'Die Absenderadresse eines Mandanten muss zu diesem Postfach passen, sonst lehnt Strato ab.'],
'mail.mailers.smtp.password' => ['SMTP-Passwort', 'secret', 'E-Mail-Versand', ''],
'fewo.mail_redirect_to' => ['Harte Umleitung aller E-Mails', 'text', 'E-Mail-Versand', 'Gefüllt: JEDE E-Mail dieses Servers geht nur hierhin, auch bei scharf geschalteten Mandanten. Leer: die Betriebsart jedes Mandanten entscheidet allein.'],
'fewo.scheduler_token' => ['Scheduler-Token', 'secret', 'Schnittstellen', 'Für GET /automation/scheduler. Leer = abgeschaltet.'],
'fewo.channels.health_token' => ['Health-Token', 'secret', 'Schnittstellen', 'Für GET /health/sync. Leer = abgeschaltet.'],
'fewo.automation.webhook_url' => ['Automation: Adresse', 'text', 'Schnittstellen', ''],
'fewo.automation.secret' => ['Automation: Geheimnis', 'secret', 'Schnittstellen', ''],
];
private const CACHE_KEY = 'system-settings';
/**
* What the server configured, before anything stored was laid over it. Captured once per process.
*
* @var array<string, mixed>
*/
private static array $baseline = [];
/**
* Lays the stored values over the configuration. Called at boot before any landlord is resolved, so
* TenantConfig takes these as its baseline.
*/
public static function apply(): void
{
$stored = self::stored();
$values = [];
foreach (array_keys(self::EDITABLE) as $key) {
if (! array_key_exists($key, self::$baseline)) {
self::$baseline[$key] = config($key);
}
// A key the operator cleared has to give the .env value back in the same request, not only
// after the next boot – otherwise an emptied field looks saved while the old value still acts.
$values[$key] = array_key_exists($key, $stored)
? self::cast($key, $stored[$key])
: self::$baseline[$key];
}
config($values);
}
/**
* The value in force: what the operator stored, else what the server's .env configured.
*/
public static function get(string $key): mixed
{
$stored = self::stored();
return array_key_exists($key, $stored) ? self::cast($key, $stored[$key]) : config($key);
}
/**
* @param array<string, mixed> $values config key => value; null or "" falls back to the .env again
*/
public static function save(array $values): void
{
foreach ($values as $key => $value) {
if (! array_key_exists($key, self::EDITABLE)) {
continue;
}
$isSecret = self::EDITABLE[$key][1] === 'secret';
if (blank($value)) {
SystemSetting::query()->where('key', $key)->delete();
continue;
}
SystemSetting::query()->updateOrCreate(
['key' => $key],
['value' => $isSecret ? encrypt((string) $value) : (string) $value, 'is_secret' => $isSecret],
);
}
Cache::forget(self::CACHE_KEY);
self::apply();
}
public static function forget(): void
{
Cache::forget(self::CACHE_KEY);
self::$baseline = [];
}
/**
* @return array<string, ?string>
*/
private static function stored(): array
{
try {
return Cache::rememberForever(self::CACHE_KEY, fn (): array => SystemSetting::query()->pluck('value', 'key')->all());
} catch (Throwable) {
return []; // before the migrations ran
}
}
private static function cast(string $key, ?string $value): mixed
{
if ($value === null) {
return null;
}
$kind = self::EDITABLE[$key][1] ?? 'text';
return match ($kind) {
'int' => (int) $value,
'secret' => rescue(fn (): string => decrypt($value), null, report: false),
default => $value,
};
}
}