mirror of
https://github.com/archtechx/tenancy.git
synced 2026-09-20 11: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:
parent
42df25ac39
commit
1a3ab476b0
2 changed files with 24 additions and 7 deletions
|
|
@ -85,13 +85,16 @@ class TenantAssetController implements HasMiddleware
|
|||
|
||||
/**
|
||||
* Directory the assets are served from -- the root of the $publicDisk, or app/public
|
||||
* inside the tenant's storage directory when no disk is configured. When no disk is
|
||||
* configured and there's no current tenant, the central app/public is used.
|
||||
* inside the tenant's storage directory when no disk is configured.
|
||||
*
|
||||
* The tenant's storage directory is resolved using the FilesystemTenancyBootstrapper::getTenantStoragePath().
|
||||
*/
|
||||
protected function assetRoot(): string
|
||||
{
|
||||
if (! tenant()) {
|
||||
throw new Exception('Tenant assets can only be served in tenant context.');
|
||||
}
|
||||
|
||||
if (static::$publicDisk) {
|
||||
$disk = Storage::disk(static::$publicDisk);
|
||||
|
||||
|
|
@ -118,11 +121,7 @@ class TenantAssetController implements HasMiddleware
|
|||
return rtrim($disk->path(''), DIRECTORY_SEPARATOR);
|
||||
}
|
||||
|
||||
if ($tenant = tenant()) {
|
||||
return FilesystemTenancyBootstrapper::getTenantStoragePath($tenant) . '/app/public';
|
||||
}
|
||||
|
||||
return storage_path('app/public');
|
||||
return FilesystemTenancyBootstrapper::getTenantStoragePath(tenant()) . '/app/public';
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue