From febf8a5f18a558e8d1a0ec4c748d348de726fcf4 Mon Sep 17 00:00:00 2001
From: Marcus Eibrink-Lunzenauer <lunzenauer@elan-ev.de>
Date: Wed, 23 Oct 2024 05:02:17 +0000
Subject: [PATCH] Add `--confirmation`/`--no-confirmation` to read password
 from stdin.

Closes #3910

Merge request studip/studip!2766
---
 cli/Commands/User/ChangePassword.php | 38 +++++++++++++++++++---------
 1 file changed, 26 insertions(+), 12 deletions(-)

diff --git a/cli/Commands/User/ChangePassword.php b/cli/Commands/User/ChangePassword.php
index f9763f94398..00de33f1ad2 100644
--- a/cli/Commands/User/ChangePassword.php
+++ b/cli/Commands/User/ChangePassword.php
@@ -29,6 +29,13 @@ class ChangePassword extends Command
             InputArgument::REQUIRED,
             'The username of the user whose password will be changed.'
         );
+        $this->addOption(
+            'confirmation',
+            null,
+            InputOption::VALUE_NEGATABLE,
+            'Enter password twice to confirm it',
+            true
+        );
     }
 
     protected function execute(InputInterface $input, OutputInterface $output)
@@ -41,24 +48,33 @@ class ChangePassword extends Command
             return Command::FAILURE;
         }
 
+        $passwords = $this->askPassword($input, $output);
+        $status = $this->changePassword($user, ...$passwords);
+        if (isset($status)) {
+            $output->writeln('<error>' . $status . '</error>');
+
+            return Command::FAILURE;
+        }
+
+        return Command::SUCCESS;
+    }
+
+    /**
+     * @return array{string,string} a pair of strings containing the password and its confirmation
+     */
+    private function askPassword(InputInterface $input, OutputInterface $output): array
+    {
         $helper = $this->getHelper('question');
 
         $question = new Question('New password: ', '');
         $question->setHidden(true);
-        $password = $helper->ask($input, $output, $question);
-
         $question2 = new Question('Re-type password: ', '');
         $question2->setHidden(true);
-        $password2 = $helper->ask($input, $output, $question2);
-
-        $status = $this->changePassword($user, $password, $password2);
-        if (isset($status)) {
-            $output->writeln('<error>' . $status . '</error>');
 
-            return Command::FAILURE;
-        }
+        $password = $helper->ask($input, $output, $question);
+        $password2 = $input->getOption('confirmation') ? $helper->ask($input, $output, $question2) : $password;
 
-        return Command::SUCCESS;
+        return [$password, $password2];
     }
 
     private function changePassword(\User $user, string $password, string $password2): ?string
@@ -88,8 +104,6 @@ class ChangePassword extends Command
             return 'The password could not be set.';
         }
 
-        StudipLog::USER_NEWPWD($user->id, null, 'Passwort neu gesetzt', null, $user->id);
-
         return null;
     }
 }
-- 
GitLab