Skip to content

Commit 3607976

Browse files
Merge pull request #51731 from nextcloud/backport/39611/stable30
[stable30] Add `occ user:welcome` command to send user welcome email from CLI
2 parents 345755c + 1ab5955 commit 3607976

4 files changed

Lines changed: 89 additions & 0 deletions

File tree

core/Command/User/Welcome.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
}

core/register_command.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
$application->add(Server::get(Command\User\AuthTokens\ListCommand::class));
123123
$application->add(Server::get(Command\User\AuthTokens\Delete::class));
124124
$application->add(Server::get(Command\User\Keys\Verify::class));
125+
$application->add(Server::get(Command\User\Welcome::class));
125126

126127
$application->add(Server::get(Command\Group\Add::class));
127128
$application->add(Server::get(Command\Group\Delete::class));

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,7 @@
12481248
'OC\\Core\\Command\\User\\ResetPassword' => $baseDir . '/core/Command/User/ResetPassword.php',
12491249
'OC\\Core\\Command\\User\\Setting' => $baseDir . '/core/Command/User/Setting.php',
12501250
'OC\\Core\\Command\\User\\SyncAccountDataCommand' => $baseDir . '/core/Command/User/SyncAccountDataCommand.php',
1251+
'OC\\Core\\Command\\User\\Welcome' => $baseDir . '/core/Command/User/Welcome.php',
12511252
'OC\\Core\\Controller\\AppPasswordController' => $baseDir . '/core/Controller/AppPasswordController.php',
12521253
'OC\\Core\\Controller\\AutoCompleteController' => $baseDir . '/core/Controller/AutoCompleteController.php',
12531254
'OC\\Core\\Controller\\AvatarController' => $baseDir . '/core/Controller/AvatarController.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
12811281
'OC\\Core\\Command\\User\\ResetPassword' => __DIR__ . '/../../..' . '/core/Command/User/ResetPassword.php',
12821282
'OC\\Core\\Command\\User\\Setting' => __DIR__ . '/../../..' . '/core/Command/User/Setting.php',
12831283
'OC\\Core\\Command\\User\\SyncAccountDataCommand' => __DIR__ . '/../../..' . '/core/Command/User/SyncAccountDataCommand.php',
1284+
'OC\\Core\\Command\\User\\Welcome' => __DIR__ . '/../../..' . '/core/Command/User/Welcome.php',
12841285
'OC\\Core\\Controller\\AppPasswordController' => __DIR__ . '/../../..' . '/core/Controller/AppPasswordController.php',
12851286
'OC\\Core\\Controller\\AutoCompleteController' => __DIR__ . '/../../..' . '/core/Controller/AutoCompleteController.php',
12861287
'OC\\Core\\Controller\\AvatarController' => __DIR__ . '/../../..' . '/core/Controller/AvatarController.php',

0 commit comments

Comments
 (0)