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

wip fix hardcoded values making assumptions about the parameters used in routing

This commit is contained in:
Samuel Štancl 2025-02-20 13:11:03 +01:00
parent b278d1f61a
commit 9755bcdc10
5 changed files with 19 additions and 5 deletions

View file

@ -88,16 +88,17 @@ class FortifyRouteBootstrapper implements TenancyBootstrapper
protected function useTenantRoutesInFortify(Tenant $tenant): void protected function useTenantRoutesInFortify(Tenant $tenant): void
{ {
$tenantKey = $tenant->getTenantKey(); // 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(); $tenantParameterName = PathTenantResolver::tenantParameterName();
$tenantParameterValue = PathTenantResolver::tenantParameterValue($tenant);
$generateLink = function (array $redirect) use ($tenantKey, $tenantParameterName) { $generateLink = function (array $redirect) use ($tenantParameterValue, $tenantParameterName) {
// Specifying the context is only required with query string identification // Specifying the context is only required with query string identification
// because with path identification, the tenant parameter should always present // because with path identification, the tenant parameter should always present
$passTenantParameter = static::$passTenantParameter && $redirect['context'] === Context::TENANT; $passTenantParameter = static::$passTenantParameter && $redirect['context'] === Context::TENANT;
// Only pass the tenant parameter when the user should be redirected to a tenant route // Only pass the tenant parameter when the user should be redirected to a tenant route
return route($redirect['route_name'], $passTenantParameter ? [$tenantParameterName => $tenantKey] : []); return route($redirect['route_name'], $passTenantParameter ? [$tenantParameterName => $tenantParameterValue] : []);
}; };
// Get redirect URLs for the configured redirect routes // Get redirect URLs for the configured redirect routes
@ -108,7 +109,8 @@ class FortifyRouteBootstrapper implements TenancyBootstrapper
if (static::$fortifyHome) { if (static::$fortifyHome) {
// Generate the home route URL with the tenant parameter and make it the Fortify home route // Generate the home route URL with the tenant parameter and make it the Fortify home route
$this->config->set('fortify.home', route(static::$fortifyHome, [$tenantParameterName => $tenantKey])); // 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.redirects', $redirects); $this->config->set('fortify.redirects', $redirects);

View file

@ -69,7 +69,10 @@ class UrlGeneratorBootstrapper implements TenancyBootstrapper
if (static::$addTenantParameterToDefaults) { if (static::$addTenantParameterToDefaults) {
$defaultParameters = array_merge( $defaultParameters = array_merge(
$defaultParameters, $defaultParameters,
[PathTenantResolver::tenantParameterName() => $tenant->getTenantKey()] [
PathTenantResolver::tenantParameterName() => PathTenantResolver::tenantParameterValue($tenant), // path identification
'tenant' => $tenant->getTenantKey(), // query string identification
],
); );
} }

View file

@ -8,6 +8,8 @@ use Illuminate\Routing\Events\RouteMatched;
use Stancl\Tenancy\Enums\RouteMode; use Stancl\Tenancy\Enums\RouteMode;
use Stancl\Tenancy\Resolvers\PathTenantResolver; use Stancl\Tenancy\Resolvers\PathTenantResolver;
// todo@earlyIdReview
/** /**
* Remove the tenant parameter from the matched route when path identification is used globally. * Remove the tenant parameter from the matched route when path identification is used globally.
* *

View file

@ -152,6 +152,7 @@ class TenancyUrlGenerator extends UrlGenerator
*/ */
protected function prefixRouteName(string $name): string protected function prefixRouteName(string $name): string
{ {
// todo0 review
$tenantPrefix = PathTenantResolver::tenantRouteNamePrefix(); $tenantPrefix = PathTenantResolver::tenantRouteNamePrefix();
if (static::$prefixRouteNames && ! str($name)->startsWith($tenantPrefix)) { if (static::$prefixRouteNames && ! str($name)->startsWith($tenantPrefix)) {
@ -166,6 +167,7 @@ class TenancyUrlGenerator extends UrlGenerator
*/ */
protected function addTenantParameter(array $parameters): array 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; return tenant() && static::$passTenantParameterToRoutes ? array_merge($parameters, [PathTenantResolver::tenantParameterName() => tenant()->getTenantKey()]) : $parameters;
} }

View file

@ -81,6 +81,11 @@ class PathTenantResolver extends Contracts\CachedTenantResolver
return config('tenancy.identification.resolvers.' . static::class . '.tenant_model_column') ?? tenancy()->model()->getTenantKeyName(); return config('tenancy.identification.resolvers.' . static::class . '.tenant_model_column') ?? tenancy()->model()->getTenantKeyName();
} }
public static function tenantParameterValue(Tenant $tenant): string
{
return $tenant->getAttribute(static::tenantModelColumn());
}
/** @return string[] */ /** @return string[] */
public static function allowedExtraModelColumns(): array public static function allowedExtraModelColumns(): array
{ {