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

Merge branch '2.x' of github.com:stancl/tenancy into 2.x

This commit is contained in:
Samuel Štancl 2019-10-30 18:11:56 +01:00
commit 9c4f678321
2 changed files with 12 additions and 5 deletions

View file

@ -22,12 +22,17 @@ class PreventAccessFromTenantDomains
*/
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.
// Tenant domains can't have routes without tenancy middleware.
$isExemptDomain = in_array($request->getHost(), config('tenancy.exempt_domains'));
$isTenantDomain = ! $isExemptDomain;
$isTenantRoute = $this->isTenantRoute($request->route());
$isTenantRoute = $this->routeHasMiddleware($request->route(), 'tenancy');
if ($isTenantDomain && ! $isTenantRoute) { // accessing web routes from tenant domains
return redirect(config('tenancy.home_url'));
@ -40,17 +45,17 @@ class PreventAccessFromTenantDomains
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;
}
// Loop one level deep and check if the route's middleware
// groups have a `tenancy` middleware group inside them
$middlewareGroups = Router::getMiddlewareGroups();
foreach ($route->gatherMiddleware() as $middleware) {
if (isset($middlewareGroups[$middleware]) && in_array('tenancy', $middlewareGroups[$middleware], true)) {
foreach ($route->gatherMiddleware() as $inner) {
if (isset($middlewareGroups[$inner]) && in_array($middleware, $middlewareGroups[$inner], true)) {
return true;
}
}

View file

@ -92,6 +92,8 @@ class TenancyServiceProvider extends ServiceProvider
Middleware\PreventAccessFromTenantDomains::class,
]);
Route::middlewareGroup('universal', []);
$this->loadRoutesFrom(__DIR__ . '/routes.php');
$this->app->singleton('globalUrl', function ($app) {