/** * Add a user - handles both form and processing. */ public function add() { if (!$_SESSION['user']->getIsAdmin()) { throw new ForbiddenException('You do not have permission to do that.'); } $this->config->set('page_title', 'Add User'); $method = $this->request->getMethod(); if ($method == 'POST') { $values = $this->getParams(); } else { $values = array(); } $form = $this->userForm($values); if ($method != 'POST' || $method == 'POST' && !$form->validate()) { $view = new b8\View('UserForm'); $view->type = 'add'; $view->user = null; $view->form = $form; return $view->render(); } $name = $this->getParam('name', null); $email = $this->getParam('email', null); $password = $this->getParam('password', null); $isAdmin = (int) $this->getParam('is_admin', 0); $this->userService->createUser($name, $email, $password, $isAdmin); header('Location: ' . PHPCI_URL . 'user'); die; }
/** * Add a user - handles both form and processing. */ public function add() { $this->requireAdmin(); $this->layout->title = Lang::get('add_user'); $method = $this->request->getMethod(); if ($method == 'POST') { $values = $this->getParams(); } else { $values = array(); } $form = $this->userForm($values); if ($method != 'POST' || $method == 'POST' && !$form->validate()) { $view = new b8\View('UserForm'); $view->type = 'add'; $view->user = null; $view->form = $form; return $view->render(); } $name = $this->getParam('name', null); $email = $this->getParam('email', null); $password = $this->getParam('password', null); $isAdmin = (int) $this->getParam('is_admin', 0); $this->userService->createUser($name, $email, $password, $isAdmin); header('Location: ' . PHPCI_URL . 'user'); die; }
/** * Creates an admin user in the existing PHPCI database */ protected function execute(InputInterface $input, OutputInterface $output) { $userStore = Factory::getStore('User'); $userService = new UserService($userStore); require PHPCI_DIR . 'bootstrap.php'; // Try to create a user account: $adminEmail = $this->ask(Lang::get('enter_email'), true, FILTER_VALIDATE_EMAIL); if (empty($adminEmail)) { return; } $adminPass = $this->ask(Lang::get('enter_pass')); $adminName = $this->ask(Lang::get('enter_name')); try { $userService->createUser($adminName, $adminEmail, $adminPass, 1); print Lang::get('user_created') . PHP_EOL; } catch (\Exception $ex) { print Lang::get('failed_to_create') . PHP_EOL; print $ex->getMessage(); print PHP_EOL; } }
/** * Creates an admin user in the existing PHPCI database */ protected function execute(InputInterface $input, OutputInterface $output) { $userStore = Factory::getStore('User'); $userService = new UserService($userStore); 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: '******'Admin name: '); try { $userService->createUser($adminName, $adminEmail, $adminPass, 1); 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; } }
/** * Creates an admin user in the existing PHPCI database * * {@inheritDoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $userService = new UserService($this->userStore); /** @var $dialog \Symfony\Component\Console\Helper\DialogHelper */ $dialog = $this->getHelperSet()->get('dialog'); // Function to validate mail address. $mailValidator = function ($answer) { if (!filter_var($answer, FILTER_VALIDATE_EMAIL)) { throw new \InvalidArgumentException(Lang::get('must_be_valid_email')); } return $answer; }; $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')); 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())); } }
/** * @covers PHPUnit::execute */ public function testExecute_CreateAdminUser() { $user = $this->testedService->createUser('Test', '*****@*****.**', 'testing', 1); $this->assertEquals(1, $user->getIsAdmin()); }
/** * Create admin user using information loaded before. * * @param array $admin * @param OutputInterface $output */ protected function createAdminUser($admin, $output) { try { $this->reloadConfig(); $userStore = Factory::getStore('User'); $userService = new UserService($userStore); $userService->createUser($admin['name'], $admin['mail'], $admin['pass'], 1); $output->writeln('<info>' . Lang::get('user_created') . '</info>'); } catch (\Exception $ex) { $output->writeln('<error>' . Lang::get('failed_to_create') . '</error>'); $output->writeln('<error>' . $ex->getMessage() . '</error>'); } }
/** * @param OutputInterface $output * @param DialogHelper $dialog */ protected function createAdminUser(OutputInterface $output, DialogHelper $dialog) { // Try to create a user account: $adminEmail = $dialog->askAndValidate($output, 'Your email address: ', function ($answer) { if (!filter_var($answer, FILTER_VALIDATE_EMAIL)) { throw new Exception('Must be a valid email address.'); } return $answer; }, false); $adminPass = $dialog->askHiddenResponse($output, 'Enter your desired admin password: '******'Enter your name: '); try { $this->reloadConfig(); $userStore = Factory::getStore('User'); $userService = new UserService($userStore); $userService->createUser($adminName, $adminEmail, $adminPass, 1); $output->writeln('<info>User account created!</info>'); } catch (\Exception $ex) { $output->writeln('<error>PHPCI failed to create your admin account.</error>'); $output->writeln('<error>' . $ex->getMessage() . '</error>'); die; } }