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() + ); + } +}