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.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); + } +}