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-12-04 16:48:52 +01:00
|
|
|
use PHPCI\Helper\Lang;
|
2014-07-23 16:56:23 +02:00
|
|
|
use PHPCI\Service\UserService;
|
2014-01-14 22:09:40 +01:00
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
use b8\Store\Factory;
|
|
|
|
use PHPCI\Builder;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create admin command - creates an admin user
|
|
|
|
* @author Wogan May (@woganmay)
|
|
|
|
* @package PHPCI
|
|
|
|
* @subpackage Console
|
|
|
|
*/
|
|
|
|
class CreateAdminCommand extends Command
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an admin user in the existing PHPCI database
|
|
|
|
*/
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
2014-07-23 16:56:23 +02:00
|
|
|
$userStore = Factory::getStore('User');
|
|
|
|
$userService = new UserService($userStore);
|
|
|
|
|
2014-01-14 22:09:40 +01:00
|
|
|
require(PHPCI_DIR . 'bootstrap.php');
|
|
|
|
|
|
|
|
// Try to create a user account:
|
2014-12-04 16:48:52 +01:00
|
|
|
$adminEmail = $this->ask(Lang::get('enter_email'), true, FILTER_VALIDATE_EMAIL);
|
2014-01-14 22:09:40 +01:00
|
|
|
|
|
|
|
if (empty($adminEmail)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-27 15:12:19 +01:00
|
|
|
|
2014-12-04 16:48:52 +01:00
|
|
|
$adminPass = $this->ask(Lang::get('enter_pass'));
|
|
|
|
$adminName = $this->ask(Lang::get('enter_name'));
|
2014-01-14 22:09:40 +01:00
|
|
|
|
|
|
|
try {
|
2014-07-23 16:56:23 +02:00
|
|
|
$userService->createUser($adminName, $adminEmail, $adminPass, 1);
|
2014-12-04 16:48:52 +01:00
|
|
|
print Lang::get('user_created') . PHP_EOL;
|
2014-01-14 22:09:40 +01:00
|
|
|
} catch (\Exception $ex) {
|
2014-12-04 16:48:52 +01:00
|
|
|
print Lang::get('failed_to_create') . PHP_EOL;
|
2014-01-14 22:09:40 +01:00
|
|
|
print $ex->getMessage();
|
2014-02-27 15:12:19 +01:00
|
|
|
print PHP_EOL;
|
2014-01-14 22:09:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function ask($question, $emptyOk = false, $validationFilter = null)
|
|
|
|
{
|
|
|
|
print $question . ' ';
|
|
|
|
|
|
|
|
$rtn = '';
|
|
|
|
$stdin = fopen('php://stdin', 'r');
|
|
|
|
$rtn = fgets($stdin);
|
|
|
|
fclose($stdin);
|
|
|
|
|
|
|
|
$rtn = trim($rtn);
|
|
|
|
|
|
|
|
if (!$emptyOk && empty($rtn)) {
|
|
|
|
$rtn = $this->ask($question, $emptyOk, $validationFilter);
|
|
|
|
} elseif ($validationFilter != null && ! empty($rtn)) {
|
|
|
|
if (! $this -> controlFormat($rtn, $validationFilter, $statusMessage)) {
|
|
|
|
print $statusMessage;
|
|
|
|
$rtn = $this->ask($question, $emptyOk, $validationFilter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rtn;
|
|
|
|
}
|
|
|
|
protected function controlFormat($valueToInspect, $filter, &$statusMessage)
|
|
|
|
{
|
|
|
|
$filters = !(is_array($filter))? array($filter) : $filter;
|
|
|
|
$statusMessage = '';
|
|
|
|
$status = true;
|
|
|
|
$options = array();
|
|
|
|
|
|
|
|
foreach ($filters as $filter) {
|
|
|
|
if (! is_int($filter)) {
|
|
|
|
$regexp = $filter;
|
|
|
|
$filter = FILTER_VALIDATE_REGEXP;
|
|
|
|
$options = array(
|
|
|
|
'options' => array(
|
|
|
|
'regexp' => $regexp,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (! filter_var($valueToInspect, $filter, $options)) {
|
|
|
|
$status = false;
|
|
|
|
|
|
|
|
switch ($filter)
|
|
|
|
{
|
|
|
|
case FILTER_VALIDATE_URL:
|
2014-12-04 16:48:52 +01:00
|
|
|
$statusMessage = Lang::get('must_be_valid_url') . PHP_EOL;
|
2014-01-14 22:09:40 +01:00
|
|
|
break;
|
|
|
|
case FILTER_VALIDATE_EMAIL:
|
2014-12-04 16:48:52 +01:00
|
|
|
$statusMessage = Lang::get('must_be_valid_email') . PHP_EOL;
|
2014-01-14 22:09:40 +01:00
|
|
|
break;
|
|
|
|
case FILTER_VALIDATE_REGEXP:
|
2014-12-04 16:48:52 +01:00
|
|
|
$statusMessage = Lang::get('incorrect_format') . PHP_EOL;
|
2014-01-14 22:09:40 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
}
|