Skip to content

Commit 7647690

Browse files
feat: Add user:welcome command
Signed-off-by: FedericoHeichou <federicoheichou@gmail.com>
1 parent 007731d commit 7647690

4 files changed

Lines changed: 107 additions & 0 deletions

File tree

core/Command/User/Welcome.php

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

core/register_command.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@
162162
$application->add(Server::get(Command\User\AuthTokens\ListCommand::class));
163163
$application->add(Server::get(Command\User\AuthTokens\Delete::class));
164164
$application->add(Server::get(Command\User\Keys\Verify::class));
165+
$application->add(Server::get(Command\User\Welcome::class));
165166

166167
$application->add(Server::get(Command\Group\Add::class));
167168
$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
@@ -1136,6 +1136,7 @@
11361136
'OC\\Core\\Command\\User\\ResetPassword' => $baseDir . '/core/Command/User/ResetPassword.php',
11371137
'OC\\Core\\Command\\User\\Setting' => $baseDir . '/core/Command/User/Setting.php',
11381138
'OC\\Core\\Command\\User\\SyncAccountDataCommand' => $baseDir . '/core/Command/User/SyncAccountDataCommand.php',
1139+
'OC\\Core\\Command\\User\\Welcome' => $baseDir . '/core/Command/User/Welcome.php',
11391140
'OC\\Core\\Controller\\AppPasswordController' => $baseDir . '/core/Controller/AppPasswordController.php',
11401141
'OC\\Core\\Controller\\AutoCompleteController' => $baseDir . '/core/Controller/AutoCompleteController.php',
11411142
'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
@@ -1177,6 +1177,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
11771177
'OC\\Core\\Command\\User\\ResetPassword' => __DIR__ . '/../../..' . '/core/Command/User/ResetPassword.php',
11781178
'OC\\Core\\Command\\User\\Setting' => __DIR__ . '/../../..' . '/core/Command/User/Setting.php',
11791179
'OC\\Core\\Command\\User\\SyncAccountDataCommand' => __DIR__ . '/../../..' . '/core/Command/User/SyncAccountDataCommand.php',
1180+
'OC\\Core\\Command\\User\\Welcome' => __DIR__ . '/../../..' . '/core/Command/User/Welcome.php',
11801181
'OC\\Core\\Controller\\AppPasswordController' => __DIR__ . '/../../..' . '/core/Controller/AppPasswordController.php',
11811182
'OC\\Core\\Controller\\AutoCompleteController' => __DIR__ . '/../../..' . '/core/Controller/AutoCompleteController.php',
11821183
'OC\\Core\\Controller\\AvatarController' => __DIR__ . '/../../..' . '/core/Controller/AvatarController.php',

0 commit comments

Comments
 (0)