mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 11:14:04 +00:00
Universal routes
This commit is contained in:
parent
7fcfeb187c
commit
ed200ab733
3 changed files with 113 additions and 0 deletions
|
|
@ -162,6 +162,7 @@ return [
|
|||
'features' => [
|
||||
// Stancl\Tenancy\Features\UserImpersonation::class,
|
||||
// Stancl\Tenancy\Features\TelescopeTags::class,
|
||||
// Stancl\Tenancy\Features\UniversalRoutes::class,
|
||||
// Stancl\Tenancy\Features\TenantConfig::class, // https://tenancy.samuelstancl.me/docs/v2/features/tenant-config/
|
||||
// Stancl\Tenancy\Features\CrossDomainRedirect::class, // https://tenancy.samuelstancl.me/docs/v2/features/tenant-redirect/
|
||||
],
|
||||
|
|
|
|||
48
src/Features/UniversalRoutes.php
Normal file
48
src/Features/UniversalRoutes.php
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Features;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Routing\Route;
|
||||
use Illuminate\Support\Facades\Route as Router;
|
||||
use Stancl\Tenancy\Contracts\Feature;
|
||||
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
|
||||
use Stancl\Tenancy\Tenancy;
|
||||
|
||||
class UniversalRoutes implements Feature
|
||||
{
|
||||
public static $identificationMiddlewares = [
|
||||
InitializeTenancyByDomain::class,
|
||||
];
|
||||
|
||||
public function bootstrap(Tenancy $tenancy): void
|
||||
{
|
||||
foreach (static::$identificationMiddlewares as $middleware) {
|
||||
$middleware::$onFail = function ($exception, $request, $next) {
|
||||
if (static::routeHasMiddleware($request->route(), 'universal')) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
throw $exception;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public static function routeHasMiddleware(Route $route, $middleware): bool
|
||||
{
|
||||
if (in_array($middleware, $route->middleware(), true)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Loop one level deep and check if the route's middleware
|
||||
// groups have the searhced middleware group inside them
|
||||
$middlewareGroups = Router::getMiddlewareGroups();
|
||||
foreach ($route->gatherMiddleware() as $inner) {
|
||||
if (!$inner instanceof Closure && isset($middlewareGroups[$inner]) && in_array($middleware, $middlewareGroups[$inner], true)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
64
tests/UniversalRouteTest.php
Normal file
64
tests/UniversalRouteTest.php
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Tests;
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Stancl\Tenancy\Features\UniversalRoutes;
|
||||
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
|
||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||
|
||||
class UniversalRouteTest extends TestCase
|
||||
{
|
||||
public function tearDown(): void
|
||||
{
|
||||
InitializeTenancyByDomain::$onFail = null;
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function a_route_can_work_in_both_central_and_tenant_context()
|
||||
{
|
||||
Route::middlewareGroup('universal', []);
|
||||
config(['tenancy.features' => [UniversalRoutes::class]]);
|
||||
|
||||
Route::get('/foo', function () {
|
||||
return tenancy()->initialized
|
||||
? 'Tenancy is initialized.'
|
||||
: 'Tenancy is not initialized.';
|
||||
})->middleware(['universal', InitializeTenancyByDomain::class]);
|
||||
|
||||
$this->get('http://localhost/foo')
|
||||
->assertSuccessful()
|
||||
->assertSee('Tenancy is not initialized.');
|
||||
|
||||
$tenant = Tenant::create([
|
||||
'id' => 'acme',
|
||||
]);
|
||||
$tenant->domains()->create([
|
||||
'domain' => 'acme.localhost',
|
||||
]);
|
||||
|
||||
$this->get('http://acme.localhost/foo')
|
||||
->assertSuccessful()
|
||||
->assertSee('Tenancy is initialized.');
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function making_one_route_universal_doesnt_make_all_routes_universal()
|
||||
{
|
||||
Route::get('/bar', function () {
|
||||
return tenant('id');
|
||||
})->middleware(InitializeTenancyByDomain::class);
|
||||
|
||||
$this->a_route_can_work_in_both_central_and_tenant_context();
|
||||
tenancy()->end();
|
||||
|
||||
$this->get('http://localhost/bar')
|
||||
->assertStatus(500);
|
||||
|
||||
$this->get('http://acme.localhost/bar')
|
||||
->assertSuccessful()
|
||||
->assertSee('acme');
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue