2014-01-14 22:09:40 +01:00
|
|
|
<?php
|
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
namespace PHPCensor\Command;
|
2014-01-14 22:09:40 +01:00
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
use PHPCensor\Service\UserService;
|
|
|
|
use PHPCensor\Store\UserStore;
|
2014-01-14 22:09:40 +01:00
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
2017-02-05 11:58:57 +01:00
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
2014-01-14 22:09:40 +01:00
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2016-05-21 19:10:18 +02:00
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper;
|
|
|
|
use Symfony\Component\Console\Question\Question;
|
2014-01-14 22:09:40 +01:00
|
|
|
|
|
|
|
/**
|
2014-12-04 12:31:21 +01:00
|
|
|
* Create admin command - creates an admin user
|
2017-12-05 14:57:53 +01:00
|
|
|
*
|
2017-02-05 05:18:33 +01:00
|
|
|
* @author Wogan May (@woganmay)
|
2014-12-04 12:31:21 +01:00
|
|
|
*/
|
2014-01-14 22:09:40 +01:00
|
|
|
class CreateAdminCommand extends Command
|
|
|
|
{
|
2014-12-04 12:31:21 +01:00
|
|
|
/**
|
|
|
|
* @var UserStore
|
|
|
|
*/
|
|
|
|
protected $userStore;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param UserStore $userStore
|
|
|
|
*/
|
|
|
|
public function __construct(UserStore $userStore)
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->userStore = $userStore;
|
|
|
|
}
|
|
|
|
|
2014-01-14 22:09:40 +01:00
|
|
|
protected function configure()
|
|
|
|
{
|
|
|
|
$this
|
2016-07-21 19:02:11 +02:00
|
|
|
->setName('php-censor:create-admin')
|
2017-02-05 11:58:57 +01:00
|
|
|
|
2017-12-05 14:57:53 +01:00
|
|
|
->addOption('admin-name', null, InputOption::VALUE_OPTIONAL, 'Admin name')
|
2017-02-05 11:58:57 +01:00
|
|
|
->addOption('admin-password', null, InputOption::VALUE_OPTIONAL, 'Admin password')
|
2017-12-05 14:57:53 +01:00
|
|
|
->addOption('admin-email', null, InputOption::VALUE_OPTIONAL, 'Admin email')
|
2017-02-05 11:58:57 +01:00
|
|
|
|
2017-02-05 05:18:33 +01:00
|
|
|
->setDescription('Create an admin user');
|
2014-01-14 22:09:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-21 10:51:05 +02:00
|
|
|
* Creates an admin user in the existing database
|
2014-12-04 12:31:21 +01:00
|
|
|
*
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2014-01-14 22:09:40 +01:00
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
2016-05-21 19:10:18 +02:00
|
|
|
/** @var $helper QuestionHelper */
|
|
|
|
$helper = $this->getHelperSet()->get('question');
|
2014-01-14 22:09:40 +01:00
|
|
|
|
2017-02-05 11:58:57 +01:00
|
|
|
// Function to validate email address.
|
|
|
|
$mailValidator = function ($answer) {
|
2014-12-04 12:31:21 +01:00
|
|
|
if (!filter_var($answer, FILTER_VALIDATE_EMAIL)) {
|
2017-02-04 11:34:19 +01:00
|
|
|
throw new \InvalidArgumentException('Must be a valid email address.');
|
2014-01-14 22:09:40 +01:00
|
|
|
}
|
|
|
|
|
2014-12-04 12:31:21 +01:00
|
|
|
return $answer;
|
2017-02-05 11:58:57 +01:00
|
|
|
};
|
2014-01-14 22:09:40 +01:00
|
|
|
|
2017-02-05 11:58:57 +01:00
|
|
|
if ($adminEmail = $input->getOption('admin-email')) {
|
|
|
|
$adminEmail = $mailValidator($adminEmail);
|
|
|
|
} else {
|
|
|
|
$questionEmail = new Question('Admin email: ');
|
|
|
|
$adminEmail = $helper->ask($input, $output, $questionEmail);
|
|
|
|
}
|
2016-05-21 19:10:18 +02:00
|
|
|
|
2017-02-05 11:58:57 +01:00
|
|
|
if (!$adminName = $input->getOption('admin-name')) {
|
|
|
|
$questionName = new Question('Admin name: ');
|
|
|
|
$adminName = $helper->ask($input, $output, $questionName);
|
|
|
|
}
|
2016-05-21 19:10:18 +02:00
|
|
|
|
2017-02-05 11:58:57 +01:00
|
|
|
if (!$adminPassword = $input->getOption('admin-password')) {
|
|
|
|
$questionPassword = new Question('Admin password: ');
|
|
|
|
$questionPassword->setHidden(true);
|
|
|
|
$questionPassword->setHiddenFallback(false);
|
|
|
|
$adminPassword = $helper->ask($input, $output, $questionPassword);
|
|
|
|
}
|
2014-01-14 22:09:40 +01:00
|
|
|
|
2014-12-04 12:31:21 +01:00
|
|
|
try {
|
2017-02-05 11:58:57 +01:00
|
|
|
$userService = new UserService($this->userStore);
|
2018-03-09 19:00:53 +01:00
|
|
|
$userService->createUser($adminName, $adminEmail, 'internal', ['type' => 'internal'], $adminPassword, true);
|
2017-02-05 11:58:57 +01:00
|
|
|
|
2017-02-04 11:34:19 +01:00
|
|
|
$output->writeln('<info>User account created!</info>');
|
2017-02-05 11:58:57 +01:00
|
|
|
} catch (\Exception $ex) {
|
|
|
|
$output->writeln('<error>PHP Censor failed to create your admin account!</error>');
|
|
|
|
$output->writeln('<error>' . $ex->getMessage() . '</error>');
|
2014-01-14 22:09:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|