Skip to content
Snippets Groups Projects
Commit 13178db4 authored by Thomas Müller's avatar Thomas Müller
Browse files

Merge pull request #15404 from Crote/occ-password-from-env

Add password input from env variable for occ user:{add, resetpassword}
parents 20eaadd7 d9323ca1
No related branches found
No related tags found
No related merge requests found
...@@ -58,10 +58,10 @@ class Add extends Command { ...@@ -58,10 +58,10 @@ class Add extends Command {
'User ID used to login (must only contain a-z, A-Z, 0-9, -, _ and @)' 'User ID used to login (must only contain a-z, A-Z, 0-9, -, _ and @)'
) )
->addOption( ->addOption(
'password', 'password-from-env',
'p', null,
InputOption::VALUE_OPTIONAL, InputOption::VALUE_NONE,
'' 'read password from environment variable OC_PASS'
) )
->addOption( ->addOption(
'display-name', 'display-name',
...@@ -84,14 +84,33 @@ class Add extends Command { ...@@ -84,14 +84,33 @@ class Add extends Command {
return 1; return 1;
} }
$password = $input->getOption('password'); if ($input->getOption('password-from-env')) {
while (!$password) { $password = getenv('OC_PASS');
$question = new Question('Please enter a non-empty password:'); if (!$password) {
$question->setHidden(true); $output->writeln('<error>--password-from-env given, but OC_PASS is empty!</error>');
$question->setHiddenFallback(false); return 1;
}
} elseif ($input->isInteractive()) {
/** @var $dialog \Symfony\Component\Console\Helper\DialogHelper */
$dialog = $this->getHelperSet()->get('dialog');
$password = $dialog->askHiddenResponse(
$output,
'<question>Enter password: </question>',
false
);
$confirm = $dialog->askHiddenResponse(
$output,
'<question>Confirm password: </question>',
false
);
$helper = $this->getHelper('question'); if ($password !== $confirm) {
$password = $helper->ask($input, $output, $question); $output->writeln("<error>Passwords did not match!</error>");
return 1;
}
} else {
$output->writeln("<error>Interactive input or --password-from-env is needed for entering a password!</error>");
return 1;
} }
$user = $this->userManager->createUser( $user = $this->userManager->createUser(
......
...@@ -26,6 +26,7 @@ namespace OC\Core\Command\User; ...@@ -26,6 +26,7 @@ namespace OC\Core\Command\User;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
class ResetPassword extends Command { class ResetPassword extends Command {
...@@ -47,6 +48,12 @@ class ResetPassword extends Command { ...@@ -47,6 +48,12 @@ class ResetPassword extends Command {
InputArgument::REQUIRED, InputArgument::REQUIRED,
'Username to reset password' 'Username to reset password'
) )
->addOption(
'password-from-env',
null,
InputOption::VALUE_NONE,
'read password from environment variable OC_PASS'
)
; ;
} }
...@@ -60,7 +67,13 @@ class ResetPassword extends Command { ...@@ -60,7 +67,13 @@ class ResetPassword extends Command {
return 1; return 1;
} }
if ($input->isInteractive()) { if ($input->getOption('password-from-env')) {
$password = getenv('OC_PASS');
if (!$password) {
$output->writeln('<error>--password-from-env given, but OC_PASS is empty!</error>');
return 1;
}
} elseif ($input->isInteractive()) {
/** @var $dialog \Symfony\Component\Console\Helper\DialogHelper */ /** @var $dialog \Symfony\Component\Console\Helper\DialogHelper */
$dialog = $this->getHelperSet()->get('dialog'); $dialog = $this->getHelperSet()->get('dialog');
...@@ -84,20 +97,20 @@ class ResetPassword extends Command { ...@@ -84,20 +97,20 @@ class ResetPassword extends Command {
false false
); );
if ($password === $confirm) { if ($password !== $confirm) {
$success = $user->setPassword($password); $output->writeln("<error>Passwords did not match!</error>");
if ($success) {
$output->writeln("<info>Successfully reset password for " . $username . "</info>");
} else {
$output->writeln("<error>Error while resetting password!</error>");
return 1; return 1;
} }
} else { } else {
$output->writeln("<error>Passwords did not match!</error>"); $output->writeln("<error>Interactive input or --password-from-env is needed for entering a new password!</error>");
return 1; return 1;
} }
$success = $user->setPassword($password);
if ($success) {
$output->writeln("<info>Successfully reset password for " . $username . "</info>");
} else { } else {
$output->writeln("<error>Interactive input is needed for entering a new password!</error>"); $output->writeln("<error>Error while resetting password!</error>");
return 1; return 1;
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment