From: Dan Brown Date: Thu, 7 Aug 2025 12:16:49 +0000 (+0100) Subject: Commands: Updated create admin skip return X-Git-Url: http://source.bookstackapp.com/bookstack/commitdiff_plain/refs/heads/admin_command_updates Commands: Updated create admin skip return Return status for skipped --initial creation will now return 2, so that it can be identified seperate from a creation and from an error. --- diff --git a/app/Console/Commands/CreateAdminCommand.php b/app/Console/Commands/CreateAdminCommand.php index 520f0822a..bf72553f7 100644 --- a/app/Console/Commands/CreateAdminCommand.php +++ b/app/Console/Commands/CreateAdminCommand.php @@ -60,7 +60,7 @@ class CreateAdminCommand extends Command if ($initialAdminOnly) { $handled = $this->handleInitialAdminIfExists($userRepo, $details, $shouldGeneratePassword, $adminRole); if ($handled !== null) { - return $handled ? 0 : 1; + return $handled; } } @@ -87,16 +87,16 @@ class CreateAdminCommand extends Command /** * Handle updates to the original admin account if it exists. - * Returns true if it's been successfully handled, false if unsuccessful, or null if not handled. + * Returns an int return status if handled, otherwise returns null if not handled (new user to be created). */ - protected function handleInitialAdminIfExists(UserRepo $userRepo, array $data, bool $generatePassword, Role $adminRole): bool|null + protected function handleInitialAdminIfExists(UserRepo $userRepo, array $data, bool $generatePassword, Role $adminRole): int|null { $defaultAdmin = $userRepo->getByEmail('admin@admin.com'); if ($defaultAdmin && $defaultAdmin->hasSystemRole('admin')) { if ($defaultAdmin->email !== $data['email'] && $userRepo->getByEmail($data['email']) !== null) { $this->error("Could not create admin account."); $this->error("An account with the email address \"{$data['email']}\" already exists."); - return false; + return 1; } $userRepo->updateWithoutActivity($defaultAdmin, $data, true); @@ -106,10 +106,10 @@ class CreateAdminCommand extends Command $this->info("The default admin user has been updated with the provided details!"); } - return true; + return 0; } else if ($adminRole->users()->count() > 0) { $this->warn('Non-default admin user already exists. Skipping creation of new admin user.'); - return true; + return 2; } return null; diff --git a/tests/Commands/CreateAdminCommandTest.php b/tests/Commands/CreateAdminCommandTest.php index fa1329ad3..f389dd942 100644 --- a/tests/Commands/CreateAdminCommandTest.php +++ b/tests/Commands/CreateAdminCommandTest.php @@ -109,7 +109,7 @@ class CreateAdminCommandTest extends TestCase '--password' => 'testing-7', '--initial' => true, ])->expectsOutput('Non-default admin user already exists. Skipping creation of new admin user.') - ->assertExitCode(0); + ->assertExitCode(2); $defaultAdmin->refresh(); @@ -156,7 +156,7 @@ class CreateAdminCommandTest extends TestCase '--password' => 'testing-7', '--initial' => true, ])->expectsOutput("Non-default admin user already exists. Skipping creation of new admin user.") - ->assertExitCode(0); + ->assertExitCode(2); } public function test_initial_option_creation_errors_if_email_already_exists()