diff options
author | Philip Gatzka <philip.gatzka@mailbox.org> | 2021-10-16 13:27:28 +0200 |
---|---|---|
committer | Anupam Kumar <kyteinsky@gmail.com> | 2024-02-24 04:56:52 +0530 |
commit | b587ec39f426a50c1ff28a091ba1c5e910bd02dd (patch) | |
tree | 334d8a67920a9fbb851e1fbe1e96bd5dc1bfb47f /core/Command/User | |
parent | eef571e4fb203e255d3bdb2a79737b0d87a79a2b (diff) | |
download | nextcloud-server-b587ec39f426a50c1ff28a091ba1c5e910bd02dd.tar.gz nextcloud-server-b587ec39f426a50c1ff28a091ba1c5e910bd02dd.zip |
Enable adding E-Mail addresses to new user accounts using the CLI
Signed-off-by: Philip Gatzka <philip.gatzka@mailbox.org>
Diffstat (limited to 'core/Command/User')
-rw-r--r-- | core/Command/User/Add.php | 125 |
1 files changed, 120 insertions, 5 deletions
diff --git a/core/Command/User/Add.php b/core/Command/User/Add.php index 1e34b435e93..2a47b8015a2 100644 --- a/core/Command/User/Add.php +++ b/core/Command/User/Add.php @@ -25,11 +25,18 @@ */ namespace OC\Core\Command\User; +use Egulias\EmailValidator\EmailValidator; +use Egulias\EmailValidator\Validation\RFCValidation; use OC\Files\Filesystem; +use OCA\Settings\Mailer\NewUserMailHelper; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\IConfig; use OCP\IGroup; use OCP\IGroupManager; use OCP\IUser; use OCP\IUserManager; +use OCP\Security\Events\GenerateSecurePasswordEvent; +use OCP\Security\ISecureRandom; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Helper\QuestionHelper; use Symfony\Component\Console\Input\InputArgument; @@ -39,11 +46,63 @@ use Symfony\Component\Console\Output\OutputInterface; 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( - protected IUserManager $userManager, - protected IGroupManager $groupManager, + IUserManager $userManager, + IGroupManager $groupManager, + EmailValidator $emailValidator, + IConfig $config, + NewUserMailHelper $mailHelper, + IEventDispatcher $eventDispatcher, + 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() { @@ -72,11 +131,22 @@ class Add extends Command { 'g', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'groups the account should be added to (The group will be created if it does not exist)' + ) + ->addOption( + 'email', + null, + InputOption::VALUE_REQUIRED, + 'When set, users may register using the default E-Mail verification workflow' ); } 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('<error>The account "' . $uid . '" already exists.</error>'); return 1; @@ -84,6 +154,7 @@ class Add extends Command { 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; @@ -109,17 +180,32 @@ class Add extends Command { return 1; } + if (trim($password) === '' && $emailIsSet) { + if ($emailIsValid) { + $output->writeln('Setting a temporary password.'); + + $temporaryPassword = $this->getTemporaryPassword(); + } else { + $output->writeln(\sprintf( + '<error>The given E-Mail address "%s" is invalid: %s</error>', + $input->getOption('email'), + $this->emailValidator->getError()->description() + )); + + return 1; + } + } + try { $user = $this->userManager->createUser( $input->getArgument('uid'), - $password + $password ?: $temporaryPassword ); } catch (\Exception $e) { $output->writeln('<error>' . $e->getMessage() . '</error>'); return 1; } - if ($user instanceof IUser) { $output->writeln('<info>The account "' . $user->getUID() . '" was created successfully</info>'); } else { @@ -129,7 +215,24 @@ class Add extends Command { if ($input->getOption('display-name')) { $user->setDisplayName($input->getOption('display-name')); - $output->writeln('Display name set to "' . $user->getDisplayName() . '"'); + $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())); + } + } } $groups = $input->getOption('group'); @@ -156,4 +259,16 @@ class Add extends Command { } return 0; } + + /** + * @return string + */ + protected function getTemporaryPassword(): string + { + $passwordEvent = new GenerateSecurePasswordEvent(); + + $this->eventDispatcher->dispatchTyped($passwordEvent); + + return $passwordEvent->getPassword() ?? $this->secureRandom->generate(20); + } } |