From 5eb87eaebb89a29b9e7b1d7d3eade6bedb4fd62f Mon Sep 17 00:00:00 2001 From: Ollie Read Date: Sun, 17 Nov 2024 16:13:48 +0000 Subject: [PATCH] chore: Default to normal drivers if there's no multitenanted context --- src/Overrides/SessionOverride.php | 45 ++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/src/Overrides/SessionOverride.php b/src/Overrides/SessionOverride.php index 4fe0c55..0ddb6e5 100644 --- a/src/Overrides/SessionOverride.php +++ b/src/Overrides/SessionOverride.php @@ -5,6 +5,7 @@ use Closure; use Illuminate\Contracts\Foundation\Application; +use Illuminate\Session\DatabaseSessionHandler as OriginalDatabaseSessionHandler; use Illuminate\Session\FileSessionHandler; use Illuminate\Session\SessionManager; use Sprout\Concerns\OverridesCookieSettings; @@ -160,25 +161,28 @@ private static function createFilesDriver(): Closure /** @var string $originalPath */ $originalPath = config('session.files'); $path = rtrim($originalPath, '/') . DIRECTORY_SEPARATOR; - $tenancy = sprout()->getCurrentTenancy(); - if ($tenancy === null) { - throw TenancyMissing::make(); - } + if (sprout()->withinContext()) { + $tenancy = sprout()->getCurrentTenancy(); - // If there's no tenant, error out - if (! $tenancy->check()) { - throw TenantMissing::make($tenancy->getName()); - } + if ($tenancy === null) { + throw TenancyMissing::make(); + } - $tenant = $tenancy->tenant(); + // If there's no tenant, error out + if (! $tenancy->check()) { + throw TenantMissing::make($tenancy->getName()); + } - // If the tenant isn't configured for resources, also error out - if (! ($tenant instanceof TenantHasResources)) { - throw MisconfigurationException::misconfigured('tenant', $tenant::class, 'resources'); - } + $tenant = $tenancy->tenant(); - $path .= $tenant->getTenantResourceKey(); + // If the tenant isn't configured for resources, also error out + if (! ($tenant instanceof TenantHasResources)) { + throw MisconfigurationException::misconfigured('tenant', $tenant::class, 'resources'); + } + + $path .= $tenant->getTenantResourceKey(); + } /** @var int $lifetime */ $lifetime = config('session.lifetime'); @@ -193,7 +197,7 @@ private static function createFilesDriver(): Closure private static function createDatabaseDriver(): Closure { - return static function (): DatabaseSessionHandler { + return static function (): OriginalDatabaseSessionHandler { $table = config('session.table'); $lifetime = config('session.lifetime'); $connection = config('session.connection'); @@ -204,7 +208,16 @@ private static function createDatabaseDriver(): Closure * @var int $lifetime */ - return new DatabaseSessionHandler( + if (sprout()->withinContext()) { + return new DatabaseSessionHandler( + app()->make('db')->connection($connection), + $table, + $lifetime, + app() + ); + } + + return new OriginalDatabaseSessionHandler( app()->make('db')->connection($connection), $table, $lifetime,