|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright Copyright (c) 2023 FedericoHeichou <federicoheichou@gmail.com> |
| 5 | + * |
| 6 | + * @author FedericoHeichou <federicoheichou@gmail.com> |
| 7 | + * |
| 8 | + * @license GNU AGPL version 3 or any later version |
| 9 | + * |
| 10 | + * This program is free software: you can redistribute it and/or modify |
| 11 | + * it under the terms of the GNU Affero General Public License as |
| 12 | + * published by the Free Software Foundation, either version 3 of the |
| 13 | + * License, or (at your option) any later version. |
| 14 | + * |
| 15 | + * This program is distributed in the hope that it will be useful, |
| 16 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + * GNU Affero General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU Affero General Public License |
| 21 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | + * |
| 23 | + */ |
| 24 | + |
| 25 | +namespace OC\Core\Command\User; |
| 26 | + |
| 27 | +use OC\Core\Command\Base; |
| 28 | +use OCA\Settings\Mailer\NewUserMailHelper; |
| 29 | +use OCP\IUserManager; |
| 30 | +use Symfony\Component\Console\Input\InputArgument; |
| 31 | +use Symfony\Component\Console\Input\InputInterface; |
| 32 | +use Symfony\Component\Console\Input\InputOption; |
| 33 | +use Symfony\Component\Console\Output\OutputInterface; |
| 34 | + |
| 35 | +class Welcome extends Base { |
| 36 | + /** @var IUserManager */ |
| 37 | + protected $userManager; |
| 38 | + |
| 39 | + /** @var NewUserMailHelper */ |
| 40 | + private $newUserMailHelper; |
| 41 | + |
| 42 | + /** |
| 43 | + * @param IUserManager $userManager |
| 44 | + * @param NewUserMailHelper $newUserMailHelper |
| 45 | + */ |
| 46 | + public function __construct( |
| 47 | + IUserManager $userManager, |
| 48 | + NewUserMailHelper $newUserMailHelper |
| 49 | + ) { |
| 50 | + parent::__construct(); |
| 51 | + |
| 52 | + $this->userManager = $userManager; |
| 53 | + $this->newUserMailHelper = $newUserMailHelper; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @return void |
| 58 | + */ |
| 59 | + protected function configure() { |
| 60 | + $this |
| 61 | + ->setName('user:welcome') |
| 62 | + ->setDescription('Sends the welcome email') |
| 63 | + ->addArgument( |
| 64 | + 'user', |
| 65 | + InputArgument::REQUIRED, |
| 66 | + 'The user to send the email to' |
| 67 | + ) |
| 68 | + ->addOption( |
| 69 | + 'reset-password', |
| 70 | + 'r', |
| 71 | + InputOption::VALUE_NONE, |
| 72 | + 'Add the reset password link to the email' |
| 73 | + ) |
| 74 | + ; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @param InputInterface $input |
| 79 | + * @param OutputInterface $output |
| 80 | + * @return int |
| 81 | + */ |
| 82 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 83 | + $userId = $input->getArgument('user'); |
| 84 | + // check if user exists |
| 85 | + $user = $this->userManager->get($userId); |
| 86 | + if ($user === null) { |
| 87 | + $output->writeln('<error>User does not exist</error>'); |
| 88 | + return 1; |
| 89 | + } |
| 90 | + $email = $user->getEMailAddress(); |
| 91 | + if ($email === '' || $email === null) { |
| 92 | + $output->writeln('<error>User does not have an email address</error>'); |
| 93 | + return 1; |
| 94 | + } |
| 95 | + try { |
| 96 | + $emailTemplate = $this->newUserMailHelper->generateTemplate($user, $input->getOption('reset-password')); |
| 97 | + $this->newUserMailHelper->sendMail($user, $emailTemplate); |
| 98 | + } catch (\Exception $e) { |
| 99 | + $output->writeln('<error>Failed to send email: ' . $e->getMessage() . '</error>'); |
| 100 | + return 1; |
| 101 | + } |
| 102 | + return 0; |
| 103 | + } |
| 104 | +} |
0 commit comments