From 9eab5216b9a0a28496b447ab42d7f50e8d9f82e7 Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Mon, 17 Oct 2022 13:56:02 +0500 Subject: [PATCH] Add cookie option on Initialize Tenancy by Request identification --- .../InitializeTenancyByRequestData.php | 15 ++++++++++----- tests/RequestDataIdentificationTest.php | 18 +++++++++++++----- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/Middleware/InitializeTenancyByRequestData.php b/src/Middleware/InitializeTenancyByRequestData.php index ba587d9a..645b145b 100644 --- a/src/Middleware/InitializeTenancyByRequestData.php +++ b/src/Middleware/InitializeTenancyByRequestData.php @@ -33,13 +33,18 @@ class InitializeTenancyByRequestData extends IdentificationMiddleware protected function getPayload(Request $request): ?string { - $tenant = null; if (static::$header && $request->hasHeader(static::$header)) { - $tenant = $request->header(static::$header); - } elseif (static::$queryParameter && $request->has(static::$queryParameter)) { - $tenant = $request->get(static::$queryParameter); + return $request->header(static::$header); } - return $tenant; + if (static::$queryParameter && $request->has(static::$queryParameter)) { + return $request->get(static::$queryParameter); + } + + if (static::$header && $request->hasCookie(static::$header)) { + return $request->cookie(static::$header); + } + + return null; } } diff --git a/tests/RequestDataIdentificationTest.php b/tests/RequestDataIdentificationTest.php index e5a05f65..75ba46cb 100644 --- a/tests/RequestDataIdentificationTest.php +++ b/tests/RequestDataIdentificationTest.php @@ -26,13 +26,11 @@ afterEach(function () { test('header identification works', function () { InitializeTenancyByRequestData::$header = 'X-Tenant'; $tenant = Tenant::create(); - $tenant2 = Tenant::create(); $this ->withoutExceptionHandling() - ->get('test', [ - 'X-Tenant' => $tenant->id, - ]) + ->withHeader('X-Tenant', $tenant->id) + ->get('test') ->assertSee($tenant->id); }); @@ -40,10 +38,20 @@ test('query parameter identification works', function () { InitializeTenancyByRequestData::$queryParameter = 'tenant'; $tenant = Tenant::create(); - $tenant2 = Tenant::create(); $this ->withoutExceptionHandling() ->get('test?tenant=' . $tenant->id) ->assertSee($tenant->id); }); + +test('cookie identification works', function () { + InitializeTenancyByRequestData::$header = 'X-Tenant'; + $tenant = Tenant::create(); + + $this + ->withoutExceptionHandling() + ->withUnencryptedCookie('X-Tenant', $tenant->id) + ->get('test',) + ->assertSee($tenant->id); +});