1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-03-21 23:24:03 +00:00

Merge branch 'master' into stop-impersonating

This commit is contained in:
lukinovec 2026-03-09 08:00:55 +01:00 committed by GitHub
commit 118f51fa73
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 54 additions and 4 deletions

View file

@ -63,7 +63,7 @@ class TenantDump extends DumpCommand
protected function getOptions(): array protected function getOptions(): array
{ {
return array_merge([ return array_merge([
['tenant', null, InputOption::VALUE_OPTIONAL, '', null], new InputOption('tenant', null, InputOption::VALUE_OPTIONAL, '', null),
], parent::getOptions()); ], parent::getOptions());
} }
} }

View file

@ -17,8 +17,8 @@ trait HasTenantOptions
protected function getOptions() protected function getOptions()
{ {
return array_merge([ return array_merge([
['tenants', null, InputOption::VALUE_IS_ARRAY|InputOption::VALUE_OPTIONAL, 'The tenants to run this command for. Leave empty for all tenants', null], new InputOption('tenants', null, InputOption::VALUE_IS_ARRAY|InputOption::VALUE_OPTIONAL, 'The tenants to run this command for. Leave empty for all tenants', null),
['with-pending', null, InputOption::VALUE_NONE, 'Include pending tenants in query'], // todo@pending should we also offer without-pending? if we add this, mention in docs new InputOption('with-pending', null, InputOption::VALUE_NONE, 'Include pending tenants in query'), // todo@pending should we also offer without-pending? if we add this, mention in docs
], parent::getOptions()); ], parent::getOptions());
} }

View file

@ -129,7 +129,15 @@ class TenancyUrlGenerator extends UrlGenerator
throw new InvalidArgumentException('Attribute [name] expects a string backed enum.'); throw new InvalidArgumentException('Attribute [name] expects a string backed enum.');
} }
[$name, $parameters] = $this->prepareRouteInputs($name, Arr::wrap($parameters)); // @phpstan-ignore argument.type $wrappedParameters = Arr::wrap($parameters);
[$name, $parameters] = $this->prepareRouteInputs($name, $wrappedParameters); // @phpstan-ignore argument.type
if (isset($wrappedParameters[static::$bypassParameter])) {
// If the bypass parameter was passed, we need to add it back to the parameters after prepareRouteInputs() removes it,
// so that the underlying route() call in parent::temporarySignedRoute() can bypass the behavior modification as well.
$parameters[static::$bypassParameter] = $wrappedParameters[static::$bypassParameter];
}
return parent::temporarySignedRoute($name, $expiration, $parameters, $absolute); return parent::temporarySignedRoute($name, $expiration, $parameters, $absolute);
} }

View file

@ -2,6 +2,7 @@
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Routing\UrlGenerator; use Illuminate\Routing\UrlGenerator;
use Illuminate\Support\Facades\URL;
use Stancl\Tenancy\Tests\Etc\Tenant; use Stancl\Tenancy\Tests\Etc\Tenant;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
@ -25,12 +26,16 @@ beforeEach(function () {
Event::listen(TenancyEnded::class, RevertToCentralContext::class); Event::listen(TenancyEnded::class, RevertToCentralContext::class);
TenancyUrlGenerator::$prefixRouteNames = false; TenancyUrlGenerator::$prefixRouteNames = false;
TenancyUrlGenerator::$passTenantParameterToRoutes = false; TenancyUrlGenerator::$passTenantParameterToRoutes = false;
TenancyUrlGenerator::$overrides = [];
TenancyUrlGenerator::$bypassParameter = 'central';
UrlGeneratorBootstrapper::$addTenantParameterToDefaults = false; UrlGeneratorBootstrapper::$addTenantParameterToDefaults = false;
}); });
afterEach(function () { afterEach(function () {
TenancyUrlGenerator::$prefixRouteNames = false; TenancyUrlGenerator::$prefixRouteNames = false;
TenancyUrlGenerator::$passTenantParameterToRoutes = false; TenancyUrlGenerator::$passTenantParameterToRoutes = false;
TenancyUrlGenerator::$overrides = [];
TenancyUrlGenerator::$bypassParameter = 'central';
UrlGeneratorBootstrapper::$addTenantParameterToDefaults = false; UrlGeneratorBootstrapper::$addTenantParameterToDefaults = false;
}); });
@ -359,3 +364,40 @@ test('both the name prefixing and the tenant parameter logic gets skipped when b
expect(route('home', ['bypassParameter' => false, 'tenant' => $tenant->getTenantKey()]))->toBe($tenantRouteUrl) expect(route('home', ['bypassParameter' => false, 'tenant' => $tenant->getTenantKey()]))->toBe($tenantRouteUrl)
->not()->toContain('bypassParameter'); ->not()->toContain('bypassParameter');
}); });
test('the temporarySignedRoute method can automatically prefix the passed route name', function() {
config(['tenancy.bootstrappers' => [UrlGeneratorBootstrapper::class]]);
Route::get('/{tenant}/foo', fn () => 'foo')->name('tenant.foo')->middleware([InitializeTenancyByPath::class]);
TenancyUrlGenerator::$prefixRouteNames = true;
$tenant = Tenant::create();
tenancy()->initialize($tenant);
// Route name ('foo') gets prefixed automatically (will be 'tenant.foo')
$tenantSignedUrl = URL::temporarySignedRoute('foo', now()->addMinutes(2), ['tenant' => $tenantKey = $tenant->getTenantKey()]);
expect($tenantSignedUrl)->toContain("localhost/{$tenantKey}/foo");
});
test('the bypass parameter works correctly with temporarySignedRoute', function() {
config(['tenancy.bootstrappers' => [UrlGeneratorBootstrapper::class]]);
Route::get('/foo', fn () => 'foo')->name('central.foo');
TenancyUrlGenerator::$prefixRouteNames = true;
TenancyUrlGenerator::$bypassParameter = 'central';
$tenant = Tenant::create();
tenancy()->initialize($tenant);
// Bypass parameter allows us to generate URL for the 'central.foo' route in tenant context
$centralSignedUrl = URL::temporarySignedRoute('central.foo', now()->addMinutes(2), ['central' => true]);
expect($centralSignedUrl)
->toContain('localhost/foo')
->not()->toContain('central='); // Bypass parameter gets removed from the generated URL
});