mirror of
https://github.com/archtechx/tenancy.git
synced 2026-05-06 15:24:03 +00:00
merge master
This commit is contained in:
commit
9b11e69ddc
11 changed files with 151 additions and 31 deletions
|
|
@ -53,8 +53,6 @@ class TenancyServiceProvider extends ServiceProvider
|
||||||
])->send(function (Events\TenantCreated $event) {
|
])->send(function (Events\TenantCreated $event) {
|
||||||
return $event->tenant;
|
return $event->tenant;
|
||||||
})->shouldBeQueued(false),
|
})->shouldBeQueued(false),
|
||||||
|
|
||||||
// Listeners\CreateTenantStorage::class,
|
|
||||||
],
|
],
|
||||||
Events\SavingTenant::class => [],
|
Events\SavingTenant::class => [],
|
||||||
Events\TenantSaved::class => [],
|
Events\TenantSaved::class => [],
|
||||||
|
|
@ -63,12 +61,11 @@ class TenancyServiceProvider extends ServiceProvider
|
||||||
Events\DeletingTenant::class => [
|
Events\DeletingTenant::class => [
|
||||||
JobPipeline::make([
|
JobPipeline::make([
|
||||||
Jobs\DeleteDomains::class,
|
Jobs\DeleteDomains::class,
|
||||||
|
// Jobs\DeleteTenantStorage::class,
|
||||||
// Jobs\RemoveStorageSymlinks::class,
|
// Jobs\RemoveStorageSymlinks::class,
|
||||||
])->send(function (Events\DeletingTenant $event) {
|
])->send(function (Events\DeletingTenant $event) {
|
||||||
return $event->tenant;
|
return $event->tenant;
|
||||||
})->shouldBeQueued(false),
|
})->shouldBeQueued(false),
|
||||||
|
|
||||||
// Listeners\DeleteTenantStorage::class,
|
|
||||||
],
|
],
|
||||||
Events\TenantDeleted::class => [
|
Events\TenantDeleted::class => [
|
||||||
JobPipeline::make([
|
JobPipeline::make([
|
||||||
|
|
|
||||||
|
|
@ -80,8 +80,8 @@ services:
|
||||||
mssql:
|
mssql:
|
||||||
image: mcr.microsoft.com/mssql/server:2022-latest
|
image: mcr.microsoft.com/mssql/server:2022-latest
|
||||||
environment:
|
environment:
|
||||||
- ACCEPT_EULA=Y
|
ACCEPT_EULA: "Y"
|
||||||
- SA_PASSWORD=P@ssword # must be the same as TENANCY_TEST_SQLSRV_PASSWORD
|
SA_PASSWORD: "P@ssword" # must be the same as TENANCY_TEST_SQLSRV_PASSWORD
|
||||||
healthcheck: # https://github.com/Microsoft/mssql-docker/issues/133#issuecomment-1995615432
|
healthcheck: # https://github.com/Microsoft/mssql-docker/issues/133#issuecomment-1995615432
|
||||||
test: timeout 2 bash -c 'cat < /dev/null > /dev/tcp/127.0.0.1/1433'
|
test: timeout 2 bash -c 'cat < /dev/null > /dev/tcp/127.0.0.1/1433'
|
||||||
interval: 10s
|
interval: 10s
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Bootstrappers;
|
namespace Stancl\Tenancy\Bootstrappers;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
use Illuminate\Foundation\Application;
|
use Illuminate\Foundation\Application;
|
||||||
use Illuminate\Session\FileSessionHandler;
|
use Illuminate\Session\FileSessionHandler;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
|
@ -75,8 +76,13 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper
|
||||||
: $this->originalStoragePath . '/framework/cache';
|
: $this->originalStoragePath . '/framework/cache';
|
||||||
|
|
||||||
if (! is_dir($path)) {
|
if (! is_dir($path)) {
|
||||||
// Create tenant framework/cache directory if it does not exist
|
// Create tenant framework/cache directory if it does not exist.
|
||||||
mkdir($path, 0750, true);
|
// We ignore errors due to TOCTOU race conditions, instead we check for success below.
|
||||||
|
@mkdir($path, 0750, true);
|
||||||
|
|
||||||
|
if (! is_dir($path)) {
|
||||||
|
throw new Exception("Unable to create tenant storage directory [{$path}].");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($suffix === false) {
|
if ($suffix === false) {
|
||||||
|
|
@ -222,8 +228,13 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper
|
||||||
: $this->originalStoragePath . '/framework/sessions';
|
: $this->originalStoragePath . '/framework/sessions';
|
||||||
|
|
||||||
if (! is_dir($path)) {
|
if (! is_dir($path)) {
|
||||||
// Create tenant framework/sessions directory if it does not exist
|
// Create tenant framework/sessions directory if it does not exist.
|
||||||
mkdir($path, 0750, true);
|
// We ignore errors due to TOCTOU race conditions, instead we check for success below.
|
||||||
|
@mkdir($path, 0750, true);
|
||||||
|
|
||||||
|
if (! is_dir($path)) {
|
||||||
|
throw new Exception("Unable to create tenant session directory [{$path}].");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->app['config']['session.files'] = $path;
|
$this->app['config']['session.files'] = $path;
|
||||||
|
|
|
||||||
|
|
@ -8,12 +8,13 @@ use Illuminate\Database\Eloquent\Builder;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Scope;
|
use Illuminate\Database\Eloquent\Scope;
|
||||||
|
|
||||||
|
/** @implements Scope<Model> */
|
||||||
class PendingScope implements Scope
|
class PendingScope implements Scope
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Apply the scope to a given Eloquent query builder.
|
* Apply the scope to a given Eloquent query builder.
|
||||||
*
|
*
|
||||||
* @param Builder<Model> $builder
|
* @param Builder<covariant Model> $builder
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
|
|
@ -57,8 +58,10 @@ class PendingScope implements Scope
|
||||||
{
|
{
|
||||||
$builder->macro('withoutPending', function (Builder $builder) {
|
$builder->macro('withoutPending', function (Builder $builder) {
|
||||||
$builder->withoutGlobalScope(static::class)
|
$builder->withoutGlobalScope(static::class)
|
||||||
->whereNull($builder->getModel()->getColumnForQuery('pending_since'))
|
->where(function (Builder $query) {
|
||||||
->orWhereNull($builder->getModel()->getDataColumn());
|
$query->whereNull($query->getModel()->getColumnForQuery('pending_since'))
|
||||||
|
->orWhereNull($query->getModel()->getDataColumn());
|
||||||
|
});
|
||||||
|
|
||||||
return $builder;
|
return $builder;
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -8,10 +8,11 @@ use Illuminate\Database\Eloquent\Builder;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Scope;
|
use Illuminate\Database\Eloquent\Scope;
|
||||||
|
|
||||||
|
/** @implements Scope<Model> */
|
||||||
class ParentModelScope implements Scope
|
class ParentModelScope implements Scope
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @param Builder<Model> $builder
|
* @param Builder<covariant Model> $builder
|
||||||
*/
|
*/
|
||||||
public function apply(Builder $builder, Model $model): void
|
public function apply(Builder $builder, Model $model): void
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -9,10 +9,11 @@ use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Scope;
|
use Illuminate\Database\Eloquent\Scope;
|
||||||
use Stancl\Tenancy\Tenancy;
|
use Stancl\Tenancy\Tenancy;
|
||||||
|
|
||||||
|
/** @implements Scope<Model> */
|
||||||
class TenantScope implements Scope
|
class TenantScope implements Scope
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @param Builder<Model> $builder
|
* @param Builder<covariant Model> $builder
|
||||||
*/
|
*/
|
||||||
public function apply(Builder $builder, Model $model)
|
public function apply(Builder $builder, Model $model)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
43
src/Jobs/DeleteTenantStorage.php
Normal file
43
src/Jobs/DeleteTenantStorage.php
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
<?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;
|
||||||
|
|
||||||
|
class DeleteTenantStorage implements ShouldQueue
|
||||||
|
{
|
||||||
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
public Tenant $tenant,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function handle(): void
|
||||||
|
{
|
||||||
|
if (config('tenancy.filesystem.suffix_storage_path') === false) {
|
||||||
|
// Skip storage deletion if path suffixing is disabled
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$centralStoragePath = tenancy()->central(fn () => storage_path());
|
||||||
|
$tenantStoragePath = tenancy()->run($this->tenant, fn () => storage_path());
|
||||||
|
|
||||||
|
if ($tenantStoragePath === $centralStoragePath) {
|
||||||
|
// Check again to ensure the tenant storage path is distinct from the central storage path
|
||||||
|
// to avoid any accidental central storage path deletion
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_dir($tenantStoragePath)) {
|
||||||
|
File::deleteDirectory($tenantStoragePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -7,11 +7,7 @@ namespace Stancl\Tenancy\Listeners;
|
||||||
use Stancl\Tenancy\Events\Contracts\TenantEvent;
|
use Stancl\Tenancy\Events\Contracts\TenantEvent;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Can be used to manually create framework directories in the tenant storage when storage_path() is scoped.
|
* @deprecated FilesystemTenancyBootstrapper creates the path automatically when suffix_storage_path is enabled.
|
||||||
*
|
|
||||||
* Useful when using real-time facades which use the framework/cache directory.
|
|
||||||
*
|
|
||||||
* Generally not needed anymore as the directory is also created by the FilesystemTenancyBootstrapper.
|
|
||||||
*/
|
*/
|
||||||
class CreateTenantStorage
|
class CreateTenantStorage
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -7,14 +7,29 @@ namespace Stancl\Tenancy\Listeners;
|
||||||
use Illuminate\Support\Facades\File;
|
use Illuminate\Support\Facades\File;
|
||||||
use Stancl\Tenancy\Events\Contracts\TenantEvent;
|
use Stancl\Tenancy\Events\Contracts\TenantEvent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use Stancl\Tenancy\Jobs\DeleteTenantStorage in a job pipeline instead.
|
||||||
|
*/
|
||||||
class DeleteTenantStorage
|
class DeleteTenantStorage
|
||||||
{
|
{
|
||||||
public function handle(TenantEvent $event): void
|
public function handle(TenantEvent $event): void
|
||||||
{
|
{
|
||||||
$path = tenancy()->run($event->tenant, fn () => storage_path());
|
if (config('tenancy.filesystem.suffix_storage_path') === false) {
|
||||||
|
// Skip storage deletion if path suffixing is disabled
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (is_dir($path)) {
|
$centralStoragePath = tenancy()->central(fn () => storage_path());
|
||||||
File::deleteDirectory($path);
|
$tenantStoragePath = tenancy()->run($event->tenant, fn () => storage_path());
|
||||||
|
|
||||||
|
if ($tenantStoragePath === $centralStoragePath) {
|
||||||
|
// Check again to ensure the tenant storage path is distinct from the central storage path
|
||||||
|
// to avoid any accidental central storage path deletion
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_dir($tenantStoragePath)) {
|
||||||
|
File::deleteDirectory($tenantStoragePath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ use Stancl\Tenancy\Events\TenancyInitialized;
|
||||||
use Stancl\Tenancy\Jobs\CreateStorageSymlinks;
|
use Stancl\Tenancy\Jobs\CreateStorageSymlinks;
|
||||||
use Stancl\Tenancy\Jobs\RemoveStorageSymlinks;
|
use Stancl\Tenancy\Jobs\RemoveStorageSymlinks;
|
||||||
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||||
use Stancl\Tenancy\Listeners\DeleteTenantStorage;
|
use Stancl\Tenancy\Jobs\DeleteTenantStorage;
|
||||||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||||
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
|
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
|
||||||
use function Stancl\Tenancy\Tests\pest;
|
use function Stancl\Tenancy\Tests\pest;
|
||||||
|
|
@ -184,21 +184,63 @@ test('create and delete storage symlinks jobs work', function() {
|
||||||
$this->assertDirectoryDoesNotExist(public_path("public-$tenantKey"));
|
$this->assertDirectoryDoesNotExist(public_path("public-$tenantKey"));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('tenant storage can get deleted after the tenant when DeletingTenant listens to DeleteTenantStorage', function() {
|
test('tenant storage gets deleted during tenant deletion when the DeletingTenant pipeline contains 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);
|
||||||
|
expect(File::isDirectory($centralStoragePath))->toBeTrue();
|
||||||
|
tenant()->delete();
|
||||||
|
|
||||||
|
expect(File::isDirectory($centralStoragePath))->toBeTrue();
|
||||||
|
|
||||||
|
config([
|
||||||
|
'tenancy.bootstrappers' => [FilesystemTenancyBootstrapper::class],
|
||||||
|
'tenancy.filesystem.suffix_storage_path' => false,
|
||||||
|
]);
|
||||||
|
|
||||||
|
tenancy()->initialize(Tenant::create());
|
||||||
|
|
||||||
|
$tenantStoragePath = storage_path();
|
||||||
|
|
||||||
|
// 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($centralStoragePath))->toBeTrue();
|
||||||
|
tenant()->delete();
|
||||||
|
|
||||||
|
expect(File::isDirectory($centralStoragePath))->toBeTrue();
|
||||||
|
|
||||||
|
config([
|
||||||
|
'tenancy.bootstrappers' => [FilesystemTenancyBootstrapper::class],
|
||||||
|
'tenancy.filesystem.suffix_storage_path' => true,
|
||||||
|
]);
|
||||||
|
|
||||||
tenancy()->initialize(Tenant::create());
|
tenancy()->initialize(Tenant::create());
|
||||||
$tenantStoragePath = storage_path();
|
$tenantStoragePath = storage_path();
|
||||||
|
|
||||||
Storage::fake('test');
|
// FilesystemTenancyBootstrapper enabled,
|
||||||
|
// suffix_storage_path enabled, so the two paths are distinct.
|
||||||
|
// Tenant storage will be deleted.
|
||||||
|
expect($tenantStoragePath)->not()->toBe($centralStoragePath);
|
||||||
expect(File::isDirectory($tenantStoragePath))->toBeTrue();
|
expect(File::isDirectory($tenantStoragePath))->toBeTrue();
|
||||||
|
|
||||||
Storage::put('test.txt', 'testing file');
|
|
||||||
|
|
||||||
tenant()->delete();
|
tenant()->delete();
|
||||||
|
|
||||||
expect(File::isDirectory($tenantStoragePath))->toBeFalse();
|
expect(File::isDirectory($tenantStoragePath))->toBeFalse();
|
||||||
|
expect(File::isDirectory($centralStoragePath))->toBeTrue();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('the framework/cache directory is created when storage_path is scoped', function (bool $suffixStoragePath) {
|
test('the framework/cache directory is created when storage_path is scoped', function (bool $suffixStoragePath) {
|
||||||
|
|
@ -256,4 +298,3 @@ test('scoped disks are scoped per tenant', function () {
|
||||||
expect(file_get_contents(storage_path() . "/app/public/scoped_disk_prefix/foo.txt"))->toBe('central2');
|
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');
|
expect(file_get_contents(storage_path() . "/tenant{$tenant->id}/app/public/scoped_disk_prefix/foo.txt"))->toBe('tenant');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -111,6 +111,18 @@ test('a new tenant gets created while pulling a pending tenant if the pending po
|
||||||
expect(Tenant::withPending()->get()->count())->toBe(1); // All tenants
|
expect(Tenant::withPending()->get()->count())->toBe(1); // All tenants
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('withoutPending chained with where clauses returns correct results', function () {
|
||||||
|
$tenant = Tenant::create();
|
||||||
|
$pendingTenant = Tenant::createPending();
|
||||||
|
|
||||||
|
// The query returned the correct tenant
|
||||||
|
expect(Tenant::withoutPending()->where('id', $tenant->id)->first()->id)->toBe($tenant->id);
|
||||||
|
// No tenant with this ID exists, the query returns null
|
||||||
|
expect(Tenant::withoutPending()->where('id', Str::random(8) . 'nonexistent-id')->first())->toBeNull();
|
||||||
|
// withoutPending() correctly excludes the pending tenant from the query
|
||||||
|
expect(Tenant::withoutPending()->where('id', $pendingTenant->id)->first())->toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
test('pending tenants are included in all queries based on the include_in_queries config', function () {
|
test('pending tenants are included in all queries based on the include_in_queries config', function () {
|
||||||
Tenant::createPending();
|
Tenant::createPending();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue