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
|
|
|
|
|
|
|
namespace PHPCI\Command;
|
|
|
|
|
2014-07-23 16:56:23 +02:00
|
|
|
use PHPCI\Service\UserService;
|
2014-12-04 12:31:21 +01:00
|
|
|
use PHPCI\Helper\Lang;
|
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;
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
/**
|
2015-05-31 11:05:49 +02:00
|
|
|
* @var UserService
|
2014-12-04 12:31:21 +01:00
|
|
|
*/
|
2015-05-31 11:05:49 +02:00
|
|
|
protected $userService;
|
2014-12-04 12:31:21 +01:00
|
|
|
|
|
|
|
/**
|
2015-05-31 11:05:49 +02:00
|
|
|
* @param UserService $userService
|
2014-12-04 12:31:21 +01:00
|
|
|
*/
|
2015-05-31 11:05:49 +02:00
|
|
|
public function __construct(UserService $userService)
|
2014-12-04 12:31:21 +01:00
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
2015-05-31 11:05:49 +02:00
|
|
|
$this->userService = $userService;
|
2014-12-04 12:31:21 +01:00
|
|
|
}
|
|
|
|
|
2014-01-14 22:09:40 +01:00
|
|
|
protected function configure()
|
|
|
|
{
|
|
|
|
$this
|
|
|
|
->setName('phpci: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
|
|
|
/** @var $dialog \Symfony\Component\Console\Helper\DialogHelper */
|
|
|
|
$dialog = $this->getHelperSet()->get('dialog');
|
2014-01-14 22:09:40 +01:00
|
|
|
|
2014-12-04 12:31:21 +01:00
|
|
|
// Function to validate mail address.
|
|
|
|
$mailValidator = function ($answer) {
|
|
|
|
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;
|
|
|
|
};
|
2014-01-14 22:09:40 +01:00
|
|
|
|
2014-12-04 12:31:21 +01:00
|
|
|
$adminEmail = $dialog->askAndValidate($output, Lang::get('enter_email'), $mailValidator, false);
|
|
|
|
$adminName = $dialog->ask($output, Lang::get('enter_name'));
|
|
|
|
$adminPass = $dialog->askHiddenResponse($output, Lang::get('enter_password'));
|
2014-01-14 22:09:40 +01:00
|
|
|
|
2014-12-04 12:31:21 +01:00
|
|
|
try {
|
2015-05-31 11:05:49 +02:00
|
|
|
$this->userService->createUser($adminName, $adminEmail, $adminPass, true);
|
2014-12-04 12:31:21 +01:00
|
|
|
$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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|