Skip to content
6 changes: 2 additions & 4 deletions assets/TenancyServiceProvider.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,14 @@ public function events()
Jobs\CreateDatabase::class,
Jobs\MigrateDatabase::class,
// Jobs\SeedDatabase::class,
// Jobs\CreateTenantStorage::class,
// Jobs\CreateStorageSymlinks::class,

// Your own jobs to prepare the tenant.
// Provision API keys, create S3 buckets, anything you want!
])->send(function (Events\TenantCreated $event) {
return $event->tenant;
})->shouldBeQueued(false),

// Listeners\CreateTenantStorage::class,
],
Events\SavingTenant::class => [],
Events\TenantSaved::class => [],
Expand All @@ -63,12 +62,11 @@ public function events()
Events\DeletingTenant::class => [
JobPipeline::make([
Jobs\DeleteDomains::class,
// Jobs\DeleteTenantStorage::class,
// Jobs\RemoveStorageSymlinks::class,
])->send(function (Events\DeletingTenant $event) {
return $event->tenant;
})->shouldBeQueued(false),

// Listeners\DeleteTenantStorage::class,
],
Events\TenantDeleted::class => [
JobPipeline::make([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

declare(strict_types=1);

namespace Stancl\Tenancy\Listeners;
namespace Stancl\Tenancy\Jobs;

use Stancl\Tenancy\Events\Contracts\TenantEvent;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Stancl\Tenancy\Contracts\Tenant;

/**
* Can be used to manually create framework directories in the tenant storage when storage_path() is scoped.
Expand All @@ -13,11 +18,17 @@
*
* Generally not needed anymore as the directory is also created by the FilesystemTenancyBootstrapper.
*/
class CreateTenantStorage
class CreateTenantStorage implements ShouldQueue
{
public function handle(TenantEvent $event): void
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public function __construct(
public Tenant $tenant,
) {}

public function handle(): void
{
$storage_path = tenancy()->run($event->tenant, fn () => storage_path());
$storage_path = tenancy()->run($this->tenant, fn () => storage_path());
$cache_path = "$storage_path/framework/cache";

if (! is_dir($cache_path)) {
Expand Down
47 changes: 47 additions & 0 deletions src/Jobs/DeleteTenantStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Stancl\Tenancy\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\File;
use Stancl\Tenancy\Contracts\Tenant;

/**
* Only delete the tenant storage if storage path suffixing is enabled
* and the tenant's storage path is different from the central storage path.
*
* This is to prevent accidental deletion of the central storage when
* a tenant's storage path is not properly suffixed.
*/
class DeleteTenantStorage implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public function __construct(
public Tenant $tenant,
) {}

public function handle(): void
{
// Skip storage deletion if path suffixing is disabled
if (config('tenancy.filesystem.suffix_storage_path') === false) {
return;
}

$centralPath = tenancy()->central(fn () => storage_path());
$path = tenancy()->run($this->tenant, fn () => storage_path());

// Skip storage deletion if tenant's storage path is the same as central storage path
$tenantPathIsCentral = realpath($path) === realpath($centralPath);

if (is_dir($path) && ! $tenantPathIsCentral) {
File::deleteDirectory($path);
Comment thread
lukinovec marked this conversation as resolved.
Outdated
}
}
}
20 changes: 0 additions & 20 deletions src/Listeners/DeleteTenantStorage.php

This file was deleted.

68 changes: 63 additions & 5 deletions tests/Bootstrappers/FilesystemTenancyBootstrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
use Stancl\Tenancy\Jobs\CreateStorageSymlinks;
use Stancl\Tenancy\Jobs\RemoveStorageSymlinks;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Listeners\DeleteTenantStorage;
use Stancl\Tenancy\Jobs\CreateTenantStorage;
use Stancl\Tenancy\Jobs\DeleteTenantStorage;
use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
use function Stancl\Tenancy\Tests\pest;
Expand Down Expand Up @@ -184,17 +185,75 @@
$this->assertDirectoryDoesNotExist(public_path("public-$tenantKey"));
});

test('tenant storage gets created when TenantCreated listens to CreateTenantStorage', function() {
config([
'tenancy.bootstrappers' => [
FilesystemTenancyBootstrapper::class,
],
]);

Event::listen(TenantCreated::class,
JobPipeline::make([CreateTenantStorage::class])->send(function (TenantCreated $event) {
return $event->tenant;
})->shouldBeQueued(false)->toListener()
);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

$centralStoragePath = storage_path();
$tenant = Tenant::create();
$tenantStoragePath = $centralStoragePath . '/tenant' . $tenant->getTenantKey();
Comment thread
lukinovec marked this conversation as resolved.
Outdated

$this->assertDirectoryExists($tenantStoragePath . '/framework/cache');
});

test('tenant storage can get deleted after the tenant when DeletingTenant listens to DeleteTenantStorage', function() {
Event::listen(DeletingTenant::class, DeleteTenantStorage::class);
Event::listen(DeletingTenant::class,
JobPipeline::make([DeleteTenantStorage::class])->send(function (DeletingTenant $event) {
return $event->tenant;
})->shouldBeQueued(false)->toListener()
);

$centralStoragePath = storage_path();
tenancy()->initialize(Tenant::create());

// FilesystemTenancyBootstrapper not enabled,
// tenant and central storage path is the same,
// the storage deletion will be skipped.
$tenantStoragePath = storage_path();
expect($tenantStoragePath)->toBe($centralStoragePath);
Comment thread
stancl marked this conversation as resolved.
expect(File::isDirectory($tenantStoragePath))->toBeTrue();
tenant()->delete();

expect(File::isDirectory($tenantStoragePath))->toBeTrue();

config([
'tenancy.bootstrappers' => [FilesystemTenancyBootstrapper::class],
'tenancy.filesystem.suffix_storage_path' => false,
]);

tenancy()->initialize(Tenant::create());

$tenantStoragePath = storage_path();

Storage::fake('test');
// FilesystemTenancyBootstrapper enabled,
// but tenant and central storage path is still the same
// because suffix_storage_path is false.
// The storage deletion will be skipped.
expect($tenantStoragePath)->toBe($centralStoragePath);
expect(File::isDirectory($tenantStoragePath))->toBeTrue();
tenant()->delete();

expect(File::isDirectory($tenantStoragePath))->toBeTrue();

Storage::put('test.txt', 'testing file');
config([
'tenancy.bootstrappers' => [FilesystemTenancyBootstrapper::class],
'tenancy.filesystem.suffix_storage_path' => true,
]);

tenancy()->initialize(Tenant::create());
$tenantStoragePath = storage_path();

expect($centralStoragePath)->not()->toBe($tenantStoragePath);
expect(File::isDirectory($tenantStoragePath))->toBeTrue();

tenant()->delete();

Expand Down Expand Up @@ -256,4 +315,3 @@
expect(file_get_contents(storage_path() . "/app/public/scoped_disk_prefix/foo.txt"))->toBe('central2');
expect(file_get_contents(storage_path() . "/tenant{$tenant->id}/app/public/scoped_disk_prefix/foo.txt"))->toBe('tenant');
});

Loading