1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 13:54:03 +00:00

Add cookie option on Initialize Tenancy by Request identification (#980)

* Add cookie option on Initialize Tenancy by Request identification

* add cookie property
This commit is contained in:
Abrar Ahmad 2022-10-18 16:52:16 +05:00 committed by GitHub
parent 693e00b9be
commit 05f1b2d6f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 10 deletions

View file

@ -12,6 +12,7 @@ use Stancl\Tenancy\Tenancy;
class InitializeTenancyByRequestData extends IdentificationMiddleware class InitializeTenancyByRequestData extends IdentificationMiddleware
{ {
public static string $header = 'X-Tenant'; public static string $header = 'X-Tenant';
public static string $cookie = 'X-Tenant';
public static string $queryParameter = 'tenant'; public static string $queryParameter = 'tenant';
public static ?Closure $onFail = null; public static ?Closure $onFail = null;
@ -33,13 +34,18 @@ class InitializeTenancyByRequestData extends IdentificationMiddleware
protected function getPayload(Request $request): ?string protected function getPayload(Request $request): ?string
{ {
$tenant = null;
if (static::$header && $request->hasHeader(static::$header)) { if (static::$header && $request->hasHeader(static::$header)) {
$tenant = $request->header(static::$header); return $request->header(static::$header);
} elseif (static::$queryParameter && $request->has(static::$queryParameter)) {
$tenant = $request->get(static::$queryParameter);
} }
return $tenant; if (static::$queryParameter && $request->has(static::$queryParameter)) {
return $request->get(static::$queryParameter);
}
if (static::$cookie && $request->hasCookie(static::$cookie)) {
return $request->cookie(static::$cookie);
}
return null;
} }
} }

View file

@ -20,19 +20,18 @@ beforeEach(function () {
afterEach(function () { afterEach(function () {
InitializeTenancyByRequestData::$header = 'X-Tenant'; InitializeTenancyByRequestData::$header = 'X-Tenant';
InitializeTenancyByRequestData::$cookie = 'X-Tenant';
InitializeTenancyByRequestData::$queryParameter = 'tenant'; InitializeTenancyByRequestData::$queryParameter = 'tenant';
}); });
test('header identification works', function () { test('header identification works', function () {
InitializeTenancyByRequestData::$header = 'X-Tenant'; InitializeTenancyByRequestData::$header = 'X-Tenant';
$tenant = Tenant::create(); $tenant = Tenant::create();
$tenant2 = Tenant::create();
$this $this
->withoutExceptionHandling() ->withoutExceptionHandling()
->get('test', [ ->withHeader('X-Tenant', $tenant->id)
'X-Tenant' => $tenant->id, ->get('test')
])
->assertSee($tenant->id); ->assertSee($tenant->id);
}); });
@ -40,10 +39,20 @@ test('query parameter identification works', function () {
InitializeTenancyByRequestData::$queryParameter = 'tenant'; InitializeTenancyByRequestData::$queryParameter = 'tenant';
$tenant = Tenant::create(); $tenant = Tenant::create();
$tenant2 = Tenant::create();
$this $this
->withoutExceptionHandling() ->withoutExceptionHandling()
->get('test?tenant=' . $tenant->id) ->get('test?tenant=' . $tenant->id)
->assertSee($tenant->id); ->assertSee($tenant->id);
}); });
test('cookie identification works', function () {
InitializeTenancyByRequestData::$cookie = 'X-Tenant';
$tenant = Tenant::create();
$this
->withoutExceptionHandling()
->withUnencryptedCookie('X-Tenant', $tenant->id)
->get('test',)
->assertSee($tenant->id);
});