diff --git a/src/Contracts/Syncable.php b/src/Contracts/Syncable.php index 1f6e36e7..5c35f317 100644 --- a/src/Contracts/Syncable.php +++ b/src/Contracts/Syncable.php @@ -15,4 +15,6 @@ interface Syncable public function getSyncedAttributeNames(): array; public function triggerSyncEvent(); + + public function isSyncEnabled(); } diff --git a/src/Database/Concerns/ResourceSyncing.php b/src/Database/Concerns/ResourceSyncing.php index f1026f7a..3d6c4cc2 100644 --- a/src/Database/Concerns/ResourceSyncing.php +++ b/src/Database/Concerns/ResourceSyncing.php @@ -14,15 +14,16 @@ trait ResourceSyncing { static::saved(function (Syncable $model) { /** @var ResourceSyncing $model */ - $model->triggerSyncEvent(); + if ($model->isSyncEnabled()) { + $model->triggerSyncEvent(); + } }); static::creating(function (self $model) { - if (! $model->getAttribute($model->getGlobalIdentifierKeyName()) && app()->bound(UniqueIdentifierGenerator::class)) { - $model->setAttribute( - $model->getGlobalIdentifierKeyName(), - app(UniqueIdentifierGenerator::class)->generate($model) - ); + $keyName = $model->getGlobalIdentifierKeyName(); + + if (! $model->getAttribute($keyName) && app()->bound(UniqueIdentifierGenerator::class)) { + $model->setAttribute($keyName, app(UniqueIdentifierGenerator::class)->generate($model)); } }); } @@ -32,4 +33,9 @@ trait ResourceSyncing /** @var Syncable $this */ event(new SyncedResourceSaved($this, tenant())); } + + public function isSyncEnabled() + { + return true; + } } diff --git a/src/Database/Models/TenantPivot.php b/src/Database/Models/TenantPivot.php index f745b45a..877d9466 100644 --- a/src/Database/Models/TenantPivot.php +++ b/src/Database/Models/TenantPivot.php @@ -16,7 +16,7 @@ class TenantPivot extends Pivot static::saved(function (self $pivot) { $parent = $pivot->pivotParent; - if ($parent instanceof Syncable) { + if ($parent instanceof Syncable && $parent->isSyncEnabled()) { $parent->triggerSyncEvent(); } }); diff --git a/tests/ResourceSyncingTest.php b/tests/ResourceSyncingTest.php index 570448d1..ea71ead4 100644 --- a/tests/ResourceSyncingTest.php +++ b/tests/ResourceSyncingTest.php @@ -89,6 +89,61 @@ class ResourceSyncingTest extends TestCase }); } + /** @test */ + public function resources_are_synced_only_sync_is_enabled() + { + CentralUser::create([ + 'global_id' => 'acme', + 'name' => 'John Doe', + 'email' => 'john@localhost', + 'password' => 'secret', + 'role' => 'commenter', // synced + ]); + + $tenant = ResourceTenant::create(); + $this->migrateTenants(); + + $tenant->run(function() { + ResourceUser::create([ + 'name' => 'Foo', + 'email' => 'foo@email.com', + 'password' => 'secret', + 'global_id' => 'acme', + 'role' => 'not_sync', + ]); + }); + + $centralUser = CentralUser::first(); + $this->assertSame('John Doe', $centralUser->name); // not sync + $this->assertSame('john@localhost', $centralUser->email); // not sync + $this->assertSame('secret', $centralUser->password); // not sync + } + + /** @test */ + public function central_users_are_synced_only_sync_is_enabled() + { + $centralUser = CentralUser::create([ + 'global_id' => 'acme', + 'name' => 'John Doe', + 'email' => 'john@localhost', + 'password' => 'secret', + 'role' => 'not_sync', // unsynced + ]); + + $t1 = ResourceTenant::create([ + 'id' => 't1', + ]); + $this->migrateTenants(); + + $centralUser->tenants()->attach('t1'); + + $t1->run(function () { + // assert user not exsits + $this->assertCount(0, ResourceUser::all()); + }); + + } + /** @test */ public function only_the_synced_columns_are_updated_in_the_central_db() { @@ -626,6 +681,11 @@ class CentralUser extends Model implements SyncMaster 'email', ]; } + + public function isSyncEnabled() + { + return $this->role !== 'not_sync'; + } } class ResourceUser extends Model implements Syncable @@ -659,4 +719,9 @@ class ResourceUser extends Model implements Syncable 'email', ]; } + + public function isSyncEnabled() + { + return $this->role !== 'not_sync'; + } }