From a41e2f01b74330b03b6311755c7a4cf02a9eca7a Mon Sep 17 00:00:00 2001 From: FedericoHeichou Date: Tue, 12 Mar 2024 15:39:29 +0000 Subject: [PATCH 1/2] feat: Add user:welcome command Signed-off-by: FedericoHeichou --- core/Command/User/Welcome.php | 104 ++++++++++++++++++++ core/register_command.php | 1 + lib/composer/composer/autoload_classmap.php | 1 + lib/composer/composer/autoload_static.php | 1 + 4 files changed, 107 insertions(+) create mode 100644 core/Command/User/Welcome.php diff --git a/core/Command/User/Welcome.php b/core/Command/User/Welcome.php new file mode 100644 index 0000000000000..a8db35b18b89b --- /dev/null +++ b/core/Command/User/Welcome.php @@ -0,0 +1,104 @@ + + * + * @author FedericoHeichou + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OC\Core\Command\User; + +use OC\Core\Command\Base; +use OCA\Settings\Mailer\NewUserMailHelper; +use OCP\IUserManager; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +class Welcome extends Base { + /** @var IUserManager */ + protected $userManager; + + /** @var NewUserMailHelper */ + private $newUserMailHelper; + + /** + * @param IUserManager $userManager + * @param NewUserMailHelper $newUserMailHelper + */ + public function __construct( + IUserManager $userManager, + NewUserMailHelper $newUserMailHelper + ) { + parent::__construct(); + + $this->userManager = $userManager; + $this->newUserMailHelper = $newUserMailHelper; + } + + /** + * @return void + */ + protected function configure() { + $this + ->setName('user:welcome') + ->setDescription('Sends the welcome email') + ->addArgument( + 'user', + InputArgument::REQUIRED, + 'The user to send the email to' + ) + ->addOption( + 'reset-password', + 'r', + InputOption::VALUE_NONE, + 'Add the reset password link to the email' + ) + ; + } + + /** + * @param InputInterface $input + * @param OutputInterface $output + * @return int + */ + protected function execute(InputInterface $input, OutputInterface $output): int { + $userId = $input->getArgument('user'); + // check if user exists + $user = $this->userManager->get($userId); + if ($user === null) { + $output->writeln('User does not exist'); + return 1; + } + $email = $user->getEMailAddress(); + if ($email === '' || $email === null) { + $output->writeln('User does not have an email address'); + return 1; + } + try { + $emailTemplate = $this->newUserMailHelper->generateTemplate($user, $input->getOption('reset-password')); + $this->newUserMailHelper->sendMail($user, $emailTemplate); + } catch (\Exception $e) { + $output->writeln('Failed to send email: ' . $e->getMessage() . ''); + return 1; + } + return 0; + } +} diff --git a/core/register_command.php b/core/register_command.php index 19d39c976b71b..f898d91df70b4 100644 --- a/core/register_command.php +++ b/core/register_command.php @@ -122,6 +122,7 @@ $application->add(Server::get(Command\User\AuthTokens\ListCommand::class)); $application->add(Server::get(Command\User\AuthTokens\Delete::class)); $application->add(Server::get(Command\User\Keys\Verify::class)); + $application->add(Server::get(Command\User\Welcome::class)); $application->add(Server::get(Command\Group\Add::class)); $application->add(Server::get(Command\Group\Delete::class)); diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index fca8883ae6cf2..1a3a86fd20776 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -1241,6 +1241,7 @@ 'OC\\Core\\Command\\User\\ResetPassword' => $baseDir . '/core/Command/User/ResetPassword.php', 'OC\\Core\\Command\\User\\Setting' => $baseDir . '/core/Command/User/Setting.php', 'OC\\Core\\Command\\User\\SyncAccountDataCommand' => $baseDir . '/core/Command/User/SyncAccountDataCommand.php', + 'OC\\Core\\Command\\User\\Welcome' => $baseDir . '/core/Command/User/Welcome.php', 'OC\\Core\\Controller\\AppPasswordController' => $baseDir . '/core/Controller/AppPasswordController.php', 'OC\\Core\\Controller\\AutoCompleteController' => $baseDir . '/core/Controller/AutoCompleteController.php', 'OC\\Core\\Controller\\AvatarController' => $baseDir . '/core/Controller/AvatarController.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index f780ee8ac31f0..c636ba769f935 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -1274,6 +1274,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OC\\Core\\Command\\User\\ResetPassword' => __DIR__ . '/../../..' . '/core/Command/User/ResetPassword.php', 'OC\\Core\\Command\\User\\Setting' => __DIR__ . '/../../..' . '/core/Command/User/Setting.php', 'OC\\Core\\Command\\User\\SyncAccountDataCommand' => __DIR__ . '/../../..' . '/core/Command/User/SyncAccountDataCommand.php', + 'OC\\Core\\Command\\User\\Welcome' => __DIR__ . '/../../..' . '/core/Command/User/Welcome.php', 'OC\\Core\\Controller\\AppPasswordController' => __DIR__ . '/../../..' . '/core/Controller/AppPasswordController.php', 'OC\\Core\\Controller\\AutoCompleteController' => __DIR__ . '/../../..' . '/core/Controller/AutoCompleteController.php', 'OC\\Core\\Controller\\AvatarController' => __DIR__ . '/../../..' . '/core/Controller/AvatarController.php', From 6fda0c15f0a97098c253ec342dc61d93581e4583 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Molakvo=C3=A6?= Date: Fri, 16 Aug 2024 15:25:04 +0200 Subject: [PATCH 2/2] fix: copyright header MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: John Molakvoæ --- core/Command/User/Welcome.php | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/core/Command/User/Welcome.php b/core/Command/User/Welcome.php index a8db35b18b89b..ba1c88a26c5bf 100644 --- a/core/Command/User/Welcome.php +++ b/core/Command/User/Welcome.php @@ -1,25 +1,7 @@ - * - * @author FedericoHeichou - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * + * SPDX-FileCopyrightText: 2023 FedericoHeichou + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OC\Core\Command\User;