mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 12:44:02 +00:00
Merge branch '2.x' of github.com:stancl/tenancy into 2.x
This commit is contained in:
commit
9c4f678321
2 changed files with 12 additions and 5 deletions
|
|
@ -22,12 +22,17 @@ class PreventAccessFromTenantDomains
|
||||||
*/
|
*/
|
||||||
public function handle($request, Closure $next)
|
public function handle($request, Closure $next)
|
||||||
{
|
{
|
||||||
|
// If the route is universal, always let the request pass.
|
||||||
|
if ($this->routeHasMiddleware($request->route(), 'universal')) {
|
||||||
|
return $next($request);
|
||||||
|
}
|
||||||
|
|
||||||
// If the domain is not in exempt domains, it's a tenant domain.
|
// If the domain is not in exempt domains, it's a tenant domain.
|
||||||
// Tenant domains can't have routes without tenancy middleware.
|
// Tenant domains can't have routes without tenancy middleware.
|
||||||
$isExemptDomain = in_array($request->getHost(), config('tenancy.exempt_domains'));
|
$isExemptDomain = in_array($request->getHost(), config('tenancy.exempt_domains'));
|
||||||
$isTenantDomain = ! $isExemptDomain;
|
$isTenantDomain = ! $isExemptDomain;
|
||||||
|
|
||||||
$isTenantRoute = $this->isTenantRoute($request->route());
|
$isTenantRoute = $this->routeHasMiddleware($request->route(), 'tenancy');
|
||||||
|
|
||||||
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'));
|
||||||
|
|
@ -40,17 +45,17 @@ class PreventAccessFromTenantDomains
|
||||||
return $next($request);
|
return $next($request);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isTenantRoute(Route $route): bool
|
public function routeHasMiddleware(Route $route, $middleware): bool
|
||||||
{
|
{
|
||||||
if (in_array('tenancy', $route->middleware(), true)) {
|
if (in_array($middleware, $route->middleware(), true)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Loop one level deep and check if the route's middleware
|
// Loop one level deep and check if the route's middleware
|
||||||
// groups have a `tenancy` middleware group inside them
|
// groups have a `tenancy` middleware group inside them
|
||||||
$middlewareGroups = Router::getMiddlewareGroups();
|
$middlewareGroups = Router::getMiddlewareGroups();
|
||||||
foreach ($route->gatherMiddleware() as $middleware) {
|
foreach ($route->gatherMiddleware() as $inner) {
|
||||||
if (isset($middlewareGroups[$middleware]) && in_array('tenancy', $middlewareGroups[$middleware], true)) {
|
if (isset($middlewareGroups[$inner]) && in_array($middleware, $middlewareGroups[$inner], true)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -92,6 +92,8 @@ class TenancyServiceProvider extends ServiceProvider
|
||||||
Middleware\PreventAccessFromTenantDomains::class,
|
Middleware\PreventAccessFromTenantDomains::class,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
Route::middlewareGroup('universal', []);
|
||||||
|
|
||||||
$this->loadRoutesFrom(__DIR__ . '/routes.php');
|
$this->loadRoutesFrom(__DIR__ . '/routes.php');
|
||||||
|
|
||||||
$this->app->singleton('globalUrl', function ($app) {
|
$this->app->singleton('globalUrl', function ($app) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue