1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-13 07:54:03 +00:00
tenancy/src/Middleware/InitializeTenancy.php
Samuel Štancl cbd3850a8f
[2.1.0] Initialize tenancy before executing controller constructors (#169)
* Update message about migrations in Install

* wip

* Apply fixes from StyleCI

* string instead of array

* Fix globalUrl binding

* Simplify if condition in TenantRouteServiceProvider

* Apply fixes from StyleCI

* Improve PreventAccessFromTenantDomains - look into middleware subgroups

* Fix typo

* gatherMiddleware() instead of middleware()

* Fix tests

* Apply fixes from StyleCI

* Update install command

* Apply fixes from StyleCI

* Add the PreventAccess MW to tenant routes by default
2019-10-15 20:23:56 +02:00

45 lines
1,012 B
PHP

<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Middleware;
use Closure;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
class InitializeTenancy
{
/** @var callable */
protected $onFail;
public function __construct(callable $onFail = null)
{
$this->onFail = $onFail ?? function ($e) {
throw $e;
};
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (tenancy()->initialized) {
return $next($request);
}
if (! in_array($request->getHost(), config('tenancy.exempt_domains', []), true)) {
try {
tenancy()->init($request->getHost());
} catch (TenantCouldNotBeIdentifiedException $e) {
($this->onFail)($e);
}
}
return $next($request);
}
}