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 @@ -3,6 +3,7 @@ namespace App\Console\Commands\Tenant; use App\Console\Command; +use App\Http\Controllers\API\V4\UsersController; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Queue; @@ -13,14 +14,14 @@ * * @var string */ - protected $signature = 'tenant:create {user} {--title=}'; + protected $signature = 'tenant:create {user} {domain} {--title=} {--password=}'; /** * The console command description. * * @var string */ - protected $description = "Create a tenant (with a set of SKUs/plans/packages) and make the user a reseller."; + protected $description = "Create a tenant (with a set of SKUs/plans/packages), and a reseller user and domain for it."; /** * Execute the console command. @@ -29,17 +30,23 @@ */ public function handle() { - $user = $this->getUser($this->argument('user')); + $email = $this->argument('user'); - if (!$user) { - $this->error('User not found.'); + if ($user = \App\User::where('email', $email)->first()) { + $this->error("The user already exists."); return 1; } + if ($domain = \App\Domain::where('namespace', $this->argument('domain'))->first()) { + $this->error("The domain already exists."); + return 1; + } + + DB::beginTransaction(); // Create a tenant - $tenant = \App\Tenant::create(['title' => $this->option('title') ?: $user->name()]); + $tenant = \App\Tenant::create(['title' => $this->option('title')]); // Clone plans, packages, skus for the tenant $sku_map = \App\Sku::withEnvTenantContext()->where('active', true)->get() @@ -131,28 +138,6 @@ // TODO: We could probably do config(['app.tenant' => $tenant->id]) here Queue::fake(); - // Assign 'reseller' role to the user - $user->role = 'reseller'; - $user->tenant_id = $tenant->id; - $user->save(); - - // Switch tenant_id for all of the user belongings - $user->wallets->each(function ($wallet) use ($tenant) { - $wallet->entitlements->each(function ($entitlement) use ($tenant) { - $entitlement->entitleable->tenant_id = $tenant->id; - $entitlement->entitleable->save(); - - // TODO: If user already has any entitlements, they will have to be - // removed/replaced by SKUs in the newly created tenant - // I think we don't really support this yet anyway. - }); - - // TODO: If the wallet has a discount we should remove/replace it too - // I think we don't really support this yet anyway. - }); - - DB::commit(); - // Make sure the transaction wasn't aborted $tenant = \App\Tenant::find($tenant->id); @@ -162,5 +147,46 @@ } $this->info("Created tenant {$tenant->id}."); + + // Set up the primary tenant domain + $domain = \App\Domain::create( + [ + 'namespace' => $this->argument('domain'), + 'type' => \App\Domain::TYPE_PUBLIC, + ] + ); + $domain->tenant_id = $tenant->id; + $domain->status = \App\Domain::STATUS_CONFIRMED | \App\Domain::STATUS_ACTIVE; + $domain->save(); + $this->info("Created domain {$domain->id}."); + + $user = new \App\User(); + $user->email = $email; + $user->password = $this->option('password'); + $user->role = 'reseller'; + $user->tenant_id = $tenant->id; + + if ($error = UsersController::validateEmail($email, $user)) { + $this->error("{$email}: {$error}"); + return 1; + } + + $user->save(); + $this->info("Created user {$user->id}."); + + $tenant->setSettings([ + "app.name" => $this->option("title"), + "app.url" => $this->argument("domain"), + "app.public_url" => "https://" . $this->argument("domain"), + "app.support_url" => "https://" . $this->argument("domain") . "/support", + "mail.sender.address" => "noreply@" . $this->argument("domain"), + "mail.sender.name" => $this->option("title"), + "mail.replyto.address" => "noreply@" . $this->argument("domain"), + "mail.replyto.name" => $this->option("title"), + ]); + + DB::commit(); + + $this->info("Applied default tenant settings."); } } diff --git a/src/tests/Feature/Console/Tenant/CreateTest.php b/src/tests/Feature/Console/Tenant/CreateTest.php --- a/src/tests/Feature/Console/Tenant/CreateTest.php +++ b/src/tests/Feature/Console/Tenant/CreateTest.php @@ -16,7 +16,10 @@ { parent::setUp(); - $this->deleteTestUser('test-tenant@kolabnow.com'); + $this->deleteTestUser('unknown@user.com'); + $this->deleteTestDomain('tenant.com'); + $this->deleteTestDomain('user.com'); + \App\Tenant::where('title', 'Test Tenant')->delete(); } /** @@ -31,8 +34,10 @@ \App\Plan::where('tenant_id', $this->tenantId)->delete(); \App\Package::where('tenant_id', $this->tenantId)->delete(); \App\Sku::where('tenant_id', $this->tenantId)->delete(); + \App\Domain::where('tenant_id', $this->tenantId)->delete(); \App\Tenant::find($this->tenantId)->delete(); } + $this->deleteTestUser('unknown@user.com'); parent::tearDown(); } @@ -47,31 +52,28 @@ // Warning: We're not using artisan() here, as this will not // allow us to test "empty output" cases - // User not existing - $code = \Artisan::call("tenant:create unknown@user.com"); + // User already existing + $user = $this->getTestUser('unknown@user.com'); + $code = \Artisan::call("tenant:create {$user->email} user.com --title=\"Test Tenant\""); $output = trim(\Artisan::output()); $this->assertSame(1, $code); - $this->assertSame("User not found.", $output); - - $user = $this->getTestUser('test-tenant@kolabnow.com'); - $this->assertEmpty($user->role); - $this->assertEquals($user->tenant_id, \config('app.tenant_id')); - - // Existing user - $code = \Artisan::call("tenant:create {$user->email} --title=\"Test Tenant\""); + // User not existing + $code = \Artisan::call("tenant:create test-tenant@tenant.com tenant.com --title=\"Test Tenant\""); $output = trim(\Artisan::output()); $this->assertSame(0, $code); $this->assertMatchesRegularExpression("/^Created tenant [0-9]+./", $output); - preg_match("/^Created tenant ([0-9]+)./", $output, $matches); + preg_match("/Created tenant ([0-9]+)./", $output, $matches); $this->tenantId = $matches[1]; - $tenant = \App\Tenant::find($this->tenantId); - $user->refresh(); - $this->assertNotEmpty($tenant); $this->assertSame('Test Tenant', $tenant->title); + + preg_match("/Created user ([0-9]+)./", $output, $matches); + $userId = $matches[1]; + $user = \App\User::find($userId); + $this->assertNotEmpty($user); $this->assertSame('reseller', $user->role); $this->assertSame($tenant->id, $user->tenant_id);