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

Identify tenants by the "Origin" header (#21)

* Add origin ID MW

* Test origin ID MW

* Test origin ID MW with early identification

* Fix code style (php-cs-fixer)

* Fix PHPStan errors

* Add getDomain() to domain ID MW, simplify origin ID MW

* Fix code style (php-cs-fixer)

* Rename InitializeTenancyByOrigin to InitializeTenancyByOriginHeader

* Add onFail test

* Stop throwing the exception in getDomain()

* FIx merge

* Improve origin identification test file

* Clean up test

---------

Co-authored-by: PHP CS Fixer <phpcsfixer@example.com>
This commit is contained in:
lukinovec 2024-01-08 00:29:01 +01:00 committed by GitHub
parent df9324b92f
commit 9e4f33e5c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 124 additions and 4 deletions

View file

@ -31,10 +31,12 @@ class InitializeTenancyByDomain extends IdentificationMiddleware implements Usab
return $next($request);
}
$domain = $this->getDomain($request);
return $this->initializeTenancy(
$request,
$next,
$request->getHost()
$domain
);
}
@ -44,6 +46,11 @@ class InitializeTenancyByDomain extends IdentificationMiddleware implements Usab
*/
public function requestHasTenant(Request $request): bool
{
return ! in_array($request->host(), config('tenancy.central_domains'));
return ! in_array($this->getDomain($request), config('tenancy.central_domains'));
}
public function getDomain(Request $request): string
{
return $request->getHost();
}
}

View file

@ -22,7 +22,7 @@ class InitializeTenancyByDomainOrSubdomain extends InitializeTenancyBySubdomain
return $next($request);
}
$domain = $request->getHost();
$domain = $this->getDomain($request);
if ($this->isSubdomain($domain)) {
$domain = $this->makeSubdomain($domain);

View file

@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Middleware;
use Illuminate\Http\Request;
class InitializeTenancyByOriginHeader extends InitializeTenancyByDomainOrSubdomain
{
public function getDomain(Request $request): string
{
return $request->header('Origin', '');
}
}

View file

@ -35,7 +35,7 @@ class InitializeTenancyBySubdomain extends InitializeTenancyByDomain
return $next($request);
}
$subdomain = $this->makeSubdomain($request->getHost());
$subdomain = $this->makeSubdomain($this->getDomain($request));
if (is_object($subdomain) && $subdomain instanceof Exception) {
$onFail = static::$onFail ?? function ($e) {