From 13422fb0901c0982b921ea7ec6e0381b284bdea7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sun, 15 Mar 2020 15:45:21 +0100 Subject: [PATCH 1/3] Use phpredis 4.3.0 (#319) --- Dockerfile | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) 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 \ From f6115d590a5f749883416dcb2afe4efc45404119 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Tue, 17 Mar 2020 00:02:57 +0100 Subject: [PATCH 2/3] [2.3.0] More identification middleware (#323) * Added request data identification middleware (#207) * Added request data identification middleware * Fixed styling * Changed to Illuminate request instead of helper * Enabled header and querystring customisation Co-authored-by: Jesper Jacobsen * Apply fixes from StyleCI * Use constructor parameter instead of config * Add tests * Apply fixes from StyleCI Co-authored-by: JapSeyz Co-authored-by: Jesper Jacobsen --- .../InitializeTenancyByRequestData.php | 70 +++++++++++++++++++ tests/RequestDataIdentificationTest.php | 62 ++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 src/Middleware/InitializeTenancyByRequestData.php create mode 100644 tests/RequestDataIdentificationTest.php 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/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); + } +} From 776af4643cbd181ea757e46c6c2737a12d85601c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Tue, 17 Mar 2020 15:30:24 +0100 Subject: [PATCH 3/3] Make null falsey for cache config because config is not merged recursively --- assets/config.php | 2 +- src/StorageDrivers/Database/DatabaseStorageDriver.php | 3 +-- tests/CachedResolverTest.php | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/assets/config.php b/assets/config.php index 96cf05db..bbd2d7bb 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/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 */