determineUniversalRouteContextFromRequest(request()) === Context::CENTRAL; } // If the middleware is not in the global stack (= it's used directly on the route) // And the route isn't universal, don't skip it if (! static::inGlobalStack()) { return false; } // Now that we're sure the MW isn't used in the global MW stack, we determine whether to skip it if ($this instanceof PreventAccessFromUnwantedDomains) { // Skip access prevention if the route directly uses a non-domain identification middleware return Tenancy::routeHasIdentificationMiddleware($route) && ! Tenancy::routeHasMiddleware($route, config('tenancy.identification.domain_identification_middleware')); } return $this->shouldSkipIdentificationMiddleware($route); } protected function determineUniversalRouteContextFromRequest(Request $request): Context { $route = Tenancy::getRoute($request); // Check if this is the identification middleware the route should be using // Route-level identification middleware is prioritized $globalIdentificationUsed = ! tenancy()->routeHasIdentificationMiddleware($route) && static::inGlobalStack(); $routeLevelIdentificationUsed = tenancy()->routeHasMiddleware($route, static::class); if (($globalIdentificationUsed || $routeLevelIdentificationUsed) && $this->requestHasTenant($request)) { return Context::TENANT; } return Context::CENTRAL; } protected function shouldSkipIdentificationMiddleware(Route $route): bool { if (! static::inGlobalStack()) { return false; } $request = app(Request::class); if (! $request->attributes->get('_tenancy_kernel_identification_skipped')) { if ( // Skip identification if the current route is central // The route is central if it's flagged as central // Or if it isn't flagged and the default route mode is set to central Tenancy::getRouteMode($route) === RouteMode::CENTRAL ) { return true; } // Skip kernel identification if the route uses route-level identification if (Tenancy::routeHasIdentificationMiddleware($route)) { // Remember that it was attempted to identify a tenant using kernel identification // By making the $kernelIdentificationSkipped property of the current Tenancy instance true // So that the next identification middleware gets executed (= route-level identification MW doesn't get skipped) $request->attributes->set('_tenancy_kernel_identification_skipped', true); // Skip kernel identification so that route-level identification middleware can get used return true; } } return false; } public static function inGlobalStack(): bool { return Tenancy::globalStackHasMiddleware(static::class); } }