Files
fewo-stommer/app/Filament/Operator/Concerns/EditsTenantWebsites.php
T
Lars StommerandClaude Opus 5 03ad4667ac Betreiber richtet Mandanten vollständig ein: Adressen, Sperrseite, Systemwerte
Ein neu angelegter Mandant war eine Sackgasse. Er bekam keine Website, also
keinen Host, also keine Adresse – und der Host wird im Mandanten-Admin
eingetragen, den er ohne Adresse nicht erreicht. „Im Admin öffnen" half
nicht: ohne Domain landete es beim Mandanten des aktuellen Hosts.

Einsprungsadressen gehören jetzt dem Betreiber, weil Webspace und Hosting
ihm gehören und ein Mandant nicht die Adresse eines anderen auf sich
zeigen lassen darf. Im Mandanten-Formular: Websites mit ihren Adressen.
Beim Anlegen entstehen Website, Host-Einträge und eine leere Startseite –
ohne Startseite antwortet die Website mit 404. Im Mandanten-Admin sind die
Adressen nur noch sichtbar, nicht änderbar. Die Domain beim Hoster auf das
Verzeichnis zu legen bleibt ein Schritt von Hand.

Gesperrter Mandant: statt 404 ein Hinweis mit Status 503, Text je Mandant
einstellbar. 503 hält die Seiten im Suchindex, weil die Sperre vorübergehend
ist.

Behoben: `URL::forceRootUrl(APP_URL)` zwang **alle** Links auf einen festen
Host. Mit einem Mandanten unsichtbar; mit zwei Domains hätte jeder interne
Link der zweiten Website auf die erste gezeigt. Der Ursprung ist jetzt der
Host der aufgelösten Website.

Neu: systemweite Einstellungen sind im Betreiber-Bereich änderbar
(Löschfristen, Bot-Schutz, Flutgrenze, Sicherungen, Portal-Fristen,
Versandweg, SMTP, harte Mail-Umleitung, Tokens). Leer heißt: der Wert der
.env gilt wieder – und zwar sofort, nicht erst beim nächsten Start.
Geheimnisse liegen verschlüsselt. Datenbank-Zugang und APP_KEY bleiben in
der .env, weil sie gebraucht werden, um die Datenbank zu öffnen und das
Gespeicherte zu entschlüsseln.

253 Feature-Tests grün.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-10-24 10:20:18 +02:00

183 lines
5.2 KiB
PHP

<?php
namespace App\Filament\Operator\Concerns;
use App\Models\Page;
use App\Models\Site;
use App\Models\SiteDomain;
use App\Models\Tenant;
use App\Tenancy\TenantContext;
use Illuminate\Validation\ValidationException;
/**
* The websites of a landlord and the addresses they answer on. These belong to the operator: the webspace
* and the hosting are the operator's, and a landlord must not be able to point an address at themselves.
*
* Wiring a new domain to this directory stays a manual step at the hoster. What is kept here is the other
* half: which host belongs to which website, and through it to which landlord.
*
* Holds no Filament hooks; EditsTenantSetup calls these.
*/
trait EditsTenantWebsites
{
/**
* @param array<string, mixed> $data
* @return array<string, mixed>
*/
private function fillWebsites(array $data): array
{
$tenant = $this->record;
if (! $tenant instanceof Tenant) {
return $data;
}
$data['websites'] = $tenant->sites()->withoutGlobalScope('tenant')->with('domains')->get()
->map(fn (Site $site): array => [
'id' => $site->id,
'name' => $site->name,
'hosts' => $site->domains->pluck('host')->all(),
])
->all();
return $data;
}
/**
* @param array<string, mixed> $data
* @return array<string, mixed>
*/
private function takeWebsites(array $data): array
{
$this->websites = is_array($data['websites'] ?? null) ? array_values($data['websites']) : [];
$this->normaliseWebsites();
unset($data['websites']);
return $data;
}
/**
* Cleans the entered addresses and refuses what already belongs elsewhere, before anything is written.
*/
private function normaliseWebsites(): void
{
$seen = [];
foreach ($this->websites as $index => $website) {
$hosts = [];
foreach ((array) ($website['hosts'] ?? []) as $host) {
$host = SiteDomain::normalise(is_string($host) ? $host : null);
if ($host === '') {
continue;
}
if (in_array($host, $seen, true)) {
throw ValidationException::withMessages([
'data.websites' => 'Die Adresse '.$host.' ist mehrfach eingetragen.',
]);
}
$seen[] = $host;
$hosts[] = $host;
}
$this->websites[$index]['hosts'] = $hosts;
}
$this->refuseForeignHosts($seen);
}
/**
* @param list<string> $hosts
*/
private function refuseForeignHosts(array $hosts): void
{
$ownIds = collect($this->websites)->pluck('id')->filter()->all();
$taken = SiteDomain::query()
->whereIn('host', $hosts)
->whereNotIn('site_id', $ownIds ?: [0])
->pluck('host');
if ($taken->isNotEmpty()) {
throw ValidationException::withMessages([
'data.websites' => 'Gehört bereits zu einer anderen Website: '.$taken->implode(', '),
]);
}
}
/**
* Writes the websites. A website that is new gets an empty home page straight away, because a website
* without one answers 404 and the landlord would have no way in.
*/
private function saveWebsites(): void
{
$tenant = $this->record;
if (! $tenant instanceof Tenant) {
return;
}
app(TenantContext::class)->run($tenant, function () use ($tenant): void {
$keep = [];
foreach ($this->websites as $website) {
$name = trim((string) ($website['name'] ?? ''));
if ($name === '') {
continue;
}
$hosts = (array) ($website['hosts'] ?? []);
$site = $this->siteFor($tenant, $website['id'] ?? null, $name, $hosts);
$keep[] = $site->id;
$site->domains()->whereNotIn('host', $hosts ?: [''])->delete();
foreach ($hosts as $host) {
SiteDomain::query()->updateOrCreate(['host' => $host], ['site_id' => $site->id]);
}
}
$tenant->sites()->whereNotIn('id', $keep ?: [0])->each(fn (Site $site) => $site->delete());
});
}
/**
* @param list<string> $hosts
*/
private function siteFor(Tenant $tenant, mixed $id, string $name, array $hosts): Site
{
$site = $id ? Site::query()->whereKey($id)->first() : null;
if ($site !== null) {
$site->update(['name' => $name, 'domain' => $hosts[0] ?? null]);
return $site;
}
$site = Site::query()->create([
'tenant_id' => $tenant->id,
'name' => $name,
'domain' => $hosts[0] ?? null,
'is_default' => $tenant->sites()->count() === 0,
'theme' => config('themes.base'),
]);
Page::query()->create([
'site_id' => $site->id,
'slug' => Page::HOME,
'title' => $name,
'sections' => [],
'is_published' => true,
]);
return $site;
}
}