From dc90e60a2f601bef1177827ffbc73f1b5fdcc30e Mon Sep 17 00:00:00 2001 From: lukinovec Date: Fri, 4 Apr 2025 03:15:37 +0200 Subject: [PATCH] [4.x] Make ScopeSessions usable on universal routes (#1342) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Skip ScopeSessions MW if the current context is central and the route is universal * Add regressiont test * Simplify code --------- Co-authored-by: Samuel Ć tancl --- src/Middleware/ScopeSessions.php | 4 ++++ tests/ScopeSessionsTest.php | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/Middleware/ScopeSessions.php b/src/Middleware/ScopeSessions.php index 46bd5dc4..879b9d97 100644 --- a/src/Middleware/ScopeSessions.php +++ b/src/Middleware/ScopeSessions.php @@ -19,6 +19,10 @@ class ScopeSessions public function handle(Request $request, Closure $next): mixed { if (! tenancy()->initialized) { + if (tenancy()->routeIsUniversal(tenancy()->getRoute($request))) { + return $next($request); + } + throw new TenancyNotInitializedException('Tenancy needs to be initialized before the session scoping middleware is executed'); } diff --git a/tests/ScopeSessionsTest.php b/tests/ScopeSessionsTest.php index e62fa370..4fccac58 100644 --- a/tests/ScopeSessionsTest.php +++ b/tests/ScopeSessionsTest.php @@ -55,3 +55,15 @@ test('an exception is thrown when the middleware is executed before tenancy is i pest()->expectException(TenancyNotInitializedException::class); $this->withoutExceptionHandling()->get('http://acme.localhost/bar'); }); + +test('scope sessions mw can be used on universal routes', function() { + Route::get('/universal', function () { + return true; + })->middleware(['universal', InitializeTenancyBySubdomain::class, ScopeSessions::class]); + + Tenant::create([ + 'id' => 'acme', + ])->createDomain('acme'); + + pest()->withoutExceptionHandling()->get('http://localhost/universal')->assertSuccessful(); +});