1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-04 14:44:05 +00:00

defaultParameterNames

This commit is contained in:
Samuel Štancl 2025-02-20 14:18:41 +01:00
parent 9755bcdc10
commit 6804735aae
4 changed files with 39 additions and 44 deletions

View file

@ -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.
*/

View file

@ -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);

View file

@ -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

View file

@ -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