From f6f5a6e111dc2adf6587915071326f964ff1673d Mon Sep 17 00:00:00 2001 From: Wogan May Date: Tue, 14 Jan 2014 23:03:23 +0200 Subject: [PATCH 1/3] Create an admin user New console command to add an admin user without trying to reinstall the whole app. --- PHPCI/Command/CreateAdminCommand.php | 130 +++++++++++++++++++++++++++ console | 2 + 2 files changed, 132 insertions(+) create mode 100644 PHPCI/Command/CreateAdminCommand.php diff --git a/PHPCI/Command/CreateAdminCommand.php b/PHPCI/Command/CreateAdminCommand.php new file mode 100644 index 00000000..6122a108 --- /dev/null +++ b/PHPCI/Command/CreateAdminCommand.php @@ -0,0 +1,130 @@ +setName('phpci:create-admin') + ->setDescription('Create an admin user'); + } + + /** + * Creates an admin user in the existing PHPCI database + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + + require(PHPCI_DIR . 'bootstrap.php'); + + // Try to create a user account: + $adminEmail = $this->ask('Admin email address: ', true, FILTER_VALIDATE_EMAIL); + + if (empty($adminEmail)) { + return; + } + + $adminPass = $this->ask('Admin password: '); + $adminName = $this->ask('Admin name: '); + + try { + $user = new \PHPCI\Model\User(); + $user->setEmail($adminEmail); + $user->setName($adminName); + $user->setIsAdmin(1); + $user->setHash(password_hash($adminPass, PASSWORD_DEFAULT)); + + $store = \b8\Store\Factory::getStore('User'); + $store->save($user); + + print 'User account created!' . PHP_EOL; + } catch (\Exception $ex) { + print 'There was a problem creating your account. :(' . PHP_EOL; + print $ex->getMessage(); + print PHP_EOL; + } + } + + 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: + $statusMessage = 'Incorrect url format.' . PHP_EOL; + break; + case FILTER_VALIDATE_EMAIL: + $statusMessage = 'Incorrect e-mail format.' . PHP_EOL; + break; + case FILTER_VALIDATE_REGEXP: + $statusMessage = 'Incorrect format.' . PHP_EOL; + break; + } + } + } + + return $status; + } +} diff --git a/console b/console index f855f5ab..74490f33 100644 --- a/console +++ b/console @@ -18,6 +18,7 @@ use PHPCI\Command\UpdateCommand; use PHPCI\Command\InstallCommand; use PHPCI\Command\DaemonCommand; use PHPCI\Command\PollCommand; +use PHPCI\Command\CreateAdminCommand; use Symfony\Component\Console\Application; $loggerConfig = \PHPCI\Helper\LoggerConfig::newFromFile(__DIR__ . "/loggerconfig.php"); @@ -30,5 +31,6 @@ $application->add(new UpdateCommand($loggerConfig->getFor('UpdateCommand'))); $application->add(new GenerateCommand); $application->add(new DaemonCommand($loggerConfig->getFor('DaemonCommand'))); $application->add(new PollCommand($loggerConfig->getFor('PollCommand'))); +$application->add(new CreateAdminCommand); $application->run(); From 89931d01b53f68063af06497102db93f1a1f5e40 Mon Sep 17 00:00:00 2001 From: Wogan May Date: Tue, 14 Jan 2014 23:07:12 +0200 Subject: [PATCH 2/3] Revert "Create an admin user" This reverts commit f6f5a6e111dc2adf6587915071326f964ff1673d. --- PHPCI/Command/CreateAdminCommand.php | 130 --------------------------- console | 2 - 2 files changed, 132 deletions(-) delete mode 100644 PHPCI/Command/CreateAdminCommand.php diff --git a/PHPCI/Command/CreateAdminCommand.php b/PHPCI/Command/CreateAdminCommand.php deleted file mode 100644 index 6122a108..00000000 --- a/PHPCI/Command/CreateAdminCommand.php +++ /dev/null @@ -1,130 +0,0 @@ -setName('phpci:create-admin') - ->setDescription('Create an admin user'); - } - - /** - * Creates an admin user in the existing PHPCI database - */ - protected function execute(InputInterface $input, OutputInterface $output) - { - - require(PHPCI_DIR . 'bootstrap.php'); - - // Try to create a user account: - $adminEmail = $this->ask('Admin email address: ', true, FILTER_VALIDATE_EMAIL); - - if (empty($adminEmail)) { - return; - } - - $adminPass = $this->ask('Admin password: '); - $adminName = $this->ask('Admin name: '); - - try { - $user = new \PHPCI\Model\User(); - $user->setEmail($adminEmail); - $user->setName($adminName); - $user->setIsAdmin(1); - $user->setHash(password_hash($adminPass, PASSWORD_DEFAULT)); - - $store = \b8\Store\Factory::getStore('User'); - $store->save($user); - - print 'User account created!' . PHP_EOL; - } catch (\Exception $ex) { - print 'There was a problem creating your account. :(' . PHP_EOL; - print $ex->getMessage(); - print PHP_EOL; - } - } - - 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: - $statusMessage = 'Incorrect url format.' . PHP_EOL; - break; - case FILTER_VALIDATE_EMAIL: - $statusMessage = 'Incorrect e-mail format.' . PHP_EOL; - break; - case FILTER_VALIDATE_REGEXP: - $statusMessage = 'Incorrect format.' . PHP_EOL; - break; - } - } - } - - return $status; - } -} diff --git a/console b/console index 74490f33..f855f5ab 100644 --- a/console +++ b/console @@ -18,7 +18,6 @@ use PHPCI\Command\UpdateCommand; use PHPCI\Command\InstallCommand; use PHPCI\Command\DaemonCommand; use PHPCI\Command\PollCommand; -use PHPCI\Command\CreateAdminCommand; use Symfony\Component\Console\Application; $loggerConfig = \PHPCI\Helper\LoggerConfig::newFromFile(__DIR__ . "/loggerconfig.php"); @@ -31,6 +30,5 @@ $application->add(new UpdateCommand($loggerConfig->getFor('UpdateCommand'))); $application->add(new GenerateCommand); $application->add(new DaemonCommand($loggerConfig->getFor('DaemonCommand'))); $application->add(new PollCommand($loggerConfig->getFor('PollCommand'))); -$application->add(new CreateAdminCommand); $application->run(); From cd3a18d73cce5ec78545f31c9b8adc064266075b Mon Sep 17 00:00:00 2001 From: Wogan May Date: Tue, 14 Jan 2014 23:09:40 +0200 Subject: [PATCH 3/3] Create admin user New console command just to create an admin user --- PHPCI/Command/CreateAdminCommand.php | 130 +++++++++++++++++++++++++++ console | 2 + 2 files changed, 132 insertions(+) create mode 100644 PHPCI/Command/CreateAdminCommand.php diff --git a/PHPCI/Command/CreateAdminCommand.php b/PHPCI/Command/CreateAdminCommand.php new file mode 100644 index 00000000..6122a108 --- /dev/null +++ b/PHPCI/Command/CreateAdminCommand.php @@ -0,0 +1,130 @@ +setName('phpci:create-admin') + ->setDescription('Create an admin user'); + } + + /** + * Creates an admin user in the existing PHPCI database + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + + require(PHPCI_DIR . 'bootstrap.php'); + + // Try to create a user account: + $adminEmail = $this->ask('Admin email address: ', true, FILTER_VALIDATE_EMAIL); + + if (empty($adminEmail)) { + return; + } + + $adminPass = $this->ask('Admin password: '); + $adminName = $this->ask('Admin name: '); + + try { + $user = new \PHPCI\Model\User(); + $user->setEmail($adminEmail); + $user->setName($adminName); + $user->setIsAdmin(1); + $user->setHash(password_hash($adminPass, PASSWORD_DEFAULT)); + + $store = \b8\Store\Factory::getStore('User'); + $store->save($user); + + print 'User account created!' . PHP_EOL; + } catch (\Exception $ex) { + print 'There was a problem creating your account. :(' . PHP_EOL; + print $ex->getMessage(); + print PHP_EOL; + } + } + + 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: + $statusMessage = 'Incorrect url format.' . PHP_EOL; + break; + case FILTER_VALIDATE_EMAIL: + $statusMessage = 'Incorrect e-mail format.' . PHP_EOL; + break; + case FILTER_VALIDATE_REGEXP: + $statusMessage = 'Incorrect format.' . PHP_EOL; + break; + } + } + } + + return $status; + } +} diff --git a/console b/console index f855f5ab..74490f33 100644 --- a/console +++ b/console @@ -18,6 +18,7 @@ use PHPCI\Command\UpdateCommand; use PHPCI\Command\InstallCommand; use PHPCI\Command\DaemonCommand; use PHPCI\Command\PollCommand; +use PHPCI\Command\CreateAdminCommand; use Symfony\Component\Console\Application; $loggerConfig = \PHPCI\Helper\LoggerConfig::newFromFile(__DIR__ . "/loggerconfig.php"); @@ -30,5 +31,6 @@ $application->add(new UpdateCommand($loggerConfig->getFor('UpdateCommand'))); $application->add(new GenerateCommand); $application->add(new DaemonCommand($loggerConfig->getFor('DaemonCommand'))); $application->add(new PollCommand($loggerConfig->getFor('PollCommand'))); +$application->add(new CreateAdminCommand); $application->run();