2014-01-14 22:09:40 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2014-05-12 18:26:17 +02:00
|
|
|
* PHPCI - Continuous Integration for PHP
|
|
|
|
*
|
|
|
|
* @copyright Copyright 2014, Block 8 Limited.
|
|
|
|
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
|
|
|
* @link https://www.phptesting.org/
|
|
|
|
*/
|
2014-01-14 22:09:40 +01:00
|
|
|
|
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\Helper\Lang;
|
|
|
|
use PHPCensor\Store\UserStore;
|
2014-01-14 22:09:40 +01:00
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
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
|
|
|
|
* @author Wogan May (@woganmay)
|
|
|
|
* @package PHPCI
|
|
|
|
* @subpackage Console
|
|
|
|
*/
|
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')
|
2014-12-04 16:48:52 +01:00
|
|
|
->setDescription(Lang::get('create_admin_user'));
|
2014-01-14 22:09:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-12-04 12:31:21 +01:00
|
|
|
* Creates an admin user in the existing PHPCI database
|
|
|
|
*
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2014-01-14 22:09:40 +01:00
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
2014-12-04 12:31:21 +01:00
|
|
|
$userService = new UserService($this->userStore);
|
2014-07-23 16:56:23 +02:00
|
|
|
|
2016-05-21 19:10:18 +02:00
|
|
|
/** @var $helper QuestionHelper */
|
|
|
|
$helper = $this->getHelperSet()->get('question');
|
2014-01-14 22:09:40 +01:00
|
|
|
|
2016-05-21 19:10:18 +02:00
|
|
|
$question = new Question(Lang::get('enter_email'));
|
|
|
|
$question->setValidator(function ($answer) {
|
2014-12-04 12:31:21 +01:00
|
|
|
if (!filter_var($answer, FILTER_VALIDATE_EMAIL)) {
|
|
|
|
throw new \InvalidArgumentException(Lang::get('must_be_valid_email'));
|
2014-01-14 22:09:40 +01:00
|
|
|
}
|
|
|
|
|
2014-12-04 12:31:21 +01:00
|
|
|
return $answer;
|
2016-05-21 19:10:18 +02:00
|
|
|
});
|
|
|
|
$adminEmail = $helper->ask($input, $output, $question);
|
2014-01-14 22:09:40 +01:00
|
|
|
|
2016-05-21 19:10:18 +02:00
|
|
|
$question = new Question(Lang::get('enter_name'));
|
|
|
|
$adminName = $helper->ask($input, $output, $question);
|
|
|
|
|
|
|
|
$question = new Question(Lang::get('enter_password'));
|
|
|
|
$question->setHidden(true);
|
|
|
|
$question->setHiddenFallback(false);
|
|
|
|
|
|
|
|
$adminPass = $helper->ask($input, $output, $question);
|
2014-01-14 22:09:40 +01:00
|
|
|
|
2014-12-04 12:31:21 +01:00
|
|
|
try {
|
|
|
|
$userService->createUser($adminName, $adminEmail, $adminPass, true);
|
|
|
|
$output->writeln(Lang::get('user_created'));
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$output->writeln(sprintf('<error>%s</error>', Lang::get('failed_to_create')));
|
|
|
|
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
|
2014-01-14 22:09:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|