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

[2.2.0] Universal routes (#210)

* Universal routes

* Fix bugs

* Add universal MW group
This commit is contained in:
Samuel Štancl 2019-10-30 15:50:00 +01:00 committed by GitHub
parent 518ac4947c
commit ab04ef025b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 5 deletions

View file

@ -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;
} }
} }

View file

@ -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) {