From 2ba9ef2a3ad14fb8632a97c4a6a4163a862a69d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marian=20B=C3=A4uerle?= Date: Tue, 19 Sep 2017 17:01:49 +0200 Subject: [PATCH 1/4] Fix querying all tenants when model was not loaded yet --- src/BelongsToTenants.php | 3 ++- tests/LandlordTest.php | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/BelongsToTenants.php b/src/BelongsToTenants.php index 6a75704..191abc1 100644 --- a/src/BelongsToTenants.php +++ b/src/BelongsToTenants.php @@ -66,7 +66,8 @@ public function getQualifiedTenant($tenant) */ public static function allTenants() { - return static::$landlord->newQueryWithoutTenants(new static()); + $model = new static(); + return static::$landlord->newQueryWithoutTenants($model); } /** diff --git a/tests/LandlordTest.php b/tests/LandlordTest.php index a397d0e..9cd58c3 100644 --- a/tests/LandlordTest.php +++ b/tests/LandlordTest.php @@ -8,6 +8,24 @@ class LandlordTest extends TestCase { + public function testApplyScopesQueryAllTenants() + { + app()->bind(TenantManager::class, function() { + $manager = \Mockery::mock(TenantManager::class); + $manager->shouldReceive('newQueryWithoutTenants')->andReturn([]); + $manager->shouldReceive('applyTenantScopes'); + return $manager; + }); + + $landlord = new TenantManager(); + + $landlord->addTenant('tenant_a_id', 1); + + $landlord->addTenant('tenant_b_id', 2); + + $this->assertEquals([], ModelStub::allTenants()); + } + public function testTenantsWithStrings() { $landlord = new TenantManager(); From b86fe4c5e7526917df7597e4f1b8b6a65ee9d275 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marian=20Ba=CC=88uerle?= Date: Mon, 16 Oct 2017 17:30:12 +0200 Subject: [PATCH 2/4] Revert merge --- src/BelongsToTenants.php | 3 +-- tests/LandlordTest.php | 18 ------------------ 2 files changed, 1 insertion(+), 20 deletions(-) diff --git a/src/BelongsToTenants.php b/src/BelongsToTenants.php index 191abc1..6a75704 100644 --- a/src/BelongsToTenants.php +++ b/src/BelongsToTenants.php @@ -66,8 +66,7 @@ public function getQualifiedTenant($tenant) */ public static function allTenants() { - $model = new static(); - return static::$landlord->newQueryWithoutTenants($model); + return static::$landlord->newQueryWithoutTenants(new static()); } /** diff --git a/tests/LandlordTest.php b/tests/LandlordTest.php index 9cd58c3..a397d0e 100644 --- a/tests/LandlordTest.php +++ b/tests/LandlordTest.php @@ -8,24 +8,6 @@ class LandlordTest extends TestCase { - public function testApplyScopesQueryAllTenants() - { - app()->bind(TenantManager::class, function() { - $manager = \Mockery::mock(TenantManager::class); - $manager->shouldReceive('newQueryWithoutTenants')->andReturn([]); - $manager->shouldReceive('applyTenantScopes'); - return $manager; - }); - - $landlord = new TenantManager(); - - $landlord->addTenant('tenant_a_id', 1); - - $landlord->addTenant('tenant_b_id', 2); - - $this->assertEquals([], ModelStub::allTenants()); - } - public function testTenantsWithStrings() { $landlord = new TenantManager(); From 6a0741f56a2c2673fbb05f795c8fa9863f0f1f88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marian=20Ba=CC=88uerle?= Date: Mon, 16 Oct 2017 17:42:25 +0200 Subject: [PATCH 3/4] Disable landlord in closure --- src/TenantManager.php | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/src/TenantManager.php b/src/TenantManager.php index 4a719ae..32595c4 100644 --- a/src/TenantManager.php +++ b/src/TenantManager.php @@ -130,10 +130,6 @@ public function getTenantId($tenant) */ public function applyTenantScopes(Model $model) { - if (!$this->enabled) { - return; - } - if ($this->tenants->isEmpty()) { // No tenants yet, defer scoping to a later stage $this->deferredModels->push($model); @@ -141,13 +137,7 @@ public function applyTenantScopes(Model $model) } $this->modelTenants($model)->each(function ($id, $tenant) use ($model) { - $model->addGlobalScope($tenant, function (Builder $builder) use ($tenant, $id, $model) { - if($this->getTenants()->first() && $this->getTenants()->first() != $id){ - $id = $this->getTenants()->first(); - } - - $builder->where($model->getQualifiedTenant($tenant), '=', $id); - }); + $this->addGlobalScopeToSingleModel($tenant, $id, $model); }); } @@ -163,19 +153,35 @@ public function applyTenantScopesToDeferredModels() $model->setAttribute($tenant, $id); } - $model->addGlobalScope($tenant, function (Builder $builder) use ($tenant, $id, $model) { - if($this->getTenants()->first() && $this->getTenants()->first() != $id){ - $id = $this->getTenants()->first(); - } - - $builder->where($model->getQualifiedTenant($tenant), '=', $id); - }); + $this->addGlobalScopeToSingleModel($tenant, $id, $model); }); }); $this->deferredModels = collect(); } + /** + * Add the global scope to a single model + * + * @param string|Model $tenant + * @param string $id + * @param Model $model + */ + private function addGlobalScopeToSingleModel($tenant, $id, $model) + { + $model->addGlobalScope($tenant, function (Builder $builder) use ($tenant, $id, $model) { + if(!$this->enabled) { + return; + } + + if($this->getTenants()->first() && $this->getTenants()->first() != $id){ + $id = $this->getTenants()->first(); + } + + $builder->where($model->getQualifiedTenant($tenant), '=', $id); + }); + } + /** * Add tenant columns as needed to a new model instance before it is created. * From 362c1efe85575c02652753f8ef1ae5250e678014 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marian=20Ba=CC=88uerle?= Date: Mon, 16 Oct 2017 18:55:53 +0200 Subject: [PATCH 4/4] Rename variables --- src/TenantManager.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/TenantManager.php b/src/TenantManager.php index 32595c4..c1d16dc 100644 --- a/src/TenantManager.php +++ b/src/TenantManager.php @@ -163,22 +163,22 @@ public function applyTenantScopesToDeferredModels() /** * Add the global scope to a single model * - * @param string|Model $tenant - * @param string $id + * @param string|Model $tenant_column + * @param string $tenant_id * @param Model $model */ - private function addGlobalScopeToSingleModel($tenant, $id, $model) + private function addGlobalScopeToSingleModel($tenant_column, $tenant_id, $model) { - $model->addGlobalScope($tenant, function (Builder $builder) use ($tenant, $id, $model) { + $model->addGlobalScope($tenant_column, function (Builder $builder) use ($tenant_column, $tenant_id, $model) { if(!$this->enabled) { return; } - if($this->getTenants()->first() && $this->getTenants()->first() != $id){ - $id = $this->getTenants()->first(); + if($this->getTenants()->first() && $this->getTenants()->first() != $tenant_id){ + $tenant_id = $this->getTenants()->first(); } - $builder->where($model->getQualifiedTenant($tenant), '=', $id); + $builder->where($model->getQualifiedTenant($tenant_column), '=', $tenant_id); }); }