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

[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
This commit is contained in:
Samuel Štancl 2019-08-23 22:24:31 +02:00 committed by GitHub
parent 2fd3662eb7
commit 0a57f9d3df
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 4 deletions

View file

@ -36,11 +36,18 @@ class Install extends Command
]); ]);
$this->info('✔️ Created config/tenancy.php'); $this->info('✔️ Created config/tenancy.php');
\file_put_contents(app_path('Http/Kernel.php'), \str_replace( $newKernel = \str_replace(
'protected $middlewarePriority = [', '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')) \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'); $this->info('✔️ Set middleware priority');
\file_put_contents( \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'); return 'This is your multi-tenant application. The uuid of the current tenant is ' . tenant('uuid');
}); });
" "

View file

@ -0,0 +1,27 @@
<?php
namespace Stancl\Tenancy\Middleware;
use Closure;
class PreventAccessFromTenantDomains
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
// If the domain is not in exempt domains, it's a tenant domain.
// Tenant domains can't have routes without tenancy middleware.
if (! \in_array(request()->getHost(), config('tenancy.exempt_domains')) &&
! \in_array('tenancy', request()->route()->middleware())) {
abort(404);
}
return $next($request);
}
}

View file

@ -246,6 +246,7 @@ class Kernel extends HttpKernel
*/ */
protected \$middlewareGroups = [ protected \$middlewareGroups = [
'web' => [ 'web' => [
\Stancl\Tenancy\Middleware\PreventAccessFromTenantDomains::class,
\App\Http\Middleware\EncryptCookies::class, \App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class, \Illuminate\Session\Middleware\StartSession::class,
@ -288,6 +289,7 @@ class Kernel extends HttpKernel
* @var array * @var array
*/ */
protected \$middlewarePriority = [ protected \$middlewarePriority = [
\Stancl\Tenancy\Middleware\PreventAccessFromTenantDomains::class,
\Stancl\Tenancy\Middleware\InitializeTenancy::class, \Stancl\Tenancy\Middleware\InitializeTenancy::class,
\Illuminate\Session\Middleware\StartSession::class, \Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class,