mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 14:34:04 +00:00
Compare commits
5 commits
87368e36d6
...
ed12b92bd8
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ed12b92bd8 | ||
|
|
2b45a01018 | ||
| f53fcb279f | |||
|
|
bc06da84ef | ||
|
|
ee0b45d800 |
12 changed files with 242 additions and 36 deletions
|
|
@ -81,6 +81,8 @@ class TenancyServiceProvider extends ServiceProvider
|
|||
])->send(function (Events\TenantDeleted $event) {
|
||||
return $event->tenant;
|
||||
})->shouldBeQueued(false),
|
||||
|
||||
// ResourceSyncing\Listeners\DeleteAllTenantMappings::class,
|
||||
],
|
||||
|
||||
Events\TenantMaintenanceModeEnabled::class => [],
|
||||
|
|
@ -129,6 +131,9 @@ class TenancyServiceProvider extends ServiceProvider
|
|||
ResourceSyncing\Events\SyncedResourceSaved::class => [
|
||||
ResourceSyncing\Listeners\UpdateOrCreateSyncedResource::class,
|
||||
],
|
||||
ResourceSyncing\Events\SyncedResourceDeleted::class => [
|
||||
ResourceSyncing\Listeners\DeleteResourceMapping::class,
|
||||
],
|
||||
ResourceSyncing\Events\SyncMasterDeleted::class => [
|
||||
ResourceSyncing\Listeners\DeleteResourcesInTenants::class,
|
||||
],
|
||||
|
|
|
|||
18
src/ResourceSyncing/Events/SyncedResourceDeleted.php
Normal file
18
src/ResourceSyncing/Events/SyncedResourceDeleted.php
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\ResourceSyncing\Events;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Stancl\Tenancy\Database\Contracts\TenantWithDatabase;
|
||||
use Stancl\Tenancy\ResourceSyncing\Syncable;
|
||||
|
||||
class SyncedResourceDeleted
|
||||
{
|
||||
public function __construct(
|
||||
public Syncable&Model $model,
|
||||
public TenantWithDatabase|null $tenant,
|
||||
public bool $forceDelete,
|
||||
) {}
|
||||
}
|
||||
38
src/ResourceSyncing/Listeners/DeleteAllTenantMappings.php
Normal file
38
src/ResourceSyncing/Listeners/DeleteAllTenantMappings.php
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\ResourceSyncing\Listeners;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Stancl\Tenancy\Events\TenantDeleted;
|
||||
use Stancl\Tenancy\Listeners\QueueableListener;
|
||||
|
||||
/**
|
||||
* Cleans up pivot records related to the deleted tenant.
|
||||
*
|
||||
* The listener only cleans up the pivot tables specified
|
||||
* in the $pivotTables property (see the property for details),
|
||||
* and is intended for use with tables that do not have tenant
|
||||
* foreign key constraints with onDelete('cascade').
|
||||
*/
|
||||
class DeleteAllTenantMappings extends QueueableListener
|
||||
{
|
||||
/**
|
||||
* Pivot tables to clean up after a tenant is deleted, in the
|
||||
* ['table_name' => 'tenant_key_column'] format.
|
||||
*
|
||||
* Since we cannot automatically detect which pivot tables
|
||||
* are being used, they have to be specified here manually.
|
||||
*
|
||||
* The default value follows the polymorphic table used by default.
|
||||
*/
|
||||
public static array $pivotTables = ['tenant_resources' => 'tenant_id'];
|
||||
|
||||
public function handle(TenantDeleted $event): void
|
||||
{
|
||||
foreach (static::$pivotTables as $table => $tenantKeyColumn) {
|
||||
DB::table($table)->where($tenantKeyColumn, $event->tenant->getKey())->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
60
src/ResourceSyncing/Listeners/DeleteResourceMapping.php
Normal file
60
src/ResourceSyncing/Listeners/DeleteResourceMapping.php
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\ResourceSyncing\Listeners;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\Pivot;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Stancl\Tenancy\Listeners\QueueableListener;
|
||||
use Stancl\Tenancy\ResourceSyncing\Events\SyncedResourceDeleted;
|
||||
use Stancl\Tenancy\ResourceSyncing\Syncable;
|
||||
use Stancl\Tenancy\ResourceSyncing\SyncMaster;
|
||||
|
||||
/**
|
||||
* Deletes pivot records when a synced resource is deleted.
|
||||
*
|
||||
* If a SyncMaster (central resource) is deleted, all pivot records for that resource are deleted.
|
||||
* If a Syncable (tenant resource) is deleted, only delete the pivot record for that tenant.
|
||||
*/
|
||||
class DeleteResourceMapping extends QueueableListener
|
||||
{
|
||||
public static bool $shouldQueue = false;
|
||||
|
||||
public function handle(SyncedResourceDeleted $event): void
|
||||
{
|
||||
$centralResource = $this->getCentralResource($event->model);
|
||||
|
||||
if (! $centralResource) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Delete pivot records if the central resource doesn't use soft deletes
|
||||
// or the central resource was deleted using forceDelete()
|
||||
if ($event->forceDelete || ! in_array(SoftDeletes::class, class_uses_recursive($centralResource::class), true)) {
|
||||
Pivot::withoutEvents(function () use ($centralResource, $event) {
|
||||
// If detach() is called with null -- if $event->tenant is null -- this means a central resource was deleted and detaches all tenants.
|
||||
// If detach() is called with a specific tenant, it means the resource was deleted in that tenant, and we only delete that single mapping.
|
||||
$centralResource->tenants()->detach($event->tenant);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public function getCentralResource(Syncable&Model $resource): SyncMaster|null
|
||||
{
|
||||
if ($resource instanceof SyncMaster) {
|
||||
return $resource;
|
||||
}
|
||||
|
||||
$centralResourceClass = $resource->getCentralModelName();
|
||||
|
||||
/** @var (SyncMaster&Model)|null $centralResource */
|
||||
$centralResource = $centralResourceClass::firstWhere(
|
||||
$resource->getGlobalIdentifierKeyName(),
|
||||
$resource->getGlobalIdentifierKey()
|
||||
);
|
||||
|
||||
return $centralResource;
|
||||
}
|
||||
}
|
||||
|
|
@ -4,7 +4,6 @@ declare(strict_types=1);
|
|||
|
||||
namespace Stancl\Tenancy\ResourceSyncing\Listeners;
|
||||
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Stancl\Tenancy\Listeners\QueueableListener;
|
||||
use Stancl\Tenancy\ResourceSyncing\Events\SyncMasterDeleted;
|
||||
|
||||
|
|
@ -21,12 +20,6 @@ class DeleteResourcesInTenants extends QueueableListener
|
|||
|
||||
tenancy()->runForMultiple($centralResource->tenants()->cursor(), function () use ($centralResource, $forceDelete) {
|
||||
$this->deleteSyncedResource($centralResource, $forceDelete);
|
||||
|
||||
// Delete pivot records if the central resource doesn't use soft deletes
|
||||
// or the central resource was deleted using forceDelete()
|
||||
if ($forceDelete || ! in_array(SoftDeletes::class, class_uses_recursive($centralResource::class), true)) {
|
||||
$centralResource->tenants()->detach(tenant());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
|
|||
use Stancl\Tenancy\Database\Contracts\TenantWithDatabase;
|
||||
use Stancl\Tenancy\ResourceSyncing\Events\CentralResourceAttachedToTenant;
|
||||
use Stancl\Tenancy\ResourceSyncing\Events\CentralResourceDetachedFromTenant;
|
||||
use Stancl\Tenancy\ResourceSyncing\Events\SyncedResourceDeleted;
|
||||
use Stancl\Tenancy\ResourceSyncing\Events\SyncedResourceSaved;
|
||||
use Stancl\Tenancy\ResourceSyncing\Events\SyncMasterDeleted;
|
||||
use Stancl\Tenancy\ResourceSyncing\Events\SyncMasterRestored;
|
||||
|
|
@ -19,37 +20,34 @@ trait ResourceSyncing
|
|||
{
|
||||
public static function bootResourceSyncing(): void
|
||||
{
|
||||
static::saved(function (Syncable&Model $model) {
|
||||
static::saved(static function (Syncable&Model $model) {
|
||||
if ($model->shouldSync() && ($model->wasRecentlyCreated || $model->wasChanged($model->getSyncedAttributeNames()))) {
|
||||
$model->triggerSyncEvent();
|
||||
}
|
||||
});
|
||||
|
||||
static::deleting(function (Syncable&Model $model) {
|
||||
if ($model->shouldSync() && $model instanceof SyncMaster) {
|
||||
static::deleted(static function (Syncable&Model $model) {
|
||||
if ($model->shouldSync()) {
|
||||
$model->triggerDeleteEvent();
|
||||
}
|
||||
});
|
||||
|
||||
static::creating(function (Syncable&Model $model) {
|
||||
if (! $model->getAttribute($model->getGlobalIdentifierKeyName()) && app()->bound(UniqueIdentifierGenerator::class)) {
|
||||
$model->setAttribute(
|
||||
$model->getGlobalIdentifierKeyName(),
|
||||
app(UniqueIdentifierGenerator::class)->generate($model)
|
||||
);
|
||||
static::creating(static function (Syncable&Model $model) {
|
||||
if (! $model->getAttribute($model->getGlobalIdentifierKeyName())) {
|
||||
$model->generateGlobalIdentifierKey();
|
||||
}
|
||||
});
|
||||
|
||||
if (in_array(SoftDeletes::class, class_uses_recursive(static::class), true)) {
|
||||
static::forceDeleting(function (Syncable&Model $model) {
|
||||
if ($model->shouldSync() && $model instanceof SyncMaster) {
|
||||
static::forceDeleting(static function (Syncable&Model $model) {
|
||||
if ($model->shouldSync()) {
|
||||
$model->triggerDeleteEvent(true);
|
||||
}
|
||||
});
|
||||
|
||||
static::restoring(function (Syncable&Model $model) {
|
||||
if ($model->shouldSync() && $model instanceof SyncMaster) {
|
||||
$model->triggerRestoredEvent();
|
||||
static::restoring(static function (Syncable&Model $model) {
|
||||
if ($model instanceof SyncMaster && $model->shouldSync()) {
|
||||
$model->triggerRestoreEvent();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -67,9 +65,11 @@ trait ResourceSyncing
|
|||
/** @var SyncMaster&Model $this */
|
||||
event(new SyncMasterDeleted($this, $forceDelete));
|
||||
}
|
||||
|
||||
event(new SyncedResourceDeleted($this, tenant(), $forceDelete));
|
||||
}
|
||||
|
||||
public function triggerRestoredEvent(): void
|
||||
public function triggerRestoreEvent(): void
|
||||
{
|
||||
if ($this instanceof SyncMaster && in_array(SoftDeletes::class, class_uses_recursive($this), true)) {
|
||||
/** @var SyncMaster&Model $this */
|
||||
|
|
@ -116,8 +116,18 @@ trait ResourceSyncing
|
|||
return 'global_id';
|
||||
}
|
||||
|
||||
public function getGlobalIdentifierKey(): string
|
||||
public function getGlobalIdentifierKey(): string|int
|
||||
{
|
||||
return $this->getAttribute($this->getGlobalIdentifierKeyName());
|
||||
}
|
||||
|
||||
protected function generateGlobalIdentifierKey(): void
|
||||
{
|
||||
if (! app()->bound(UniqueIdentifierGenerator::class)) return;
|
||||
|
||||
$this->setAttribute(
|
||||
$this->getGlobalIdentifierKeyName(),
|
||||
app(UniqueIdentifierGenerator::class)->generate($this),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,7 +25,5 @@ interface SyncMaster extends Syncable
|
|||
|
||||
public function triggerAttachEvent(TenantWithDatabase&Model $tenant): void;
|
||||
|
||||
public function triggerDeleteEvent(bool $forceDelete = false): void;
|
||||
|
||||
public function triggerRestoredEvent(): void;
|
||||
public function triggerRestoreEvent(): void;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,8 @@ interface Syncable
|
|||
|
||||
public function triggerSyncEvent(): void;
|
||||
|
||||
public function triggerDeleteEvent(bool $forceDelete = false): void;
|
||||
|
||||
/**
|
||||
* Get the attributes used for creating the *other* model (i.e. tenant if this is the central one, and central if this is the tenant one).
|
||||
*
|
||||
|
|
|
|||
|
|
@ -20,14 +20,14 @@ trait TriggerSyncingEvents
|
|||
{
|
||||
public static function bootTriggerSyncingEvents(): void
|
||||
{
|
||||
static::saving(function (self $pivot) {
|
||||
static::saving(static function (self $pivot) {
|
||||
// Try getting the central resource to see if it is available
|
||||
// If it is not available, throw an exception to interrupt the saving process
|
||||
// And prevent creating a pivot record without a central resource
|
||||
$pivot->getCentralResourceAndTenant();
|
||||
});
|
||||
|
||||
static::saved(function (self $pivot) {
|
||||
static::saved(static function (self $pivot) {
|
||||
/**
|
||||
* @var static&Pivot $pivot
|
||||
* @var SyncMaster|null $centralResource
|
||||
|
|
@ -40,7 +40,7 @@ trait TriggerSyncingEvents
|
|||
}
|
||||
});
|
||||
|
||||
static::deleting(function (self $pivot) {
|
||||
static::deleting(static function (self $pivot) {
|
||||
/**
|
||||
* @var static&Pivot $pivot
|
||||
* @var SyncMaster|null $centralResource
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ use Stancl\Tenancy\ResourceSyncing\SyncMaster;
|
|||
class CentralUser extends Model implements SyncMaster
|
||||
{
|
||||
use ResourceSyncing, CentralConnection;
|
||||
|
||||
protected $guarded = [];
|
||||
|
||||
public $timestamps = false;
|
||||
|
|
|
|||
|
|
@ -16,9 +16,6 @@ class CreateTenantUsersTable extends Migration
|
|||
$table->string('global_user_id');
|
||||
|
||||
$table->unique(['tenant_id', 'global_user_id']);
|
||||
|
||||
$table->foreign('tenant_id')->references('id')->on('tenants')->onUpdate('cascade')->onDelete('cascade');
|
||||
$table->foreign('global_user_id')->references('global_id')->on('users')->onUpdate('cascade')->onDelete('cascade');
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -46,6 +46,12 @@ use Stancl\Tenancy\ResourceSyncing\Events\SyncedResourceSavedInForeignDatabase;
|
|||
use Illuminate\Database\Eloquent\Scope;
|
||||
use Illuminate\Database\QueryException;
|
||||
use function Stancl\Tenancy\Tests\pest;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Stancl\Tenancy\Events\TenantDeleted;
|
||||
use Stancl\Tenancy\ResourceSyncing\Events\SyncedResourceDeleted;
|
||||
use Stancl\Tenancy\ResourceSyncing\Listeners\DeleteAllTenantMappings;
|
||||
use Stancl\Tenancy\ResourceSyncing\Listeners\DeleteResourceMapping;
|
||||
|
||||
beforeEach(function () {
|
||||
config(['tenancy.bootstrappers' => [
|
||||
|
|
@ -69,6 +75,7 @@ beforeEach(function () {
|
|||
CreateTenantResource::$shouldQueue = false;
|
||||
DeleteResourceInTenant::$shouldQueue = false;
|
||||
UpdateOrCreateSyncedResource::$scopeGetModelQuery = null;
|
||||
DeleteAllTenantMappings::$pivotTables = ['tenant_resources' => 'tenant_id'];
|
||||
|
||||
// Reset global scopes on models (should happen automatically but to make this more explicit)
|
||||
Model::clearBootedModels();
|
||||
|
|
@ -92,6 +99,7 @@ beforeEach(function () {
|
|||
CentralUser::$creationAttributes = $creationAttributes;
|
||||
|
||||
Event::listen(SyncedResourceSaved::class, UpdateOrCreateSyncedResource::class);
|
||||
Event::listen(SyncedResourceDeleted::class, DeleteResourceMapping::class);
|
||||
Event::listen(SyncMasterDeleted::class, DeleteResourcesInTenants::class);
|
||||
Event::listen(SyncMasterRestored::class, RestoreResourcesInTenants::class);
|
||||
Event::listen(CentralResourceAttachedToTenant::class, CreateTenantResource::class);
|
||||
|
|
@ -890,7 +898,54 @@ test('deleting SyncMaster automatically deletes its Syncables', function (bool $
|
|||
'basic pivot' => false,
|
||||
]);
|
||||
|
||||
test('tenant pivot records are deleted along with the tenants to which they belong to', function() {
|
||||
test('tenant pivot records are deleted along with the tenants to which they belong', function (bool $dbLevelOnCascadeDelete, bool $morphPivot) {
|
||||
[$tenant] = createTenantsAndRunMigrations();
|
||||
|
||||
if ($morphPivot) {
|
||||
config(['tenancy.models.tenant' => MorphTenant::class]);
|
||||
$centralUserModel = BaseCentralUser::class;
|
||||
|
||||
// The default pivot table, no need to configure the listener
|
||||
$pivotTable = 'tenant_resources';
|
||||
} else {
|
||||
$centralUserModel = CentralUser::class;
|
||||
|
||||
// Custom pivot table
|
||||
$pivotTable = 'tenant_users';
|
||||
}
|
||||
|
||||
if ($dbLevelOnCascadeDelete) {
|
||||
addTenantIdConstraintToPivot($pivotTable);
|
||||
} else {
|
||||
// Event-based cleanup
|
||||
Event::listen(TenantDeleted::class, DeleteAllTenantMappings::class);
|
||||
|
||||
DeleteAllTenantMappings::$pivotTables = [$pivotTable => 'tenant_id'];
|
||||
}
|
||||
|
||||
$syncMaster = $centralUserModel::create([
|
||||
'global_id' => 'user',
|
||||
'name' => 'Central user',
|
||||
'email' => 'central@localhost',
|
||||
'password' => 'password',
|
||||
'role' => 'user',
|
||||
]);
|
||||
|
||||
$syncMaster->tenants()->attach($tenant);
|
||||
|
||||
// Pivot records should be deleted along with the tenant
|
||||
$tenant->delete();
|
||||
|
||||
expect(DB::select("SELECT * FROM {$pivotTable} WHERE tenant_id = ?", [$tenant->getTenantKey()]))->toHaveCount(0);
|
||||
})->with([
|
||||
'db level on cascade delete' => true,
|
||||
'event-based on cascade delete' => false,
|
||||
])->with([
|
||||
'polymorphic pivot' => true,
|
||||
'basic pivot' => false,
|
||||
]);
|
||||
|
||||
test('pivot record is automatically deleted with the tenant resource', function() {
|
||||
[$tenant] = createTenantsAndRunMigrations();
|
||||
|
||||
$syncMaster = CentralUser::create([
|
||||
|
|
@ -903,10 +958,32 @@ test('tenant pivot records are deleted along with the tenants to which they belo
|
|||
|
||||
$syncMaster->tenants()->attach($tenant);
|
||||
|
||||
$tenant->delete();
|
||||
expect(DB::select("SELECT * FROM tenant_users WHERE tenant_id = ?", [$tenant->id]))->toHaveCount(1);
|
||||
|
||||
// Deleting tenant deletes its pivot records
|
||||
expect(DB::select("SELECT * FROM tenant_users WHERE tenant_id = ?", [$tenant->getTenantKey()]))->toHaveCount(0);
|
||||
$tenant->run(function () {
|
||||
TenantUser::firstWhere('global_id', 'cascade_user')->delete();
|
||||
});
|
||||
|
||||
// Deleting tenant resource deletes its pivot record
|
||||
expect(DB::select("SELECT * FROM tenant_users WHERE tenant_id = ?", [$tenant->id]))->toHaveCount(0);
|
||||
});
|
||||
|
||||
test('DeleteAllTenantMappings handles incorrect configuration correctly', function() {
|
||||
Event::listen(TenantDeleted::class, DeleteAllTenantMappings::class);
|
||||
|
||||
[$tenant1, $tenant2] = createTenantsAndRunMigrations();
|
||||
|
||||
// Existing table, non-existent tenant key column
|
||||
// The listener should throw an 'unknown column' exception
|
||||
DeleteAllTenantMappings::$pivotTables = ['tenant_users' => 'non_existent_column'];
|
||||
|
||||
// Should throw an exception when tenant is deleted
|
||||
expect(fn() => $tenant1->delete())->toThrow(QueryException::class, "Unknown column 'non_existent_column' in 'where clause'");
|
||||
|
||||
// Non-existent table
|
||||
DeleteAllTenantMappings::$pivotTables = ['nonexistent_pivot' => 'non_existent_column'];
|
||||
|
||||
expect(fn() => $tenant2->delete())->toThrow(QueryException::class, "Table 'main.nonexistent_pivot' doesn't exist");
|
||||
});
|
||||
|
||||
test('trashed resources are synced correctly', function () {
|
||||
|
|
@ -1265,6 +1342,13 @@ test('global scopes on syncable models can break resource syncing', function ()
|
|||
expect($tenant1->run(fn () => TenantUser::first()->name))->toBe('tenant2 user');
|
||||
});
|
||||
|
||||
function addTenantIdConstraintToPivot(string $pivotTable): void
|
||||
{
|
||||
Schema::table($pivotTable, function (Blueprint $table) {
|
||||
$table->foreign('tenant_id')->references('id')->on('tenants')->onDelete('cascade');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Create two tenants and run migrations for those tenants.
|
||||
*
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue