Skip to content

Commit

Permalink
Don't set email if invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
tomneedham committed Aug 3, 2017
1 parent 6e219ec commit e8b3c0f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
29 changes: 25 additions & 4 deletions core/Command/User/Add.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -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('<error>Invalid email address supplied</error>');
return 1;
} else {
$email = $input->getOption('email');
}
} else {
$email = null;
}

if ($input->getOption('password-from-env')) {
$password = getenv('OC_PASS');
if (!$password) {
Expand Down Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion core/register_command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down

0 comments on commit e8b3c0f

Please sign in to comment.