1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 19:14:04 +00:00
tenancy/src/Middleware/InitializeTenancyByDomainOrSubdomain.php
lukinovec 9e4f33e5c5
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>
2024-01-08 00:29:01 +01:00

55 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Middleware;
use Closure;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Str;
use Stancl\Tenancy\Concerns\UsableWithEarlyIdentification;
class InitializeTenancyByDomainOrSubdomain extends InitializeTenancyBySubdomain
{
use UsableWithEarlyIdentification;
/** @return Response|mixed */
public function handle(Request $request, Closure $next): mixed
{
if ($this->shouldBeSkipped(tenancy()->getRoute($request))) {
return $next($request);
}
$domain = $this->getDomain($request);
if ($this->isSubdomain($domain)) {
$domain = $this->makeSubdomain($domain);
if (is_object($domain) && $domain instanceof Exception) {
$onFail = static::$onFail ?? function ($e) {
throw $e;
};
return $onFail($domain, $request, $next);
}
// If a Response instance was returned, we return it immediately.
if (is_object($domain) && $domain instanceof Response) {
return $domain;
}
}
return $this->initializeTenancy(
$request,
$next,
$domain
);
}
protected function isSubdomain(string $hostname): bool
{
return Str::endsWith($hostname, config('tenancy.central_domains'));
}
}