From 0a57f9d3dff154f31372d0d80166d1275ab4906d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Fri, 23 Aug 2019 22:24:31 +0200 Subject: [PATCH] [1.8.0] Allow conflicting routes (#114) * PreventAccessFromTenantDomains middleware * Apply fixes from StyleCI * Install command * Switch order of middleware * Apply middleware to $middleware instead of web, fix tests * Apply fixes from StyleCI * Fix tests * Fix tests * wip * wip --- src/Commands/Install.php | 15 ++++++++--- .../PreventAccessFromTenantDomains.php | 27 +++++++++++++++++++ tests/CommandsTest.php | 2 ++ 3 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 src/Middleware/PreventAccessFromTenantDomains.php diff --git a/src/Commands/Install.php b/src/Commands/Install.php index c536495f..3c10cedb 100644 --- a/src/Commands/Install.php +++ b/src/Commands/Install.php @@ -36,11 +36,18 @@ class Install extends Command ]); $this->info('✔️ Created config/tenancy.php'); - \file_put_contents(app_path('Http/Kernel.php'), \str_replace( + $newKernel = \str_replace( 'protected $middlewarePriority = [', - "protected \$middlewarePriority = [\n \Stancl\Tenancy\Middleware\InitializeTenancy::class,", + "protected \$middlewarePriority = [ + \Stancl\Tenancy\Middleware\PreventAccessFromTenantDomains::class, + \Stancl\Tenancy\Middleware\InitializeTenancy::class,", \file_get_contents(app_path('Http/Kernel.php')) - )); + ); + + $newKernel = \str_replace("'web' => [", "'web' => [ + \Stancl\Tenancy\Middleware\PreventAccessFromTenantDomains::class,", $newKernel); + + \file_put_contents(app_path('Http/Kernel.php'), $newKernel); $this->info('✔️ Set middleware priority'); \file_put_contents( @@ -58,7 +65,7 @@ class Install extends Command | */ -Route::get('/your/application/homepage', function () { +Route::get('/', function () { return 'This is your multi-tenant application. The uuid of the current tenant is ' . tenant('uuid'); }); " diff --git a/src/Middleware/PreventAccessFromTenantDomains.php b/src/Middleware/PreventAccessFromTenantDomains.php new file mode 100644 index 00000000..567695ad --- /dev/null +++ b/src/Middleware/PreventAccessFromTenantDomains.php @@ -0,0 +1,27 @@ +getHost(), config('tenancy.exempt_domains')) && + ! \in_array('tenancy', request()->route()->middleware())) { + abort(404); + } + + return $next($request); + } +} diff --git a/tests/CommandsTest.php b/tests/CommandsTest.php index 34f98462..7b3fc454 100644 --- a/tests/CommandsTest.php +++ b/tests/CommandsTest.php @@ -246,6 +246,7 @@ class Kernel extends HttpKernel */ protected \$middlewareGroups = [ 'web' => [ + \Stancl\Tenancy\Middleware\PreventAccessFromTenantDomains::class, \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, @@ -288,6 +289,7 @@ class Kernel extends HttpKernel * @var array */ protected \$middlewarePriority = [ + \Stancl\Tenancy\Middleware\PreventAccessFromTenantDomains::class, \Stancl\Tenancy\Middleware\InitializeTenancy::class, \Illuminate\Session\Middleware\StartSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class,