diff --git a/Dockerfile b/Dockerfile index a6512668..285a645f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,14 +16,16 @@ RUN apt-get update \ && php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer \ && mkdir /run/php -RUN apt-get install -y php7.2-redis - RUN apt-get install -y python3 RUN apt-get install -y php7.2-dev php-pear + +RUN pecl install redis-4.3.0 +RUN echo "extension=redis.so" > /etc/php/7.2/mods-available/redis.ini +RUN ln -sf /etc/php/7.2/mods-available/redis.ini /etc/php/7.2/fpm/conf.d/20-redis.ini +RUN ln -sf /etc/php/7.2/mods-available/redis.ini /etc/php/7.2/cli/conf.d/20-redis.ini + RUN pecl install xdebug -# RUN echo '' > /etc/php/7.2/cli/conf.d/20-xdebug.ini -# RUN echo 'zend_extension=/usr/lib/php/20170718/xdebug.so' >> /etc/php/7.2/cli/php.ini RUN echo 'zend_extension=/usr/lib/php/20170718/xdebug.so' > /etc/php/7.2/cli/conf.d/20-xdebug.ini RUN apt-get -y autoremove \ diff --git a/assets/config.php b/assets/config.php index fad8ac41..8f0a3668 100644 --- a/assets/config.php +++ b/assets/config.php @@ -16,7 +16,7 @@ return [ 'tenants' => 'tenants', 'domains' => 'domains', ], - 'cache_store' => false, // What store should be used to cache tenant resolution. Set to false to disable cache, null to use default store, or a string with a specific cache store name. + 'cache_store' => null, // What store should be used to cache tenant resolution. Set to null to disable cache or a string with a specific cache store name. 'cache_ttl' => 3600, // seconds ], 'redis' => [ diff --git a/src/Middleware/InitializeTenancyByRequestData.php b/src/Middleware/InitializeTenancyByRequestData.php new file mode 100644 index 00000000..7753d034 --- /dev/null +++ b/src/Middleware/InitializeTenancyByRequestData.php @@ -0,0 +1,70 @@ +header = $header; + $this->queryParameter = $queryParameter; + $this->onFail = $onFail ?? function ($e) { + throw $e; + }; + } + + /** + * Handle an incoming request. + * + * @param \Illuminate\Http\Request $request + * @param \Closure $next + * @return mixed + */ + public function handle($request, Closure $next) + { + if ($request->method() !== 'OPTIONS') { + try { + $this->initializeTenancy($request); + } catch (TenantCouldNotBeIdentifiedException $e) { + ($this->onFail)($e); + } + } + + return $next($request); + } + + protected function initializeTenancy(Request $request) + { + if (tenancy()->initialized) { + return; + } + + $tenant = null; + if ($this->header && $request->hasHeader($this->header)) { + $tenant = $request->header($this->header); + } elseif ($this->queryParameter && $request->has($this->queryParameter)) { + $tenant = $request->get($this->queryParameter); + } + + if (! $tenant) { + throw new TenantCouldNotBeIdentifiedException($request->getHost()); + } + + tenancy()->initialize(tenancy()->find($tenant)); + } +} diff --git a/src/StorageDrivers/Database/DatabaseStorageDriver.php b/src/StorageDrivers/Database/DatabaseStorageDriver.php index 42ce00b4..1303ba38 100644 --- a/src/StorageDrivers/Database/DatabaseStorageDriver.php +++ b/src/StorageDrivers/Database/DatabaseStorageDriver.php @@ -251,7 +251,6 @@ class DatabaseStorageDriver implements StorageDriver, CanDeleteKeys, CanFindByAn public function usesCache(): bool { - // null is also truthy here - return $this->app['config']['tenancy.storage_drivers.db.cache_store'] !== false; + return $this->app['config']['tenancy.storage_drivers.db.cache_store'] !== null; } } diff --git a/tests/CachedResolverTest.php b/tests/CachedResolverTest.php index 903539fd..0d32915e 100644 --- a/tests/CachedResolverTest.php +++ b/tests/CachedResolverTest.php @@ -21,7 +21,7 @@ class CachedResolverTest extends TestCase $this->markTestSkipped('This test is only relevant for the DB storage driver.'); } - config(['tenancy.storage_drivers.db.cache_store' => null]); // default driver + config(['tenancy.storage_drivers.db.cache_store' => config('cache.default')]); } /** @test */ diff --git a/tests/RequestDataIdentificationTest.php b/tests/RequestDataIdentificationTest.php new file mode 100644 index 00000000..fcbd0997 --- /dev/null +++ b/tests/RequestDataIdentificationTest.php @@ -0,0 +1,62 @@ + [ + 'localhost', + ], + ]); + + Route::middleware(InitializeTenancyByRequestData::class)->get('/test', function () { + return 'Tenant id: ' . tenant('id'); + }); + } + + /** @test */ + public function header_identification_works() + { + $this->app->bind(InitializeTenancyByRequestData::class, function () { + return new InitializeTenancyByRequestData('X-Tenant'); + }); + $tenant = Tenant::new()->save(); + $tenant2 = Tenant::new()->save(); + + $this + ->withoutExceptionHandling() + ->get('test', [ + 'X-Tenant' => $tenant->id, + ]) + ->assertSee($tenant->id); + } + + /** @test */ + public function query_parameter_identification_works() + { + $this->app->bind(InitializeTenancyByRequestData::class, function () { + return new InitializeTenancyByRequestData(null, 'tenant'); + }); + $tenant = Tenant::new()->save(); + $tenant2 = Tenant::new()->save(); + + $this + ->withoutExceptionHandling() + ->get('test?tenant=' . $tenant->id) + ->assertSee($tenant->id); + } +}