commit 08-2025
This commit is contained in:
parent
9ae662f63e
commit
480fdc65ed
404 changed files with 65310 additions and 2600431 deletions
146
app/Http/Middleware/DomainResolver.php
Normal file
146
app/Http/Middleware/DomainResolver.php
Normal file
|
|
@ -0,0 +1,146 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use App\Services\Util;
|
||||
use App\Models\UserShop;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Domain\DomainContext;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Illuminate\Support\Facades\Session;
|
||||
|
||||
class DomainResolver
|
||||
{
|
||||
/**
|
||||
* Behandelt eine eingehende Anfrage, um den Domain-Kontext aufzulösen.
|
||||
*
|
||||
* Diese Middleware ist schlank gehalten. Die Hauptlogik zur Erstellung
|
||||
* des DomainContext befindet sich im DomainServiceProvider, um eine
|
||||
* saubere Trennung der Verantwortlichkeiten zu gewährleisten.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle(Request $request, Closure $next)
|
||||
{
|
||||
/** @var DomainContext $context */
|
||||
$context = app(DomainContext::class);
|
||||
// Session-Domain je nach Kontext setzen
|
||||
if ($context->type === 'shop') {
|
||||
Config::set('session.domain', '.'.config('app.domain').config('app.tld_shop'));
|
||||
} else {
|
||||
Config::set('session.domain', '.'.config('app.domain').config('app.tld_care'));
|
||||
}
|
||||
// Wenn der DomainServiceProvider die Domain nicht identifizieren konnte,
|
||||
// leiten wir sicher auf die Hauptdomain um.
|
||||
if ($context->isUnknown()) {
|
||||
// Detailliertes Logging für spätere Analyse
|
||||
\Log::warning('Unknown domain accessed', [
|
||||
'host' => $request->getHost(),
|
||||
'subdomain' => $context->subdomain,
|
||||
'user_agent' => $request->userAgent(),
|
||||
'ip' => $request->ip(),
|
||||
'referer' => $request->header('referer'),
|
||||
'path' => $request->getPathInfo()
|
||||
]);
|
||||
|
||||
// Holt die URL der Hauptdomain vom DomainService und leitet um.
|
||||
$mainUrl = app(\App\Services\DomainService::class)->buildUrl('main');
|
||||
return redirect()->away($mainUrl, 301);
|
||||
}
|
||||
\Log::debug('DomainResolver: context', [
|
||||
'context' => $context,
|
||||
'subdomain' => $context->subdomain
|
||||
]);
|
||||
|
||||
// Für User-Shop-Domains: Validierung und Route-Parameter-Bereinigung
|
||||
if ($context->isUserShop()) {
|
||||
// Validiere UserShop-Berechtigung (bereits im DomainServiceProvider geprüft,
|
||||
// aber zusätzliche Sicherheitsebene)
|
||||
if (!$context->userShop) {
|
||||
\Log::warning('UserShop not found', [
|
||||
'subdomain' => $context->subdomain,
|
||||
'host' => $context->host
|
||||
]);
|
||||
abort(503, 'Shop not available');
|
||||
}
|
||||
|
||||
if (!$context->userShop->active) {
|
||||
\Log::info('UserShop inactive accessed', [
|
||||
'shop_id' => $context->userShop->id,
|
||||
'subdomain' => $context->subdomain
|
||||
]);
|
||||
abort(503, 'Shop temporarily unavailable');
|
||||
}
|
||||
|
||||
if (!$context->userShop->user || !$context->userShop->user->isActiveShop()) {
|
||||
\Log::info('UserShop with expired payment accessed', [
|
||||
'shop_id' => $context->userShop->id,
|
||||
'user_id' => $context->userShop->user_id ?? null,
|
||||
'subdomain' => $context->subdomain
|
||||
]);
|
||||
abort(503, 'Shop access denied');
|
||||
}
|
||||
|
||||
// Entferne subdomain Parameter aus der Route
|
||||
// damit catch-all Routen wie /{site}/{subsite?}/{product_slug?} funktionieren
|
||||
if ($request->route('subdomain')) {
|
||||
$request->route()->forgetParameter('subdomain');
|
||||
}
|
||||
}
|
||||
|
||||
// Richtet den Anwendungskontext für Abwärtskompatibilität ein.
|
||||
$this->setupLegacyContext($context);
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stellt die Kompatibilität mit älteren Teilen der Anwendung her,
|
||||
* die direkt auf Session-Daten oder dynamische Konfigurationen zugreifen.
|
||||
*
|
||||
* @param DomainContext $context
|
||||
*/
|
||||
private function setupLegacyContext(DomainContext $context): void
|
||||
{
|
||||
// TODO: [TECH-DEBT] Diese Methode sollte langfristig entfernt werden.
|
||||
// Alle Teile der Anwendung sollten den DomainContext direkt verwenden.
|
||||
if ($context->userShop) {
|
||||
// Setzt die alten Session-Variablen, die von einigen Views/Controllern erwartet werden.
|
||||
Session::put('user_shop', $context->userShop);
|
||||
Session::put('user_shop_domain', $context->host);
|
||||
\Log::debug('DomainResolver: user_shop gesetzt', ['user_id' => $context->userShop->user_id ?? null]);
|
||||
|
||||
// Setzt die app.url zur Laufzeit, um URL-Generierung in alten Teilen zu ermöglichen.
|
||||
Config::set('app.url', $context->host);
|
||||
|
||||
// Kompatibilität mit der Util-Klasse.
|
||||
Util::setPostRoute('user/');
|
||||
} else {
|
||||
if($context->type === 'main'){
|
||||
Session::forget('user_shop');
|
||||
Session::forget('user_shop_domain');
|
||||
Session::save(); // Sofortige Session-Speicherung
|
||||
\Log::debug('DomainResolver: user_shop entfernt (' . $context->type . ' domain)', ['user_shop' => session('user_shop')]);
|
||||
Config::set('app.url', $context->host);
|
||||
}elseif($context->type === 'shop'){
|
||||
Util::setPostRoute('user/');
|
||||
$user_shop = UserShop::where('slug', 'aloevera')->first();
|
||||
Session::put('user_shop', $user_shop);
|
||||
Session::put('user_shop_domain', $context->host);
|
||||
Session::save(); // Sofortige Session-Speicherung
|
||||
\Log::debug('DomainResolver: user_shop hinzugefügt (' . $context->type . ' domain)', ['user_shop' => session('user_shop')]);
|
||||
|
||||
Config::set('app.url', $context->host);
|
||||
}else{
|
||||
// Für Domains ohne UserShop: Session-Daten sofort löschen
|
||||
// Session::forget('user_shop');
|
||||
// Session::put('user_shop_domain', $context->host);
|
||||
// Session::save(); // Sofortige Session-Speicherung
|
||||
// \Log::debug('DomainResolver: user_shop_domain hinzugefügt (' . $context->type . ' domain)', ['user_shop' => session('user_shop')]);
|
||||
Config::set('app.url', $context->host);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue