From 10a9ae4553e44ff8b3242f9585399e36ddc0eea9 Mon Sep 17 00:00:00 2001 From: Boen_Shi Date: Wed, 20 May 2026 11:59:43 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E5=88=9D=E5=A7=8B=E5=8C=96=E5=AE=89?= =?UTF-8?q?=E8=A3=85):=20=E6=96=B0=E5=A2=9E=E5=88=9D=E5=A7=8B=E5=8C=96?= =?UTF-8?q?=E5=AE=89=E8=A3=85=E7=9A=84=E8=84=9A=E6=9C=AC=EF=BC=8C=E4=B8=80?= =?UTF-8?q?=E9=94=AE=E8=BF=81=E7=A7=BB=E6=95=B0=E6=8D=AE=E5=BA=93=EF=BC=8C?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E5=9F=BA=E7=A1=80=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0=E9=BB=98=E8=AE=A4=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E5=91=98=E8=B4=A6=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Commands/InstallApplicationCommand.php | 87 +++++++++++++++++++ .../Feature/InstallApplicationCommandTest.php | 46 ++++++++++ 2 files changed, 133 insertions(+) create mode 100644 app/Console/Commands/InstallApplicationCommand.php create mode 100644 tests/Feature/InstallApplicationCommandTest.php diff --git a/app/Console/Commands/InstallApplicationCommand.php b/app/Console/Commands/InstallApplicationCommand.php new file mode 100644 index 0000000..e74dd96 --- /dev/null +++ b/app/Console/Commands/InstallApplicationCommand.php @@ -0,0 +1,87 @@ +components->info('Starting application installation...'); + + $migrationExitCode = $this->call( + $this->option('fresh') ? 'migrate:fresh' : 'migrate', + ['--force' => (bool) $this->option('force')] + ); + + if ($migrationExitCode !== self::SUCCESS) { + $this->error('Database migration failed.'); + + return self::FAILURE; + } + + $rbacExitCode = $this->call('user:manage', [ + 'action' => 'init-rbac', + ]); + + if ($rbacExitCode !== self::SUCCESS) { + $this->error('Default RBAC initialization failed.'); + + return self::FAILURE; + } + + $adminEmail = trim((string) $this->option('admin-email')); + $adminPhone = trim((string) $this->option('admin-phone')); + $adminNickname = trim((string) $this->option('admin-nickname')); + $adminPassword = (string) $this->option('admin-password'); + + if ($adminEmail === '' || $adminNickname === '' || $adminPassword === '') { + $this->error('admin-email, admin-nickname and admin-password are required.'); + + return self::FAILURE; + } + + $adminUser = User::query()->updateOrCreate( + ['email' => $adminEmail], + [ + 'nickname' => $adminNickname, + 'phone' => $adminPhone !== '' ? $adminPhone : null, + 'password' => $adminPassword, + 'force_password_change' => false, + ] + ); + + $setAdminExitCode = $this->call('user:manage', [ + 'action' => 'set-admin', + '--email' => $adminUser->email, + ]); + + if ($setAdminExitCode !== self::SUCCESS) { + $this->error('Failed to set admin role and permissions.'); + + return self::FAILURE; + } + + $this->newLine(); + $this->components->info('Application installed successfully.'); + $this->line("Admin email: {$adminUser->email}"); + if ($adminUser->phone) { + $this->line("Admin phone: {$adminUser->phone}"); + } + $this->line("Admin password: {$adminPassword}"); + + return self::SUCCESS; + } +} diff --git a/tests/Feature/InstallApplicationCommandTest.php b/tests/Feature/InstallApplicationCommandTest.php new file mode 100644 index 0000000..6462203 --- /dev/null +++ b/tests/Feature/InstallApplicationCommandTest.php @@ -0,0 +1,46 @@ +artisan('app:install') + ->assertExitCode(0); + + $this->assertDatabaseHas('permissions', [ + 'name' => 'platform.users.manage', + 'guard_name' => 'api', + ]); + $this->assertDatabaseHas('roles', [ + 'name' => 'admin', + 'guard_name' => 'api', + ]); + $this->assertDatabaseHas('users', [ + 'email' => 'admin@example.com', + 'nickname' => 'admin', + ]); + + $adminUser = User::query()->where('email', 'admin@example.com')->first(); + $this->assertNotNull($adminUser); + $this->assertTrue(Hash::check('admin', (string) $adminUser->password)); + $this->assertTrue($adminUser->hasRole('admin', 'api')); + + $adminRole = Role::query()->where('name', 'admin')->where('guard_name', 'api')->first(); + $this->assertNotNull($adminRole); + $this->assertSame( + Permission::query()->where('guard_name', 'api')->count(), + $adminRole->permissions()->count() + ); + } +}