Pflege Adresse und Zugangsschlüssel der Shinkan-Anbindung in der Verwaltung
Werte aus der Umgebung des Servers haben Vorrang, sonst gilt der in der Verwaltung gepflegte. Der Schlüssel liegt verschlüsselt in site_settings und wird nie angezeigt. Änderung nur für Katalog- und Systemverwalter. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bcda189267
commit
26ca79cd22
8 files changed
+320
-17
No files matched your search
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Catalog;
|
||||
|
||||
use App\Domain\Design\SiteSetting;
|
||||
use App\Models\User;
|
||||
use Illuminate\Contracts\Encryption\DecryptException;
|
||||
use Illuminate\Support\Facades\Crypt;
|
||||
|
||||
/**
|
||||
* Adresse und Zugangsschlüssel der Shinkan-Anbindung. Zwei Quellen mit fester Rangfolge: Ein Wert aus der Umgebung des
|
||||
* Servers (`SHINKAN_DISTRIBUTION_URL`, `SHINKAN_DISTRIBUTION_KEY`) hat Vorrang. Fehlt er dort, gilt der in der Verwaltung
|
||||
* gepflegte Wert. So lässt sich die Schnittstelle ändern, ohne die Umgebung des Servers anzufassen.
|
||||
*
|
||||
* Der in der Verwaltung gepflegte Schlüssel liegt verschlüsselt (Anwendungsschlüssel) in den Einstellungen. Er wird nie
|
||||
* angezeigt, nie protokolliert und nicht in Fehlermeldungen genannt.
|
||||
*/
|
||||
final class ShinkanConnection
|
||||
{
|
||||
public const SETTING = 'shinkan.anbindung';
|
||||
|
||||
public const SERVER = 'server';
|
||||
|
||||
public const ADMIN = 'verwaltung';
|
||||
|
||||
public function url(): ?string
|
||||
{
|
||||
$value = $this->fromServer('services.shinkan.url') ?? ($this->stored()['url'] ?? null);
|
||||
|
||||
return filled($value) ? rtrim(trim((string) $value), '/') : null;
|
||||
}
|
||||
|
||||
public function apiKey(): ?string
|
||||
{
|
||||
$server = $this->fromServer('services.shinkan.key');
|
||||
|
||||
if ($server !== null) {
|
||||
return $server;
|
||||
}
|
||||
|
||||
$encrypted = $this->stored()['key'] ?? null;
|
||||
|
||||
if (! is_string($encrypted) || $encrypted === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return Crypt::decryptString($encrypted);
|
||||
} catch (DecryptException) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Woher kommt der Wert? `server`, `verwaltung` oder null.
|
||||
*/
|
||||
public function source(string $field): ?string
|
||||
{
|
||||
$config = $field === 'url' ? 'services.shinkan.url' : 'services.shinkan.key';
|
||||
|
||||
if ($this->fromServer($config) !== null) {
|
||||
return self::SERVER;
|
||||
}
|
||||
|
||||
return filled($this->stored()[$field] ?? null) && ($field === 'url' || $this->apiKey() !== null) ? self::ADMIN : null;
|
||||
}
|
||||
|
||||
public function configured(): bool
|
||||
{
|
||||
return $this->url() !== null && $this->apiKey() !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Speichert die in der Verwaltung gepflegten Werte. Ein leerer Schlüssel lässt den gespeicherten unverändert.
|
||||
* Werte aus der Umgebung des Servers bleiben davon unberührt und behalten den Vorrang.
|
||||
*/
|
||||
public function save(?string $url, ?string $key, ?User $by = null): void
|
||||
{
|
||||
$stored = $this->stored();
|
||||
$url = trim((string) $url);
|
||||
|
||||
if ($url !== '') {
|
||||
$parts = parse_url($url);
|
||||
|
||||
if ($parts === false || ! in_array($parts['scheme'] ?? '', ['http', 'https'], true) || empty($parts['host'])) {
|
||||
throw new DomainRuleException('Die Adresse ist keine gültige Internetadresse. Sie beginnt mit https:// (oder http://).');
|
||||
}
|
||||
|
||||
$stored['url'] = rtrim($url, '/');
|
||||
} else {
|
||||
unset($stored['url']);
|
||||
}
|
||||
|
||||
$key = trim((string) $key);
|
||||
|
||||
if ($key !== '') {
|
||||
$stored['key'] = Crypt::encryptString($key);
|
||||
}
|
||||
|
||||
$this->write($stored, $by);
|
||||
}
|
||||
|
||||
public function forgetKey(?User $by = null): void
|
||||
{
|
||||
$stored = $this->stored();
|
||||
unset($stored['key']);
|
||||
$this->write($stored, $by);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
private function stored(): array
|
||||
{
|
||||
$value = SiteSetting::query()->find(self::SETTING)?->value;
|
||||
|
||||
return is_array($value) ? $value : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, string> $stored
|
||||
*/
|
||||
private function write(array $stored, ?User $by): void
|
||||
{
|
||||
SiteSetting::query()->updateOrCreate(['key' => self::SETTING], ['value' => $stored, 'updated_by' => $by?->id]);
|
||||
}
|
||||
|
||||
private function fromServer(string $config): ?string
|
||||
{
|
||||
$value = config($config);
|
||||
|
||||
return is_string($value) && trim($value) !== '' ? trim($value) : null;
|
||||
}
|
||||
}
|
||||
@@ -34,22 +34,25 @@ class ShinkanImport
|
||||
|
||||
public function configured(): bool
|
||||
{
|
||||
return filled(config('services.shinkan.url')) && filled(config('services.shinkan.key'));
|
||||
return app(ShinkanConnection::class)->configured();
|
||||
}
|
||||
|
||||
/**
|
||||
* Zustand der Anbindung ohne Werte: Adresse und Schlüssel stehen nur in der Umgebungsdatei des Servers.
|
||||
* Zustand der Anbindung ohne Werte: ob Adresse und Schlüssel vorhanden sind und woher sie kommen.
|
||||
*
|
||||
* @return array{url: bool, key: bool, host: string|null}
|
||||
* @return array{url: bool, key: bool, host: string|null, url_source: string|null, key_source: string|null}
|
||||
*/
|
||||
public function status(): array
|
||||
{
|
||||
$url = (string) config('services.shinkan.url');
|
||||
$connection = app(ShinkanConnection::class);
|
||||
$url = $connection->url();
|
||||
|
||||
return [
|
||||
'url' => filled($url),
|
||||
'key' => filled(config('services.shinkan.key')),
|
||||
'host' => filled($url) ? (parse_url($url, PHP_URL_HOST) ?: null) : null,
|
||||
'url' => $url !== null,
|
||||
'key' => $connection->apiKey() !== null,
|
||||
'host' => $url !== null ? (parse_url($url, PHP_URL_HOST) ?: null) : null,
|
||||
'url_source' => $connection->source('url'),
|
||||
'key_source' => $connection->source('key'),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -62,7 +65,7 @@ public function status(): array
|
||||
public function check(): array
|
||||
{
|
||||
if (! $this->configured()) {
|
||||
return ['ok' => false, 'message' => 'Die Anbindung ist nicht eingerichtet. Adresse und Schlüssel fehlen in der Umgebung des Servers.'];
|
||||
return ['ok' => false, 'message' => 'Die Anbindung ist nicht eingerichtet. Adresse und Zugangsschlüssel fehlen.'];
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -81,7 +84,7 @@ public function fetch(): ExerciseImportRun
|
||||
$run = ExerciseImportRun::query()->create(['status' => 'laeuft', 'started_at' => now()]);
|
||||
|
||||
if (! $this->configured()) {
|
||||
return $this->finish($run, 'fehler', error: 'Die Anbindung ist nicht eingerichtet. Adresse und Schlüssel fehlen in der Umgebung.');
|
||||
return $this->finish($run, 'fehler', error: 'Die Anbindung ist nicht eingerichtet. Adresse und Zugangsschlüssel fehlen.');
|
||||
}
|
||||
|
||||
$previous = ExerciseImportRun::query()->where('status', 'ok')->orderByDesc('id')->value('cursor');
|
||||
@@ -340,10 +343,11 @@ private function createComposition(string $title, string $summary): Composition
|
||||
*/
|
||||
private function get(string $path, array $query = []): array
|
||||
{
|
||||
$response = Http::withToken((string) config('services.shinkan.key'))
|
||||
$connection = app(ShinkanConnection::class);
|
||||
$response = Http::withToken((string) $connection->apiKey())
|
||||
->acceptJson()
|
||||
->timeout(20)
|
||||
->get(rtrim((string) config('services.shinkan.url'), '/').'/api/distribution/v1'.$path, $query)
|
||||
->get((string) $connection->url().'/api/distribution/v1'.$path, $query)
|
||||
->throw();
|
||||
|
||||
$json = $response->json();
|
||||
|
||||
@@ -6,7 +6,9 @@
|
||||
use App\Domain\Catalog\ExerciseImportRun;
|
||||
use App\Domain\Catalog\ExerciseSource;
|
||||
use App\Domain\Catalog\ExerciseSourceSnapshot;
|
||||
use App\Domain\Catalog\ShinkanConnection;
|
||||
use App\Domain\Catalog\ShinkanImport;
|
||||
use App\Domain\Identity\StaffRole;
|
||||
use App\Filament\Resources\ContentElements\ContentElementResource;
|
||||
use App\Filament\Resources\Practices\PracticeResource;
|
||||
use App\Models\User;
|
||||
@@ -34,13 +36,57 @@ class Uebernahme extends Page
|
||||
|
||||
protected string $view = 'filament.pages.uebernahme';
|
||||
|
||||
public string $url = '';
|
||||
|
||||
public string $apiKey = '';
|
||||
|
||||
public string $feedback = '';
|
||||
|
||||
public bool $feedbackError = false;
|
||||
|
||||
public static function canAccess(): bool
|
||||
{
|
||||
return ContentElementResource::editor();
|
||||
return ContentElementResource::editor() || static::canConfigure();
|
||||
}
|
||||
|
||||
/** Adresse und Schlüssel ändern dürfen nur Katalogverwalter und Systemverwalter. */
|
||||
public static function canConfigure(): bool
|
||||
{
|
||||
$user = Filament::auth()->user();
|
||||
|
||||
return $user instanceof User
|
||||
&& ($user->hasStaffRole(StaffRole::Katalogverwalter) || $user->hasStaffRole(StaffRole::Systemverwalter));
|
||||
}
|
||||
|
||||
public function mount(): void
|
||||
{
|
||||
$this->url = app(ShinkanConnection::class)->source('url') === ShinkanConnection::ADMIN ? (string) app(ShinkanConnection::class)->url() : '';
|
||||
}
|
||||
|
||||
public function saveConnection(): void
|
||||
{
|
||||
abort_unless(static::canConfigure(), 403);
|
||||
|
||||
try {
|
||||
$user = Filament::auth()->user();
|
||||
app(ShinkanConnection::class)->save($this->url, $this->apiKey, $user instanceof User ? $user : null);
|
||||
$this->apiKey = '';
|
||||
$this->feedbackError = false;
|
||||
$this->feedback = 'Die Anbindung ist gespeichert. Werte aus der Umgebung des Servers haben weiterhin Vorrang. Prüfe die Verbindung, bevor du den Katalog abrufst.';
|
||||
} catch (DomainRuleException $exception) {
|
||||
$this->feedbackError = true;
|
||||
$this->feedback = $exception->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public function removeKey(): void
|
||||
{
|
||||
abort_unless(static::canConfigure(), 403);
|
||||
|
||||
$user = Filament::auth()->user();
|
||||
app(ShinkanConnection::class)->forgetKey($user instanceof User ? $user : null);
|
||||
$this->feedbackError = false;
|
||||
$this->feedback = 'Der in der Verwaltung gepflegte Zugangsschlüssel ist entfernt.';
|
||||
}
|
||||
|
||||
public function getHeading(): string|Htmlable|null
|
||||
@@ -124,6 +170,7 @@ public function daten(): array
|
||||
'zeilen' => $rows,
|
||||
'eingerichtet' => app(ShinkanImport::class)->configured(),
|
||||
'anbindung' => app(ShinkanImport::class)->status(),
|
||||
'darfAendern' => static::canConfigure(),
|
||||
'letzterLauf' => ExerciseImportRun::query()->orderByDesc('id')->first(),
|
||||
];
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user