diff --git a/core/Command/User/Add.php b/core/Command/User/Add.php index f2970c951a00..9c504bcd0425 100644 --- a/core/Command/User/Add.php +++ b/core/Command/User/Add.php @@ -27,6 +27,7 @@ use OCP\IGroupManager; use OCP\IUser; use OCP\IUserManager; +use OCP\Mail\IMailer; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -41,14 +42,19 @@ class Add extends Command { /** @var \OCP\IGroupManager */ protected $groupManager; + /** @var IMailer */ + protected $mailer; + /** * @param IUserManager $userManager * @param IGroupManager $groupManager + * @param IMailer $mailer */ - public function __construct(IUserManager $userManager, IGroupManager $groupManager) { + public function __construct(IUserManager $userManager, IGroupManager $groupManager, IMailer $mailer) { parent::__construct(); $this->userManager = $userManager; $this->groupManager = $groupManager; + $this->mailer = $mailer; } protected function configure() { @@ -93,6 +99,20 @@ protected function execute(InputInterface $input, OutputInterface $output) { return 1; } + // Validate email before we create the user + if ($input->getOption('email')) { + // Validate first + if(!$this->mailer->validateMailAddress($input->getOption('email'))) { + // Invalid! Error + $output->writeln('Invalid email address supplied'); + return 1; + } else { + $email = $input->getOption('email'); + } + } else { + $email = null; + } + if ($input->getOption('password-from-env')) { $password = getenv('OC_PASS'); if (!$password) { @@ -135,9 +155,10 @@ protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln('Display name set to "' . $user->getDisplayName() . '"'); } - if ($input->getOption('email')) { - $user->setEMailAddress($input->getOption('email')); - $output->writeln('Email address set to "' . $user->getEMailAddress() . '"'); + // Set email if supplied & valid + if(!is_null($email)) { + $user->setEMailAddress($email); + $output->writeln('Email address set to "' . $user->getEMailAddress() . '"'); } $groups = $input->getOption('group'); diff --git a/core/register_command.php b/core/register_command.php index 34e22cbb9044..e19e160cc187 100644 --- a/core/register_command.php +++ b/core/register_command.php @@ -135,7 +135,7 @@ new \OC\Repair(\OC\Repair::getRepairSteps(), \OC::$server->getEventDispatcher()), \OC::$server->getConfig(), \OC::$server->getEventDispatcher())); - $application->add(new OC\Core\Command\User\Add(\OC::$server->getUserManager(), \OC::$server->getGroupManager())); + $application->add(new OC\Core\Command\User\Add(\OC::$server->getUserManager(), \OC::$server->getGroupManager(), \OC::$server->getMailer())); $application->add(new OC\Core\Command\User\Delete(\OC::$server->getUserManager())); $application->add(new OC\Core\Command\User\Disable(\OC::$server->getUserManager())); $application->add(new OC\Core\Command\User\Enable(\OC::$server->getUserManager()));