Bisher schrieb das Feld „Domain" genau einen Host nach `site_domains` und löschte den vorherigen. Solange es einen Mandanten gibt, fällt das nicht auf: jede Adresse liefert die Standard-Website. Ab dem zweiten Mandanten bekommt jede nicht eingetragene Adresse 404 – und „www.beispiel.de" ist eine eigene Adresse. stommer.info hätte einen zweiten Mandanten nicht überlebt. Das Formular hat jetzt „Hauptadresse" (steht in Gästelinks) und „Weitere Adressen". Eingaben werden normalisiert, „https://WWW.Beispiel.de/" wird zu „www.beispiel.de". Eine Adresse, die schon zu einer anderen Website gehört, wird abgelehnt. Ohne diese Sperre könnte ein Mandant die Besucher eines anderen auf seine eigene Seite ziehen, indem er dessen Adresse einträgt. 242 Feature-Tests grün. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
137 lines
3.7 KiB
PHP
137 lines
3.7 KiB
PHP
<?php
|
||
|
||
namespace App\Filament\Resources\Sites\Concerns;
|
||
|
||
use App\Models\Site;
|
||
use App\Models\SiteDomain;
|
||
use Illuminate\Validation\ValidationException;
|
||
|
||
/**
|
||
* The further hostnames of a website. The field "Domain" stays the main address used for guest links;
|
||
* everything entered here answers as well. Needed as soon as there is a second landlord, because from
|
||
* then on an unknown host is a 404 and "www" is a host of its own.
|
||
*/
|
||
trait EditsSiteHosts
|
||
{
|
||
/**
|
||
* @param array<string, mixed> $data
|
||
* @return array<string, mixed>
|
||
*/
|
||
protected function mutateFormDataBeforeFill(array $data): array
|
||
{
|
||
$site = $this->record;
|
||
|
||
if (! $site instanceof Site) {
|
||
return $data;
|
||
}
|
||
|
||
$primary = SiteDomain::normalise($site->domain);
|
||
|
||
$data['extra_hosts'] = $site->domains()
|
||
->pluck('host')
|
||
->reject(fn (string $host): bool => $host === $primary)
|
||
->values()
|
||
->all();
|
||
|
||
return $data;
|
||
}
|
||
|
||
/**
|
||
* @param array<string, mixed> $data
|
||
* @return array<string, mixed>
|
||
*/
|
||
protected function mutateFormDataBeforeCreate(array $data): array
|
||
{
|
||
return $this->withoutHosts($data);
|
||
}
|
||
|
||
/**
|
||
* @param array<string, mixed> $data
|
||
* @return array<string, mixed>
|
||
*/
|
||
protected function mutateFormDataBeforeSave(array $data): array
|
||
{
|
||
return $this->withoutHosts($data);
|
||
}
|
||
|
||
protected function afterCreate(): void
|
||
{
|
||
$this->saveHosts();
|
||
}
|
||
|
||
protected function afterSave(): void
|
||
{
|
||
$this->saveHosts();
|
||
}
|
||
|
||
/**
|
||
* @param array<string, mixed> $data
|
||
* @return array<string, mixed>
|
||
*/
|
||
private function withoutHosts(array $data): array
|
||
{
|
||
$hosts = is_array($data['extra_hosts'] ?? null) ? $data['extra_hosts'] : [];
|
||
|
||
$this->extraHosts = array_values(array_filter(array_map(
|
||
fn (mixed $host): string => SiteDomain::normalise(is_string($host) ? $host : null),
|
||
$hosts,
|
||
)));
|
||
|
||
$this->refuseForeignHosts();
|
||
|
||
unset($data['extra_hosts']);
|
||
|
||
return $data;
|
||
}
|
||
|
||
/**
|
||
* A hostname that already answers for another website must not be taken over – otherwise one landlord
|
||
* could pull another landlord's visitors onto their own site just by typing that address here.
|
||
*/
|
||
private function refuseForeignHosts(): void
|
||
{
|
||
$site = $this->record;
|
||
|
||
$taken = SiteDomain::query()
|
||
->whereIn('host', $this->extraHosts)
|
||
->when($site instanceof Site, fn ($query) => $query->where('site_id', '!=', $site->id))
|
||
->pluck('host');
|
||
|
||
if ($taken->isNotEmpty()) {
|
||
throw ValidationException::withMessages([
|
||
'data.extra_hosts' => 'Gehört bereits zu einer anderen Website: '.$taken->implode(', '),
|
||
]);
|
||
}
|
||
}
|
||
|
||
private function saveHosts(): void
|
||
{
|
||
$site = $this->record;
|
||
|
||
if (! $site instanceof Site) {
|
||
return;
|
||
}
|
||
|
||
$primary = SiteDomain::normalise($site->domain);
|
||
|
||
$wanted = collect($this->extraHosts)
|
||
->filter()
|
||
->when(filled($primary), fn ($hosts) => $hosts->push($primary))
|
||
->unique()
|
||
->values();
|
||
|
||
$site->domains()->whereNotIn('host', $wanted->all())->delete();
|
||
|
||
foreach ($wanted as $host) {
|
||
// Never take a host away from another website: that would hand one landlord's visitors to another.
|
||
$owner = SiteDomain::query()->where('host', $host)->value('site_id');
|
||
|
||
if ($owner !== null && $owner !== $site->id) {
|
||
continue;
|
||
}
|
||
|
||
SiteDomain::query()->updateOrCreate(['host' => $host], ['site_id' => $site->id]);
|
||
}
|
||
}
|
||
}
|