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

Throw an exception if a scoped disk is listed in tenant-aware disks without its base disk

In FSBootstrapper::forgetDisks():
- `tenancy.filesystem.disks => ['scoped']` throws
- `tenancy.filesystem.disks => ['scoped', 'parent']` does NOT throw
- `tenancy.filesystem.disks => ['scoped_with_scoped_parent', 'scoped_parent']` (invalid config where a scoped disk's base disk doesn't actually exist because the scoped disks just reference themselves) throws
This commit is contained in:
lukinovec 2026-09-07 11:04:29 +02:00 committed by Samuel Stancl
parent e7c0193931
commit dde9be6f58
2 changed files with 57 additions and 5 deletions

View file

@ -128,10 +128,16 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper
$scopedDisks = []; $scopedDisks = [];
foreach ($this->app['config']['filesystems.disks'] as $name => $disk) { foreach ($this->app['config']['filesystems.disks'] as $name => $disk) {
if (isset($disk['driver']) if (($disk['driver'] ?? null) !== 'scoped') {
&& $disk['driver'] === 'scoped' continue;
&& in_array(static::baseDiskName($name), $tenantDisks, true)) { }
$baseDisk = static::baseDiskName($name);
if (in_array($baseDisk, $tenantDisks, true)) {
$scopedDisks[] = $name; $scopedDisks[] = $name;
} elseif (in_array($name, $tenantDisks, true)) {
throw new Exception("A disk using the 'scoped' driver cannot be tenant-aware. List its base disk in tenancy.filesystem.disks instead.");
} }
} }
@ -142,6 +148,7 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper
{ {
if ($this->app['config']["filesystems.disks.$disk.driver"] === 'scoped') { if ($this->app['config']["filesystems.disks.$disk.driver"] === 'scoped') {
// Skip scoped disks since they have no root to override // Skip scoped disks since they have no root to override
// (reachable when a scoped disk is listed in tenancy.filesystem.disks alongside its base disk).
return; return;
} }

View file

@ -326,7 +326,53 @@ test('scoped disks based on a non-local disk are scoped per tenant', function ()
expect(Storage::disk('scoped_s3')->path('foo.txt'))->toBe('scoped_s3_prefix/foo.txt'); expect(Storage::disk('scoped_s3')->path('foo.txt'))->toBe('scoped_s3_prefix/foo.txt');
}); });
test('adding a scoped disk to tenancy.filesystem.disks has no effect on the disk', function () { test('adding a scoped disk to tenancy.filesystem.disks throws an exception if its base disk is not listed', function (string $disk) {
config([
'tenancy.bootstrappers' => [
FilesystemTenancyBootstrapper::class,
],
'filesystems.disks.foo' => [
'driver' => 'scoped',
'disk' => 'public',
'prefix' => 'foo',
],
'filesystems.disks.bar' => [
'driver' => 'scoped',
'disk' => 'foo',
'prefix' => 'bar',
],
// Disks referencing each other (neither has a base disk)
'filesystems.disks.abc' => [
'driver' => 'scoped',
'disk' => 'def',
'prefix' => 'abc',
],
'filesystems.disks.def' => [
'driver' => 'scoped',
'disk' => 'abc',
'prefix' => 'def',
],
'tenancy.filesystem.disks' => [$disk],
]);
expect(fn () => tenancy()->initialize(Tenant::create()))
->toThrow(Exception::class, "List its base disk in tenancy.filesystem.disks instead");
// Parent of 'abc' is 'def', whose parent is 'abc' -- there's no base disk for these, so these are
// still invalid and the exception will still be thrown.
if ($disk !== 'abc') {
config(['tenancy.filesystem.disks' => ['public', $disk]]);
expect(fn () => tenancy()->initialize(Tenant::create()))
->not()->toThrow(Exception::class, "List its base disk in tenancy.filesystem.disks instead");
}
})->with([
'scoped disk' => 'foo',
'nested scoped disk' => 'bar',
'scoped disk with no base disk' => 'abc',
]);
test('adding a scoped disk to tenancy.filesystem.disks has no effect on the disk when its base disk is listed too', function () {
config([ config([
'tenancy.bootstrappers' => [ 'tenancy.bootstrappers' => [
FilesystemTenancyBootstrapper::class, FilesystemTenancyBootstrapper::class,
@ -345,7 +391,6 @@ test('adding a scoped disk to tenancy.filesystem.disks has no effect on the disk
tenancy()->initialize($tenant); tenancy()->initialize($tenant);
// The 'foo' disk's parent disk ('public') is tenant-aware, so its root is scoped the same way. // The 'foo' disk's parent disk ('public') is tenant-aware, so its root is scoped the same way.
// It doesn't matter that 'foo' itself is tenant-aware.
expect(Storage::disk('foo')->path('testing.txt'))->toBe(storage_path('app/public/foo/testing.txt')); expect(Storage::disk('foo')->path('testing.txt'))->toBe(storage_path('app/public/foo/testing.txt'));
// Scoped disks have no root or url of their own, so the bootstrapper leaves their config alone // Scoped disks have no root or url of their own, so the bootstrapper leaves their config alone