public function testIsTenantConstraint() { $scope = new TenantScope(); $model = m::mock('Illuminate\\Database\\Eloquent\\Model'); $tenantColumn = 'column'; $tenantId = 1; $model->shouldReceive('getTenantWhereClause')->with($tenantColumn, $tenantId)->andReturn("table.column = '1'"); $where = ['type' => 'raw', 'sql' => "table.column = '1'"]; $this->assertTrue($scope->isTenantConstraint($model, $where, $tenantColumn, $tenantId)); $where = ['type' => 'raw', 'sql' => "table.column = '2'"]; $this->assertFalse($scope->isTenantConstraint($model, $where, $tenantColumn, $tenantId)); }
/** * Sets the tenant id automatically when creating models * * @param Model|ScopedByTenant $model */ public function creating(Model $model) { // If the model has had the global scope removed, bail if (!$model->hasGlobalScope(new TenantScope())) { return; } // If there is no tenant set, bail if (is_null(TenantScope::getTenantId())) { return; } // Otherwise, scope the new model $model->{$model->getTenantColumn()} = TenantScope::getTenantId(); }
/** * Prepare a raw where clause. Do it this way instead of using where() * to avoid issues with bindings when removing. * * @return string */ public function getTenantWhereClause() { $tenantColumn = $this->getQualifiedTenantColumn(); $tenantId = TenantScope::getTenantId(); return "{$tenantColumn} = '{$tenantId}'"; }