From 2e7b4a377d2697a054a3d3e247acc59144fd7ff0 Mon Sep 17 00:00:00 2001 From: Anupam Kumar Date: Sat, 30 Sep 2023 10:47:59 +0530 Subject: [PATCH] new user password email option, improved on #29368 Signed-off-by: Anupam Kumar --- core/Command/User/Add.php | 149 +++++++++------------------- tests/Core/Command/User/AddTest.php | 1 - 2 files changed, 45 insertions(+), 105 deletions(-) diff --git a/core/Command/User/Add.php b/core/Command/User/Add.php index b670756c7aef1..dcb414cd429ff 100644 --- a/core/Command/User/Add.php +++ b/core/Command/User/Add.php @@ -7,6 +7,7 @@ * @author Joas Schilling * @author Laurens Post * @author Roeland Jago Douma + * @author Anupam Kumar * * @license AGPL-3.0 * @@ -46,63 +47,16 @@ use Symfony\Component\Console\Question\Question; class Add extends Command { - /** - * @var IUserManager - */ - protected $userManager; - - /** - * @var IGroupManager - */ - protected $groupManager; - - /** - * @var EmailValidator - */ - protected $emailValidator; - - /** - * @var IConfig - */ - private $config; - - /** - * @var NewUserMailHelper - */ - private $mailHelper; - - /** - * @var IEventDispatcher - */ - private $eventDispatcher; - - /** - * @var ISecureRandom - */ - private $secureRandom; - - /** - * @param IUserManager $userManager - * @param IGroupManager $groupManager - * @param EmailValidator $emailValidator - */ public function __construct( - IUserManager $userManager, - IGroupManager $groupManager, - EmailValidator $emailValidator, - IConfig $config, - NewUserMailHelper $mailHelper, - IEventDispatcher $eventDispatcher, - ISecureRandom $secureRandom + protected IUserManager $userManager, + protected IGroupManager $groupManager, + protected EmailValidator $emailValidator, + private IConfig $config, + private NewUserMailHelper $mailHelper, + private IEventDispatcher $eventDispatcher, + private ISecureRandom $secureRandom ) { parent::__construct(); - $this->userManager = $userManager; - $this->groupManager = $groupManager; - $this->emailValidator = $emailValidator; - $this->config = $config; - $this->mailHelper = $mailHelper; - $this->eventDispatcher = $eventDispatcher; - $this->secureRandom = $secureRandom; } protected function configure() { @@ -142,16 +96,14 @@ protected function configure() { protected function execute(InputInterface $input, OutputInterface $output): int { $uid = $input->getArgument('uid'); - $emailIsSet = \is_string($input->getOption('email')) && \mb_strlen($input->getOption('email')) > 0; - $emailIsValid = $this->emailValidator->isValid($input->getOption('email') ?? '', new RFCValidation()); - $password = ''; - $temporaryPassword = ''; - if ($this->userManager->userExists($uid)) { $output->writeln('The user "' . $uid . '" already exists.'); return 1; } + $password = ''; + $sendPasswordEmail = false; + if ($input->getOption('password-from-env')) { $password = getenv('OC_PASS'); @@ -159,6 +111,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int $output->writeln('--password-from-env given, but OC_PASS is empty!'); return 1; } + } elseif (\mb_strlen($input->getOption('email')) > 0) { + if (!$this->emailValidator->isValid($input->getOption('email') ?? '', new RFCValidation())) { + $output->writeln(\sprintf( + 'The given E-Mail address "%s" is invalid: %s', + $input->getOption('email'), + $this->emailValidator->getError()->description() + )); + + return 1; + } + + $output->writeln('Setting a temporary password.'); + + $passwordEvent = new GenerateSecurePasswordEvent(); + $this->eventDispatcher->dispatchTyped($passwordEvent); + $password = $passwordEvent->getPassword() ?? $this->secureRandom->generate(20); + + $sendPasswordEmail = true; } elseif ($input->isInteractive()) { /** @var QuestionHelper $helper */ $helper = $this->getHelper('question'); @@ -180,26 +150,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 1; } - if (trim($password) === '' && $emailIsSet) { - if ($emailIsValid) { - $output->writeln('Setting a temporary password.'); - - $temporaryPassword = $this->getTemporaryPassword(); - } else { - $output->writeln(\sprintf( - 'The given E-Mail address "%s" is invalid: %s', - $input->getOption('email'), - $this->emailValidator->getError()->description() - )); - - return 1; - } - } - try { $user = $this->userManager->createUser( $input->getArgument('uid'), - $password ?: $temporaryPassword + $password, ); } catch (\Exception $e) { $output->writeln('' . $e->getMessage() . ''); @@ -215,24 +169,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int if ($input->getOption('display-name')) { $user->setDisplayName($input->getOption('display-name')); - $output->writeln(sprintf('Display name set to "%s"', $user->getDisplayName())); - } - - if ($emailIsSet && $emailIsValid) { - $user->setSystemEMailAddress($input->getOption('email')); - $output->writeln(sprintf('E-Mail set to "%s"', (string) $user->getSystemEMailAddress())); - - if (trim($password) === '' && $this->config->getAppValue('core', 'newUser.sendEmail', 'yes') === 'yes') { - try { - $this->mailHelper->sendMail( - $user, - $this->mailHelper->generateTemplate($user, true) - ); - $output->writeln('Invitation E-Mail sent.'); - } catch (\Exception $e) { - $output->writeln(\sprintf('Unable to send the invitation mail to %s', $user->getEMailAddress())); - } - } + $output->writeln(\sprintf('Display name set to "%s"', $user->getDisplayName())); } $groups = $input->getOption('group'); @@ -257,18 +194,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int $output->writeln('User "' . $user->getUID() . '" added to group "' . $group->getGID() . '"'); } } - return 0; - } - /** - * @return string - */ - protected function getTemporaryPassword(): string - { - $passwordEvent = new GenerateSecurePasswordEvent(); + // Send email to user if we set a temporary password + if ($sendPasswordEmail) { + $email = $input->getOption('email'); + $user->setSystemEMailAddress($email); - $this->eventDispatcher->dispatchTyped($passwordEvent); + if ($this->config->getAppValue('core', 'newUser.sendEmail', 'yes') === 'yes') { + try { + $this->mailHelper->sendMail($user, $this->mailHelper->generateTemplate($user, true)); + $output->writeln(\sprintf('Invitation E-Mail sent to %s', $email)); + } catch (\Exception $e) { + $output->writeln(\sprintf('Unable to send the invitation mail to %s', $email)); + } + } + } - return $passwordEvent->getPassword() ?? $this->secureRandom->generate(20); + return 0; } } diff --git a/tests/Core/Command/User/AddTest.php b/tests/Core/Command/User/AddTest.php index 1445e3cfec6b2..4e6305153fc19 100644 --- a/tests/Core/Command/User/AddTest.php +++ b/tests/Core/Command/User/AddTest.php @@ -40,7 +40,6 @@ use Test\TestCase; class AddTest extends TestCase { - /** * @dataProvider addEmailDataProvider */