Skip to content

Commit

Permalink
chore: Default to normal drivers if there's no multitenanted context
Browse files Browse the repository at this point in the history
  • Loading branch information
ollieread committed Nov 17, 2024
1 parent bf8a85d commit 5eb87ea
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions src/Overrides/SessionOverride.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Check warning on line 166 in src/Overrides/SessionOverride.php

View check run for this annotation

Codecov / codecov/patch

src/Overrides/SessionOverride.php#L165-L166

Added lines #L165 - L166 were not covered by tests

// If there's no tenant, error out
if (! $tenancy->check()) {
throw TenantMissing::make($tenancy->getName());
}
if ($tenancy === null) {
throw TenancyMissing::make();

Check warning on line 169 in src/Overrides/SessionOverride.php

View check run for this annotation

Codecov / codecov/patch

src/Overrides/SessionOverride.php#L168-L169

Added lines #L168 - L169 were not covered by tests
}

$tenant = $tenancy->tenant();
// If there's no tenant, error out
if (! $tenancy->check()) {
throw TenantMissing::make($tenancy->getName());

Check warning on line 174 in src/Overrides/SessionOverride.php

View check run for this annotation

Codecov / codecov/patch

src/Overrides/SessionOverride.php#L173-L174

Added lines #L173 - L174 were not covered by tests
}

// 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();

Check warning on line 177 in src/Overrides/SessionOverride.php

View check run for this annotation

Codecov / codecov/patch

src/Overrides/SessionOverride.php#L177

Added line #L177 was not covered by tests

$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');

Check warning on line 181 in src/Overrides/SessionOverride.php

View check run for this annotation

Codecov / codecov/patch

src/Overrides/SessionOverride.php#L180-L181

Added lines #L180 - L181 were not covered by tests
}

$path .= $tenant->getTenantResourceKey();

Check warning on line 184 in src/Overrides/SessionOverride.php

View check run for this annotation

Codecov / codecov/patch

src/Overrides/SessionOverride.php#L184

Added line #L184 was not covered by tests
}

/** @var int $lifetime */
$lifetime = config('session.lifetime');
Expand All @@ -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');
Expand All @@ -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()
);

Check warning on line 217 in src/Overrides/SessionOverride.php

View check run for this annotation

Codecov / codecov/patch

src/Overrides/SessionOverride.php#L211-L217

Added lines #L211 - L217 were not covered by tests
}

return new OriginalDatabaseSessionHandler(

Check warning on line 220 in src/Overrides/SessionOverride.php

View check run for this annotation

Codecov / codecov/patch

src/Overrides/SessionOverride.php#L220

Added line #L220 was not covered by tests
app()->make('db')->connection($connection),
$table,
$lifetime,
Expand Down

0 comments on commit 5eb87ea

Please sign in to comment.