diff --git a/src/app/Console/Command.php b/src/app/Console/Command.php --- a/src/app/Console/Command.php +++ b/src/app/Console/Command.php @@ -36,6 +36,10 @@ */ protected $tenantId; + /** @var bool Adds --tenant option handler */ + protected $withTenant = false; + + /** * Apply current tenant context to the query * @@ -223,7 +227,7 @@ */ public function getWallet($wallet) { - return $this->getObject(\App\Wallet::class, $wallet, null); + return $this->getObject(\App\Wallet::class, $wallet); } /** @@ -247,6 +251,19 @@ $this->info("VĂ¡monos!"); } + + // @phpstan-ignore-next-line + if ($this->withTenant && $this->hasOption('tenant') && ($tenantId = $this->option('tenant'))) { + $tenant = $this->getObject(\App\Tenant::class, $tenantId, 'title'); + if (!$tenant) { + $this->error("Tenant {$tenantId} not found"); + return 1; + } + + $this->tenantId = $tenant->id; + } else { + $this->tenantId = \config('app.tenant_id'); + } } /** diff --git a/src/app/Console/Commands/Data/Import/SignupTokensCommand.php b/src/app/Console/Commands/Data/Import/SignupTokensCommand.php --- a/src/app/Console/Commands/Data/Import/SignupTokensCommand.php +++ b/src/app/Console/Commands/Data/Import/SignupTokensCommand.php @@ -22,6 +22,9 @@ */ protected $description = 'Imports signup tokens from a file.'; + /** @var bool Adds --tenant option handler */ + protected $withTenant = true; + /** * Execute the console command. * @@ -29,17 +32,7 @@ */ public function handle() { - if ($tenantId = $this->option('tenant')) { - $tenant = $this->getObject(\App\Tenant::class, $tenantId, 'title'); - if (!$tenant) { - $this->error("Tenant {$tenantId} not found"); - return 1; - } - - $this->tenantId = $tenant->id; - } else { - $this->tenantId = \config('app.tenant_id'); - } + parent::handle(); $plan = $this->getObject(Plan::class, $this->argument('plan'), 'title', false); diff --git a/src/app/Console/Commands/Discount/MergeCommand.php b/src/app/Console/Commands/Discount/MergeCommand.php --- a/src/app/Console/Commands/Discount/MergeCommand.php +++ b/src/app/Console/Commands/Discount/MergeCommand.php @@ -69,6 +69,11 @@ return 1; } + if ($source->tenant_id !== $target->tenant_id) { + $this->error("Can't merge two discounts that have different tenants"); + return 1; + } + foreach ($source->wallets as $wallet) { $wallet->discount_id = $target->id; $wallet->timestamps = false; diff --git a/src/app/Console/Commands/Domain/CreateCommand.php b/src/app/Console/Commands/Domain/CreateCommand.php --- a/src/app/Console/Commands/Domain/CreateCommand.php +++ b/src/app/Console/Commands/Domain/CreateCommand.php @@ -23,6 +23,9 @@ */ protected $description = "Create a domain"; + /** @var bool Adds --tenant option handler */ + protected $withTenant = true; + /** * Execute the console command. * @@ -30,6 +33,8 @@ */ public function handle() { + parent::handle(); + $namespace = \strtolower($this->argument('domain')); // must use withTrashed(), because unique constraint @@ -73,18 +78,10 @@ return 1; } } else { - if ($tenantId = $this->option('tenant')) { - $tenant = $this->getObject(Tenant::class, $tenantId, 'title'); - if (!$tenant) { - $this->error("Tenant {$tenantId} not found"); - return 1; - } - } - $domain = new Domain(); $domain->namespace = $namespace; $domain->type = Domain::TYPE_EXTERNAL; - $domain->tenant_id = !empty($tenant) ? $tenant->id : null; + $domain->tenant_id = $this->tenantId; $domain->save(); $this->info( diff --git a/src/app/Console/Commands/Package/SkusCommand.php b/src/app/Console/Commands/Package/SkusCommand.php --- a/src/app/Console/Commands/Package/SkusCommand.php +++ b/src/app/Console/Commands/Package/SkusCommand.php @@ -12,7 +12,7 @@ * * @var string */ - protected $signature = 'package:skus'; + protected $signature = 'package:skus {--tenant=}'; /** * The console command description. @@ -21,6 +21,9 @@ */ protected $description = "List SKUs for packages."; + /** @var bool Adds --tenant option handler */ + protected $withTenant = true; + /** * Execute the console command. * @@ -28,7 +31,9 @@ */ public function handle() { - $packages = Package::withEnvTenantContext()->get(); + parent::handle(); + + $packages = Package::where('tenant_id', $this->tenantId)->get(); foreach ($packages as $package) { $this->info(sprintf("Package: %s", $package->title)); diff --git a/src/app/Console/Commands/Plan/PackagesCommand.php b/src/app/Console/Commands/Plan/PackagesCommand.php --- a/src/app/Console/Commands/Plan/PackagesCommand.php +++ b/src/app/Console/Commands/Plan/PackagesCommand.php @@ -2,8 +2,8 @@ namespace App\Console\Commands\Plan; +use App\Console\Command; use App\Plan; -use Illuminate\Console\Command; class PackagesCommand extends Command { @@ -12,7 +12,7 @@ * * @var string */ - protected $signature = 'plan:packages'; + protected $signature = 'plan:packages {--tenant=}'; /** * The console command description. @@ -21,6 +21,9 @@ */ protected $description = "List packages for plans."; + /** @var bool Adds --tenant option handler */ + protected $withTenant = true; + /** * Execute the console command. * @@ -28,7 +31,9 @@ */ public function handle() { - $plans = Plan::withEnvTenantContext()->get(); + parent::handle(); + + $plans = Plan::where('tenant_id', $this->tenantId)->get(); foreach ($plans as $plan) { $this->info(sprintf("Plan: %s", $plan->title)); diff --git a/src/app/Console/Commands/Sku/ListUsersCommand.php b/src/app/Console/Commands/Sku/ListUsersCommand.php --- a/src/app/Console/Commands/Sku/ListUsersCommand.php +++ b/src/app/Console/Commands/Sku/ListUsersCommand.php @@ -11,7 +11,7 @@ * * @var string */ - protected $signature = 'sku:list-users {sku}'; + protected $signature = 'sku:list-users {sku} {--tenant=}'; /** * The console command description. @@ -20,6 +20,9 @@ */ protected $description = 'List users with the SKU entitlement.'; + /** @var bool Adds --tenant option handler */ + protected $withTenant = true; + /** * Execute the console command. * @@ -27,6 +30,8 @@ */ public function handle() { + parent::handle(); + $sku = $this->getObject(\App\Sku::class, $this->argument('sku'), 'title'); if (!$sku) { diff --git a/src/app/Console/Commands/Tenant/CreateCommand.php b/src/app/Console/Commands/Tenant/CreateCommand.php --- a/src/app/Console/Commands/Tenant/CreateCommand.php +++ b/src/app/Console/Commands/Tenant/CreateCommand.php @@ -42,7 +42,6 @@ return 1; } - DB::beginTransaction(); // Create a tenant diff --git a/src/app/Console/Commands/User/CreateCommand.php b/src/app/Console/Commands/User/CreateCommand.php --- a/src/app/Console/Commands/User/CreateCommand.php +++ b/src/app/Console/Commands/User/CreateCommand.php @@ -15,7 +15,7 @@ * * @var string */ - protected $signature = 'user:create {email} {--package=*} {--password=} {--role=}'; + protected $signature = 'user:create {email} {--package=*} {--password=} {--role=} {--tenant=}'; /** * The console command description. @@ -24,6 +24,9 @@ */ protected $description = "Create a user."; + /** @var bool Adds --tenant option handler */ + protected $withTenant = true; + /** * Execute the console command. * @@ -31,6 +34,8 @@ */ public function handle() { + parent::handle(); + $email = $this->argument('email'); $packages = $this->option('package'); $password = $this->option('password') ?: \App\Utils::generatePassphrase(); diff --git a/src/app/Console/Commands/User/GreylistCommand.php b/src/app/Console/Commands/User/GreylistCommand.php --- a/src/app/Console/Commands/User/GreylistCommand.php +++ b/src/app/Console/Commands/User/GreylistCommand.php @@ -43,7 +43,6 @@ $this->info("Going from timestamp (now) {$timestamp}"); } - \App\Policy\Greylist\Connect::where('recipient_hash', $recipientHash) ->where('greylisting', true) ->whereDate('updated_at', '>=', $timestamp->copy()->subDays(7)) diff --git a/src/database/migrations/2024_06_13_100000_signup_codes_tenant_id.php b/src/database/migrations/2024_06_13_100000_signup_codes_tenant_id.php --- a/src/database/migrations/2024_06_13_100000_signup_codes_tenant_id.php +++ b/src/database/migrations/2024_06_13_100000_signup_codes_tenant_id.php @@ -36,6 +36,7 @@ Schema::table( 'signup_codes', function (Blueprint $table) { + $table->dropForeign(['tenant_id']); $table->dropColumn('tenant_id'); } ); diff --git a/src/tests/Feature/Console/Sku/ListUsersTest.php b/src/tests/Feature/Console/Sku/ListUsersTest.php --- a/src/tests/Feature/Console/Sku/ListUsersTest.php +++ b/src/tests/Feature/Console/Sku/ListUsersTest.php @@ -2,36 +2,19 @@ namespace Tests\Feature\Console\Sku; +use App\Tenant; use Illuminate\Contracts\Console\Kernel; use Tests\TestCase; class ListUsersTest extends TestCase { - /** - * {@inheritDoc} - */ - public function setUp(): void - { - parent::setUp(); - - $this->deleteTestUser('sku-list-users@kolabnow.com'); - } - - /** - * {@inheritDoc} - */ - public function tearDown(): void - { - $this->deleteTestUser('sku-list-users@kolabnow.com'); - - parent::tearDown(); - } - /** * Test command runs */ public function testHandle(): void { + $tenant = Tenant::where('title', 'kanarip.ch')->first(); + // Warning: We're not using artisan() here, as this will not // allow us to test "empty output" cases $code = \Artisan::call('sku:list-users domain-registration'); @@ -67,5 +50,11 @@ $output = trim(\Artisan::output()); $this->assertSame(0, $code); $this->assertSame("john@kolab.org", $output); + + // Another tenant + $code = \Artisan::call("sku:list-users mailbox --tenant={$tenant->id}"); + $output = trim(\Artisan::output()); + $this->assertSame(0, $code); + $this->assertSame("user@kanarip.ch.dev-local", $output); } }