From 2e3f2ca9384bda695f370eded3986db655b3321c Mon Sep 17 00:00:00 2001 From: Lars Stommer Date: Sat, 24 Oct 2026 09:25:22 +0200 Subject: [PATCH] Mehrere Adressen je Website, damit ein zweiter Mandant niemanden abschaltet MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .claude/settings.local.json | 7 + .../Sites/Concerns/EditsSiteHosts.php | 136 ++++++++++++++++++ .../Resources/Sites/Pages/CreateSite.php | 6 + .../Resources/Sites/Pages/EditSite.php | 6 + .../Resources/Sites/Schemas/SiteForm.php | 11 +- .../Resources/Sites/Tables/SitesTable.php | 3 +- app/Models/SiteDomain.php | 13 ++ tests/Feature/WebsiteTenancyTest.php | 57 ++++++++ 8 files changed, 236 insertions(+), 3 deletions(-) create mode 100644 .claude/settings.local.json create mode 100644 app/Filament/Resources/Sites/Concerns/EditsSiteHosts.php diff --git a/.claude/settings.local.json b/.claude/settings.local.json new file mode 100644 index 0000000..6708181 --- /dev/null +++ b/.claude/settings.local.json @@ -0,0 +1,7 @@ +{ + "permissions": { + "allow": [ + "Bash(git merge *)" + ] + } +} diff --git a/app/Filament/Resources/Sites/Concerns/EditsSiteHosts.php b/app/Filament/Resources/Sites/Concerns/EditsSiteHosts.php new file mode 100644 index 0000000..ddb9099 --- /dev/null +++ b/app/Filament/Resources/Sites/Concerns/EditsSiteHosts.php @@ -0,0 +1,136 @@ + $data + * @return array + */ + 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 $data + * @return array + */ + protected function mutateFormDataBeforeCreate(array $data): array + { + return $this->withoutHosts($data); + } + + /** + * @param array $data + * @return array + */ + protected function mutateFormDataBeforeSave(array $data): array + { + return $this->withoutHosts($data); + } + + protected function afterCreate(): void + { + $this->saveHosts(); + } + + protected function afterSave(): void + { + $this->saveHosts(); + } + + /** + * @param array $data + * @return array + */ + 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]); + } + } +} diff --git a/app/Filament/Resources/Sites/Pages/CreateSite.php b/app/Filament/Resources/Sites/Pages/CreateSite.php index e98951a..4fd9dab 100644 --- a/app/Filament/Resources/Sites/Pages/CreateSite.php +++ b/app/Filament/Resources/Sites/Pages/CreateSite.php @@ -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 */ + public array $extraHosts = []; } diff --git a/app/Filament/Resources/Sites/Pages/EditSite.php b/app/Filament/Resources/Sites/Pages/EditSite.php index 21ed400..947fe92 100644 --- a/app/Filament/Resources/Sites/Pages/EditSite.php +++ b/app/Filament/Resources/Sites/Pages/EditSite.php @@ -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 */ + public array $extraHosts = []; + protected function getHeaderActions(): array { return [ diff --git a/app/Filament/Resources/Sites/Schemas/SiteForm.php b/app/Filament/Resources/Sites/Schemas/SiteForm.php index 563fe96..710fb95 100644 --- a/app/Filament/Resources/Sites/Schemas/SiteForm.php +++ b/app/Filament/Resources/Sites/Schemas/SiteForm.php @@ -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()) diff --git a/app/Filament/Resources/Sites/Tables/SitesTable.php b/app/Filament/Resources/Sites/Tables/SitesTable.php index 71714f9..da0e572 100644 --- a/app/Filament/Resources/Sites/Tables/SitesTable.php +++ b/app/Filament/Resources/Sites/Tables/SitesTable.php @@ -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(), diff --git a/app/Models/SiteDomain.php b/app/Models/SiteDomain.php index 7f07e86..2e466b9 100644 --- a/app/Models/SiteDomain.php +++ b/app/Models/SiteDomain.php @@ -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 */ diff --git a/tests/Feature/WebsiteTenancyTest.php b/tests/Feature/WebsiteTenancyTest.php index 8389579..1751068 100644 --- a/tests/Feature/WebsiteTenancyTest.php +++ b/tests/Feature/WebsiteTenancyTest.php @@ -3,12 +3,18 @@ namespace Tests\Feature; use App\Actions\SendOffer; +use App\Enums\UserRole; +use App\Filament\Resources\Sites\Pages\EditSite; use App\Models\Booking; use App\Models\Page; use App\Models\Property; use App\Models\Site; use App\Models\Tenant; +use App\Models\User; +use App\Tenancy\TenantContext; use Illuminate\Foundation\Testing\RefreshDatabase; +use Livewire\Features\SupportTesting\Testable; +use Livewire\Livewire; use Tests\TestCase; class WebsiteTenancyTest extends TestCase @@ -40,6 +46,42 @@ public function test_a_second_landlord_is_reached_only_on_their_own_address(): v $this->get('http://unbekannt.test/')->assertNotFound(); } + /** + * The trap that would take a live site down: as long as there is one landlord every address works, so + * nobody notices that no host is entered. The second landlord turns every unentered address into a 404 – + * including "www", which is a host of its own. + */ + public function test_a_website_answers_on_every_address_entered_for_it(): void + { + $site = $this->website('Nordhaus', 'nordhaus.test'); + $this->website('Südheim', 'suedheim.test'); + + $this->get('http://www.nordhaus.test/')->assertNotFound(); + + $this->editHosts($site, ['www.nordhaus.test', 'https://ALT.nordhaus.test/'])->assertHasNoFormErrors(); + + $this->get('http://nordhaus.test/')->assertOk()->assertSee('Nordhaus'); + $this->get('http://www.nordhaus.test/')->assertOk()->assertSee('Nordhaus'); + $this->get('http://alt.nordhaus.test/')->assertOk()->assertSee('Nordhaus'); + $this->get('http://unbekannt.test/')->assertNotFound(); + + $this->assertSame( + ['alt.nordhaus.test', 'nordhaus.test', 'www.nordhaus.test'], + $site->domains()->orderBy('host')->pluck('host')->all(), + ); + } + + public function test_a_host_of_another_website_cannot_be_taken_over(): void + { + $nord = $this->website('Nordhaus', 'nordhaus.test'); + $sued = $this->website('Südheim', 'suedheim.test'); + + $this->editHosts($nord, ['suedheim.test'])->assertHasFormErrors(); + + $this->get('http://suedheim.test/')->assertOk()->assertSee('Südheim')->assertDontSee('Nordhaus'); + $this->assertSame([$sued->id], $sued->domains()->pluck('site_id')->all()); + } + public function test_the_offer_link_uses_the_website_address(): void { $site = $this->website('Nordhaus', 'nordhaus.test'); @@ -55,6 +97,21 @@ public function test_the_offer_link_uses_the_website_address(): void $this->assertStringContainsString('signature=', $url); } + /** + * Enters the further addresses through the real admin form, so the form's own defences are under test. + * + * @param list $hosts + */ + private function editHosts(Site $site, array $hosts): Testable + { + app(TenantContext::class)->set($site->tenant); + $this->actingAs(User::factory()->role(UserRole::Admin)->create()); + + return Livewire::test(EditSite::class, ['record' => $site->getRouteKey()]) + ->fillForm(['extra_hosts' => $hosts]) + ->call('save'); + } + private function website(string $name, ?string $domain = null): Site { $tenant = Tenant::factory()->create();