From bd965b3684940e5a5cc1e616faa9b68c64f7692f Mon Sep 17 00:00:00 2001
From: Benjamin Franzke <ben@bnf.dev>
Date: Mon, 11 Mar 2024 10:19:55 +0100
Subject: [PATCH] [TASK] Make admin user creation optional in CLI installer
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Improve the CLI command `setup` to allow instance creation
without enforcing admin user and password to be defined.

This enables to create tiny test setups where no admin user is
needed or where users are imported from fixtures.

Resolves: #103358
Releases: main, 12.4
Change-Id: Idb5e309eb873ade7fee153a1e9b1398e4e99bfb1
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/83397
Reviewed-by: Benjamin Franzke <ben@bnf.dev>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Stefan Bürk <stefan@buerk.tech>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: core-ci <typo3@b13.com>
Reviewed-by: Stefan Bürk <stefan@buerk.tech>
Tested-by: Benjamin Franzke <ben@bnf.dev>
---
 .../install/Classes/Command/SetupCommand.php  | 22 +++++++++++++------
 1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/typo3/sysext/install/Classes/Command/SetupCommand.php b/typo3/sysext/install/Classes/Command/SetupCommand.php
index aaec25928d3c..5654d25aaf67 100644
--- a/typo3/sysext/install/Classes/Command/SetupCommand.php
+++ b/typo3/sysext/install/Classes/Command/SetupCommand.php
@@ -22,6 +22,7 @@ use Symfony\Component\Console\Command\Command;
 use Symfony\Component\Console\Helper\QuestionHelper;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\ConsoleOutputInterface;
 use Symfony\Component\Console\Output\OutputInterface;
 use Symfony\Component\Console\Question\ChoiceQuestion;
 use Symfony\Component\Console\Question\ConfirmationQuestion;
@@ -242,9 +243,14 @@ EOT
 
         $username = $this->getAdminUserName($questionHelper, $input, $output);
         $password = $this->getAdminUserPassword($questionHelper, $input, $output);
-        $email = $this->getAdminEmailAddress($questionHelper, $input, $output);
-        $this->setupService->createUser($username, $password, $email);
-        $this->setupService->setInstallToolPassword($password);
+        if ($password !== null) {
+            $email = $this->getAdminEmailAddress($questionHelper, $input, $output);
+            $this->setupService->createUser($username, $password, $email);
+            $this->setupService->setInstallToolPassword($password);
+        } elseif ($output->isVerbose()) {
+            $errOutput = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
+            $errOutput->writeln('<info>No admin password defined. Skipped user creation.</info>');
+        }
 
         $siteName = $this->getProjectName($questionHelper, $input, $output);
         $this->setupService->setSiteName($siteName);
@@ -544,7 +550,7 @@ EOT
         return $usernameValidator($usernameFromCli);
     }
 
-    protected function getAdminUserPassword(QuestionHelper $questionHelper, InputInterface $input, OutputInterface $output): string
+    protected function getAdminUserPassword(QuestionHelper $questionHelper, InputInterface $input, OutputInterface $output): ?string
     {
         $passwordValidator = function ($password) {
             $passwordValidationErrors = $this->setupDatabaseService->getBackendUserPasswordValidationErrors((string)$password);
@@ -560,9 +566,7 @@ EOT
         };
 
         $passwordFromCli = $this->getFallbackValueEnvOrOption($input, 'admin-user-password', 'TYPO3_SETUP_ADMIN_PASSWORD');
-        if ($passwordFromCli === false) {
-            // Force this question if `TYPO3_SETUP_ADMIN_PASSWORD` is not set via cli.
-            // Thus, the user will always be prompted for a password even --no-interaction is set.
+        if ($passwordFromCli === false && $input->isInteractive()) {
             $currentlyInteractive = $input->isInteractive();
             $input->setInteractive(true);
             $questionPassword = new Question('Admin user and installer password ? ');
@@ -575,6 +579,10 @@ EOT
             return $password;
         }
 
+        if ($passwordFromCli === false) {
+            return null;
+        }
+
         return $passwordValidator($passwordFromCli);
     }
 
-- 
GitLab