From 07849aa614b7daeba4fbc120cef1d588b06eb3d3 Mon Sep 17 00:00:00 2001 From: franck Date: Wed, 28 Aug 2013 14:23:50 +0200 Subject: [PATCH] Added command for creating admin users on console --- core/lib/Thelia/Command/ClearImageCache.php | 1 - core/lib/Thelia/Command/CreateAdminUser.php | 119 ++++++++++++++++++++ core/lib/Thelia/Config/Resources/config.xml | 1 + core/lib/Thelia/Model/Admin.php | 2 +- core/lib/Thelia/Model/Category.php | 2 +- 5 files changed, 122 insertions(+), 3 deletions(-) create mode 100644 core/lib/Thelia/Command/CreateAdminUser.php diff --git a/core/lib/Thelia/Command/ClearImageCache.php b/core/lib/Thelia/Command/ClearImageCache.php index 8f11770a3..babbdd619 100755 --- a/core/lib/Thelia/Command/ClearImageCache.php +++ b/core/lib/Thelia/Command/ClearImageCache.php @@ -24,7 +24,6 @@ namespace Thelia\Command; use Thelia\Command\ContainerAwareCommand; -use Thelia\Core\Event\TheliaEvents; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; diff --git a/core/lib/Thelia/Command/CreateAdminUser.php b/core/lib/Thelia/Command/CreateAdminUser.php new file mode 100644 index 000000000..dc7118399 --- /dev/null +++ b/core/lib/Thelia/Command/CreateAdminUser.php @@ -0,0 +1,119 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Command; + +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +use Thelia\Command\ContainerAwareCommand; +use Thelia\Model\Admin; + +class CreateAdminUser extends ContainerAwareCommand +{ + /** + * Configure the command + */ + protected function configure() + { + $this + ->setName("thelia:create-admin") + ->setDescription("Create a new adminsitration user") + ->setHelp("The thelia:create-admin command create a new administration user.") + ; + + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $output->writeln('Please enter the admin user information:'); + + $admin = $this->getAdminInfo($input, $output); // new Admin(); + + $admin->save(); + + $output->writeln(array( + "", + "User ".$admin->getLogin()." successfully created.", + "" + )); + } + + protected function enterData($dialog, $output, $label, $error_message) + { + return $dialog->askAndValidate( + $output, + $this->decorateInfo($label), + function ($answer) { + $answer = trim($answer); + if (empty($answer)) { + throw new \RuntimeException("This information is mandatory."); + } + + return $answer; + } + ); + } + + /** + * Ask to user all needed information + * + * @param InputInterface $input + * @param OutputInterface $output + * @return array + */ + protected function getAdminInfo(InputInterface $input, OutputInterface $output) + { + $dialog = $this->getHelperSet()->get('dialog'); + + $admin = new Admin(); + + $admin->setLogin($this->enterData($dialog, $output, "Admin login name : ", "Please enter a login name.")); + $admin->setFirstname($this->enterData($dialog, $output, "User first name : ", "Please enter user first name.")); + $admin->setLastname($this->enterData($dialog, $output, "User last name : ", "Please enter user last name.")); + + do { + $password = $this->enterData($dialog, $output, "Password : ", "Please enter a password."); + $password_again = $this->enterData($dialog, $output, "Password (again): ", "Please enter the password again."); + + if (! empty($password) && $password == $password_again) { + + $admin->setPassword($password); + + break; + } + + $output->writeln("Passwords are different, please try again."); + } + while (true); + + return $admin; + } + + protected function decorateInfo($text) + { + return sprintf("%s", $text); + } + +} diff --git a/core/lib/Thelia/Config/Resources/config.xml b/core/lib/Thelia/Config/Resources/config.xml index 39d8bc905..a0d8db77b 100755 --- a/core/lib/Thelia/Config/Resources/config.xml +++ b/core/lib/Thelia/Config/Resources/config.xml @@ -54,6 +54,7 @@ + diff --git a/core/lib/Thelia/Model/Admin.php b/core/lib/Thelia/Model/Admin.php index 34dc7fbfd..a6deb7e60 100755 --- a/core/lib/Thelia/Model/Admin.php +++ b/core/lib/Thelia/Model/Admin.php @@ -26,7 +26,7 @@ class Admin extends BaseAdmin implements UserInterface \Thelia\Log\Tlog::getInstance()->debug($password); if ($this->isNew() && ($password === null || trim($password) == "")) { - throw new InvalidArgumentException("customer password is mandatory on creation"); + throw new \InvalidArgumentException("customer password is mandatory on creation"); } if($password !== null && trim($password) != "") { diff --git a/core/lib/Thelia/Model/Category.php b/core/lib/Thelia/Model/Category.php index 35ac49e0e..a2ad5e52a 100755 --- a/core/lib/Thelia/Model/Category.php +++ b/core/lib/Thelia/Model/Category.php @@ -16,8 +16,8 @@ class Category extends BaseCategory { public function getUrl() { - } + /** * Create a new category. *