From 6804735aaefc53ad95692d1863793d2740020b6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Thu, 20 Feb 2025 14:18:41 +0100 Subject: [PATCH] defaultParameterNames --- assets/config.php | 1 + .../Integrations/FortifyRouteBootstrapper.php | 60 +++++++------------ src/Overrides/TenancyUrlGenerator.php | 20 ++++++- src/Resolvers/PathTenantResolver.php | 2 +- 4 files changed, 39 insertions(+), 44 deletions(-) diff --git a/assets/config.php b/assets/config.php index 8241e973..e423e434 100644 --- a/assets/config.php +++ b/assets/config.php @@ -402,6 +402,7 @@ return [ /** * Make all routes central, tenant, or universal by default. + * todo@earlyIdReview todo0 * * To override the default route mode, apply the middleware of another route mode ('central', 'tenant', 'universal') to the route. */ diff --git a/src/Bootstrappers/Integrations/FortifyRouteBootstrapper.php b/src/Bootstrappers/Integrations/FortifyRouteBootstrapper.php index 7da654f5..81c001cd 100644 --- a/src/Bootstrappers/Integrations/FortifyRouteBootstrapper.php +++ b/src/Bootstrappers/Integrations/FortifyRouteBootstrapper.php @@ -24,41 +24,20 @@ use Stancl\Tenancy\Resolvers\PathTenantResolver; class FortifyRouteBootstrapper implements TenancyBootstrapper { /** - * Make Fortify actions redirect to custom routes. + * Fortify redirects that should be used in tenant context. * - * For each route redirect, specify the intended route context (central or tenant). - * Based on the provided context, we pass the tenant parameter to the route (or not). - * The tenant parameter is only passed to the route when you specify its context as tenant. - * - * The route redirects should be in the following format: - * - * 'fortify_action' => [ - * 'route_name' => 'tenant.route', - * 'context' => Context::TENANT, - * ] - * - * For example: - * - * FortifyRouteBootstrapper::$fortifyRedirectMap = [ - * // On logout, redirect the user to the "bye" route in the central app - * 'logout' => [ - * 'route_name' => 'bye', - * 'context' => Context::CENTRAL, - * ], - * - * // On login, redirect the user to the "welcome" route in the tenant app - * 'login' => [ - * 'route_name' => 'welcome', - * 'context' => Context::TENANT, - * ], - * ]; + * Syntax: ['redirect_name' => 'tenant_route_name'] */ public static array $fortifyRedirectMap = []; /** * Should the tenant parameter be passed to fortify routes in the tenant context. * - * This should be enabled with path/query string identification and disabled with domain identification + * This should be enabled with path/query string identification and disabled with domain identification. + * + * You may also disable this when using path/query string identification if passing the tenant parameter + * is handled in another way (TenancyUrlGenerator::$passTenantParameter for both, + * UrlGeneratorBootstrapper:$addTenantParameterToDefaults for path identification). */ public static bool $passTenantParameter = true; @@ -66,7 +45,15 @@ class FortifyRouteBootstrapper implements TenancyBootstrapper * Tenant route that serves as Fortify's home (e.g. a tenant dashboard route). * This route will always receive the tenant parameter. */ - public static string $fortifyHome = 'tenant.dashboard'; + public static string|null $fortifyHome = 'tenant.dashboard'; + + /** + * Use default parameter names ('tenant' name and tenant key value) instead of the parameter name + * and column name configured in the path resolver config. + * + * You want to enable this when using query string identification while having customized that config. + */ + public static bool $defaultParameterNames = false; protected array $originalFortifyConfig = []; @@ -88,17 +75,11 @@ class FortifyRouteBootstrapper implements TenancyBootstrapper protected function useTenantRoutesInFortify(Tenant $tenant): void { - // todo0 this should be just using 'tenant' and the tenant key with query identification - if we can't detect that easily, just add a static property for query id (default false) - $tenantParameterName = PathTenantResolver::tenantParameterName(); - $tenantParameterValue = PathTenantResolver::tenantParameterValue($tenant); + $tenantParameterName = static::$defaultParameterNames ? 'tenant' : PathTenantResolver::tenantParameterName(); + $tenantParameterValue = static::$defaultParameterNames ? $tenant->getTenantKey() : PathTenantResolver::tenantParameterValue($tenant); $generateLink = function (array $redirect) use ($tenantParameterValue, $tenantParameterName) { - // Specifying the context is only required with query string identification - // because with path identification, the tenant parameter should always present - $passTenantParameter = static::$passTenantParameter && $redirect['context'] === Context::TENANT; - - // Only pass the tenant parameter when the user should be redirected to a tenant route - return route($redirect['route_name'], $passTenantParameter ? [$tenantParameterName => $tenantParameterValue] : []); + return route($redirect['route_name'], static::$passTenantParameter ? [$tenantParameterName => $tenantParameterValue] : []); }; // Get redirect URLs for the configured redirect routes @@ -109,8 +90,7 @@ class FortifyRouteBootstrapper implements TenancyBootstrapper if (static::$fortifyHome) { // Generate the home route URL with the tenant parameter and make it the Fortify home route - // todo0 this should ALSO be only when static::$passTenantParameter, otherwise [], but shouldn't we also check the context here? - $this->config->set('fortify.home', route(static::$fortifyHome, [$tenantParameterName => $tenantParameterValue])); + $this->config->set('fortify.home', route(static::$fortifyHome, static::$passTenantParameter ? [$tenantParameterName => $tenantParameterValue] : [])); } $this->config->set('fortify.redirects', $redirects); diff --git a/src/Overrides/TenancyUrlGenerator.php b/src/Overrides/TenancyUrlGenerator.php index 435dc9d3..531a711a 100644 --- a/src/Overrides/TenancyUrlGenerator.php +++ b/src/Overrides/TenancyUrlGenerator.php @@ -85,6 +85,14 @@ class TenancyUrlGenerator extends UrlGenerator */ public static array $overrides = []; + /** + * Use default parameter names ('tenant' name and tenant key value) instead of the parameter name + * and column name configured in the path resolver config. + * + * You want to enable this when using query string identification while having customized that config. + */ + public static bool $defaultParameterNames = false; + /** * Override the route() method so that the route name gets prefixed * and the tenant parameter gets added when in tenant context. @@ -152,7 +160,6 @@ class TenancyUrlGenerator extends UrlGenerator */ protected function prefixRouteName(string $name): string { - // todo0 review $tenantPrefix = PathTenantResolver::tenantRouteNamePrefix(); if (static::$prefixRouteNames && ! str($name)->startsWith($tenantPrefix)) { @@ -167,8 +174,15 @@ class TenancyUrlGenerator extends UrlGenerator */ protected function addTenantParameter(array $parameters): array { - // todo0 fix - should use tenantParameterValue(), but with query identification this should just be 'tenant', not even tenantParameterName() - return tenant() && static::$passTenantParameterToRoutes ? array_merge($parameters, [PathTenantResolver::tenantParameterName() => tenant()->getTenantKey()]) : $parameters; + if (tenant() && static::$passTenantParameterToRoutes) { + if (static::$defaultParameterNames) { + return array_merge($parameters, ['tenant' => $tenant->getTenantKey()]); + } else { + return array_merge($parameters, [PathTenantResolver::tenantParameterName() => PathTenantResolver::tenantParameterValue($tenant)]); + } + } else { + return $parameters; + } } protected function routeNameOverride(string $name): string|null diff --git a/src/Resolvers/PathTenantResolver.php b/src/Resolvers/PathTenantResolver.php index ac879f45..f8d28786 100644 --- a/src/Resolvers/PathTenantResolver.php +++ b/src/Resolvers/PathTenantResolver.php @@ -73,7 +73,7 @@ class PathTenantResolver extends Contracts\CachedTenantResolver public static function tenantRouteNamePrefix(): string { - return config('tenancy.identification.resolvers.' . static::class . '.tenant_route_name_prefix') ?? static::tenantParameterName() . '.'; + return (config('tenancy.identification.resolvers.' . static::class . '.tenant_route_name_prefix') ?? 'tenant') . '.'; } public static function tenantModelColumn(): string