Welcome.php 2.1 KB

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