Skip to content

Commit

Permalink
Add user:welcome command
Browse files Browse the repository at this point in the history
Signed-off-by: FedericoHeichou <[email protected]>
  • Loading branch information
FedericoHeichou authored Aug 24, 2023
1 parent 313a06a commit a07c9e7
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
101 changes: 101 additions & 0 deletions core/Command/User/Welcome.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

/**
* @copyright Copyright (c) 2023 FedericoHeichou <[email protected]>
*
* @author FedericoHeichou <[email protected]>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/

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;
}

protected function configure() {

Check notice

Code scanning / Psalm

MissingReturnType Note

Method OC\Core\Command\User\Welcome::configure does not have a return type, expecting void
$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('<error>User does not exist</error>');
return 1;
}
$email = $user->getEMailAddress();
if ($email === '' || $email === null) {
$output->writeln('<error>User does not have an email address</error>');
return 1;
}
try {
$emailTemplate = $this->newUserMailHelper->generateTemplate($user, $input->getOption('reset-password'));
$this->newUserMailHelper->sendMail($user, $emailTemplate);
} catch (\Exception $e) {
$output->writeln('<error>Failed to send email: ' . $e->getMessage() . '</error>');
return 1;
}
return 0;
}
}
1 change: 1 addition & 0 deletions core/register_command.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@
$application->add(new OC\Core\Command\User\ListCommand(\OC::$server->getUserManager(), \OC::$server->getGroupManager()));
$application->add(new OC\Core\Command\User\Info(\OC::$server->getUserManager(), \OC::$server->getGroupManager()));
$application->add(new OC\Core\Command\User\AddAppPassword(\OC::$server->get(\OCP\IUserManager::class), \OC::$server->get(\OC\Authentication\Token\IProvider::class), \OC::$server->get(\OCP\Security\ISecureRandom::class), \OC::$server->get(\OCP\EventDispatcher\IEventDispatcher::class)));
$application->add(new OC\Core\Command\User\Welcome(\OC::$server->get(\OCP\IUserManager::class), \OC::$server->get(\OCA\Settings\Mailer\NewUserMailHelper::class)));

$application->add(new OC\Core\Command\Group\Add(\OC::$server->getGroupManager()));
$application->add(new OC\Core\Command\Group\Delete(\OC::$server->getGroupManager()));
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,7 @@
'OC\\Core\\Command\\User\\Report' => $baseDir . '/core/Command/User/Report.php',
'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\\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',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Core\\Command\\User\\Report' => __DIR__ . '/../../..' . '/core/Command/User/Report.php',
'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\\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',
Expand Down

0 comments on commit a07c9e7

Please sign in to comment.