mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 19:14:03 +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
|
||||
// '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,
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue