1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 14:14:04 +00:00

[4.x] URL generation, request data identification improvements (#1357)

* UrlGenerator: set defaults based on config; request data: move config to config file+resolver

* Claude code adjustments

* improve request data tests, simplify complex test in UrlGeneratorBootstrapperTest

* url generator test: test changing tenant parameter name

* request data identification: add tenant_model_column configuration

* defaultParameterNames -> passQueryParameter

* move comment

* minor refactor in PathIdentificationTest, expand CLAUDE.md to include early identification section

* Fix COLOR_FLAG

* improve test name

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* TenancyUrlGenerator: add a check for queryParameterName being null

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Fix code style (php-cs-fixer)

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
This commit is contained in:
Samuel Štancl 2025-06-02 03:43:47 +02:00 committed by GitHub
parent f4cc99b317
commit 5f7fd38e5a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 440 additions and 126 deletions

View file

@ -6,7 +6,9 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
### Testing ### Testing
- `composer test` - Run tests without coverage using Docker - `composer test` - Run tests without coverage using Docker
- `./test tests/TestFile.php` - Run an entire test file
- `./t 'test name'` - Run a specific test - `./t 'test name'` - Run a specific test
- You can append `-v` to get a full stack trace if a test fails due to an exception
### Code Quality ### Code Quality
- `composer phpstan` - Run PHPStan static analysis (level 8) - `composer phpstan` - Run PHPStan static analysis (level 8)
@ -75,6 +77,36 @@ All of these work as flags, i.e. middleware groups that are empty arrays with a
- `universal` - Routes working in both contexts - `universal` - Routes working in both contexts
- `clone` - Tells route cloning logic to clone the route - `clone` - Tells route cloning logic to clone the route
### Early Identification
**Early identification** ensures tenancy is initialized before controller instantiation, which is critical for certain scenarios.
**When needed:**
- Controllers using constructor dependency injection
- Integration with packages that inject dependencies in constructors
**The Problem:**
Laravel executes controller constructors and route model binding before route-level middleware runs, causing services to use central context instead of tenant context.
**Solutions:**
1. **Avoid Constructor Injection** - Use method injection instead
2. **Laravel's Native Solution** - Use controllers that implement `HasMiddleware` interface
3. **Kernel Identification** - Add middleware to HTTP Kernel's global stack:
```php
// In HttpKernel.php
protected $middleware = [
\Stancl\Tenancy\Middleware\InitializeTenancyByDomain::class,
// other middleware...
];
```
Note you also need to flag the route with the `'tenant'` middleware if default route mode (set in config) isn't set to TENANT.
**Benefits:**
- Constructor dependency injection receives tenant-aware services
- Seamless integration with existing Laravel applications
### Testing Environment ### Testing Environment
Tests use Docker with MySQL/PostgreSQL/Redis. The `./test` script runs Pest tests inside containers with proper database isolation. Tests use Docker with MySQL/PostgreSQL/Redis. The `./test` script runs Pest tests inside containers with proper database isolation.

View file

@ -119,7 +119,7 @@ return [
Resolvers\PathTenantResolver::class => [ Resolvers\PathTenantResolver::class => [
'tenant_parameter_name' => 'tenant', 'tenant_parameter_name' => 'tenant',
'tenant_model_column' => null, // null = tenant key 'tenant_model_column' => null, // null = tenant key
'tenant_route_name_prefix' => null, // null = 'tenant.' 'tenant_route_name_prefix' => 'tenant.',
'allowed_extra_model_columns' => [], // used with binding route fields 'allowed_extra_model_columns' => [], // used with binding route fields
'cache' => false, 'cache' => false,
@ -127,6 +127,13 @@ return [
'cache_store' => null, // null = default 'cache_store' => null, // null = default
], ],
Resolvers\RequestDataTenantResolver::class => [ Resolvers\RequestDataTenantResolver::class => [
// Set any of these to null to disable that method of identification
'header' => 'X-Tenant',
'cookie' => 'tenant',
'query_parameter' => 'tenant',
'tenant_model_column' => null, // null = tenant key
'cache' => false, 'cache' => false,
'cache_ttl' => 3600, // seconds 'cache_ttl' => 3600, // seconds
'cache_store' => null, // null = default 'cache_store' => null, // null = default

View file

@ -8,6 +8,7 @@ use Illuminate\Config\Repository;
use Stancl\Tenancy\Contracts\TenancyBootstrapper; use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant; use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Resolvers\PathTenantResolver; use Stancl\Tenancy\Resolvers\PathTenantResolver;
use Stancl\Tenancy\Resolvers\RequestDataTenantResolver;
/** /**
* Allows customizing Fortify action redirects so that they can also redirect * Allows customizing Fortify action redirects so that they can also redirect
@ -38,7 +39,7 @@ class FortifyRouteBootstrapper implements TenancyBootstrapper
* is handled in another way (TenancyUrlGenerator::$passTenantParameter for both, * is handled in another way (TenancyUrlGenerator::$passTenantParameter for both,
* UrlGeneratorBootstrapper:$addTenantParameterToDefaults for path identification). * UrlGeneratorBootstrapper:$addTenantParameterToDefaults for path identification).
*/ */
public static bool $passTenantParameter = true; public static bool $passTenantParameter = false;
/** /**
* Tenant route that serves as Fortify's home (e.g. a tenant dashboard route). * Tenant route that serves as Fortify's home (e.g. a tenant dashboard route).
@ -47,12 +48,22 @@ class FortifyRouteBootstrapper implements TenancyBootstrapper
public static string|null $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 * Follow the query_parameter config instead of the tenant_parameter_name (path identification) config.
* and column name configured in the path resolver config.
* *
* You want to enable this when using query string identification while having customized that config. * This only has an effect when:
* - $passTenantParameter is enabled, and
* - the tenant_parameter_name config for the path resolver differs from the query_parameter config for the request data resolver.
*
* In such a case, instead of adding ['tenant' => '...'] to the route parameters (or whatever your tenant_parameter_name is if not 'tenant'),
* the query_parameter will be passed instead, e.g. ['team' => '...'] if your query_parameter config is 'team'.
*
* This is enabled by default because typically you will not need $passTenantParameter with path identification.
* UrlGeneratorBootstrapper::$addTenantParameterToDefaults is recommended instead when using path identification.
*
* On the other hand, when using request data identification (specifically query string) you WILL need to
* pass the parameter therefore you would use $passTenantParameter.
*/ */
public static bool $defaultParameterNames = false; public static bool $passQueryParameter = true;
protected array $originalFortifyConfig = []; protected array $originalFortifyConfig = [];
@ -74,8 +85,14 @@ class FortifyRouteBootstrapper implements TenancyBootstrapper
protected function useTenantRoutesInFortify(Tenant $tenant): void protected function useTenantRoutesInFortify(Tenant $tenant): void
{ {
$tenantParameterName = static::$defaultParameterNames ? 'tenant' : PathTenantResolver::tenantParameterName(); if (static::$passQueryParameter) {
$tenantParameterValue = static::$defaultParameterNames ? $tenant->getTenantKey() : PathTenantResolver::tenantParameterValue($tenant); // todo@tests
$tenantParameterName = RequestDataTenantResolver::queryParameterName();
$tenantParameterValue = RequestDataTenantResolver::payloadValue($tenant);
} else {
$tenantParameterName = PathTenantResolver::tenantParameterName();
$tenantParameterValue = PathTenantResolver::tenantParameterValue($tenant);
}
$generateLink = function (string $redirect) use ($tenantParameterValue, $tenantParameterName) { $generateLink = function (string $redirect) use ($tenantParameterValue, $tenantParameterName) {
return route($redirect, static::$passTenantParameter ? [$tenantParameterName => $tenantParameterValue] : []); return route($redirect, static::$passTenantParameter ? [$tenantParameterName => $tenantParameterValue] : []);
@ -89,7 +106,7 @@ 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, static::$passTenantParameter ? [$tenantParameterName => $tenantParameterValue] : [])); $this->config->set('fortify.home', $generateLink(static::$fortifyHome));
} }
$this->config->set('fortify.redirects', $redirects); $this->config->set('fortify.redirects', $redirects);

View file

@ -67,13 +67,15 @@ class UrlGeneratorBootstrapper implements TenancyBootstrapper
$defaultParameters = $this->originalUrlGenerator->getDefaultParameters(); $defaultParameters = $this->originalUrlGenerator->getDefaultParameters();
if (static::$addTenantParameterToDefaults) { if (static::$addTenantParameterToDefaults) {
$defaultParameters = array_merge( $tenantParameterName = PathTenantResolver::tenantParameterName();
$defaultParameters,
[ $defaultParameters = array_merge($defaultParameters, [
PathTenantResolver::tenantParameterName() => PathTenantResolver::tenantParameterValue($tenant), // path identification $tenantParameterName => PathTenantResolver::tenantParameterValue($tenant),
'tenant' => $tenant->getTenantKey(), // query string identification ]);
],
); foreach (PathTenantResolver::allowedExtraModelColumns() as $column) {
$defaultParameters["$tenantParameterName:$column"] = $tenant->getAttribute($column);
}
} }
$newGenerator->defaults($defaultParameters); $newGenerator->defaults($defaultParameters);

View file

@ -18,9 +18,6 @@ class InitializeTenancyByRequestData extends IdentificationMiddleware
{ {
use UsableWithEarlyIdentification; use UsableWithEarlyIdentification;
public static string $header = 'X-Tenant';
public static string $cookie = 'tenant';
public static string $queryParameter = 'tenant';
public static ?Closure $onFail = null; public static ?Closure $onFail = null;
public static bool $requireCookieEncryption = false; public static bool $requireCookieEncryption = false;
@ -54,18 +51,19 @@ class InitializeTenancyByRequestData extends IdentificationMiddleware
protected function getPayload(Request $request): string|null protected function getPayload(Request $request): string|null
{ {
if (static::$header && $request->hasHeader(static::$header)) { $headerName = RequestDataTenantResolver::headerName();
$payload = $request->header(static::$header); $queryParameterName = RequestDataTenantResolver::queryParameterName();
} elseif ( $cookieName = RequestDataTenantResolver::cookieName();
static::$queryParameter &&
$request->has(static::$queryParameter) if ($headerName && $request->hasHeader($headerName)) {
) { $payload = $request->header($headerName);
$payload = $request->get(static::$queryParameter); } elseif ($queryParameterName && $request->has($queryParameterName)) {
} elseif (static::$cookie && $request->hasCookie(static::$cookie)) { $payload = $request->get($queryParameterName);
$payload = $request->cookie(static::$cookie); } elseif ($cookieName && $request->hasCookie($cookieName)) {
$payload = $request->cookie($cookieName);
if ($payload && is_string($payload)) { if ($payload && is_string($payload)) {
$payload = $this->getTenantFromCookie($payload); $payload = $this->getTenantFromCookie($cookieName, $payload);
} }
} else { } else {
$payload = null; $payload = null;
@ -86,12 +84,12 @@ class InitializeTenancyByRequestData extends IdentificationMiddleware
return (bool) $this->getPayload($request); return (bool) $this->getPayload($request);
} }
protected function getTenantFromCookie(string $cookie): string|null protected function getTenantFromCookie(string $cookieName, string $cookieValue): string|null
{ {
// If the cookie looks like it's encrypted, we try decrypting it // If the cookie looks like it's encrypted, we try decrypting it
if (str_starts_with($cookie, 'eyJpdiI')) { if (str_starts_with($cookieValue, 'eyJpdiI')) {
try { try {
$json = base64_decode($cookie); $json = base64_decode($cookieValue);
$data = json_decode($json, true); $data = json_decode($json, true);
if ( if (
@ -100,9 +98,9 @@ class InitializeTenancyByRequestData extends IdentificationMiddleware
) { ) {
// We can confidently assert that the cookie is encrypted. If this call were to fail, this method would just // We can confidently assert that the cookie is encrypted. If this call were to fail, this method would just
// return null and the cookie payload would get skipped. // return null and the cookie payload would get skipped.
$cookie = CookieValuePrefix::validate( $cookieValue = CookieValuePrefix::validate(
static::$cookie, $cookieName,
Crypt::decryptString($cookie), Crypt::decryptString($cookieValue),
Crypt::getAllKeys() Crypt::getAllKeys()
); );
} }
@ -113,6 +111,6 @@ class InitializeTenancyByRequestData extends IdentificationMiddleware
return null; return null;
} }
return $cookie; return $cookieValue;
} }
} }

View file

@ -9,6 +9,7 @@ use Illuminate\Routing\UrlGenerator;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use InvalidArgumentException; use InvalidArgumentException;
use Stancl\Tenancy\Resolvers\PathTenantResolver; use Stancl\Tenancy\Resolvers\PathTenantResolver;
use Stancl\Tenancy\Resolvers\RequestDataTenantResolver;
/** /**
* This class is used in place of the default UrlGenerator when UrlGeneratorBootstrapper is enabled. * This class is used in place of the default UrlGenerator when UrlGeneratorBootstrapper is enabled.
@ -86,12 +87,22 @@ class TenancyUrlGenerator extends UrlGenerator
public static array $overrides = []; public static array $overrides = [];
/** /**
* Use default parameter names ('tenant' name and tenant key value) instead of the parameter name * Follow the query_parameter config instead of the tenant_parameter_name (path identification) config.
* and column name configured in the path resolver config.
* *
* You want to enable this when using query string identification while having customized that config. * This only has an effect when:
* - $passTenantParameterToRoutes is enabled, and
* - the tenant_parameter_name config for the path resolver differs from the query_parameter config for the request data resolver.
*
* In such a case, instead of adding ['tenant' => '...'] to the route parameters (or whatever your tenant_parameter_name is if not 'tenant'),
* the query_parameter will be passed instead, e.g. ['team' => '...'] if your query_parameter config is 'team'.
*
* This is enabled by default because typically you will not need $passTenantParameterToRoutes with path identification.
* UrlGeneratorBootstrapper::$addTenantParameterToDefaults is recommended instead when using path identification.
*
* On the other hand, when using request data identification (specifically query string) you WILL need to pass the parameter
* directly to route() calls, therefore you would use $passTenantParameterToRoutes to avoid having to do that manually.
*/ */
public static bool $defaultParameterNames = false; public static bool $passQueryParameter = true;
/** /**
* Override the route() method so that the route name gets prefixed * Override the route() method so that the route name gets prefixed
@ -175,11 +186,14 @@ class TenancyUrlGenerator extends UrlGenerator
protected function addTenantParameter(array $parameters): array protected function addTenantParameter(array $parameters): array
{ {
if (tenant() && static::$passTenantParameterToRoutes) { if (tenant() && static::$passTenantParameterToRoutes) {
if (static::$defaultParameterNames) { if (static::$passQueryParameter) {
return array_merge($parameters, ['tenant' => tenant()->getTenantKey()]); $queryParameterName = RequestDataTenantResolver::queryParameterName();
} else { if ($queryParameterName !== null) {
return array_merge($parameters, [PathTenantResolver::tenantParameterName() => PathTenantResolver::tenantParameterValue(tenant())]); return array_merge($parameters, [$queryParameterName => RequestDataTenantResolver::payloadValue(tenant())]);
} }
}
return array_merge($parameters, [PathTenantResolver::tenantParameterName() => PathTenantResolver::tenantParameterValue(tenant())]);
} else { } else {
return $parameters; return $parameters;
} }

View file

@ -20,7 +20,9 @@ class RequestDataTenantResolver extends Contracts\CachedTenantResolver
{ {
$payload = (string) $args[0]; $payload = (string) $args[0];
if ($payload && $tenant = tenancy()->find($payload, withRelations: true)) { $column = static::tenantModelColumn();
if ($payload && $tenant = tenancy()->find($payload, $column, withRelations: true)) {
return $tenant; return $tenant;
} }
@ -29,8 +31,43 @@ class RequestDataTenantResolver extends Contracts\CachedTenantResolver
public function getPossibleCacheKeys(Tenant&Model $tenant): array public function getPossibleCacheKeys(Tenant&Model $tenant): array
{ {
// todo@tests
return [ return [
$this->formatCacheKey($tenant->getTenantKey()), $this->formatCacheKey(static::payloadValue($tenant)),
]; ];
} }
public static function payloadValue(Tenant $tenant): string
{
return $tenant->getAttribute(static::tenantModelColumn());
}
public static function tenantModelColumn(): string
{
return config('tenancy.identification.resolvers.' . static::class . '.tenant_model_column') ?? tenancy()->model()->getTenantKeyName();
}
/**
* Returns the name of the header used for identification, or null if header identification is disabled.
*/
public static function headerName(): string|null
{
return config('tenancy.identification.resolvers.' . static::class . '.header');
}
/**
* Returns the name of the query parameter used for identification, or null if query parameter identification is disabled.
*/
public static function queryParameterName(): string|null
{
return config('tenancy.identification.resolvers.' . static::class . '.query_parameter');
}
/**
* Returns the name of the cookie used for identification, or null if cookie identification is disabled.
*/
public static function cookieName(): string|null
{
return config('tenancy.identification.resolvers.' . static::class . '.cookie');
}
} }

8
t
View file

@ -1,3 +1,9 @@
#!/bin/bash #!/bin/bash
docker compose exec -e COLUMNS=$(tput cols) -T test vendor/bin/pest --color=always --no-coverage --filter "$@" if [[ "${CLAUDECODE}" != "1" ]]; then
COLOR_FLAG="--colors=always"
else
COLOR_FLAG="--colors=never"
fi
docker compose exec -e COLUMNS=$(tput cols) -T test vendor/bin/pest ${COLOR_FLAG} --no-coverage --filter "$@"

8
test
View file

@ -1,4 +1,10 @@
#!/bin/bash #!/bin/bash
if [[ "${CLAUDECODE}" != "1" ]]; then
COLOR_FLAG="--colors=always"
else
COLOR_FLAG="--colors=never"
fi
# --columns doesn't seem to work at the moment, so we're setting it using an environment variable # --columns doesn't seem to work at the moment, so we're setting it using an environment variable
docker compose exec -e COLUMNS=$(tput cols) -T test vendor/bin/pest --colors=always "$@" docker compose exec -e COLUMNS=$(tput cols) -T test vendor/bin/pest ${COLOR_FLAG} "$@"

View file

@ -20,7 +20,7 @@ afterEach(function () {
FortifyRouteBootstrapper::$passTenantParameter = true; FortifyRouteBootstrapper::$passTenantParameter = true;
FortifyRouteBootstrapper::$fortifyRedirectMap = []; FortifyRouteBootstrapper::$fortifyRedirectMap = [];
FortifyRouteBootstrapper::$fortifyHome = 'tenant.dashboard'; FortifyRouteBootstrapper::$fortifyHome = 'tenant.dashboard';
FortifyRouteBootstrapper::$defaultParameterNames = false; FortifyRouteBootstrapper::$passQueryParameter = false;
}); });
test('fortify route tenancy bootstrapper updates fortify config correctly', function() { test('fortify route tenancy bootstrapper updates fortify config correctly', function() {

View file

@ -1,5 +1,6 @@
<?php <?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Routing\UrlGenerator; use Illuminate\Routing\UrlGenerator;
use Stancl\Tenancy\Tests\Etc\Tenant; use Stancl\Tenancy\Tests\Etc\Tenant;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
@ -12,8 +13,13 @@ use Stancl\Tenancy\Overrides\TenancyUrlGenerator;
use Stancl\Tenancy\Listeners\RevertToCentralContext; use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Stancl\Tenancy\Middleware\InitializeTenancyByPath; use Stancl\Tenancy\Middleware\InitializeTenancyByPath;
use Illuminate\Routing\Exceptions\UrlGenerationException; use Illuminate\Routing\Exceptions\UrlGenerationException;
use Illuminate\Support\Facades\Schema;
use Stancl\Tenancy\Bootstrappers\UrlGeneratorBootstrapper; use Stancl\Tenancy\Bootstrappers\UrlGeneratorBootstrapper;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedByRequestDataException;
use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData; use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData;
use Stancl\Tenancy\Resolvers\RequestDataTenantResolver;
use function Stancl\Tenancy\Tests\pest;
beforeEach(function () { beforeEach(function () {
Event::listen(TenancyInitialized::class, BootstrapTenancy::class); Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
@ -44,82 +50,224 @@ test('url generator bootstrapper swaps the url generator instance correctly', fu
}); });
test('tenancy url generator can prefix route names passed to the route helper', function() { test('tenancy url generator can prefix route names passed to the route helper', function() {
Route::get('/central/home', fn () => route('home'))->name('home'); config([
// Tenant route name prefix is 'tenant.' by default 'tenancy.identification.resolvers.' . PathTenantResolver::class . '.tenant_route_name_prefix' => 'custom_prefix.',
Route::get('/tenant/home', fn () => route('tenant.home'))->name('tenant.home'); ]);
Route::get('/central/home', fn () => '')->name('home');
Route::get('/tenant/home', fn () => '')->name('custom_prefix.home');
$tenant = Tenant::create(); $tenant = Tenant::create();
$centralRouteUrl = route('home');
$tenantRouteUrl = route('tenant.home');
config(['tenancy.bootstrappers' => [UrlGeneratorBootstrapper::class]]); config(['tenancy.bootstrappers' => [UrlGeneratorBootstrapper::class]]);
tenancy()->initialize($tenant); tenancy()->initialize($tenant);
// Route names don't get prefixed when TenancyUrlGenerator::$prefixRouteNames is false (default) // Route names don't get prefixed when TenancyUrlGenerator::$prefixRouteNames is false (default)
expect(route('home'))->toBe($centralRouteUrl); expect(route('home'))->toBe('http://localhost/central/home');
// When $prefixRouteNames is true, the route name passed to the route() helper ('home') gets prefixed with 'tenant.' automatically. // When $prefixRouteNames is true, the route name passed to the route() helper ('home') gets prefixed automatically.
TenancyUrlGenerator::$prefixRouteNames = true; TenancyUrlGenerator::$prefixRouteNames = true;
expect(route('home'))->toBe($tenantRouteUrl); expect(route('home'))->toBe('http://localhost/tenant/home');
// The 'tenant.home' route name doesn't get prefixed -- it is already prefixed with 'tenant.' // The 'custom_prefix.home' route name doesn't get prefixed -- it is already prefixed with 'custom_prefix.'
expect(route('tenant.home'))->toBe($tenantRouteUrl); expect(route('custom_prefix.home'))->toBe('http://localhost/tenant/home');
// Ending tenancy reverts route() behavior changes // Ending tenancy reverts route() behavior changes
tenancy()->end(); tenancy()->end();
expect(route('home'))->toBe($centralRouteUrl); expect(route('home'))->toBe('http://localhost/central/home');
}); });
test('the route helper can receive the tenant parameter automatically', function ( test('path identification route helper behavior', function (bool $addTenantParameterToDefaults, bool $passTenantParameterToRoutes) {
string $identification,
bool $addTenantParameterToDefaults,
bool $passTenantParameterToRoutes,
) {
config(['tenancy.bootstrappers' => [UrlGeneratorBootstrapper::class]]); config(['tenancy.bootstrappers' => [UrlGeneratorBootstrapper::class]]);
$appUrl = config('app.url');
UrlGeneratorBootstrapper::$addTenantParameterToDefaults = $addTenantParameterToDefaults; UrlGeneratorBootstrapper::$addTenantParameterToDefaults = $addTenantParameterToDefaults;
// When the tenant parameter isn't added to defaults, the tenant parameter has to be passed "manually"
// by setting $passTenantParameterToRoutes to true. This is only preferable with query string identification.
// With path identification, this ultimately doesn't have any effect
// if UrlGeneratorBootstrapper::$addTenantParameterToDefaults is true,
// but TenancyUrlGenerator::$passTenantParameterToRoutes can still be used instead.
TenancyUrlGenerator::$passTenantParameterToRoutes = $passTenantParameterToRoutes; TenancyUrlGenerator::$passTenantParameterToRoutes = $passTenantParameterToRoutes;
$tenant = Tenant::create(); $tenant = Tenant::create();
$tenantKey = $tenant->getTenantKey();
Route::get('/central/home', fn () => route('home'))->name('home'); Route::get('/{tenant}/home', fn () => tenant('id'))
$tenantRoute = $identification === InitializeTenancyByPath::class ? "/{tenant}/home" : "/tenant/home";
Route::get($tenantRoute, fn () => route('tenant.home'))
->name('tenant.home') ->name('tenant.home')
->middleware(['tenant', $identification]); ->middleware([InitializeTenancyByPath::class]);
tenancy()->initialize($tenant); tenancy()->initialize($tenant);
$expectedUrl = match (true) { if (! $addTenantParameterToDefaults && ! $passTenantParameterToRoutes) {
$identification === InitializeTenancyByRequestData::class && $passTenantParameterToRoutes => "{$appUrl}/tenant/home?tenant={$tenantKey}",
$identification === InitializeTenancyByRequestData::class => "{$appUrl}/tenant/home", // $passTenantParameterToRoutes is false
$identification === InitializeTenancyByPath::class && ($addTenantParameterToDefaults || $passTenantParameterToRoutes) => "{$appUrl}/{$tenantKey}/home",
$identification === InitializeTenancyByPath::class => null, // Should throw an exception -- route() doesn't receive the tenant parameter in this case
};
if ($expectedUrl === null) {
expect(fn () => route('tenant.home'))->toThrow(UrlGenerationException::class, 'Missing parameter: tenant'); expect(fn () => route('tenant.home'))->toThrow(UrlGenerationException::class, 'Missing parameter: tenant');
} else { } else {
expect(route('tenant.home'))->toBe($expectedUrl); // If at least *one* of the approaches was used, the parameter will make its way to the route
expect(route('tenant.home'))->toBe("http://localhost/{$tenant->id}/home");
pest()->get(route('tenant.home'))->assertSee($tenant->id);
} }
})->with([InitializeTenancyByPath::class, InitializeTenancyByRequestData::class]) })->with([true, false]) // UrlGeneratorBootstrapper::$addTenantParameterToDefaults
->with([true, false]) // UrlGeneratorBootstrapper::$addTenantParameterToDefaults
->with([true, false]); // TenancyUrlGenerator::$passTenantParameterToRoutes ->with([true, false]); // TenancyUrlGenerator::$passTenantParameterToRoutes
test('request data identification route helper behavior', function (bool $addTenantParameterToDefaults, bool $passTenantParameterToRoutes) {
config(['tenancy.bootstrappers' => [UrlGeneratorBootstrapper::class]]);
UrlGeneratorBootstrapper::$addTenantParameterToDefaults = $addTenantParameterToDefaults;
TenancyUrlGenerator::$passTenantParameterToRoutes = $passTenantParameterToRoutes;
$tenant = Tenant::create();
Route::get('/tenant/home', fn () => tenant('id'))
->name('tenant.home')
->middleware([InitializeTenancyByRequestData::class]);
tenancy()->initialize($tenant);
if ($passTenantParameterToRoutes) {
// Only $passTenantParameterToRoutes has an effect, defaults do not affect request data URL generation
expect(route('tenant.home'))->toBe("http://localhost/tenant/home?tenant={$tenant->id}");
pest()->get(route('tenant.home'))->assertSee($tenant->id);
} else {
expect(route('tenant.home'))->toBe("http://localhost/tenant/home");
expect(fn () => $this->withoutExceptionHandling()->get(route('tenant.home')))->toThrow(TenantCouldNotBeIdentifiedByRequestDataException::class);
}
})->with([true, false]) // UrlGeneratorBootstrapper::$addTenantParameterToDefaults
->with([true, false]); // TenancyUrlGenerator::$passTenantParameterToRoutes
test('changing request data query parameter and model column is respected by the url generator', function () {
config([
'tenancy.bootstrappers' => [UrlGeneratorBootstrapper::class],
'tenancy.identification.resolvers.' . RequestDataTenantResolver::class . '.query_parameter' => 'team',
'tenancy.identification.resolvers.' . RequestDataTenantResolver::class . '.tenant_model_column' => 'slug',
]);
Tenant::$extraCustomColumns = ['slug'];
Schema::table('tenants', function (Blueprint $table) {
$table->string('slug')->unique();
});
TenancyUrlGenerator::$passTenantParameterToRoutes = true;
$tenant = Tenant::create(['slug' => 'acme']);
Route::get('/tenant/home', fn () => tenant('id'))
->name('tenant.home')
->middleware([InitializeTenancyByRequestData::class]);
tenancy()->initialize($tenant);
expect(route('tenant.home'))->toBe("http://localhost/tenant/home?team=acme");
pest()->get(route('tenant.home'))->assertSee($tenant->id);
});
test('setting extra model columns sets additional URL defaults', function () {
Tenant::$extraCustomColumns = ['slug'];
TenancyUrlGenerator::$passTenantParameterToRoutes = false;
UrlGeneratorBootstrapper::$addTenantParameterToDefaults = true;
config(['tenancy.bootstrappers' => [UrlGeneratorBootstrapper::class]]);
config(['tenancy.identification.resolvers.' . PathTenantResolver::class . '.allowed_extra_model_columns' => ['slug']]);
Schema::table('tenants', function (Blueprint $table) {
$table->string('slug')->unique();
});
Route::get('/{tenant}/foo/{user}', function (string $user) {
return tenant()->getTenantKey() . " $user";
})->middleware([InitializeTenancyByPath::class, 'web'])->name('foo');
Route::get('/{tenant:slug}/fooslug/{user}', function (string $user) {
return tenant()->getTenantKey() . " $user";
})->middleware([InitializeTenancyByPath::class, 'web'])->name('fooslug');
$tenant = Tenant::create(['slug' => 'acme']);
// In central context, no URL defaults are applied
expect(route('foo', [$tenant->getTenantKey(), 'bar']))->toBe("http://localhost/{$tenant->getTenantKey()}/foo/bar");
pest()->get(route('foo', [$tenant->getTenantKey(), 'bar']))->assertSee(tenant()->getTenantKey() . ' bar');
tenancy()->end();
expect(route('fooslug', ['acme', 'bar']))->toBe('http://localhost/acme/fooslug/bar');
pest()->get(route('fooslug', ['acme', 'bar']))->assertSee(tenant()->getTenantKey() . ' bar');
tenancy()->end();
// In tenant context, URL defaults are applied
tenancy()->initialize($tenant);
expect(route('foo', ['bar']))->toBe("http://localhost/{$tenant->getTenantKey()}/foo/bar");
pest()->get(route('foo', ['bar']))->assertSee(tenant()->getTenantKey() . ' bar');
expect(route('fooslug', ['bar']))->toBe('http://localhost/acme/fooslug/bar');
pest()->get(route('fooslug', ['bar']))->assertSee(tenant()->getTenantKey() . ' bar');
});
test('changing the tenant model column changes the default value for the tenant parameter', function () {
Tenant::$extraCustomColumns = ['slug'];
TenancyUrlGenerator::$passTenantParameterToRoutes = false;
UrlGeneratorBootstrapper::$addTenantParameterToDefaults = true;
config(['tenancy.bootstrappers' => [UrlGeneratorBootstrapper::class]]);
config(['tenancy.identification.resolvers.' . PathTenantResolver::class . '.tenant_model_column' => 'slug']);
Schema::table('tenants', function (Blueprint $table) {
$table->string('slug')->unique();
});
Route::get('/{tenant}/foo/{user}', function (string $user) {
return tenant()->getTenantKey() . " $user";
})->middleware([InitializeTenancyByPath::class, 'web'])->name('foo');
$tenant = Tenant::create(['slug' => 'acme']);
// In central context, no URL defaults are applied
expect(route('foo', ['acme', 'bar']))->toBe("http://localhost/acme/foo/bar");
pest()->get(route('foo', ['acme', 'bar']))->assertSee(tenant()->getTenantKey() . ' bar');
tenancy()->end();
// In tenant context, URL defaults are applied
tenancy()->initialize($tenant);
expect(route('foo', ['bar']))->toBe("http://localhost/acme/foo/bar");
pest()->get(route('foo', ['bar']))->assertSee(tenant()->getTenantKey() . ' bar');
});
test('changing the tenant parameter name is respected by the url generator', function () {
Tenant::$extraCustomColumns = ['slug', 'slug2'];
TenancyUrlGenerator::$passTenantParameterToRoutes = false;
UrlGeneratorBootstrapper::$addTenantParameterToDefaults = true;
config(['tenancy.bootstrappers' => [UrlGeneratorBootstrapper::class]]);
config(['tenancy.identification.resolvers.' . PathTenantResolver::class . '.tenant_parameter_name' => 'team']);
config(['tenancy.identification.resolvers.' . PathTenantResolver::class . '.tenant_model_column' => 'slug']);
config(['tenancy.identification.resolvers.' . PathTenantResolver::class . '.allowed_extra_model_columns' => ['slug2']]);
Schema::table('tenants', function (Blueprint $table) {
$table->string('slug')->unique();
$table->string('slug2')->unique();
});
Route::get('/{team}/foo/{user}', function (string $user) {
return tenant()->getTenantKey() . " $user";
})->middleware([InitializeTenancyByPath::class, 'web'])->name('foo');
Route::get('/{team:slug2}/fooslug2/{user}', function (string $user) {
return tenant()->getTenantKey() . " $user";
})->middleware([InitializeTenancyByPath::class, 'web'])->name('fooslug2');
$tenant = Tenant::create(['slug' => 'acme', 'slug2' => 'acme2']);
// In central context, no URL defaults are applied
expect(route('foo', ['acme', 'bar']))->toBe("http://localhost/acme/foo/bar");
pest()->get(route('foo', ['acme', 'bar']))->assertSee(tenant()->getTenantKey() . ' bar');
tenancy()->end();
expect(route('fooslug2', ['acme2', 'bar']))->toBe("http://localhost/acme2/fooslug2/bar");
pest()->get(route('fooslug2', ['acme2', 'bar']))->assertSee(tenant()->getTenantKey() . ' bar');
tenancy()->end();
// In tenant context, URL defaults are applied
tenancy()->initialize($tenant);
expect(route('foo', ['bar']))->toBe("http://localhost/acme/foo/bar");
pest()->get(route('foo', ['bar']))->assertSee(tenant()->getTenantKey() . ' bar');
expect(route('fooslug2', ['bar']))->toBe("http://localhost/acme2/fooslug2/bar");
pest()->get(route('fooslug2', ['bar']))->assertSee(tenant()->getTenantKey() . ' bar');
});
test('url generator can override specific route names', function() { test('url generator can override specific route names', function() {
config(['tenancy.bootstrappers' => [UrlGeneratorBootstrapper::class]]); config(['tenancy.bootstrappers' => [UrlGeneratorBootstrapper::class]]);

View file

@ -35,6 +35,11 @@ beforeEach(function () {
}); });
}); });
afterEach(function () {
InitializeTenancyByPath::$onFail = null;
Tenant::$extraCustomColumns = [];
});
test('tenant can be identified by path', function () { test('tenant can be identified by path', function () {
Tenant::create([ Tenant::create([
'id' => 'acme', 'id' => 'acme',
@ -150,6 +155,7 @@ test('central route can have a parameter with the same name as the tenant parame
config(['tenancy.identification.resolvers.' . PathTenantResolver::class . '.tenant_parameter_name' => 'team']); config(['tenancy.identification.resolvers.' . PathTenantResolver::class . '.tenant_parameter_name' => 'team']);
$tenantKey = Tenant::create()->getTenantKey(); $tenantKey = Tenant::create()->getTenantKey();
// The route is flagged as central (while using kernel identification) so the {team} parameter should not be used for tenancy initialization
Route::get('/central/route/{team}/{a}/{b}', function ($team, $a, $b) { Route::get('/central/route/{team}/{a}/{b}', function ($team, $a, $b) {
return "$a + $b + $team"; return "$a + $b + $team";
})->middleware('central')->name('central-route'); })->middleware('central')->name('central-route');
@ -185,8 +191,6 @@ test('the tenant model column can be customized in the config', function () {
$this->withoutExceptionHandling(); $this->withoutExceptionHandling();
pest()->get('/acme/foo')->assertSee($tenant->getTenantKey()); pest()->get('/acme/foo')->assertSee($tenant->getTenantKey());
expect(fn () => pest()->get($tenant->id . '/foo'))->toThrow(TenantCouldNotBeIdentifiedByPathException::class); expect(fn () => pest()->get($tenant->id . '/foo'))->toThrow(TenantCouldNotBeIdentifiedByPathException::class);
Tenant::$extraCustomColumns = []; // static property reset
}); });
test('the tenant model column can be customized in the route definition', function () { test('the tenant model column can be customized in the route definition', function () {
@ -218,8 +222,6 @@ test('the tenant model column can be customized in the route definition', functi
// Binding field defined // Binding field defined
pest()->get('/acme/bar')->assertSee($tenant->getTenantKey()); pest()->get('/acme/bar')->assertSee($tenant->getTenantKey());
expect(fn () => pest()->get($tenant->id . '/bar'))->toThrow(TenantCouldNotBeIdentifiedByPathException::class); expect(fn () => pest()->get($tenant->id . '/bar'))->toThrow(TenantCouldNotBeIdentifiedByPathException::class);
Tenant::$extraCustomColumns = []; // static property reset
}); });
test('any extra model column needs to be whitelisted', function () { test('any extra model column needs to be whitelisted', function () {
@ -243,6 +245,4 @@ test('any extra model column needs to be whitelisted', function () {
// After whitelisting the column it works // After whitelisting the column it works
config(['tenancy.identification.resolvers.' . PathTenantResolver::class . '.allowed_extra_model_columns' => ['slug']]); config(['tenancy.identification.resolvers.' . PathTenantResolver::class . '.allowed_extra_model_columns' => ['slug']]);
pest()->get('/acme/foo')->assertSee($tenant->getTenantKey()); pest()->get('/acme/foo')->assertSee($tenant->getTenantKey());
Tenant::$extraCustomColumns = []; // static property reset
}); });

View file

@ -2,9 +2,11 @@
declare(strict_types=1); declare(strict_types=1);
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedByRequestDataException; use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedByRequestDataException;
use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData; use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData;
use Stancl\Tenancy\Resolvers\RequestDataTenantResolver;
use Stancl\Tenancy\Tests\Etc\Tenant; use Stancl\Tenancy\Tests\Etc\Tenant;
use function Stancl\Tenancy\Tests\pest; use function Stancl\Tenancy\Tests\pest;
@ -15,45 +17,90 @@ beforeEach(function () {
], ],
]); ]);
InitializeTenancyByRequestData::$header = 'X-Tenant'; Route::middleware([InitializeTenancyByRequestData::class])->get('/test', function () {
InitializeTenancyByRequestData::$cookie = 'X-Tenant';
InitializeTenancyByRequestData::$queryParameter = 'tenant';
Route::middleware(['tenant', InitializeTenancyByRequestData::class])->get('/test', function () {
return 'Tenant id: ' . tenant('id'); return 'Tenant id: ' . tenant('id');
}); });
}); });
test('header identification works', function () { test('header identification works', function (string|null $tenantModelColumn) {
$tenant = Tenant::create(); if ($tenantModelColumn) {
Schema::table('tenants', function (Blueprint $table) use ($tenantModelColumn) {
$table->string($tenantModelColumn)->unique();
});
Tenant::$extraCustomColumns = [$tenantModelColumn];
}
$this config(['tenancy.identification.resolvers.' . RequestDataTenantResolver::class . '.tenant_model_column' => $tenantModelColumn]);
->withoutExceptionHandling()
->withHeader('X-Tenant', $tenant->id)
->get('test')
->assertSee($tenant->id);
});
test('query parameter identification works', function () { $tenant = Tenant::create($tenantModelColumn ? [$tenantModelColumn => 'acme'] : []);
$tenant = Tenant::create(); $payload = $tenantModelColumn ? 'acme' : $tenant->id;
$this // Default header name
->withoutExceptionHandling() $this->withoutExceptionHandling()->withHeader('X-Tenant', $payload)->get('test')->assertSee($tenant->id);
->get('test?tenant=' . $tenant->id)
->assertSee($tenant->id);
});
test('cookie identification works', function () { // Custom header name
$tenant = Tenant::create(); config(['tenancy.identification.resolvers.' . RequestDataTenantResolver::class . '.header' => 'X-Custom-Tenant']);
$this->withoutExceptionHandling()->withHeader('X-Custom-Tenant', $payload)->get('test')->assertSee($tenant->id);
$this // Setting the header to null disables header identification
->withoutExceptionHandling() config(['tenancy.identification.resolvers.' . RequestDataTenantResolver::class . '.header' => null]);
->withUnencryptedCookie('X-Tenant', $tenant->id) expect(fn () => $this->withoutExceptionHandling()->withHeader('X-Tenant', $payload)->get('test'))->toThrow(TenantCouldNotBeIdentifiedByRequestDataException::class);
->get('test') })->with([null, 'slug']);
->assertSee($tenant->id);
});
test('middleware throws exception when tenant data is not provided in the request', function () { test('query parameter identification works', function (string|null $tenantModelColumn) {
if ($tenantModelColumn) {
Schema::table('tenants', function (Blueprint $table) use ($tenantModelColumn) {
$table->string($tenantModelColumn)->unique();
});
Tenant::$extraCustomColumns = [$tenantModelColumn];
}
config(['tenancy.identification.resolvers.' . RequestDataTenantResolver::class . '.tenant_model_column' => $tenantModelColumn]);
$tenant = Tenant::create($tenantModelColumn ? [$tenantModelColumn => 'acme'] : []);
$payload = $tenantModelColumn ? 'acme' : $tenant->id;
// Default query parameter name
$this->withoutExceptionHandling()->get('test?tenant=' . $payload)->assertSee($tenant->id);
// Custom query parameter name
config(['tenancy.identification.resolvers.' . RequestDataTenantResolver::class . '.query_parameter' => 'custom_tenant']);
$this->withoutExceptionHandling()->get('test?custom_tenant=' . $payload)->assertSee($tenant->id);
// Setting the query parameter to null disables query parameter identification
config(['tenancy.identification.resolvers.' . RequestDataTenantResolver::class . '.query_parameter' => null]);
expect(fn () => $this->withoutExceptionHandling()->get('test?tenant=' . $payload))->toThrow(TenantCouldNotBeIdentifiedByRequestDataException::class);
})->with([null, 'slug']);
test('cookie identification works', function (string|null $tenantModelColumn) {
if ($tenantModelColumn) {
Schema::table('tenants', function (Blueprint $table) use ($tenantModelColumn) {
$table->string($tenantModelColumn)->unique();
});
Tenant::$extraCustomColumns = [$tenantModelColumn];
}
config(['tenancy.identification.resolvers.' . RequestDataTenantResolver::class . '.tenant_model_column' => $tenantModelColumn]);
$tenant = Tenant::create($tenantModelColumn ? [$tenantModelColumn => 'acme'] : []);
$payload = $tenantModelColumn ? 'acme' : $tenant->id;
// Default cookie name
$this->withoutExceptionHandling()->withUnencryptedCookie('tenant', $payload)->get('test')->assertSee($tenant->id);
// Custom cookie name
config(['tenancy.identification.resolvers.' . RequestDataTenantResolver::class . '.cookie' => 'custom_tenant_id']);
$this->withoutExceptionHandling()->withUnencryptedCookie('custom_tenant_id', $payload)->get('test')->assertSee($tenant->id);
// Setting the cookie to null disables cookie identification
config(['tenancy.identification.resolvers.' . RequestDataTenantResolver::class . '.cookie' => null]);
expect(fn () => $this->withoutExceptionHandling()->withUnencryptedCookie('tenant', $payload)->get('test'))->toThrow(TenantCouldNotBeIdentifiedByRequestDataException::class);
})->with([null, 'slug']);
// todo@tests encrypted cookie
test('an exception is thrown when no tenant data is provided in the request', function () {
pest()->expectException(TenantCouldNotBeIdentifiedByRequestDataException::class); pest()->expectException(TenantCouldNotBeIdentifiedByRequestDataException::class);
$this->withoutExceptionHandling()->get('test'); $this->withoutExceptionHandling()->get('test');
}); });