Skip to content

Commit 841d8cd

Browse files
authored
Merge branch 'master' into feat/skip-tenants
2 parents e68aedb + 8f3ea62 commit 841d8cd

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/Overrides/TenancyUrlGenerator.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,15 @@ public function temporarySignedRoute($name, $expiration, $parameters = [], $abso
129129
throw new InvalidArgumentException('Attribute [name] expects a string backed enum.');
130130
}
131131

132-
[$name, $parameters] = $this->prepareRouteInputs($name, Arr::wrap($parameters)); // @phpstan-ignore argument.type
132+
$wrappedParameters = Arr::wrap($parameters);
133+
134+
[$name, $parameters] = $this->prepareRouteInputs($name, $wrappedParameters); // @phpstan-ignore argument.type
135+
136+
if (isset($wrappedParameters[static::$bypassParameter])) {
137+
// If the bypass parameter was passed, we need to add it back to the parameters after prepareRouteInputs() removes it,
138+
// so that the underlying route() call in parent::temporarySignedRoute() can bypass the behavior modification as well.
139+
$parameters[static::$bypassParameter] = $wrappedParameters[static::$bypassParameter];
140+
}
133141

134142
return parent::temporarySignedRoute($name, $expiration, $parameters, $absolute);
135143
}

tests/Bootstrappers/UrlGeneratorBootstrapperTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Illuminate\Database\Schema\Blueprint;
44
use Illuminate\Routing\UrlGenerator;
5+
use Illuminate\Support\Facades\URL;
56
use Stancl\Tenancy\Tests\Etc\Tenant;
67
use Illuminate\Support\Facades\Event;
78
use Illuminate\Support\Facades\Route;
@@ -25,12 +26,16 @@
2526
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
2627
TenancyUrlGenerator::$prefixRouteNames = false;
2728
TenancyUrlGenerator::$passTenantParameterToRoutes = false;
29+
TenancyUrlGenerator::$overrides = [];
30+
TenancyUrlGenerator::$bypassParameter = 'central';
2831
UrlGeneratorBootstrapper::$addTenantParameterToDefaults = false;
2932
});
3033

3134
afterEach(function () {
3235
TenancyUrlGenerator::$prefixRouteNames = false;
3336
TenancyUrlGenerator::$passTenantParameterToRoutes = false;
37+
TenancyUrlGenerator::$overrides = [];
38+
TenancyUrlGenerator::$bypassParameter = 'central';
3439
UrlGeneratorBootstrapper::$addTenantParameterToDefaults = false;
3540
});
3641

@@ -359,3 +364,40 @@
359364
expect(route('home', ['bypassParameter' => false, 'tenant' => $tenant->getTenantKey()]))->toBe($tenantRouteUrl)
360365
->not()->toContain('bypassParameter');
361366
});
367+
368+
test('the temporarySignedRoute method can automatically prefix the passed route name', function() {
369+
config(['tenancy.bootstrappers' => [UrlGeneratorBootstrapper::class]]);
370+
371+
Route::get('/{tenant}/foo', fn () => 'foo')->name('tenant.foo')->middleware([InitializeTenancyByPath::class]);
372+
373+
TenancyUrlGenerator::$prefixRouteNames = true;
374+
375+
$tenant = Tenant::create();
376+
377+
tenancy()->initialize($tenant);
378+
379+
// Route name ('foo') gets prefixed automatically (will be 'tenant.foo')
380+
$tenantSignedUrl = URL::temporarySignedRoute('foo', now()->addMinutes(2), ['tenant' => $tenantKey = $tenant->getTenantKey()]);
381+
382+
expect($tenantSignedUrl)->toContain("localhost/{$tenantKey}/foo");
383+
});
384+
385+
test('the bypass parameter works correctly with temporarySignedRoute', function() {
386+
config(['tenancy.bootstrappers' => [UrlGeneratorBootstrapper::class]]);
387+
388+
Route::get('/foo', fn () => 'foo')->name('central.foo');
389+
390+
TenancyUrlGenerator::$prefixRouteNames = true;
391+
TenancyUrlGenerator::$bypassParameter = 'central';
392+
393+
$tenant = Tenant::create();
394+
395+
tenancy()->initialize($tenant);
396+
397+
// Bypass parameter allows us to generate URL for the 'central.foo' route in tenant context
398+
$centralSignedUrl = URL::temporarySignedRoute('central.foo', now()->addMinutes(2), ['central' => true]);
399+
400+
expect($centralSignedUrl)
401+
->toContain('localhost/foo')
402+
->not()->toContain('central='); // Bypass parameter gets removed from the generated URL
403+
});

0 commit comments

Comments
 (0)