Loheide-eu-mitarbeiterplatt.../plugins-core/it-service/src/OpenNit/Settings.php
Friederich Loheide f1202bea08
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m50s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 7m34s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m45s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m30s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m7s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 7m9s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m8s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m5s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 48s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m5s
Sicherheitsupdate nach Audit (0.29.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 11:34:54 +00:00

114 lines
3.3 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 Inhaus\Plugins\ItService\OpenNit;
use App\Support\Settings as PortalSettings;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Validation\ValidationException;
use Throwable;
/**
* Zugangsdaten zu OpenNIT. Token und Signing-Secret liegen verschlüsselt in den Einstellungen.
*/
class Settings
{
public const URL = 'it_service.url';
public const TOKEN = 'it_service.token';
public const SECRET = 'it_service.secret';
/** null = Einstellung in OpenNIT, true = sofort reservieren, false = immer Anfrage */
public const AUTO_RESERVE = 'it_service.auto_reserve';
public const INTRO = 'it_service.intro';
public function __construct(private readonly PortalSettings $settings) {}
public function url(): string
{
return self::normalizeUrl((string) $this->settings->get(self::URL, ''));
}
/**
* Nur die Adresse des Servers eingetragen? Dann gehört „/api/ex“ dazu.
*/
public static function normalizeUrl(string $url): string
{
$url = rtrim(trim($url), '/');
if ($url !== '' && in_array((string) parse_url($url, PHP_URL_PATH), ['', '/'], true)) {
$url .= '/api/ex';
}
return $url;
}
public function token(): string
{
return $this->decrypt(self::TOKEN);
}
public function secret(): string
{
return $this->decrypt(self::SECRET);
}
public function configured(): bool
{
return $this->url() !== '' && $this->token() !== '' && $this->secret() !== '';
}
public function autoReserve(): ?bool
{
$value = $this->settings->get(self::AUTO_RESERVE);
return $value === null ? null : (bool) $value;
}
public function intro(): ?string
{
return $this->settings->get(self::INTRO) ?: null;
}
/**
* @param array{url: string, token?: ?string, secret?: ?string, auto_reserve: ?string, intro: ?string} $data leere Token/Secret = unverändert
*/
public function save(array $data): void
{
// Neue Adresse: Token und Secret neu eingeben – sonst gingen die gespeicherten an den neuen Server
if (self::normalizeUrl($data['url']) !== $this->url() && $this->token() !== '' && (blank($data['token'] ?? null) || blank($data['secret'] ?? null))) {
throw ValidationException::withMessages(['data.token' => 'Die Adresse wurde geändert – bitte Token und Signier-Secret erneut eingeben.']);
}
$this->settings->set(self::URL, self::normalizeUrl($data['url']));
foreach ([self::TOKEN => $data['token'] ?? null, self::SECRET => $data['secret'] ?? null] as $key => $value) {
if (filled($value)) {
$this->settings->set($key, Crypt::encryptString(trim($value)));
}
}
$this->settings->set(self::AUTO_RESERVE, match ($data['auto_reserve'] ?? '') {
'yes' => true,
'no' => false,
default => null,
});
$this->settings->set(self::INTRO, $data['intro'] ?? null);
}
private function decrypt(string $key): string
{
$value = $this->settings->get($key);
if (! is_string($value) || $value === '') {
return '';
}
try {
return Crypt::decryptString($value);
} catch (Throwable) {
return '';
}
}
}