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>
94 lines
2.8 KiB
PHP
94 lines
2.8 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Middleware;
|
||
|
||
use App\Models\Site;
|
||
use App\Models\Tenant;
|
||
use App\Models\User;
|
||
use App\Tenancy\TenantContext;
|
||
use App\Themes\Theme;
|
||
use App\Themes\ThemeFactory;
|
||
use Closure;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Support\Facades\URL;
|
||
use Symfony\Component\HttpFoundation\Response;
|
||
|
||
/**
|
||
* Determines site, theme and appearance for the website and activates the "theme::" views.
|
||
*/
|
||
class ResolveTheme
|
||
{
|
||
public function __construct(private ThemeFactory $factory) {}
|
||
|
||
public function handle(Request $request, Closure $next): Response
|
||
{
|
||
$context = app(TenantContext::class);
|
||
$context->forget();
|
||
|
||
$theme = $this->factory->forRequest($request)->activate();
|
||
app()->instance(Theme::class, $theme);
|
||
|
||
$tenant = null;
|
||
|
||
if ($theme->site->exists && $theme->site->tenant_id !== null) {
|
||
$tenant = $theme->site->tenant;
|
||
} elseif (Tenant::query()->count() === 1) {
|
||
$tenant = Tenant::query()->first();
|
||
}
|
||
|
||
if ($tenant !== null) {
|
||
if (! $tenant->is_active && ! $this->operatorMayPass($request)) {
|
||
return $this->suspended($tenant);
|
||
}
|
||
|
||
$context->set($tenant);
|
||
}
|
||
|
||
$this->useSiteOrigin($theme->site);
|
||
|
||
return $next($request);
|
||
}
|
||
|
||
/**
|
||
* Links must carry the address the visitor came in on. AppServiceProvider forces APP_URL for every
|
||
* generated URL, because on Strato the document root is a release folder; with one landlord that was
|
||
* harmless, but a second website on its own domain would otherwise send every visitor to the first
|
||
* landlord's host. The forced root stays – only its value becomes the website that was resolved.
|
||
*/
|
||
private function useSiteOrigin(Site $site): void
|
||
{
|
||
$origin = $site->exists ? $site->origin() : null;
|
||
|
||
if ($origin === null) {
|
||
return;
|
||
}
|
||
|
||
URL::forceRootUrl($origin);
|
||
URL::forceScheme(parse_url($origin, PHP_URL_SCHEME) ?: 'https');
|
||
}
|
||
|
||
/**
|
||
* What a visitor of a suspended landlord sees: a plain notice instead of a dead link.
|
||
*/
|
||
private function suspended(Tenant $tenant): Response
|
||
{
|
||
return response()->view('suspended', [
|
||
'message' => $tenant->suspended_message ?: 'Diese Webseite ist zurzeit nicht erreichbar. Bitte versuchen Sie es später noch einmal.',
|
||
], 503);
|
||
}
|
||
|
||
/**
|
||
* A suspended landlord stays reachable for the operator, and the operator panel itself stays open.
|
||
*/
|
||
private function operatorMayPass(Request $request): bool
|
||
{
|
||
if ($request->is('betreiber', 'betreiber/*')) {
|
||
return true;
|
||
}
|
||
|
||
$user = $request->user();
|
||
|
||
return $user instanceof User && $user->is_operator;
|
||
}
|
||
}
|