fix(tenancy): implement UniversalTenancy middleware to handle central domain on IP
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 52s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped

This commit is contained in:
2026-01-15 13:39:04 +08:00
parent 79e5916d19
commit a6b5496529
2 changed files with 32 additions and 1 deletions

View File

@@ -0,0 +1,31 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
use Symfony\Component\HttpFoundation\Response;
class UniversalTenancy
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
// 判斷是否為中央域名
$centralDomains = config('tenancy.central_domains', []);
if (in_array($request->getHost(), $centralDomains)) {
// 如果是中央域名,不進行租戶初始化,直接繼續往下執行 (使用預設資料庫)
return $next($request);
}
// 如果不是中央域名,嘗試透過域名初始化租戶
// 若找不到租戶InitializeTenancyByDomain 會拋出異常 (這正是我們要的,避免未授權訪問)
return app(InitializeTenancyByDomain::class)->handle($request, $next);
}
}