Mehrere Adressen je Website, damit ein zweiter Mandant niemanden abschaltet

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>
This commit is contained in:
Lars StommerandClaude Opus 5 committed 2026-10-24 09:25:22 +02:00
1 parent fd2a7f7ded
commit 2e3f2ca938
8 files changed
+236 -3

No files matched your search

@@ -0,0 +1,136 @@
<?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]);
}
}
}
@@ -2,10 +2,16 @@
namespace App\Filament\Resources\Sites\Pages;
use App\Filament\Resources\Sites\Concerns\EditsSiteHosts;
use App\Filament\Resources\Sites\SiteResource;
use Filament\Resources\Pages\CreateRecord;
class CreateSite extends CreateRecord
{
use EditsSiteHosts;
protected static string $resource = SiteResource::class;
/** @var list<string> */
public array $extraHosts = [];
}
@@ -2,14 +2,20 @@
namespace App\Filament\Resources\Sites\Pages;
use App\Filament\Resources\Sites\Concerns\EditsSiteHosts;
use App\Filament\Resources\Sites\SiteResource;
use Filament\Actions\DeleteAction;
use Filament\Resources\Pages\EditRecord;
class EditSite extends EditRecord
{
use EditsSiteHosts;
protected static string $resource = SiteResource::class;
/** @var list<string> */
public array $extraHosts = [];
protected function getHeaderActions(): array
{
return [
@@ -7,6 +7,7 @@
use App\Themes\ThemeRegistry;
use Filament\Actions\Action;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TagsInput;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Components\Section;
@@ -26,11 +27,17 @@ public static function configure(Schema $schema): Schema
->schema([
TextInput::make('name')->label('Name')->required()->maxLength(120),
TextInput::make('domain')
->label('Domain')
->label('Hauptadresse')
->placeholder('www.fewo-stommer.de')
->helperText('Leer = Standard-Website, solange es nur diesen Mandanten gibt. Ab dem zweiten Mandanten antwortet nur eine eingetragene Domain.')
->helperText('Steht in Links an Gäste. Leer = Standard-Website, solange es nur diesen Mandanten gibt. Ab dem zweiten Mandanten antwortet nur eine eingetragene Adresse, alles andere bekommt 404.')
->unique(ignoreRecord: true)
->maxLength(190),
TagsInput::make('extra_hosts')
->label('Weitere Adressen')
->placeholder('stommer.info')
->helperText('Jede Schreibweise ist eine eigene Adresse: „www.beispiel.de“ und „beispiel.de“ sind zwei. Alle hier eingetragenen liefern dieselbe Website. Enter nach jeder Adresse.')
->nestedRecursiveRules(['string', 'max:190'])
->columnSpanFull(),
Select::make('theme')
->label('Theme')
->options(fn (): array => app(ThemeRegistry::class)->options())
@@ -15,7 +15,8 @@ public static function configure(Table $table): Table
return $table
->columns([
TextColumn::make('name')->label('Website')->weight('medium'),
TextColumn::make('domain')->label('Domain')->placeholder('alle übrigen Adressen'),
TextColumn::make('domain')->label('Hauptadresse')->placeholder('alle übrigen Adressen'),
TextColumn::make('domains.host')->label('Antwortet auf')->badge()->placeholder('jede Adresse'),
TextColumn::make('theme')->label('Theme')->formatStateUsing(fn (string $state): string => app(ThemeRegistry::class)->get($state)->name),
TextColumn::make('properties_count')->label('Wohnungen')->counts('properties'),
IconColumn::make('is_default')->label('Standard')->boolean(),
+13
View File
@@ -12,6 +12,19 @@
#[Fillable(['site_id', 'host'])]
class SiteDomain extends Model
{
/**
* Turns what someone typed into a bare hostname: "https://WWW.Example.de/" becomes "www.example.de".
* Returns an empty string when nothing usable is left, so callers can skip it.
*/
public static function normalise(?string $value): string
{
$host = mb_strtolower(trim((string) $value));
$host = (string) preg_replace('~^[a-z]+://~', '', $host);
$host = (string) preg_replace('~[/?#].*$~', '', $host);
return trim($host, '.');
}
/**
* @return BelongsTo<Site, $this>
*/