mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 09:54:05 +00:00
Use constructor parameter instead of config
This commit is contained in:
parent
80e9a3cc60
commit
04e59449a0
2 changed files with 19 additions and 19 deletions
|
|
@ -95,10 +95,6 @@ return [
|
||||||
'storage_to_config_map' => [ // Used by the TenantConfig feature
|
'storage_to_config_map' => [ // Used by the TenantConfig feature
|
||||||
// 'paypal_api_key' => 'services.paypal.api_key',
|
// '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',
|
'home_url' => '/app',
|
||||||
'create_database' => true,
|
'create_database' => true,
|
||||||
'queue_database_creation' => false,
|
'queue_database_creation' => false,
|
||||||
|
|
|
||||||
|
|
@ -7,14 +7,23 @@ namespace Stancl\Tenancy\Middleware;
|
||||||
use Closure;
|
use Closure;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
|
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
|
||||||
|
use Stancl\Tenancy\TenantManager;
|
||||||
|
|
||||||
class InitializeTenancyByRequestData
|
class InitializeTenancyByRequestData
|
||||||
{
|
{
|
||||||
|
/** @var string|null */
|
||||||
|
protected $header;
|
||||||
|
|
||||||
|
/** @var string|null */
|
||||||
|
protected $queryParameter;
|
||||||
|
|
||||||
/** @var callable */
|
/** @var callable */
|
||||||
protected $onFail;
|
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) {
|
$this->onFail = $onFail ?? function ($e) {
|
||||||
throw $e;
|
throw $e;
|
||||||
};
|
};
|
||||||
|
|
@ -27,11 +36,11 @@ class InitializeTenancyByRequestData
|
||||||
* @param \Closure $next
|
* @param \Closure $next
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function handle($request, Closure $next)
|
public function handle($request, Closure $next, TenantManager $tenantManager)
|
||||||
{
|
{
|
||||||
if ($request->method() !== 'OPTIONS') {
|
if ($request->method() !== 'OPTIONS') {
|
||||||
try {
|
try {
|
||||||
$this->parseTenant($request);
|
$this->initializeTenancy($request, $tenantManager);
|
||||||
} catch (TenantCouldNotBeIdentifiedException $e) {
|
} catch (TenantCouldNotBeIdentifiedException $e) {
|
||||||
($this->onFail)($e);
|
($this->onFail)($e);
|
||||||
}
|
}
|
||||||
|
|
@ -40,28 +49,23 @@ class InitializeTenancyByRequestData
|
||||||
return $next($request);
|
return $next($request);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function parseTenant(Request $request)
|
protected function initializeTenancy(Request $request, TenantManager $tenancy)
|
||||||
{
|
{
|
||||||
if (tenancy()->initialized) {
|
if ($tenancy->initialized) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$header = config('tenancy.identification.header');
|
|
||||||
$query = config('tenancy.identification.query_parameter');
|
|
||||||
|
|
||||||
$tenant = null;
|
$tenant = null;
|
||||||
if ($request->hasHeader($header)) {
|
if ($this->header && $request->hasHeader($this->header)) {
|
||||||
$tenant = $request->header($header);
|
$tenant = $request->header($this->header);
|
||||||
} elseif ($request->has($query)) {
|
} elseif ($this->queryParameter && $request->has($this->queryParameter)) {
|
||||||
$tenant = $request->get($query);
|
$tenant = $request->get($this->queryParameter);
|
||||||
} elseif (! in_array($request->getHost(), config('tenancy.exempt_domains', []), true)) {
|
|
||||||
$tenant = explode('.', $request->getHost())[0];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! $tenant) {
|
if (! $tenant) {
|
||||||
throw new TenantCouldNotBeIdentifiedException($request->getHost());
|
throw new TenantCouldNotBeIdentifiedException($request->getHost());
|
||||||
}
|
}
|
||||||
|
|
||||||
tenancy()->initialize(tenancy()->find($tenant));
|
$tenancy->initialize($tenancy->find($tenant));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue