From 04e59449a0a82ee0763d67a32336b7e929d53559 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Mon, 16 Mar 2020 22:55:25 +0100 Subject: [PATCH] Use constructor parameter instead of config --- assets/config.php | 4 --- .../InitializeTenancyByRequestData.php | 34 +++++++++++-------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/assets/config.php b/assets/config.php index bb9873fc..96cf05db 100644 --- a/assets/config.php +++ b/assets/config.php @@ -95,10 +95,6 @@ return [ 'storage_to_config_map' => [ // Used by the TenantConfig feature // 'paypal_api_key' => 'services.paypal.api_key', ], - 'identification' => [ - 'header' => 'X-Tenant', // Can be anything, but should really start with "X-", - 'query_parameter' => '_tenant', - ], 'home_url' => '/app', 'create_database' => true, 'queue_database_creation' => false, diff --git a/src/Middleware/InitializeTenancyByRequestData.php b/src/Middleware/InitializeTenancyByRequestData.php index e92cc76d..95a76c0b 100644 --- a/src/Middleware/InitializeTenancyByRequestData.php +++ b/src/Middleware/InitializeTenancyByRequestData.php @@ -7,14 +7,23 @@ namespace Stancl\Tenancy\Middleware; use Closure; use Illuminate\Http\Request; use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException; +use Stancl\Tenancy\TenantManager; class InitializeTenancyByRequestData { + /** @var string|null */ + protected $header; + + /** @var string|null */ + protected $queryParameter; + /** @var callable */ protected $onFail; - public function __construct(callable $onFail = null) + public function __construct(?string $header = 'X-Tenant', ?string $queryParameter = 'tenant', callable $onFail = null) { + $this->header = $header; + $this->queryParameter = $queryParameter; $this->onFail = $onFail ?? function ($e) { throw $e; }; @@ -27,11 +36,11 @@ class InitializeTenancyByRequestData * @param \Closure $next * @return mixed */ - public function handle($request, Closure $next) + public function handle($request, Closure $next, TenantManager $tenantManager) { if ($request->method() !== 'OPTIONS') { try { - $this->parseTenant($request); + $this->initializeTenancy($request, $tenantManager); } catch (TenantCouldNotBeIdentifiedException $e) { ($this->onFail)($e); } @@ -40,28 +49,23 @@ class InitializeTenancyByRequestData return $next($request); } - protected function parseTenant(Request $request) + protected function initializeTenancy(Request $request, TenantManager $tenancy) { - if (tenancy()->initialized) { + if ($tenancy->initialized) { return; } - $header = config('tenancy.identification.header'); - $query = config('tenancy.identification.query_parameter'); - $tenant = null; - if ($request->hasHeader($header)) { - $tenant = $request->header($header); - } elseif ($request->has($query)) { - $tenant = $request->get($query); - } elseif (! in_array($request->getHost(), config('tenancy.exempt_domains', []), true)) { - $tenant = explode('.', $request->getHost())[0]; + 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)); + $tenancy->initialize($tenancy->find($tenant)); } }