mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 19:34:04 +00:00
Give universal flag highest priority (#27)
* Give universal flag the highest priority (wip) * Stop forgetting tenant parameter when route-level path ID is used * Fix PHPStan errors * Simplify annotation * Fix comment * Correct annotations * Improve requestHasTenant comment * Make cloning logic only clone universal routes, delete the universal flag from the new (tenant) route * Delete APP_DEBUG * make if condition easier to read * Update DealsWithRouteContexts.php * Fix test * Fix code style (php-cs-fixer) * Move tests * Delete incorrectly committed file * Cloning routes update wip * Route cloning rework WIP * Add todo to clone routes * Fix code style (php-cs-fixer) * Route cloning fix WIP * Set CloneRoutesAsTenant::$tenantMiddleware to ID MW * Revert CloneRoutesAsTenant::$tenantMiddleware-related changes * Simplify requestHasTenant * Add and test 'ckone' flag * Delete setting $skippedRoutes from CloneRoutesAsTenant * Fix code style (php-cs-fixer) * make config key used for testing distinct from normal tenancy config keys * Update src/Actions/CloneRoutesAsTenant.php * Move 'path identification types' dataset to CloneActionTest --------- Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com> Co-authored-by: Samuel Štancl <samuel@archte.ch> Co-authored-by: PHP CS Fixer <phpcsfixer@example.com>
This commit is contained in:
parent
070828a81e
commit
c312156c18
10 changed files with 315 additions and 296 deletions
|
|
@ -118,10 +118,10 @@ class CloneRoutesAsTenant
|
|||
$pathIdentificationUsed = (! $routeHasNonPathIdentificationMiddleware) &&
|
||||
($routeHasPathIdentificationMiddleware || $pathIdentificationMiddlewareInGlobalStack);
|
||||
|
||||
$routeMode = tenancy()->getRouteMode($route);
|
||||
$routeIsUniversalOrTenant = $routeMode === RouteMode::TENANT || $routeMode === RouteMode::UNIVERSAL;
|
||||
|
||||
if ($pathIdentificationUsed && $routeIsUniversalOrTenant) {
|
||||
if (
|
||||
$pathIdentificationUsed &&
|
||||
(tenancy()->getRouteMode($route) === RouteMode::UNIVERSAL || tenancy()->routeHasMiddleware($route, 'clone'))
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -167,7 +167,11 @@ class CloneRoutesAsTenant
|
|||
// Add original route middleware to ensure there's no duplicate middleware
|
||||
unset($newRoute->action['middleware']);
|
||||
|
||||
$newRoute->middleware(tenancy()->getRouteMiddleware($route));
|
||||
// Exclude `universal` and `clone` middleware from the new route -- it should specifically be a tenant route
|
||||
$newRoute->middleware(array_filter(
|
||||
tenancy()->getRouteMiddleware($route),
|
||||
fn (string $middleware) => ! in_array($middleware, ['universal', 'clone'])
|
||||
));
|
||||
|
||||
if ($routeName && ! $route->named($tenantRouteNamePrefix . '*')) {
|
||||
// Clear the route name so that `name()` sets the route name instead of suffixing it
|
||||
|
|
|
|||
|
|
@ -20,46 +20,43 @@ trait DealsWithRouteContexts
|
|||
* Get route's middleware context (tenant, central or universal).
|
||||
* The context is determined by the route's middleware.
|
||||
*
|
||||
* If the route has the 'universal' middleware, the context is universal,
|
||||
* and the route is accessible from both contexts.
|
||||
* The universal flag has the highest priority.
|
||||
*
|
||||
* If the route has the 'central' middleware, the context is central.
|
||||
* If the route has the 'tenant' middleware, or any tenancy identification middleware (and the route isn't flagged as universal), the context is tenant.
|
||||
* If the route has the 'tenant' middleware, or any tenancy identification middleware, the context is tenant.
|
||||
*
|
||||
* If the route doesn't have any of the mentioned middleware,
|
||||
* the context is determined by the `tenancy.default_route_mode` config.
|
||||
*/
|
||||
public static function getRouteMode(Route $route): RouteMode
|
||||
{
|
||||
// If the route is universal, you have to determine its actual context using
|
||||
// the identification middleware's determineUniversalRouteContextFromRequest
|
||||
if (static::routeIsUniversal($route)) {
|
||||
return RouteMode::UNIVERSAL;
|
||||
}
|
||||
|
||||
if (static::routeHasMiddleware($route, 'central')) {
|
||||
return RouteMode::CENTRAL;
|
||||
}
|
||||
|
||||
$routeIsUniversal = static::routeIsUniversal($route);
|
||||
|
||||
// If the route is flagged as tenant, consider it tenant
|
||||
// If the route has an identification middleware and the route is not universal, consider it tenant
|
||||
if (
|
||||
static::routeHasMiddleware($route, 'tenant') ||
|
||||
(static::routeHasIdentificationMiddleware($route) && ! $routeIsUniversal)
|
||||
) {
|
||||
// If the route is flagged as tenant or it has identification middleware, consider it tenant
|
||||
if (static::routeHasMiddleware($route, 'tenant') || static::routeHasIdentificationMiddleware($route)) {
|
||||
return RouteMode::TENANT;
|
||||
}
|
||||
|
||||
// If the route is universal, you have to determine its actual context using
|
||||
// The identification middleware's determineUniversalRouteContextFromRequest
|
||||
if ($routeIsUniversal) {
|
||||
return RouteMode::UNIVERSAL;
|
||||
}
|
||||
|
||||
return config('tenancy.default_route_mode');
|
||||
}
|
||||
|
||||
public static function routeIsUniversal(Route $route): bool
|
||||
{
|
||||
$routeFlaggedAsTenantOrCentral = static::routeHasMiddleware($route, 'tenant') || static::routeHasMiddleware($route, 'central');
|
||||
$routeFlaggedAsUniversal = static::routeHasMiddleware($route, 'universal');
|
||||
$universalFlagUsedInGlobalStack = app(Kernel::class)->hasMiddleware('universal');
|
||||
$defaultRouteModeIsUniversal = config('tenancy.default_route_mode') === RouteMode::UNIVERSAL;
|
||||
|
||||
return ! $routeFlaggedAsTenantOrCentral && ($routeFlaggedAsUniversal || $universalFlagUsedInGlobalStack || $defaultRouteModeIsUniversal);
|
||||
return $routeFlaggedAsUniversal || $universalFlagUsedInGlobalStack || $defaultRouteModeIsUniversal;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -231,7 +231,7 @@ class DatabaseConfig
|
|||
$databaseManagers = config('tenancy.database.managers');
|
||||
|
||||
if (! array_key_exists($driver, $databaseManagers)) {
|
||||
throw new Exceptions\DatabaseManagerNotRegisteredException($driver);
|
||||
throw new DatabaseManagerNotRegisteredException($driver);
|
||||
}
|
||||
|
||||
return app($databaseManagers[$driver]);
|
||||
|
|
|
|||
|
|
@ -27,9 +27,10 @@ class ForgetTenantParameter
|
|||
public function handle(RouteMatched $event): void
|
||||
{
|
||||
$kernelPathIdentificationUsed = PathIdentificationManager::pathIdentificationInGlobalStack() && ! tenancy()->routeHasIdentificationMiddleware($event->route);
|
||||
$routeModeIsTenant = tenancy()->getRouteMode($event->route) === RouteMode::TENANT;
|
||||
$routeMode = tenancy()->getRouteMode($event->route);
|
||||
$routeModeIsTenantOrUniversal = $routeMode === RouteMode::TENANT || ($routeMode === RouteMode::UNIVERSAL && $event->route->hasParameter(PathIdentificationManager::getTenantParameterName()));
|
||||
|
||||
if ($kernelPathIdentificationUsed && $routeModeIsTenant) {
|
||||
if ($kernelPathIdentificationUsed && $routeModeIsTenantOrUniversal) {
|
||||
$event->route->forgetParameter(PathIdentificationManager::getTenantParameterName());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,8 +8,8 @@ use Closure;
|
|||
use Illuminate\Http\Request;
|
||||
use Stancl\Tenancy\Concerns\UsableWithEarlyIdentification;
|
||||
use Stancl\Tenancy\Concerns\UsableWithUniversalRoutes;
|
||||
use Stancl\Tenancy\Enums\RouteMode;
|
||||
use Stancl\Tenancy\Exceptions\RouteIsMissingTenantParameterException;
|
||||
use Stancl\Tenancy\PathIdentificationManager;
|
||||
use Stancl\Tenancy\Resolvers\PathTenantResolver;
|
||||
use Stancl\Tenancy\Tenancy;
|
||||
|
||||
|
|
@ -52,17 +52,10 @@ class InitializeTenancyByPath extends IdentificationMiddleware implements Usable
|
|||
}
|
||||
|
||||
/**
|
||||
* Path identification request has a tenant if the middleware context is tenant.
|
||||
*
|
||||
* With path identification, we can just check the MW context because we're cloning the universal routes,
|
||||
* and the routes are flagged with the 'tenant' MW group (= their MW context is tenant).
|
||||
*
|
||||
* With other identification middleware, we have to determine the context differently because we only have one
|
||||
* truly universal route available ('truly universal' because with path identification, applying 'universal' to a route just means that
|
||||
* it should get cloned, whereas with other ID MW, it means that the route you apply the 'universal' flag to will be accessible in both contexts).
|
||||
* Request has tenant if the request's route has the tenant parameter.
|
||||
*/
|
||||
public function requestHasTenant(Request $request): bool
|
||||
{
|
||||
return tenancy()->getRouteMode(tenancy()->getRoute($request)) === RouteMode::TENANT;
|
||||
return tenancy()->getRoute($request)->hasParameter(PathIdentificationManager::getTenantParameterName());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -138,6 +138,7 @@ class TenancyServiceProvider extends ServiceProvider
|
|||
return $instance;
|
||||
});
|
||||
|
||||
Route::middlewareGroup('clone', []);
|
||||
Route::middlewareGroup('universal', []);
|
||||
Route::middlewareGroup('tenant', []);
|
||||
Route::middlewareGroup('central', []);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue