1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-09-20 15:14:03 +00:00

In the asset controller, throw if tenancy isn't initialized

Instead of falling back to returning storage_path('app/public') in assetRoot(), throw an exception *at the start of the method* -- tenant assets shouldn't be served in central context.
This commit is contained in:
lukinovec 2026-09-14 12:57:37 +02:00
parent 42df25ac39
commit 1a3ab476b0
2 changed files with 24 additions and 7 deletions

View file

@ -17,6 +17,7 @@ use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData;
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\UrlGeneratorBootstrapper;
use Stancl\Tenancy\Controllers\TenantAssetController;
use Stancl\Tenancy\Enums\RouteMode;
use Stancl\Tenancy\Events\TenancyEnded;
use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Stancl\Tenancy\Overrides\TenancyUrlGenerator;
@ -180,6 +181,23 @@ test('tenant asset controller throws when the configured disk is not local or no
}
});
test('tenant assets cannot be served in central context', function () {
config([
'tenancy.identification.default_middleware' => InitializeTenancyByRequestData::class,
// Make the asset route skip tenant identification
'tenancy.default_route_mode' => RouteMode::UNIVERSAL,
]);
$this->withoutExceptionHandling();
foreach ([null, 'local'] as $publicDisk) {
TenantAssetController::$publicDisk = $publicDisk;
expect(fn () => pest()->get(tenant_asset('foo.txt')))
->toThrow(Exception::class, 'Tenant assets can only be served in tenant context.');
}
});
test('tenant assets are served from the resolved root of the configured disk', function () {
config([
'tenancy.identification.default_middleware' => InitializeTenancyByRequestData::class,