From f941df3a82e6856a1dc299ff89e063c2baaed4a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Thu, 1 Sep 2022 19:06:54 +0200 Subject: [PATCH 1/6] minor improvements for phpstan --- src/Features/CrossDomainRedirect.php | 11 ++++++++--- src/Tenancy.php | 2 +- src/helpers.php | 12 ++++++++---- tests/TestCase.php | 2 +- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/Features/CrossDomainRedirect.php b/src/Features/CrossDomainRedirect.php index 0b6d7682..4efc767b 100644 --- a/src/Features/CrossDomainRedirect.php +++ b/src/Features/CrossDomainRedirect.php @@ -15,11 +15,16 @@ class CrossDomainRedirect implements Feature RedirectResponse::macro('domain', function (string $domain) { /** @var RedirectResponse $this */ - // Replace first occurrence of the hostname fragment with $domain $url = $this->getTargetUrl(); + + /** + * The original hostname in the redirect response. + * + * @var string $hostname + */ $hostname = parse_url($url, PHP_URL_HOST); - $position = strpos($url, $hostname); - $this->setTargetUrl(substr_replace($url, $domain, $position, strlen($hostname))); + + $this->setTargetUrl((string) str($url)->replace($hostname, $domain)); return $this; }); diff --git a/src/Tenancy.php b/src/Tenancy.php index 012881ae..0a8d4542 100644 --- a/src/Tenancy.php +++ b/src/Tenancy.php @@ -146,7 +146,7 @@ class Tenancy $tenants = is_string($tenants) ? [$tenants] : $tenants; // Use all tenants if $tenants is falsey - $tenants = $tenants ?: $this->model()->cursor(); + $tenants = $tenants ?: $this->model()->cursor(); // todo0 phpstan thinks this isn't needed, but tests fail without it $originalTenant = $this->tenant; diff --git a/src/helpers.php b/src/helpers.php index 23b5a627..ac805aa5 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -58,11 +58,15 @@ if (! function_exists('global_cache')) { if (! function_exists('tenant_route')) { function tenant_route(string $domain, string $route, array $parameters = [], bool $absolute = true): string { - // replace the first occurrence of the hostname fragment with $domain $url = route($route, $parameters, $absolute); - $hostname = parse_url($url, PHP_URL_HOST); - $position = strpos($url, $hostname); - return substr_replace($url, $domain, $position, strlen($hostname)); + /** + * The original hostname in the generated route. + * + * @var string $hostname + */ + $hostname = parse_url($url, PHP_URL_HOST); + + return (string) str($url)->replace($hostname, $domain); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 1c6c6d8a..67029422 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -96,7 +96,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase '--realpath' => true, '--force' => true, ], - 'tenancy.bootstrappers.redis' => \Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class, + 'tenancy.bootstrappers.redis' => \Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class, // todo0 change this to []? two tests in TenantDatabaseManagerTest are failing with that 'queue.connections.central' => [ 'driver' => 'sync', 'central' => true, From 020039bf89f6836c3c948ecc913ac9808cfee0f6 Mon Sep 17 00:00:00 2001 From: PHP CS Fixer Date: Thu, 1 Sep 2022 17:07:17 +0000 Subject: [PATCH 2/6] Fix code style (php-cs-fixer) --- src/Features/CrossDomainRedirect.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Features/CrossDomainRedirect.php b/src/Features/CrossDomainRedirect.php index 4efc767b..a48be6ea 100644 --- a/src/Features/CrossDomainRedirect.php +++ b/src/Features/CrossDomainRedirect.php @@ -14,7 +14,6 @@ class CrossDomainRedirect implements Feature { RedirectResponse::macro('domain', function (string $domain) { /** @var RedirectResponse $this */ - $url = $this->getTargetUrl(); /** From f83504ac6f61e260d5382a5feaac0fa98ef98664 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Fri, 2 Sep 2022 17:24:37 +0200 Subject: [PATCH 3/6] [4.x] Add ScoutTenancyBootstrapper (#936) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add ScoutTenancyBootstrapper * Fix code style (php-cs-fixer) * extract getTenantPrefix method * Fix code style (php-cs-fixer) Co-authored-by: PHP CS Fixer Co-authored-by: Samuel Štancl --- .../Integrations/ScoutTenancyBootstrapper.php | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/Bootstrappers/Integrations/ScoutTenancyBootstrapper.php diff --git a/src/Bootstrappers/Integrations/ScoutTenancyBootstrapper.php b/src/Bootstrappers/Integrations/ScoutTenancyBootstrapper.php new file mode 100644 index 00000000..49869bb5 --- /dev/null +++ b/src/Bootstrappers/Integrations/ScoutTenancyBootstrapper.php @@ -0,0 +1,42 @@ +config = $config; + } + + public function bootstrap(Tenant $tenant) + { + if (! isset($this->originalScoutPrefix)) { + $this->originalScoutPrefix = $this->config->get('scout.prefix'); + } + + $this->config->set('scout.prefix', $this->getTenantPrefix($tenant)); + } + + public function revert() + { + $this->config->set('scout.prefix', $this->originalScoutPrefix); + } + + protected function getTenantPrefix(Tenant $tenant): string + { + return (string) $tenant->getTenantKey(); + } +} From 3bf2c39e1a38672a2a9345ab46a9076609deb901 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Fri, 2 Sep 2022 17:46:27 +0200 Subject: [PATCH 4/6] [4.x] Make impersonation tokens require stateful guards (#935) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Throw an exception on attempt to create impersonation token with a non-stateful guard * Test that impersonation tokens can only be created with a stateful guard * Fix code style (php-cs-fixer) * Escape backslashes in the exception's message Co-authored-by: Samuel Štancl * Make the exception only about requiring a stateful guard Co-authored-by: PHP CS Fixer Co-authored-by: Samuel Štancl --- src/Database/Models/ImpersonationToken.php | 11 +++- .../StatefulGuardRequiredException.php | 15 +++++ tests/TenantUserImpersonationTest.php | 62 ++++++++++++++++--- 3 files changed, 77 insertions(+), 11 deletions(-) create mode 100644 src/Exceptions/StatefulGuardRequiredException.php diff --git a/src/Database/Models/ImpersonationToken.php b/src/Database/Models/ImpersonationToken.php index 8161aca7..05d17ad4 100644 --- a/src/Database/Models/ImpersonationToken.php +++ b/src/Database/Models/ImpersonationToken.php @@ -5,9 +5,12 @@ declare(strict_types=1); namespace Stancl\Tenancy\Database\Models; use Carbon\Carbon; +use Illuminate\Contracts\Auth\StatefulGuard; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Facades\Auth; use Illuminate\Support\Str; use Stancl\Tenancy\Database\Concerns\CentralConnection; +use Stancl\Tenancy\Exceptions\StatefulGuardRequiredException; /** * @property string $token @@ -38,9 +41,15 @@ class ImpersonationToken extends Model public static function booted(): void { static::creating(function ($model) { + $authGuard = $model->auth_guard ?? config('auth.defaults.guard'); + + if (! Auth::guard($authGuard) instanceof StatefulGuard) { + throw new StatefulGuardRequiredException($authGuard); + } + $model->created_at = $model->created_at ?? $model->freshTimestamp(); $model->token = $model->token ?? Str::random(128); - $model->auth_guard = $model->auth_guard ?? config('auth.defaults.guard'); + $model->auth_guard = $authGuard; }); } } diff --git a/src/Exceptions/StatefulGuardRequiredException.php b/src/Exceptions/StatefulGuardRequiredException.php new file mode 100644 index 00000000..fe8bef6e --- /dev/null +++ b/src/Exceptions/StatefulGuardRequiredException.php @@ -0,0 +1,15 @@ +artisan('migrate', [ @@ -223,6 +225,46 @@ test('impersonation works with multiple models and guards', function () { }); }); +test('impersonation tokens can be created only with stateful guards', function () { + config([ + 'auth.guards' => [ + 'nonstateful' => [ + 'driver' => 'nonstateful', + 'provider' => 'provider', + ], + 'stateful' => [ + 'driver' => 'session', + 'provider' => 'provider', + ], + ], + 'auth.providers.provider' => [ + 'driver' => 'eloquent', + 'model' => ImpersonationUser::class, + ], + ]); + + $tenant = Tenant::create(); + migrateTenants(); + + $user = $tenant->run(function () { + return ImpersonationUser::create([ + 'name' => 'Joe', + 'email' => 'joe@local', + 'password' => bcrypt('secret'), + ]); + }); + + Auth::extend('nonstateful', fn($app, $name, array $config) => new TokenGuard(Auth::createUserProvider($config['provider']), request())); + + expect(fn() => tenancy()->impersonate($tenant, $user->id, '/dashboard', 'nonstateful')) + ->toThrow(StatefulGuardRequiredException::class); + + Auth::extend('stateful', fn ($app, $name, array $config) => new SessionGuard($name, Auth::createUserProvider($config['provider']), session())); + + expect(tenancy()->impersonate($tenant, $user->id, '/dashboard', 'stateful')) + ->toBeInstanceOf(ImpersonationToken::class); +}); + function migrateTenants() { pest()->artisan('tenants:migrate')->assertExitCode(0); From 409190fae1584d17ee6ed86318c0055d9f239ce6 Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Fri, 2 Sep 2022 21:46:13 +0500 Subject: [PATCH 5/6] [4.x] Improve `tenants:run` command to execute Input\Output commands (#923) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * run command useable for questions asking commands * move console classes to Console directory * fix styling * Update src/Commands/Run.php Co-authored-by: Samuel Štancl * remove tenant migration line * assert command executed in tenant context * improve test * cleanup code * Update CommandsTest.php * remove irrelevant assertions Co-authored-by: Samuel Štancl --- src/Commands/Run.php | 27 +++++++++--- tests/CommandsTest.php | 24 ++++++++++ tests/Etc/{ => Console}/AddUserCommand.php | 3 +- tests/Etc/{ => Console}/ConsoleKernel.php | 3 +- tests/Etc/{ => Console}/ExampleCommand.php | 2 +- tests/Etc/Console/ExampleQuestionCommand.php | 46 ++++++++++++++++++++ tests/TestCase.php | 2 +- 7 files changed, 98 insertions(+), 9 deletions(-) rename tests/Etc/{ => Console}/AddUserCommand.php (91%) rename tests/Etc/{ => Console}/ConsoleKernel.php (72%) rename tests/Etc/{ => Console}/ExampleCommand.php (94%) create mode 100644 tests/Etc/Console/ExampleQuestionCommand.php diff --git a/src/Commands/Run.php b/src/Commands/Run.php index 075f9116..a24fb9c7 100644 --- a/src/Commands/Run.php +++ b/src/Commands/Run.php @@ -5,7 +5,9 @@ declare(strict_types=1); namespace Stancl\Tenancy\Commands; use Illuminate\Console\Command; -use Illuminate\Support\Facades\Artisan; +use Illuminate\Contracts\Console\Kernel; +use Symfony\Component\Console\Input\ArgvInput; +use Symfony\Component\Console\Output\ConsoleOutput; class Run extends Command { @@ -29,12 +31,27 @@ class Run extends Command */ public function handle() { - tenancy()->runForMultiple($this->option('tenants'), function ($tenant) { + $argvInput = $this->ArgvInput(); + tenancy()->runForMultiple($this->option('tenants'), function ($tenant) use ($argvInput) { $this->line("Tenant: {$tenant->getTenantKey()}"); - Artisan::call($this->argument('commandname')); - $this->comment('Command output:'); - $this->info(Artisan::output()); + $this->getLaravel() + ->make(Kernel::class) + ->handle($argvInput, new ConsoleOutput); }); } + + /** + * Get command as ArgvInput instance. + */ + protected function ArgvInput(): ArgvInput + { + // Convert string command to array + $subCommand = explode(' ', $this->argument('commandname')); + + // Add "artisan" as first parameter because ArgvInput expects "artisan" as first parameter and later removes it + array_unshift($subCommand, 'artisan'); + + return new ArgvInput($subCommand); + } } diff --git a/tests/CommandsTest.php b/tests/CommandsTest.php index 7415b74f..19018c9a 100644 --- a/tests/CommandsTest.php +++ b/tests/CommandsTest.php @@ -16,6 +16,7 @@ use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Listeners\RevertToCentralContext; use Stancl\Tenancy\Tests\Etc\ExampleSeeder; use Stancl\Tenancy\Tests\Etc\Tenant; +use Stancl\Tenancy\Tests\Etc\User; beforeEach(function () { Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) { @@ -179,6 +180,29 @@ test('run command with array of tenants works', function () { ->expectsOutput('Tenant: ' . $tenantId2); }); +test('run command works when sub command asks questions and accepts arguments', function () { + $tenant = Tenant::create(); + $id = $tenant->getTenantKey(); + + Artisan::call('tenants:migrate'); + + pest()->artisan("tenants:run --tenants=$id 'user:addwithname Abrar' ") + ->expectsQuestion('What is your email?', 'email@localhost') + ->expectsOutput("Tenant: $id") + ->expectsOutput("User created: Abrar(email@localhost)"); + + // Assert we are in central context + expect(tenancy()->initialized)->toBeFalse(); + + // Assert user was created in tenant context + tenancy()->initialize($tenant); + $user = User::first(); + + // Assert user is same as provided using the command + expect($user->name)->toBe('Abrar'); + expect($user->email)->toBe('email@localhost'); +}); + // todo@tests function runCommandWorks(): void { diff --git a/tests/Etc/AddUserCommand.php b/tests/Etc/Console/AddUserCommand.php similarity index 91% rename from tests/Etc/AddUserCommand.php rename to tests/Etc/Console/AddUserCommand.php index 46e1fcbb..f102bae6 100644 --- a/tests/Etc/AddUserCommand.php +++ b/tests/Etc/Console/AddUserCommand.php @@ -2,12 +2,13 @@ declare(strict_types=1); -namespace Stancl\Tenancy\Tests\Etc; +namespace Stancl\Tenancy\Tests\Etc\Console; use Illuminate\Console\Command; use Illuminate\Support\Str; use Stancl\Tenancy\Concerns\HasATenantsOption; use Stancl\Tenancy\Concerns\TenantAwareCommand; +use Stancl\Tenancy\Tests\Etc\User; class AddUserCommand extends Command { diff --git a/tests/Etc/ConsoleKernel.php b/tests/Etc/Console/ConsoleKernel.php similarity index 72% rename from tests/Etc/ConsoleKernel.php rename to tests/Etc/Console/ConsoleKernel.php index a548f113..c5e5ee85 100644 --- a/tests/Etc/ConsoleKernel.php +++ b/tests/Etc/Console/ConsoleKernel.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Stancl\Tenancy\Tests\Etc; +namespace Stancl\Tenancy\Tests\Etc\Console; use Orchestra\Testbench\Foundation\Console\Kernel; @@ -10,6 +10,7 @@ class ConsoleKernel extends Kernel { protected $commands = [ ExampleCommand::class, + ExampleQuestionCommand::class, AddUserCommand::class, ]; } diff --git a/tests/Etc/ExampleCommand.php b/tests/Etc/Console/ExampleCommand.php similarity index 94% rename from tests/Etc/ExampleCommand.php rename to tests/Etc/Console/ExampleCommand.php index 49e7189b..72263b37 100644 --- a/tests/Etc/ExampleCommand.php +++ b/tests/Etc/Console/ExampleCommand.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Stancl\Tenancy\Tests\Etc; +namespace Stancl\Tenancy\Tests\Etc\Console; use Illuminate\Console\Command; diff --git a/tests/Etc/Console/ExampleQuestionCommand.php b/tests/Etc/Console/ExampleQuestionCommand.php new file mode 100644 index 00000000..9a967054 --- /dev/null +++ b/tests/Etc/Console/ExampleQuestionCommand.php @@ -0,0 +1,46 @@ +ask('What is your email?'); + + User::create([ + 'name' => $this->argument('name'), + 'email' => $email, + 'email_verified_at' => now(), + 'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password + 'remember_token' => Str::random(10), + ]); + + $this->line("User created: ". $this->argument('name') . "($email)"); + + return 0; + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 67029422..ed567497 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -142,7 +142,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase */ protected function resolveApplicationConsoleKernel($app) { - $app->singleton('Illuminate\Contracts\Console\Kernel', Etc\ConsoleKernel::class); + $app->singleton('Illuminate\Contracts\Console\Kernel', Etc\Console\ConsoleKernel::class); } public function randomString(int $length = 10) From f2c64088ed950f85787e7489b7d56cf40f02c96c Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Fri, 2 Sep 2022 22:04:00 +0500 Subject: [PATCH 6/6] [4.x] Set tenant as a default parameter for the URLs when using Path identification (#925) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * set tenant as default url parameter * Update PathIdentificationTest.php * assertion * test rename * fix tests * fix string Co-authored-by: Samuel Štancl --- src/Middleware/InitializeTenancyByPath.php | 8 +++++++ tests/PathIdentificationTest.php | 26 +++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/Middleware/InitializeTenancyByPath.php b/src/Middleware/InitializeTenancyByPath.php index e66400c5..ae15323c 100644 --- a/src/Middleware/InitializeTenancyByPath.php +++ b/src/Middleware/InitializeTenancyByPath.php @@ -7,6 +7,9 @@ namespace Stancl\Tenancy\Middleware; use Closure; use Illuminate\Http\Request; use Illuminate\Routing\Route; +use Illuminate\Support\Facades\Event; +use Illuminate\Support\Facades\URL; +use Stancl\Tenancy\Events\InitializingTenancy; use Stancl\Tenancy\Exceptions\RouteIsMissingTenantParameterException; use Stancl\Tenancy\Resolvers\PathTenantResolver; use Stancl\Tenancy\Tenancy; @@ -37,6 +40,11 @@ class InitializeTenancyByPath extends IdentificationMiddleware // We don't want to initialize tenancy if the tenant is // simply injected into some route controller action. if ($route->parameterNames()[0] === PathTenantResolver::$tenantParameterName) { + // Set tenant as a default parameter for the URLs in the current request + Event::listen(InitializingTenancy::class, function (InitializingTenancy $event) { + URL::defaults([PathTenantResolver::$tenantParameterName => $event->tenancy->tenant->getTenantKey()]); + }); + return $this->initializeTenancy( $request, $next, diff --git a/tests/PathIdentificationTest.php b/tests/PathIdentificationTest.php index bda0cfcb..bfa8f8ad 100644 --- a/tests/PathIdentificationTest.php +++ b/tests/PathIdentificationTest.php @@ -18,7 +18,11 @@ beforeEach(function () { ], function () { Route::get('/foo/{a}/{b}', function ($a, $b) { return "$a + $b"; - }); + })->name('foo'); + + Route::get('/baz/{a}/{b}', function ($a, $b) { + return "$a - $b"; + })->name('baz'); }); }); @@ -123,3 +127,23 @@ test('tenant parameter name can be customized', function () { ->withoutExceptionHandling() ->get('/acme/foo/abc/xyz'); }); + +test('tenant parameter is set for all routes as the default parameter once the tenancy initialized', function () { + Tenant::create([ + 'id' => 'acme', + ]); + + expect(tenancy()->initialized)->toBeFalse(); + + // make a request that will initialize tenancy + pest()->get(route('foo', ['tenant' => 'acme', 'a' => 1, 'b' => 2])); + + expect(tenancy()->initialized)->toBeTrue(); + expect(tenant('id'))->toBe('acme'); + + // assert that the route WITHOUT the tenant parameter matches the route WITH the tenant parameter + expect(route('baz', ['a' => 1, 'b' => 2]))->toBe(route('baz', ['tenant' => 'acme', 'a' => 1, 'b' => 2])); + + expect(route('baz', ['a' => 1, 'b' => 2]))->toBe('http://localhost/acme/baz/1/2'); // assert the full route string + pest()->get(route('baz', ['a' => 1, 'b' => 2]))->assertOk(); // Assert route don't need tenant parameter +});