mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 17:34:03 +00:00
Improve PreventAccessFromTenantDomains - look into middleware subgroups
This commit is contained in:
parent
dbed57fcf8
commit
c81dd5582d
1 changed files with 21 additions and 1 deletions
|
|
@ -5,6 +5,8 @@ declare(strict_types=1);
|
||||||
namespace Stancl\Tenancy\Middleware;
|
namespace Stancl\Tenancy\Middleware;
|
||||||
|
|
||||||
use Closure;
|
use Closure;
|
||||||
|
use Illuminate\Routing\Route;
|
||||||
|
use Illuminate\Support\Facades\Route as Router;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prevent access to non-tenant routes from domains that are not exempt from tenancy.
|
* Prevent access to non-tenant routes from domains that are not exempt from tenancy.
|
||||||
|
|
@ -26,7 +28,7 @@ class PreventAccessFromTenantDomains
|
||||||
$isExemptDomain = in_array($request->getHost(), config('tenancy.exempt_domains'));
|
$isExemptDomain = in_array($request->getHost(), config('tenancy.exempt_domains'));
|
||||||
$isTenantDomain = ! $isExemptDomain;
|
$isTenantDomain = ! $isExemptDomain;
|
||||||
|
|
||||||
$isTenantRoute = in_array('tenancy', $request->route()->middleware());
|
$isTenantRoute = $this->isTenantRoute($request->route());
|
||||||
|
|
||||||
if ($isTenantDomain && ! $isTenantRoute) { // accessing web routes from tenant domains
|
if ($isTenantDomain && ! $isTenantRoute) { // accessing web routes from tenant domains
|
||||||
return redirect(config('tenancy.home_url'));
|
return redirect(config('tenancy.home_url'));
|
||||||
|
|
@ -38,4 +40,22 @@ class PreventAccessFromTenantDomains
|
||||||
|
|
||||||
return $next($request);
|
return $next($request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function isTenantRoute(Route $route): bool
|
||||||
|
{
|
||||||
|
if (in_array('tenancy', $route->middleware(), true)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Loop one level deep and check if the route's middleware
|
||||||
|
// groups have a `tenancy` middleware grouú inside them
|
||||||
|
$middlewareGroups = Router::getMiddlewareGroups();
|
||||||
|
foreach ($route->middleware() as $middleware) {
|
||||||
|
if (isset($middlewareGroups[$middleware]) && in_array('tenancy', $middlewareGroups[$middleware], true)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue