|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * SPDX-FileCopyrightText: 2023 FedericoHeichou <federicoheichou@gmail.com> |
| 4 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 5 | + */ |
| 6 | + |
| 7 | +namespace OC\Core\Command\User; |
| 8 | + |
| 9 | +use OC\Core\Command\Base; |
| 10 | +use OCA\Settings\Mailer\NewUserMailHelper; |
| 11 | +use OCP\IUserManager; |
| 12 | +use Symfony\Component\Console\Input\InputArgument; |
| 13 | +use Symfony\Component\Console\Input\InputInterface; |
| 14 | +use Symfony\Component\Console\Input\InputOption; |
| 15 | +use Symfony\Component\Console\Output\OutputInterface; |
| 16 | + |
| 17 | +class Welcome extends Base { |
| 18 | + /** @var IUserManager */ |
| 19 | + protected $userManager; |
| 20 | + |
| 21 | + /** @var NewUserMailHelper */ |
| 22 | + private $newUserMailHelper; |
| 23 | + |
| 24 | + /** |
| 25 | + * @param IUserManager $userManager |
| 26 | + * @param NewUserMailHelper $newUserMailHelper |
| 27 | + */ |
| 28 | + public function __construct( |
| 29 | + IUserManager $userManager, |
| 30 | + NewUserMailHelper $newUserMailHelper |
| 31 | + ) { |
| 32 | + parent::__construct(); |
| 33 | + |
| 34 | + $this->userManager = $userManager; |
| 35 | + $this->newUserMailHelper = $newUserMailHelper; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @return void |
| 40 | + */ |
| 41 | + protected function configure() { |
| 42 | + $this |
| 43 | + ->setName('user:welcome') |
| 44 | + ->setDescription('Sends the welcome email') |
| 45 | + ->addArgument( |
| 46 | + 'user', |
| 47 | + InputArgument::REQUIRED, |
| 48 | + 'The user to send the email to' |
| 49 | + ) |
| 50 | + ->addOption( |
| 51 | + 'reset-password', |
| 52 | + 'r', |
| 53 | + InputOption::VALUE_NONE, |
| 54 | + 'Add the reset password link to the email' |
| 55 | + ) |
| 56 | + ; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @param InputInterface $input |
| 61 | + * @param OutputInterface $output |
| 62 | + * @return int |
| 63 | + */ |
| 64 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 65 | + $userId = $input->getArgument('user'); |
| 66 | + // check if user exists |
| 67 | + $user = $this->userManager->get($userId); |
| 68 | + if ($user === null) { |
| 69 | + $output->writeln('<error>User does not exist</error>'); |
| 70 | + return 1; |
| 71 | + } |
| 72 | + $email = $user->getEMailAddress(); |
| 73 | + if ($email === '' || $email === null) { |
| 74 | + $output->writeln('<error>User does not have an email address</error>'); |
| 75 | + return 1; |
| 76 | + } |
| 77 | + try { |
| 78 | + $emailTemplate = $this->newUserMailHelper->generateTemplate($user, $input->getOption('reset-password')); |
| 79 | + $this->newUserMailHelper->sendMail($user, $emailTemplate); |
| 80 | + } catch (\Exception $e) { |
| 81 | + $output->writeln('<error>Failed to send email: ' . $e->getMessage() . '</error>'); |
| 82 | + return 1; |
| 83 | + } |
| 84 | + return 0; |
| 85 | + } |
| 86 | +} |
0 commit comments