From 0102fa3f2d051173762736b6a1a1ba15be466b15 Mon Sep 17 00:00:00 2001 From: LEZY Thomas Date: Tue, 10 Jun 2014 11:07:46 +0200 Subject: [PATCH 01/26] [ticket/12684] Add command user:add PHPBB3-12684 --- phpBB/language/en/acp/common.php | 2 + phpBB/phpbb/console/command/user/add.php | 167 +++++++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 phpBB/phpbb/console/command/user/add.php diff --git a/phpBB/language/en/acp/common.php b/phpBB/language/en/acp/common.php index 4a70aafc6f..300f1c2626 100644 --- a/phpBB/language/en/acp/common.php +++ b/phpBB/language/en/acp/common.php @@ -309,6 +309,8 @@ $lang = array_merge($lang, array( 'SHOW_ALL_OPERATIONS' => 'Show all operations', + 'SUCCESS_ADD_USER' => 'Successfully added user %s.', + 'TASKS_NOT_READY' => 'Not ready tasks:', 'TASKS_READY' => 'Ready tasks:', 'TOTAL_SIZE' => 'Total size', diff --git a/phpBB/phpbb/console/command/user/add.php b/phpBB/phpbb/console/command/user/add.php new file mode 100644 index 0000000000..e7a94a74da --- /dev/null +++ b/phpBB/phpbb/console/command/user/add.php @@ -0,0 +1,167 @@ +user = $user; + $this->db = $db; + $this->config = $config; + $this->password_manager = $password_manager; + + $this->user->add_lang('ucp'); + parent::__construct(); + } + + /** + * Sets the command name and description + * + * @return null + */ + protected function configure() + { + $this + ->setName('user:add') + ->setDescription($this->user->lang('CLI_DESCRIPTION_USER_ADD')) + ->addOption('username', null, InputOption::VALUE_REQUIRED, $this->user->lang('CLI_DESCRIPTION_USER_ADD_OPTION_USERNAME')) + ->addOption('password', null, InputOption::VALUE_REQUIRED, $this->user->lang('CLI_DESCRIPTION_USER_ADD_OPTION_PASSWORD')) + ->addOption('email', null, InputOption::VALUE_REQUIRED, $this->user->lang('CLI_DESCRIPTION_USER_ADD_OPTION_EMAIL')) + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $dialog = $this->getHelperSet()->get('dialog'); + + $username = $input->getOption('username'); + if (!$username) { + $username = $dialog->ask( + $output, + $this->user->lang('USERNAME') . $this->user->lang('COLON') . ' ', + null + ); + } + + $password = $input->getOption('password'); + if (!$password) + { + $password = $this->get_password($output, $dialog); + } + + $email = $input->getOption('email'); + if (!$email) + { + $email = $dialog->ask( + $output, + $this->user->lang('EMAIL_ADDRESS') . $this->user->lang('COLON') . ' ', + null + ); + } + + try + { + $group_id = $this->get_group_id(); + } + catch (\RunTimeException $e) + { + $output->writeln($e->getMessage()); + return 1; + } + + $user_row = array( + 'username' => $username, + 'user_password' => $this->password_manager->hash($password), + 'user_email' => $email, + 'group_id' => $group_id, + 'user_timezone' => $config['board_timezone'], + 'user_lang' => $config['default_lang'], + 'user_type' => USER_NORMAL, + 'user_regdate' => time(), + ); + + if (!function_exists(user_add)) + { + require_once dirname(__FILE__) . '/../../../../includes/functions_user.php'; + } + user_add($user_row); + + $output->writeln('' . $this->user->lang('SUCCESS_ADD_USER', $username) . ''); + return 0; + } + + protected function get_password($output, $dialog) + { + $current_user = $this->user; + return $dialog->askHiddenResponseAndValidate( + $output, + $current_user->lang('PASSWORD') . $current_user->lang('COLON') . ' ', + function ($answer) use ($dialog, $output, $current_user) + { + $confirm = $dialog->askHiddenResponse( + $output, + $current_user->lang('CONFIRM_PASSWORD') . $current_user->lang('COLON') . ' ', + null + ); + if ($confirm != $answer) + { + throw new \RunTimeException($current_user->lang('NEW_PASSWORD_ERROR')); + } + return $answer; + }, + false, + null + ); + } + + protected function get_group_id() + { + $sql = 'SELECT group_id + FROM ' . GROUPS_TABLE . " + WHERE group_name = '" . $this->db->sql_escape('REGISTERED') . "' + AND group_type = " . GROUP_SPECIAL; + $result = $this->db->sql_query($sql); + $row = $this->db->sql_fetchrow($result); + $this->db->sql_freeresult($result); + + if (!$row) + { + throw new \RunTimeException($this->user->lang('NO_GROUP')); + } + + return $row['group_id']; + } +} From 50761104ba6590de86ec651b5679983eaf2ff600 Mon Sep 17 00:00:00 2001 From: LEZY Thomas Date: Fri, 13 Jun 2014 11:38:02 +0200 Subject: [PATCH 02/26] [ticket/12684] Add doc blocks and test file for user:add PHPBB3-12684 --- phpBB/phpbb/console/command/user/add.php | 35 +++++- tests/console/user/add_test.php | 138 +++++++++++++++++++++++ tests/console/user/fixtures/config.xml | 43 +++++++ 3 files changed, 215 insertions(+), 1 deletion(-) create mode 100644 tests/console/user/add_test.php create mode 100644 tests/console/user/fixtures/config.xml diff --git a/phpBB/phpbb/console/command/user/add.php b/phpBB/phpbb/console/command/user/add.php index e7a94a74da..e3f8243c02 100644 --- a/phpBB/phpbb/console/command/user/add.php +++ b/phpBB/phpbb/console/command/user/add.php @@ -35,6 +35,9 @@ class add extends \phpbb\console\command\command * Construct method * * @param \phpbb\user $user The user object used for language information + * @param \phpbb\db\driver\driver_interface $db The database in wich will be inserted the user + * @param \phpbb\config\config $config The config object used to get default language and timezone + * @param \phpbb\passwords\manager $password_manager The password manager used to store the user's password */ public function __construct(\phpbb\user $user, \phpbb\db\driver\driver_interface $db, \phpbb\config\config $config, \phpbb\passwords\manager $password_manager) { @@ -63,6 +66,17 @@ class add extends \phpbb\console\command\command ; } + /** + * Executes the command user:add + * + * If not given in option, asks the username, password and email. + * Then a new user is added in the database, with language and timezone found in the $config passed to the constructor, and the group_id found in the database. + * + * @param InputInterface $input The input stream used to get the options + * @param OutputInterface $output The output stream, used to print messages + * + * @return int 0 if all is well, 1 if a database error occured while trying to get the group_id + */ protected function execute(InputInterface $input, OutputInterface $output) { $dialog = $this->getHelperSet()->get('dialog'); @@ -123,6 +137,17 @@ class add extends \phpbb\console\command\command return 0; } + /** + * Get the password + * + * Asks a password to the user and asks for confirmation. + * This is repeted while the two are not the same + * + * @param OutputInterface $output The output stream, where messages are printed + * @param Symfony\Component\Console\Helper\DialogHelper $dialog The dialog helper used to get answers to questions asked to the user + * + * @return null + */ protected function get_password($output, $dialog) { $current_user = $this->user; @@ -147,6 +172,14 @@ class add extends \phpbb\console\command\command ); } + /** + * Get the group id + * + * Go and find in the database the group_id corresponding to 'REGISTERED' + * + * @throws RunTimeException if the group id does not exist in database. + * @return null + */ protected function get_group_id() { $sql = 'SELECT group_id @@ -157,7 +190,7 @@ class add extends \phpbb\console\command\command $row = $this->db->sql_fetchrow($result); $this->db->sql_freeresult($result); - if (!$row) + if (!$row || !$row['group_id']) { throw new \RunTimeException($this->user->lang('NO_GROUP')); } diff --git a/tests/console/user/add_test.php b/tests/console/user/add_test.php new file mode 100644 index 0000000000..6c28c46815 --- /dev/null +++ b/tests/console/user/add_test.php @@ -0,0 +1,138 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +use Symfony\Component\Console\Application; +use Symfony\Component\Console\Tester\CommandTester; +use phpbb\console\command\user\add; + +require_once dirname(__FILE__) . '/../../../phpBB/includes/functions_user.php'; +require_once dirname(__FILE__) . '/../../../phpBB/includes/functions.php'; +require_once dirname(__FILE__) . '/../../../phpBB/includes/utf/utf_tools.php'; + +class phpbb_console_command_user_add_test extends phpbb_database_test_case +{ + protected $db; + protected $config; + protected $user; + protected $passwords_manager; + protected $command_name; + protected $dialog; + + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/config.xml'); + } + + public function setUp() + { + global $db, $config, $phpbb_dispatcher, $phpbb_container; + + $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); + $phpbb_container = new phpbb_mock_container_builder(); + $phpbb_container->set('cache.driver', new phpbb_mock_cache()); + + $config = $this->config = new \phpbb\config\config(array( + 'board_timezone' => 'UTC', + 'default_lang' => 'en', + )); + set_config(null, null, null, $this->config); + set_config_count(null, null, null, $this->config); + + $db = $this->db = $this->new_dbal(); + + $this->user = $this->getMock('\phpbb\user'); + $this->user->method('lang')->will($this->returnArgument(0)); + + $driver_helper = new \phpbb\passwords\driver\helper($this->config); + $passwords_drivers = array( + 'passwords.driver.bcrypt_2y' => new \phpbb\passwords\driver\bcrypt_2y($this->config, $driver_helper), + 'passwords.driver.bcrypt' => new \phpbb\passwords\driver\bcrypt($this->config, $driver_helper), + 'passwords.driver.salted_md5' => new \phpbb\passwords\driver\salted_md5($this->config, $driver_helper), + 'passwords.driver.phpass' => new \phpbb\passwords\driver\phpass($this->config, $driver_helper), + ); + + $passwords_helper = new \phpbb\passwords\helper; + $this->passwords_manager = new \phpbb\passwords\manager($this->config, $passwords_drivers, $passwords_helper, array_keys($passwords_drivers)); + + parent::setUp(); + } + + public function test_add_no_dialog() + { + $command_tester = $this->get_command_tester(); + + $this->assertEquals(2, $this->get_user_id('Admin')); + + $command_tester->execute(array( + 'command' => $this->command_name, + '--username' => 'foo', + '--password' => 'bar', + '--email' => 'foo@test.com' + )); + + $this->assertNotEquals(null, $this->get_user_id('foo')); + $this->assertContains('SUCCESS_ADD_USER', $command_tester->getDisplay()); + } + + public function test_add_dialog() + { + $command_tester = $this->get_command_tester(); + + $this->assertEquals(2, $this->get_user_id('Admin')); + + $this->dialog->setInputStream($this->getInputStream("bar\npass\npass\nbar@test.com\n")); + + $command_tester->execute(array( + 'command' => $this->command_name, + )); + + $this->assertNotEquals(null, $this->get_user_id('bar')); + $this->assertContains('SUCCESS_ADD_USER', $command_tester->getDisplay()); + + } + + public function get_command_tester() + { + $application = new Application(); + $application->add(new add($this->user, $this->db, $this->config, $this->passwords_manager)); + + $command = $application->find('user:add'); + $this->command_name = $command->getName(); + $this->dialog = $command->getHelper('dialog'); + return new CommandTester($command); + } + + public function get_user_id($username) + { + $sql = 'SELECT user_id + FROM ' . USERS_TABLE . ' + WHERE ' . 'username = ' . "'" . $username . "'"; + + $result = $this->db->sql_query($sql); + + $row = $this->db->sql_fetchrow($result); + + $this->db->sql_freeresult($result); + + return $row['user_id']; + } + + public function getInputStream($input) + { + $stream = fopen('php://memory', 'r+', false); + fputs($stream, $input); + rewind($stream); + + return $stream; + } +} diff --git a/tests/console/user/fixtures/config.xml b/tests/console/user/fixtures/config.xml new file mode 100644 index 0000000000..fed30dc20d --- /dev/null +++ b/tests/console/user/fixtures/config.xml @@ -0,0 +1,43 @@ + + + + user_id + user_permissions + username + username_clean + user_sig + + 1 + + Guest + guest + + + + 2 + + Admin + admin + + + + 3 + + Test + test + + +
+ + group_id + group_name + group_type + group_desc + + 1 + REGISTERED + 3 + foobar + +
+
From df4a620ba146d895be4910761bc030045abbae93 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Wed, 27 Aug 2014 16:56:46 +0200 Subject: [PATCH 03/26] [ticket/12684] Fix tests PHPBB3-12684 --- phpBB/phpbb/console/command/user/add.php | 18 +++++++----------- tests/console/user/add_test.php | 4 ++-- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/phpBB/phpbb/console/command/user/add.php b/phpBB/phpbb/console/command/user/add.php index e3f8243c02..fab69738e3 100644 --- a/phpBB/phpbb/console/command/user/add.php +++ b/phpBB/phpbb/console/command/user/add.php @@ -19,9 +19,6 @@ use Symfony\Component\Console\Output\OutputInterface; class add extends \phpbb\console\command\command { - /** @var \phpbb\user */ - protected $user; - /** @var \phpbb\db\driver\driver_interface */ protected $db; @@ -41,13 +38,12 @@ class add extends \phpbb\console\command\command */ public function __construct(\phpbb\user $user, \phpbb\db\driver\driver_interface $db, \phpbb\config\config $config, \phpbb\passwords\manager $password_manager) { - $this->user = $user; $this->db = $db; $this->config = $config; $this->password_manager = $password_manager; - $this->user->add_lang('ucp'); - parent::__construct(); + $user->add_lang('ucp'); + parent::__construct($user); } /** @@ -121,13 +117,13 @@ class add extends \phpbb\console\command\command 'user_password' => $this->password_manager->hash($password), 'user_email' => $email, 'group_id' => $group_id, - 'user_timezone' => $config['board_timezone'], - 'user_lang' => $config['default_lang'], + 'user_timezone' => $this->config['board_timezone'], + 'user_lang' => $this->config['default_lang'], 'user_type' => USER_NORMAL, 'user_regdate' => time(), ); - if (!function_exists(user_add)) + if (!function_exists('user_add')) { require_once dirname(__FILE__) . '/../../../../includes/functions_user.php'; } @@ -144,7 +140,7 @@ class add extends \phpbb\console\command\command * This is repeted while the two are not the same * * @param OutputInterface $output The output stream, where messages are printed - * @param Symfony\Component\Console\Helper\DialogHelper $dialog The dialog helper used to get answers to questions asked to the user + * @param \Symfony\Component\Console\Helper\DialogHelper $dialog The dialog helper used to get answers to questions asked to the user * * @return null */ @@ -177,7 +173,7 @@ class add extends \phpbb\console\command\command * * Go and find in the database the group_id corresponding to 'REGISTERED' * - * @throws RunTimeException if the group id does not exist in database. + * @throws \RunTimeException if the group id does not exist in database. * @return null */ protected function get_group_id() diff --git a/tests/console/user/add_test.php b/tests/console/user/add_test.php index 6c28c46815..6f80a8658a 100644 --- a/tests/console/user/add_test.php +++ b/tests/console/user/add_test.php @@ -50,7 +50,7 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case $db = $this->db = $this->new_dbal(); - $this->user = $this->getMock('\phpbb\user'); + $this->user = $this->getMock('\phpbb\user', array(), array('\phpbb\datetime')); $this->user->method('lang')->will($this->returnArgument(0)); $driver_helper = new \phpbb\passwords\driver\helper($this->config); @@ -90,7 +90,7 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case $this->assertEquals(2, $this->get_user_id('Admin')); - $this->dialog->setInputStream($this->getInputStream("bar\npass\npass\nbar@test.com\n")); + $this->dialog->setInputStream($this->getInputStream("bar\npassword\npassword\nbar@test.com\n")); $command_tester->execute(array( 'command' => $this->command_name, From 8f7aba6582c3fb748ed67720292f1d236d4e1270 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Mon, 15 Sep 2014 00:13:57 +0200 Subject: [PATCH 04/26] [ticket/12684] Fix header file PHPBB3-12684 --- phpBB/phpbb/console/command/user/add.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/phpbb/console/command/user/add.php b/phpBB/phpbb/console/command/user/add.php index fab69738e3..10b0d0f727 100644 --- a/phpBB/phpbb/console/command/user/add.php +++ b/phpBB/phpbb/console/command/user/add.php @@ -3,7 +3,7 @@ * * This file is part of the phpBB Forum Software package. * -* @copyright (c) phpBB Limited +* @copyright (c) phpBB Limited * @license GNU General Public License, version 2 (GPL-2.0) * * For full copyright and license information, please see From d17a8ab523f5cc77352170c679767c5523f57ec9 Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Mon, 29 Feb 2016 08:28:37 -0800 Subject: [PATCH 05/26] [ticket/12684] Fix merge errors PHPBB3-12684 --- phpBB/config/default/container/services_console.yml | 10 ++++++++++ phpBB/language/en/cli.php | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/phpBB/config/default/container/services_console.yml b/phpBB/config/default/container/services_console.yml index 2055fb68c5..b39353d841 100644 --- a/phpBB/config/default/container/services_console.yml +++ b/phpBB/config/default/container/services_console.yml @@ -175,6 +175,16 @@ services: tags: - { name: console.command } + console.command.user.add: + class: phpbb\console\command\user\add + arguments: + - '@user' + - '@dbal.conn' + - '@config' + - '@passwords.manager' + tags: + - { name: console.command } + console.command.reparser.list: class: phpbb\console\command\reparser\list_all arguments: diff --git a/phpBB/language/en/cli.php b/phpBB/language/en/cli.php index 1c549e5f9f..9a082b0c57 100644 --- a/phpBB/language/en/cli.php +++ b/phpBB/language/en/cli.php @@ -82,6 +82,11 @@ $lang = array_merge($lang, array( 'CLI_DESCRIPTION_THUMBNAIL_GENERATE' => 'Generate all missing thumbnails.', 'CLI_DESCRIPTION_THUMBNAIL_RECREATE' => 'Recreate all thumbnails.', + 'CLI_DESCRIPTION_USER_ADD' => 'Add a new user', + 'CLI_DESCRIPTION_USER_ADD_OPTION_USERNAME' => 'Username of the new user', + 'CLI_DESCRIPTION_USER_ADD_OPTION_PASSWORD' => 'Password of the new user', + 'CLI_DESCRIPTION_USER_ADD_OPTION_EMAIL' => 'E-mail address of the new user', + 'CLI_EXTENSION_DISABLE_FAILURE' => 'Could not disable extension %s', 'CLI_EXTENSION_DISABLE_SUCCESS' => 'Successfully disabled extension %s', 'CLI_EXTENSION_ENABLE_FAILURE' => 'Could not enable extension %s', From 6fe084a2fd967c188bdca827a46647120a5ea58d Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Mon, 29 Feb 2016 11:38:49 -0800 Subject: [PATCH 06/26] [ticket/12684] Updates for 3.2 API PHPBB3-12684 --- .../default/container/services_console.yml | 2 + phpBB/language/en/cli.php | 2 +- phpBB/phpbb/console/command/user/add.php | 42 ++++++++++++++----- tests/console/user/add_test.php | 19 ++++++--- 4 files changed, 49 insertions(+), 16 deletions(-) diff --git a/phpBB/config/default/container/services_console.yml b/phpBB/config/default/container/services_console.yml index b39353d841..006df0d7b3 100644 --- a/phpBB/config/default/container/services_console.yml +++ b/phpBB/config/default/container/services_console.yml @@ -182,6 +182,8 @@ services: - '@dbal.conn' - '@config' - '@passwords.manager' + - '%core.root_path%' + - '%core.php_ext%' tags: - { name: console.command } diff --git a/phpBB/language/en/cli.php b/phpBB/language/en/cli.php index 9a082b0c57..4c0fbde286 100644 --- a/phpBB/language/en/cli.php +++ b/phpBB/language/en/cli.php @@ -82,7 +82,7 @@ $lang = array_merge($lang, array( 'CLI_DESCRIPTION_THUMBNAIL_GENERATE' => 'Generate all missing thumbnails.', 'CLI_DESCRIPTION_THUMBNAIL_RECREATE' => 'Recreate all thumbnails.', - 'CLI_DESCRIPTION_USER_ADD' => 'Add a new user', + 'CLI_DESCRIPTION_USER_ADD' => 'Add a new user.', 'CLI_DESCRIPTION_USER_ADD_OPTION_USERNAME' => 'Username of the new user', 'CLI_DESCRIPTION_USER_ADD_OPTION_PASSWORD' => 'Password of the new user', 'CLI_DESCRIPTION_USER_ADD_OPTION_EMAIL' => 'E-mail address of the new user', diff --git a/phpBB/phpbb/console/command/user/add.php b/phpBB/phpbb/console/command/user/add.php index 10b0d0f727..f3b52349b7 100644 --- a/phpBB/phpbb/console/command/user/add.php +++ b/phpBB/phpbb/console/command/user/add.php @@ -13,9 +13,11 @@ namespace phpbb\console\command\user; +use phpbb\exception\runtime_exception; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; class add extends \phpbb\console\command\command { @@ -28,6 +30,19 @@ class add extends \phpbb\console\command\command /** @var \phpbb\passwords\manager */ protected $password_manager; + /** + * phpBB root path + * @var string + */ + protected $phpbb_root_path; + + /** + * PHP extension. + * + * @var string + */ + protected $php_ext; + /** * Construct method * @@ -35,12 +50,16 @@ class add extends \phpbb\console\command\command * @param \phpbb\db\driver\driver_interface $db The database in wich will be inserted the user * @param \phpbb\config\config $config The config object used to get default language and timezone * @param \phpbb\passwords\manager $password_manager The password manager used to store the user's password + * @param string $phpbb_root_path Root path + * @param string $php_ext PHP extension */ - public function __construct(\phpbb\user $user, \phpbb\db\driver\driver_interface $db, \phpbb\config\config $config, \phpbb\passwords\manager $password_manager) + public function __construct(\phpbb\user $user, \phpbb\db\driver\driver_interface $db, \phpbb\config\config $config, \phpbb\passwords\manager $password_manager, $phpbb_root_path, $php_ext) { $this->db = $db; $this->config = $config; $this->password_manager = $password_manager; + $this->phpbb_root_path = $phpbb_root_path; + $this->php_ext = $php_ext; $user->add_lang('ucp'); parent::__construct($user); @@ -75,6 +94,8 @@ class add extends \phpbb\console\command\command */ protected function execute(InputInterface $input, OutputInterface $output) { + $io = new SymfonyStyle($input, $output); + $dialog = $this->getHelperSet()->get('dialog'); $username = $input->getOption('username'); @@ -106,9 +127,9 @@ class add extends \phpbb\console\command\command { $group_id = $this->get_group_id(); } - catch (\RunTimeException $e) + catch (runtime_exception $e) { - $output->writeln($e->getMessage()); + $io->error($e->getMessage()); return 1; } @@ -125,11 +146,12 @@ class add extends \phpbb\console\command\command if (!function_exists('user_add')) { - require_once dirname(__FILE__) . '/../../../../includes/functions_user.php'; + require($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext); } - user_add($user_row); - $output->writeln('' . $this->user->lang('SUCCESS_ADD_USER', $username) . ''); + $user_id = user_add($user_row); + $io->success($this->user->lang('SUCCESS_ADD_USER', $username)); + return 0; } @@ -137,7 +159,7 @@ class add extends \phpbb\console\command\command * Get the password * * Asks a password to the user and asks for confirmation. - * This is repeted while the two are not the same + * This is repeated until the password match is confirmed. * * @param OutputInterface $output The output stream, where messages are printed * @param \Symfony\Component\Console\Helper\DialogHelper $dialog The dialog helper used to get answers to questions asked to the user @@ -159,7 +181,7 @@ class add extends \phpbb\console\command\command ); if ($confirm != $answer) { - throw new \RunTimeException($current_user->lang('NEW_PASSWORD_ERROR')); + throw new runtime_exception($current_user->lang('NEW_PASSWORD_ERROR')); } return $answer; }, @@ -173,7 +195,7 @@ class add extends \phpbb\console\command\command * * Go and find in the database the group_id corresponding to 'REGISTERED' * - * @throws \RunTimeException if the group id does not exist in database. + * @throws runtime_exception if the group id does not exist in database. * @return null */ protected function get_group_id() @@ -188,7 +210,7 @@ class add extends \phpbb\console\command\command if (!$row || !$row['group_id']) { - throw new \RunTimeException($this->user->lang('NO_GROUP')); + throw new runtime_exception($this->user->lang('NO_GROUP')); } return $row['group_id']; diff --git a/tests/console/user/add_test.php b/tests/console/user/add_test.php index 6f80a8658a..5e4b76063e 100644 --- a/tests/console/user/add_test.php +++ b/tests/console/user/add_test.php @@ -27,6 +27,8 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case protected $passwords_manager; protected $command_name; protected $dialog; + protected $phpbb_root_path; + protected $php_ext; public function getDataSet() { @@ -35,22 +37,26 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case public function setUp() { - global $db, $config, $phpbb_dispatcher, $phpbb_container; + global $db, $cache, $config, $user, $phpbb_dispatcher, $phpbb_container, $phpbb_root_path, $phpEx; $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); $phpbb_container = new phpbb_mock_container_builder(); $phpbb_container->set('cache.driver', new phpbb_mock_cache()); + $phpbb_container->set('notification_manager', new phpbb_mock_notification_manager()); + + $cache = $phpbb_container->get('cache.driver'); $config = $this->config = new \phpbb\config\config(array( 'board_timezone' => 'UTC', 'default_lang' => 'en', )); - set_config(null, null, null, $this->config); - set_config_count(null, null, null, $this->config); $db = $this->db = $this->new_dbal(); - $this->user = $this->getMock('\phpbb\user', array(), array('\phpbb\datetime')); + $user = $this->user = $this->getMock('\phpbb\user', array(), array( + new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)), + '\phpbb\datetime') + ); $this->user->method('lang')->will($this->returnArgument(0)); $driver_helper = new \phpbb\passwords\driver\helper($this->config); @@ -64,6 +70,9 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case $passwords_helper = new \phpbb\passwords\helper; $this->passwords_manager = new \phpbb\passwords\manager($this->config, $passwords_drivers, $passwords_helper, array_keys($passwords_drivers)); + $this->phpbb_root_path = $phpbb_root_path; + $this->php_ext = $phpEx; + parent::setUp(); } @@ -104,7 +113,7 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case public function get_command_tester() { $application = new Application(); - $application->add(new add($this->user, $this->db, $this->config, $this->passwords_manager)); + $application->add(new add($this->user, $this->db, $this->config, $this->passwords_manager, $this->phpbb_root_path, $this->php_ext)); $command = $application->find('user:add'); $this->command_name = $command->getName(); From d373428180e884f03a830aa69fe8ff2cd6a5140a Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Mon, 29 Feb 2016 11:41:47 -0800 Subject: [PATCH 07/26] [ticket/12684] Add input validation PHPBB3-12684 --- phpBB/phpbb/console/command/user/add.php | 48 ++++++++++++++++++++++++ tests/console/user/add_test.php | 23 ++++++++++++ 2 files changed, 71 insertions(+) diff --git a/phpBB/phpbb/console/command/user/add.php b/phpBB/phpbb/console/command/user/add.php index f3b52349b7..db06037947 100644 --- a/phpBB/phpbb/console/command/user/add.php +++ b/phpBB/phpbb/console/command/user/add.php @@ -123,6 +123,22 @@ class add extends \phpbb\console\command\command ); } + $data = array( + 'username' => $username, + 'new_password' => $password, + 'email' => $email, + ); + + try + { + $this->validate_user_data($data); + } + catch (runtime_exception $e) + { + $io->error($e->getMessage()); + return 1; + } + try { $group_id = $this->get_group_id(); @@ -190,6 +206,38 @@ class add extends \phpbb\console\command\command ); } + /** + * Validate the submitted user data + * + * @param array $data The user data array + * @throws runtime_exception if any data fails validation + * @return null + */ + protected function validate_user_data($data) + { + if (!function_exists('validate_data')) + { + require($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext); + } + + $error = validate_data($data, array( + 'username' => array( + array('string', false, $this->config['min_name_chars'], $this->config['max_name_chars']), + array('username', '')), + 'new_password' => array( + array('string', false, $this->config['min_pass_chars'], $this->config['max_pass_chars']), + array('password')), + 'email' => array( + array('string', false, 6, 60), + array('user_email')), + )); + + if ($error) + { + throw new runtime_exception(implode("\n", array_map(array($this->user, 'lang'), $error))); + } + } + /** * Get the group id * diff --git a/tests/console/user/add_test.php b/tests/console/user/add_test.php index 5e4b76063e..280adb101d 100644 --- a/tests/console/user/add_test.php +++ b/tests/console/user/add_test.php @@ -49,6 +49,11 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case $config = $this->config = new \phpbb\config\config(array( 'board_timezone' => 'UTC', 'default_lang' => 'en', + 'min_name_chars' => 3, + 'max_name_chars' => 10, + 'min_pass_chars' => 3, + 'max_pass_chars' => 10, + 'pass_complex' => 'PASS_TYPE_ANY', )); $db = $this->db = $this->new_dbal(); @@ -110,6 +115,24 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case } + public function test_add_no_dialog_invalid() + { + $command_tester = $this->get_command_tester(); + + $this->assertEquals(3, $this->get_user_id('Test')); + + $command_tester->execute(array( + 'command' => $this->command_name, + '--username' => 'Test', + '--password' => '1', + '--email' => 'foo' + )); + + $this->assertContains('USERNAME_TAKEN', $command_tester->getDisplay()); + $this->assertContains('TOO_SHORT', $command_tester->getDisplay()); + $this->assertContains('EMAIL_INVALID', $command_tester->getDisplay()); + } + public function get_command_tester() { $application = new Application(); From f32b4c0547a596fcdd935560b23ed660f3d3f87c Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Mon, 29 Feb 2016 11:42:23 -0800 Subject: [PATCH 08/26] [ticket/12684] Add send email option PHPBB3-12684 --- phpBB/language/en/cli.php | 1 + phpBB/phpbb/console/command/user/add.php | 55 ++++++++++++++++++++++++ tests/console/user/add_test.php | 1 + 3 files changed, 57 insertions(+) diff --git a/phpBB/language/en/cli.php b/phpBB/language/en/cli.php index 4c0fbde286..6fb637f3c1 100644 --- a/phpBB/language/en/cli.php +++ b/phpBB/language/en/cli.php @@ -86,6 +86,7 @@ $lang = array_merge($lang, array( 'CLI_DESCRIPTION_USER_ADD_OPTION_USERNAME' => 'Username of the new user', 'CLI_DESCRIPTION_USER_ADD_OPTION_PASSWORD' => 'Password of the new user', 'CLI_DESCRIPTION_USER_ADD_OPTION_EMAIL' => 'E-mail address of the new user', + 'CLI_DESCRIPTION_USER_ADD_OPTION_NOTIFY' => 'Send account activation email to the new user', 'CLI_EXTENSION_DISABLE_FAILURE' => 'Could not disable extension %s', 'CLI_EXTENSION_DISABLE_SUCCESS' => 'Successfully disabled extension %s', diff --git a/phpBB/phpbb/console/command/user/add.php b/phpBB/phpbb/console/command/user/add.php index db06037947..57100a773c 100644 --- a/phpBB/phpbb/console/command/user/add.php +++ b/phpBB/phpbb/console/command/user/add.php @@ -78,6 +78,7 @@ class add extends \phpbb\console\command\command ->addOption('username', null, InputOption::VALUE_REQUIRED, $this->user->lang('CLI_DESCRIPTION_USER_ADD_OPTION_USERNAME')) ->addOption('password', null, InputOption::VALUE_REQUIRED, $this->user->lang('CLI_DESCRIPTION_USER_ADD_OPTION_PASSWORD')) ->addOption('email', null, InputOption::VALUE_REQUIRED, $this->user->lang('CLI_DESCRIPTION_USER_ADD_OPTION_EMAIL')) + ->addOption('send-email', null, InputOption::VALUE_NONE, $this->user->lang('CLI_CONFIG_PRINT_WITHOUT_NEWLINE')) ; } @@ -166,6 +167,12 @@ class add extends \phpbb\console\command\command } $user_id = user_add($user_row); + + if ($input->getOption('send-email') && $this->config['email_enable']) + { + $this->send_activation_email($user_id, $data); + } + $io->success($this->user->lang('SUCCESS_ADD_USER', $username)); return 0; @@ -263,4 +270,52 @@ class add extends \phpbb\console\command\command return $row['group_id']; } + + /** + * Send account activation email + * + * @param int $user_id The new user's id + * @param array $data The user data array + * @return null + */ + protected function send_activation_email($user_id, $data) + { + if ($this->config['require_activation'] == USER_ACTIVATION_SELF) + { + $email_template = 'user_welcome_inactive'; + $user_actkey = gen_rand_string(mt_rand(6, 10)); + } + else if ($this->config['require_activation'] == USER_ACTIVATION_ADMIN) + { + $email_template = 'admin_welcome_inactive'; + $user_actkey = gen_rand_string(mt_rand(6, 10)); + } + else + { + $email_template = 'user_welcome'; + $user_actkey = ''; + } + + if (!class_exists('messenger')) + { + require($this->phpbb_root_path . 'includes/functions_messenger.' . $this->php_ext); + } + + $messenger = new \messenger(false); + + $messenger->template($email_template, $this->user->lang_name); + + $messenger->to($data['email'], $data['username']); + + $messenger->anti_abuse_headers($this->config, $this->user); + + $messenger->assign_vars(array( + 'WELCOME_MSG' => htmlspecialchars_decode($this->user->lang('WELCOME_SUBJECT', $this->config['sitename'])), + 'USERNAME' => htmlspecialchars_decode($data['username']), + 'PASSWORD' => htmlspecialchars_decode($data['new_password']), + 'U_ACTIVATE' => generate_board_url() . "/ucp.{$this->php_ext}?mode=activate&u=$user_id&k=$user_actkey") + ); + + $messenger->send(NOTIFY_EMAIL); + } } diff --git a/tests/console/user/add_test.php b/tests/console/user/add_test.php index 280adb101d..a673d5eb6c 100644 --- a/tests/console/user/add_test.php +++ b/tests/console/user/add_test.php @@ -49,6 +49,7 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case $config = $this->config = new \phpbb\config\config(array( 'board_timezone' => 'UTC', 'default_lang' => 'en', + 'email_enable' => false, 'min_name_chars' => 3, 'max_name_chars' => 10, 'min_pass_chars' => 3, From 637b02690db0d3b7af2915c1da4bacc8fa054f6c Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Mon, 29 Feb 2016 13:28:42 -0800 Subject: [PATCH 09/26] [ticket/12684] Update to use non-deprecated methods PHPBB3-12684 --- .../default/container/services_console.yml | 25 +- phpBB/phpbb/console/command/user/add.php | 289 +++++++++--------- 2 files changed, 154 insertions(+), 160 deletions(-) diff --git a/phpBB/config/default/container/services_console.yml b/phpBB/config/default/container/services_console.yml index 006df0d7b3..0a28c0ed1f 100644 --- a/phpBB/config/default/container/services_console.yml +++ b/phpBB/config/default/container/services_console.yml @@ -175,18 +175,6 @@ services: tags: - { name: console.command } - console.command.user.add: - class: phpbb\console\command\user\add - arguments: - - '@user' - - '@dbal.conn' - - '@config' - - '@passwords.manager' - - '%core.root_path%' - - '%core.php_ext%' - tags: - - { name: console.command } - console.command.reparser.list: class: phpbb\console\command\reparser\list_all arguments: @@ -231,3 +219,16 @@ services: - '@user' tags: - { name: console.command } + + console.command.user.add: + class: phpbb\console\command\user\add + arguments: + - '@user' + - '@dbal.conn' + - '@config' + - '@language' + - '@passwords.manager' + - '%core.root_path%' + - '%core.php_ext%' + tags: + - { name: console.command } diff --git a/phpBB/phpbb/console/command/user/add.php b/phpBB/phpbb/console/command/user/add.php index 57100a773c..be9f484aeb 100644 --- a/phpBB/phpbb/console/command/user/add.php +++ b/phpBB/phpbb/console/command/user/add.php @@ -1,15 +1,15 @@ -* @license GNU General Public License, version 2 (GPL-2.0) -* -* For full copyright and license information, please see -* the docs/CREDITS.txt file. -* -*/ + * + * This file is part of the phpBB Forum Software package. + * + * @copyright (c) phpBB Limited + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ namespace phpbb\console\command\user; @@ -17,6 +17,7 @@ use phpbb\exception\runtime_exception; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Question\Question; use Symfony\Component\Console\Style\SymfonyStyle; class add extends \phpbb\console\command\command @@ -27,109 +28,125 @@ class add extends \phpbb\console\command\command /** @var \phpbb\config\config */ protected $config; + /** @var \phpbb\language\language */ + protected $language; + /** @var \phpbb\passwords\manager */ protected $password_manager; /** - * phpBB root path - * @var string - */ + * phpBB root path + * + * @var string + */ protected $phpbb_root_path; /** - * PHP extension. - * - * @var string - */ + * PHP extension. + * + * @var string + */ protected $php_ext; /** - * Construct method - * - * @param \phpbb\user $user The user object used for language information - * @param \phpbb\db\driver\driver_interface $db The database in wich will be inserted the user - * @param \phpbb\config\config $config The config object used to get default language and timezone - * @param \phpbb\passwords\manager $password_manager The password manager used to store the user's password - * @param string $phpbb_root_path Root path - * @param string $php_ext PHP extension - */ - public function __construct(\phpbb\user $user, \phpbb\db\driver\driver_interface $db, \phpbb\config\config $config, \phpbb\passwords\manager $password_manager, $phpbb_root_path, $php_ext) + * Construct method + * + * @param \phpbb\user $user + * @param \phpbb\db\driver\driver_interface $db + * @param \phpbb\config\config $config + * @param \phpbb\language\language $language + * @param \phpbb\passwords\manager $password_manager + * @param string $phpbb_root_path + * @param string $php_ext + */ + public function __construct(\phpbb\user $user, \phpbb\db\driver\driver_interface $db, \phpbb\config\config $config, \phpbb\language\language $language, \phpbb\passwords\manager $password_manager, $phpbb_root_path, $php_ext) { $this->db = $db; $this->config = $config; + $this->language = $language; $this->password_manager = $password_manager; $this->phpbb_root_path = $phpbb_root_path; $this->php_ext = $php_ext; - $user->add_lang('ucp'); + $language->add_lang('ucp'); parent::__construct($user); } /** - * Sets the command name and description - * - * @return null - */ + * Sets the command name and description + * + * @return null + */ protected function configure() { $this ->setName('user:add') - ->setDescription($this->user->lang('CLI_DESCRIPTION_USER_ADD')) - ->addOption('username', null, InputOption::VALUE_REQUIRED, $this->user->lang('CLI_DESCRIPTION_USER_ADD_OPTION_USERNAME')) - ->addOption('password', null, InputOption::VALUE_REQUIRED, $this->user->lang('CLI_DESCRIPTION_USER_ADD_OPTION_PASSWORD')) - ->addOption('email', null, InputOption::VALUE_REQUIRED, $this->user->lang('CLI_DESCRIPTION_USER_ADD_OPTION_EMAIL')) - ->addOption('send-email', null, InputOption::VALUE_NONE, $this->user->lang('CLI_CONFIG_PRINT_WITHOUT_NEWLINE')) + ->setDescription($this->language->lang('CLI_DESCRIPTION_USER_ADD')) + ->addOption('username', null, InputOption::VALUE_REQUIRED, $this->language->lang('CLI_DESCRIPTION_USER_ADD_OPTION_USERNAME')) + ->addOption('password', null, InputOption::VALUE_REQUIRED, $this->language->lang('CLI_DESCRIPTION_USER_ADD_OPTION_PASSWORD')) + ->addOption('email', null, InputOption::VALUE_REQUIRED, $this->language->lang('CLI_DESCRIPTION_USER_ADD_OPTION_EMAIL')) + ->addOption('send-email', null, InputOption::VALUE_NONE, $this->language->lang('CLI_CONFIG_PRINT_WITHOUT_NEWLINE')) ; } /** - * Executes the command user:add - * - * If not given in option, asks the username, password and email. - * Then a new user is added in the database, with language and timezone found in the $config passed to the constructor, and the group_id found in the database. - * - * @param InputInterface $input The input stream used to get the options - * @param OutputInterface $output The output stream, used to print messages - * - * @return int 0 if all is well, 1 if a database error occured while trying to get the group_id - */ + * Executes the command user:add + * + * Adds a new user to the database. If options are not provided, it will ask for the username, password and email. + * User is added to the registered user group. Language and timezone default to $config settings. + * + * @param InputInterface $input The input stream used to get the options + * @param OutputInterface $output The output stream, used to print messages + * + * @return int 0 if all is well, 1 if any errors occurred + */ protected function execute(InputInterface $input, OutputInterface $output) { $io = new SymfonyStyle($input, $output); - $dialog = $this->getHelperSet()->get('dialog'); - - $username = $input->getOption('username'); - if (!$username) { - $username = $dialog->ask( - $output, - $this->user->lang('USERNAME') . $this->user->lang('COLON') . ' ', - null - ); - } - - $password = $input->getOption('password'); - if (!$password) - { - $password = $this->get_password($output, $dialog); - } - - $email = $input->getOption('email'); - if (!$email) - { - $email = $dialog->ask( - $output, - $this->user->lang('EMAIL_ADDRESS') . $this->user->lang('COLON') . ' ', - null - ); - } + $helper = $this->getHelper('question'); $data = array( - 'username' => $username, - 'new_password' => $password, - 'email' => $email, + 'username' => $input->getOption('username'), + 'new_password' => $input->getOption('password'), + 'email' => $input->getOption('email'), ); + if (!$data['username']) + { + $question = new Question($this->ask_user('USERNAME'), null); + $data['username'] = $helper->ask($input, $output, $question); + } + + if (!$data['new_password']) + { + $self = $this; + $question = new Question($this->ask_user('PASSWORD')); + $question->setValidator(function ($value) use ($self, $helper, $input, $output) { + $question = new Question($self->ask_user('CONFIRM_PASSWORD')); + $question->setHidden(true); + $question->setHiddenFallback(false); + + $confirm = $helper->ask($input, $output, $question); + if ($confirm != $value) + { + throw new runtime_exception($self->language->lang('NEW_PASSWORD_ERROR')); + } + return $value; + }); + $question->setHidden(true); + $question->setHiddenFallback(false); + $question->setMaxAttempts(5); + + $data['new_password'] = $helper->ask($input, $output, $question); + } + + if (!$data['email']) + { + $question = new Question($this->ask_user('EMAIL_ADDRESS'), null); + $data['email'] = $helper->ask($input, $output, $question); + } + try { $this->validate_user_data($data); @@ -151,14 +168,14 @@ class add extends \phpbb\console\command\command } $user_row = array( - 'username' => $username, - 'user_password' => $this->password_manager->hash($password), - 'user_email' => $email, - 'group_id' => $group_id, - 'user_timezone' => $this->config['board_timezone'], - 'user_lang' => $this->config['default_lang'], - 'user_type' => USER_NORMAL, - 'user_regdate' => time(), + 'username' => $data['username'], + 'user_password' => $this->password_manager->hash($data['new_password']), + 'user_email' => $data['email'], + 'group_id' => $group_id, + 'user_timezone' => $this->config['board_timezone'], + 'user_lang' => $this->config['default_lang'], + 'user_type' => USER_NORMAL, + 'user_regdate' => time(), ); if (!function_exists('user_add')) @@ -166,60 +183,25 @@ class add extends \phpbb\console\command\command require($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext); } - $user_id = user_add($user_row); + $user_id = (int) user_add($user_row); if ($input->getOption('send-email') && $this->config['email_enable']) { $this->send_activation_email($user_id, $data); } - $io->success($this->user->lang('SUCCESS_ADD_USER', $username)); + $io->success($this->language->lang('SUCCESS_ADD_USER', $data['username'])); return 0; } /** - * Get the password - * - * Asks a password to the user and asks for confirmation. - * This is repeated until the password match is confirmed. - * - * @param OutputInterface $output The output stream, where messages are printed - * @param \Symfony\Component\Console\Helper\DialogHelper $dialog The dialog helper used to get answers to questions asked to the user - * - * @return null - */ - protected function get_password($output, $dialog) - { - $current_user = $this->user; - return $dialog->askHiddenResponseAndValidate( - $output, - $current_user->lang('PASSWORD') . $current_user->lang('COLON') . ' ', - function ($answer) use ($dialog, $output, $current_user) - { - $confirm = $dialog->askHiddenResponse( - $output, - $current_user->lang('CONFIRM_PASSWORD') . $current_user->lang('COLON') . ' ', - null - ); - if ($confirm != $answer) - { - throw new runtime_exception($current_user->lang('NEW_PASSWORD_ERROR')); - } - return $answer; - }, - false, - null - ); - } - - /** - * Validate the submitted user data - * - * @param array $data The user data array - * @throws runtime_exception if any data fails validation - * @return null - */ + * Validate the submitted user data + * + * @param array $data The user data array + * @throws runtime_exception if any data fails validation + * @return null + */ protected function validate_user_data($data) { if (!function_exists('validate_data')) @@ -228,13 +210,13 @@ class add extends \phpbb\console\command\command } $error = validate_data($data, array( - 'username' => array( + 'username' => array( array('string', false, $this->config['min_name_chars'], $this->config['max_name_chars']), array('username', '')), - 'new_password' => array( + 'new_password' => array( array('string', false, $this->config['min_pass_chars'], $this->config['max_pass_chars']), array('password')), - 'email' => array( + 'email' => array( array('string', false, 6, 60), array('user_email')), )); @@ -246,13 +228,13 @@ class add extends \phpbb\console\command\command } /** - * Get the group id - * - * Go and find in the database the group_id corresponding to 'REGISTERED' - * - * @throws runtime_exception if the group id does not exist in database. - * @return null - */ + * Get the group id + * + * Go and find in the database the group_id corresponding to 'REGISTERED' + * + * @throws runtime_exception if the group id does not exist in database. + * @return null + */ protected function get_group_id() { $sql = 'SELECT group_id @@ -265,19 +247,19 @@ class add extends \phpbb\console\command\command if (!$row || !$row['group_id']) { - throw new runtime_exception($this->user->lang('NO_GROUP')); + throw new runtime_exception($this->language->lang('NO_GROUP')); } return $row['group_id']; } /** - * Send account activation email - * - * @param int $user_id The new user's id - * @param array $data The user data array - * @return null - */ + * Send account activation email + * + * @param int $user_id The new user's id + * @param array $data The user data array + * @return null + */ protected function send_activation_email($user_id, $data) { if ($this->config['require_activation'] == USER_ACTIVATION_SELF) @@ -310,12 +292,23 @@ class add extends \phpbb\console\command\command $messenger->anti_abuse_headers($this->config, $this->user); $messenger->assign_vars(array( - 'WELCOME_MSG' => htmlspecialchars_decode($this->user->lang('WELCOME_SUBJECT', $this->config['sitename'])), - 'USERNAME' => htmlspecialchars_decode($data['username']), - 'PASSWORD' => htmlspecialchars_decode($data['new_password']), - 'U_ACTIVATE' => generate_board_url() . "/ucp.{$this->php_ext}?mode=activate&u=$user_id&k=$user_actkey") + 'WELCOME_MSG' => htmlspecialchars_decode($this->language->lang('WELCOME_SUBJECT', $this->config['sitename'])), + 'USERNAME' => htmlspecialchars_decode($data['username']), + 'PASSWORD' => htmlspecialchars_decode($data['new_password']), + 'U_ACTIVATE' => generate_board_url() . "/ucp.{$this->php_ext}?mode=activate&u=$user_id&k=$user_actkey") ); $messenger->send(NOTIFY_EMAIL); } + + /** + * Helper to translate questions to the user + * + * @param string $key The language key + * @return string The language key translated with a colon and space appended + */ + protected function ask_user($key) + { + return $this->language->lang($key) . $this->language->lang('COLON') . ' '; + } } From fe31060fca4a07696029c50af2c74c88ab5d85fe Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Mon, 29 Feb 2016 14:23:32 -0800 Subject: [PATCH 10/26] [ticket/12684] Fix tests PHPBB3-12684 --- phpBB/phpbb/console/command/user/add.php | 4 +--- tests/console/user/add_test.php | 22 ++++++++++++++-------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/phpBB/phpbb/console/command/user/add.php b/phpBB/phpbb/console/command/user/add.php index be9f484aeb..46056caffd 100644 --- a/phpBB/phpbb/console/command/user/add.php +++ b/phpBB/phpbb/console/command/user/add.php @@ -125,7 +125,6 @@ class add extends \phpbb\console\command\command $question->setValidator(function ($value) use ($self, $helper, $input, $output) { $question = new Question($self->ask_user('CONFIRM_PASSWORD')); $question->setHidden(true); - $question->setHiddenFallback(false); $confirm = $helper->ask($input, $output, $question); if ($confirm != $value) @@ -135,7 +134,6 @@ class add extends \phpbb\console\command\command return $value; }); $question->setHidden(true); - $question->setHiddenFallback(false); $question->setMaxAttempts(5); $data['new_password'] = $helper->ask($input, $output, $question); @@ -223,7 +221,7 @@ class add extends \phpbb\console\command\command if ($error) { - throw new runtime_exception(implode("\n", array_map(array($this->user, 'lang'), $error))); + throw new runtime_exception(implode("\n", array_map(array($this->language, 'lang'), $error))); } } diff --git a/tests/console/user/add_test.php b/tests/console/user/add_test.php index a673d5eb6c..8fc921babf 100644 --- a/tests/console/user/add_test.php +++ b/tests/console/user/add_test.php @@ -24,9 +24,10 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case protected $db; protected $config; protected $user; + protected $language; protected $passwords_manager; protected $command_name; - protected $dialog; + protected $question; protected $phpbb_root_path; protected $php_ext; @@ -59,11 +60,16 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case $db = $this->db = $this->new_dbal(); + $this->language = $this->getMockBuilder('\phpbb\language\language') + ->disableOriginalConstructor() + ->getMock(); + $this->language->expects($this->any()) + ->method('lang') + ->will($this->returnArgument(0)); $user = $this->user = $this->getMock('\phpbb\user', array(), array( - new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)), - '\phpbb\datetime') - ); - $this->user->method('lang')->will($this->returnArgument(0)); + $this->language, + '\phpbb\datetime' + )); $driver_helper = new \phpbb\passwords\driver\helper($this->config); $passwords_drivers = array( @@ -105,7 +111,7 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case $this->assertEquals(2, $this->get_user_id('Admin')); - $this->dialog->setInputStream($this->getInputStream("bar\npassword\npassword\nbar@test.com\n")); + $this->question->setInputStream($this->getInputStream("bar\npassword\npassword\nbar@test.com\n")); $command_tester->execute(array( 'command' => $this->command_name, @@ -137,11 +143,11 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case public function get_command_tester() { $application = new Application(); - $application->add(new add($this->user, $this->db, $this->config, $this->passwords_manager, $this->phpbb_root_path, $this->php_ext)); + $application->add(new add($this->user, $this->db, $this->config, $this->language, $this->passwords_manager, $this->phpbb_root_path, $this->php_ext)); $command = $application->find('user:add'); $this->command_name = $command->getName(); - $this->dialog = $command->getHelper('dialog'); + $this->question = $command->getHelper('question'); return new CommandTester($command); } From a84f77bf25c897067f5078ae2139214e5882192b Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Mon, 29 Feb 2016 14:28:02 -0800 Subject: [PATCH 11/26] [ticket/12684] Another little fix PHPBB3-12684 --- phpBB/phpbb/console/command/user/add.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/phpbb/console/command/user/add.php b/phpBB/phpbb/console/command/user/add.php index 46056caffd..647c353dd0 100644 --- a/phpBB/phpbb/console/command/user/add.php +++ b/phpBB/phpbb/console/command/user/add.php @@ -68,7 +68,7 @@ class add extends \phpbb\console\command\command $this->phpbb_root_path = $phpbb_root_path; $this->php_ext = $php_ext; - $language->add_lang('ucp'); + $this->language->add_lang('ucp'); parent::__construct($user); } From 97c6cce687d94071d6f902af272631c21dbacfcb Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Mon, 29 Feb 2016 14:50:19 -0800 Subject: [PATCH 12/26] [ticket/12684] Some code clean up PHPBB3-12684 --- phpBB/phpbb/console/command/user/add.php | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/phpBB/phpbb/console/command/user/add.php b/phpBB/phpbb/console/command/user/add.php index 647c353dd0..d85c9599c5 100644 --- a/phpBB/phpbb/console/command/user/add.php +++ b/phpBB/phpbb/console/command/user/add.php @@ -125,9 +125,7 @@ class add extends \phpbb\console\command\command $question->setValidator(function ($value) use ($self, $helper, $input, $output) { $question = new Question($self->ask_user('CONFIRM_PASSWORD')); $question->setHidden(true); - - $confirm = $helper->ask($input, $output, $question); - if ($confirm != $value) + if ($helper->ask($input, $output, $question) != $value) { throw new runtime_exception($self->language->lang('NEW_PASSWORD_ERROR')); } @@ -148,15 +146,6 @@ class add extends \phpbb\console\command\command try { $this->validate_user_data($data); - } - catch (runtime_exception $e) - { - $io->error($e->getMessage()); - return 1; - } - - try - { $group_id = $this->get_group_id(); } catch (runtime_exception $e) From e905c6226db367ab3a04e373bee56a85d8251cc7 Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Mon, 29 Feb 2016 16:52:17 -0800 Subject: [PATCH 13/26] [ticket/12684] Fix a few mistakes and clean it up PHPBB3-12684 --- phpBB/phpbb/console/command/user/add.php | 32 ++++++++++++++++++------ 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/phpBB/phpbb/console/command/user/add.php b/phpBB/phpbb/console/command/user/add.php index d85c9599c5..59d9eaa117 100644 --- a/phpBB/phpbb/console/command/user/add.php +++ b/phpBB/phpbb/console/command/user/add.php @@ -82,10 +82,30 @@ class add extends \phpbb\console\command\command $this ->setName('user:add') ->setDescription($this->language->lang('CLI_DESCRIPTION_USER_ADD')) - ->addOption('username', null, InputOption::VALUE_REQUIRED, $this->language->lang('CLI_DESCRIPTION_USER_ADD_OPTION_USERNAME')) - ->addOption('password', null, InputOption::VALUE_REQUIRED, $this->language->lang('CLI_DESCRIPTION_USER_ADD_OPTION_PASSWORD')) - ->addOption('email', null, InputOption::VALUE_REQUIRED, $this->language->lang('CLI_DESCRIPTION_USER_ADD_OPTION_EMAIL')) - ->addOption('send-email', null, InputOption::VALUE_NONE, $this->language->lang('CLI_CONFIG_PRINT_WITHOUT_NEWLINE')) + ->addOption( + 'username', + null, + InputOption::VALUE_REQUIRED, + $this->language->lang('CLI_DESCRIPTION_USER_ADD_OPTION_USERNAME') + ) + ->addOption( + 'password', + null, + InputOption::VALUE_REQUIRED, + $this->language->lang('CLI_DESCRIPTION_USER_ADD_OPTION_PASSWORD') + ) + ->addOption( + 'email', + null, + InputOption::VALUE_REQUIRED, + $this->language->lang('CLI_DESCRIPTION_USER_ADD_OPTION_EMAIL') + ) + ->addOption( + 'send-email', + null, + InputOption::VALUE_NONE, + $this->language->lang('CLI_DESCRIPTION_USER_ADD_OPTION_NOTIFY') + ) ; } @@ -271,13 +291,9 @@ class add extends \phpbb\console\command\command } $messenger = new \messenger(false); - $messenger->template($email_template, $this->user->lang_name); - $messenger->to($data['email'], $data['username']); - $messenger->anti_abuse_headers($this->config, $this->user); - $messenger->assign_vars(array( 'WELCOME_MSG' => htmlspecialchars_decode($this->language->lang('WELCOME_SUBJECT', $this->config['sitename'])), 'USERNAME' => htmlspecialchars_decode($data['username']), From 0ca4484525f69ea5e7c5cd28f671364604725e40 Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Mon, 29 Feb 2016 21:03:32 -0800 Subject: [PATCH 14/26] [ticket/12684] Move all lang keys to cli PHPBB3-12684 --- phpBB/language/en/acp/common.php | 2 -- phpBB/language/en/cli.php | 2 ++ phpBB/phpbb/console/command/user/add.php | 2 +- tests/console/user/add_test.php | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/phpBB/language/en/acp/common.php b/phpBB/language/en/acp/common.php index 300f1c2626..4a70aafc6f 100644 --- a/phpBB/language/en/acp/common.php +++ b/phpBB/language/en/acp/common.php @@ -309,8 +309,6 @@ $lang = array_merge($lang, array( 'SHOW_ALL_OPERATIONS' => 'Show all operations', - 'SUCCESS_ADD_USER' => 'Successfully added user %s.', - 'TASKS_NOT_READY' => 'Not ready tasks:', 'TASKS_READY' => 'Ready tasks:', 'TOTAL_SIZE' => 'Total size', diff --git a/phpBB/language/en/cli.php b/phpBB/language/en/cli.php index 6fb637f3c1..dab7e2efdd 100644 --- a/phpBB/language/en/cli.php +++ b/phpBB/language/en/cli.php @@ -124,6 +124,8 @@ $lang = array_merge($lang, array( 'CLI_THUMBNAIL_NOTHING_TO_GENERATE' => 'No thumbnails to generate.', 'CLI_THUMBNAIL_NOTHING_TO_DELETE' => 'No thumbnails to delete.', + + 'CLI_USER_ADD_SUCCESS' => 'Successfully added user %s.', )); // Additional help for commands. diff --git a/phpBB/phpbb/console/command/user/add.php b/phpBB/phpbb/console/command/user/add.php index 59d9eaa117..6edd214d14 100644 --- a/phpBB/phpbb/console/command/user/add.php +++ b/phpBB/phpbb/console/command/user/add.php @@ -197,7 +197,7 @@ class add extends \phpbb\console\command\command $this->send_activation_email($user_id, $data); } - $io->success($this->language->lang('SUCCESS_ADD_USER', $data['username'])); + $io->success($this->language->lang('CLI_USER_ADD_SUCCESS', $data['username'])); return 0; } diff --git a/tests/console/user/add_test.php b/tests/console/user/add_test.php index 8fc921babf..de146ef6ec 100644 --- a/tests/console/user/add_test.php +++ b/tests/console/user/add_test.php @@ -102,7 +102,7 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case )); $this->assertNotEquals(null, $this->get_user_id('foo')); - $this->assertContains('SUCCESS_ADD_USER', $command_tester->getDisplay()); + $this->assertContains('CLI_USER_ADD_SUCCESS', $command_tester->getDisplay()); } public function test_add_dialog() @@ -118,7 +118,7 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case )); $this->assertNotEquals(null, $this->get_user_id('bar')); - $this->assertContains('SUCCESS_ADD_USER', $command_tester->getDisplay()); + $this->assertContains('CLI_USER_ADD_SUCCESS', $command_tester->getDisplay()); } From b17f9fc81c40a6b7a5a45cc53e378274e1b6922b Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Mon, 29 Feb 2016 21:04:16 -0800 Subject: [PATCH 15/26] [ticket/12684] Allowed to use $this in enclosure PHPBB3-12684 --- phpBB/phpbb/console/command/user/add.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/phpBB/phpbb/console/command/user/add.php b/phpBB/phpbb/console/command/user/add.php index 6edd214d14..6f0988db5e 100644 --- a/phpBB/phpbb/console/command/user/add.php +++ b/phpBB/phpbb/console/command/user/add.php @@ -140,14 +140,13 @@ class add extends \phpbb\console\command\command if (!$data['new_password']) { - $self = $this; $question = new Question($this->ask_user('PASSWORD')); - $question->setValidator(function ($value) use ($self, $helper, $input, $output) { - $question = new Question($self->ask_user('CONFIRM_PASSWORD')); + $question->setValidator(function ($value) use ($helper, $input, $output) { + $question = new Question($this->ask_user('CONFIRM_PASSWORD')); $question->setHidden(true); if ($helper->ask($input, $output, $question) != $value) { - throw new runtime_exception($self->language->lang('NEW_PASSWORD_ERROR')); + throw new runtime_exception($this->language->lang('NEW_PASSWORD_ERROR')); } return $value; }); From 07b8c0663d17bc891dcb19b7394a24a108316d36 Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Mon, 29 Feb 2016 21:04:41 -0800 Subject: [PATCH 16/26] [ticket/12684] Additional clean up PHPBB3-12684 --- phpBB/phpbb/console/command/user/add.php | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/phpBB/phpbb/console/command/user/add.php b/phpBB/phpbb/console/command/user/add.php index 6f0988db5e..3e4fbe994f 100644 --- a/phpBB/phpbb/console/command/user/add.php +++ b/phpBB/phpbb/console/command/user/add.php @@ -184,11 +184,6 @@ class add extends \phpbb\console\command\command 'user_regdate' => time(), ); - if (!function_exists('user_add')) - { - require($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext); - } - $user_id = (int) user_add($user_row); if ($input->getOption('send-email') && $this->config['email_enable']) @@ -294,10 +289,10 @@ class add extends \phpbb\console\command\command $messenger->to($data['email'], $data['username']); $messenger->anti_abuse_headers($this->config, $this->user); $messenger->assign_vars(array( - 'WELCOME_MSG' => htmlspecialchars_decode($this->language->lang('WELCOME_SUBJECT', $this->config['sitename'])), - 'USERNAME' => htmlspecialchars_decode($data['username']), - 'PASSWORD' => htmlspecialchars_decode($data['new_password']), - 'U_ACTIVATE' => generate_board_url() . "/ucp.{$this->php_ext}?mode=activate&u=$user_id&k=$user_actkey") + 'WELCOME_MSG' => htmlspecialchars_decode($this->language->lang('WELCOME_SUBJECT', $this->config['sitename'])), + 'USERNAME' => htmlspecialchars_decode($data['username']), + 'PASSWORD' => htmlspecialchars_decode($data['new_password']), + 'U_ACTIVATE' => generate_board_url() . "/ucp.{$this->php_ext}?mode=activate&u=$user_id&k=$user_actkey") ); $messenger->send(NOTIFY_EMAIL); From 7b0452e53aa750761c3042183e5ad7dd2cb3b344 Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Mon, 29 Feb 2016 23:24:01 -0800 Subject: [PATCH 17/26] [ticket/12684] Remove unnecessary null arguments PHPBB3-12684 --- phpBB/phpbb/console/command/user/add.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpBB/phpbb/console/command/user/add.php b/phpBB/phpbb/console/command/user/add.php index 3e4fbe994f..6937cd17d2 100644 --- a/phpBB/phpbb/console/command/user/add.php +++ b/phpBB/phpbb/console/command/user/add.php @@ -134,7 +134,7 @@ class add extends \phpbb\console\command\command if (!$data['username']) { - $question = new Question($this->ask_user('USERNAME'), null); + $question = new Question($this->ask_user('USERNAME')); $data['username'] = $helper->ask($input, $output, $question); } @@ -158,7 +158,7 @@ class add extends \phpbb\console\command\command if (!$data['email']) { - $question = new Question($this->ask_user('EMAIL_ADDRESS'), null); + $question = new Question($this->ask_user('EMAIL_ADDRESS')); $data['email'] = $helper->ask($input, $output, $question); } From 8c5d6efad5262417db341491f7c625832b226b22 Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Sat, 5 Mar 2016 09:20:04 -0800 Subject: [PATCH 18/26] [ticket/12684] Add an error on user creation failure PHPBB3-12684 --- phpBB/phpbb/console/command/user/add.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/phpBB/phpbb/console/command/user/add.php b/phpBB/phpbb/console/command/user/add.php index 6937cd17d2..187062f020 100644 --- a/phpBB/phpbb/console/command/user/add.php +++ b/phpBB/phpbb/console/command/user/add.php @@ -185,6 +185,12 @@ class add extends \phpbb\console\command\command ); $user_id = (int) user_add($user_row); + + if (!$user_id) + { + $io->error($this->language->lang('AUTH_NO_PROFILE_CREATED')); + return 1; + } if ($input->getOption('send-email') && $this->config['email_enable']) { From cc941e6e05d3be75ef60d9473dee43cd6320c85b Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Sat, 5 Mar 2016 10:41:35 -0800 Subject: [PATCH 19/26] [ticket/12684] Remove whitespace PHPBB3-12684 --- phpBB/phpbb/console/command/user/add.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/phpbb/console/command/user/add.php b/phpBB/phpbb/console/command/user/add.php index 187062f020..943cdb67a5 100644 --- a/phpBB/phpbb/console/command/user/add.php +++ b/phpBB/phpbb/console/command/user/add.php @@ -185,7 +185,7 @@ class add extends \phpbb\console\command\command ); $user_id = (int) user_add($user_row); - + if (!$user_id) { $io->error($this->language->lang('AUTH_NO_PROFILE_CREATED')); From 0c1e7c2f9cf81c1e487a0dcd2fb9e053de14c275 Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Tue, 8 Mar 2016 00:06:10 -0800 Subject: [PATCH 20/26] [ticket/12684] Add shorthand alternates to the options PHPBB3-12684 --- phpBB/phpbb/console/command/user/add.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/phpBB/phpbb/console/command/user/add.php b/phpBB/phpbb/console/command/user/add.php index 943cdb67a5..42ff000ca4 100644 --- a/phpBB/phpbb/console/command/user/add.php +++ b/phpBB/phpbb/console/command/user/add.php @@ -84,19 +84,19 @@ class add extends \phpbb\console\command\command ->setDescription($this->language->lang('CLI_DESCRIPTION_USER_ADD')) ->addOption( 'username', - null, + 'U', InputOption::VALUE_REQUIRED, $this->language->lang('CLI_DESCRIPTION_USER_ADD_OPTION_USERNAME') ) ->addOption( 'password', - null, + 'P', InputOption::VALUE_REQUIRED, $this->language->lang('CLI_DESCRIPTION_USER_ADD_OPTION_PASSWORD') ) ->addOption( 'email', - null, + 'E', InputOption::VALUE_REQUIRED, $this->language->lang('CLI_DESCRIPTION_USER_ADD_OPTION_EMAIL') ) From 0f8790dd2e804ccbc7d6c1e444c1d31412d7aaa4 Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Tue, 8 Mar 2016 00:32:59 -0800 Subject: [PATCH 21/26] [ticket/12684] Add extended help for the user:add command PHPBB3-12684 --- phpBB/language/en/cli.php | 3 +++ phpBB/phpbb/console/command/user/add.php | 1 + 2 files changed, 4 insertions(+) diff --git a/phpBB/language/en/cli.php b/phpBB/language/en/cli.php index dab7e2efdd..481a417553 100644 --- a/phpBB/language/en/cli.php +++ b/phpBB/language/en/cli.php @@ -131,4 +131,7 @@ $lang = array_merge($lang, array( // Additional help for commands. $lang = array_merge($lang, array( 'CLI_HELP_CRON_RUN' => $lang['CLI_DESCRIPTION_CRON_RUN'] . ' Optionally you can specify a cron task name to run only the specified cron task.', + 'CLI_HELP_USER_ADD' => 'The %command.name% command adds a new user: +If this command is run without options, you will be prompted to enter them. +To optionally send an email to the new user, use the --send-email option.', )); diff --git a/phpBB/phpbb/console/command/user/add.php b/phpBB/phpbb/console/command/user/add.php index 42ff000ca4..a0da15915d 100644 --- a/phpBB/phpbb/console/command/user/add.php +++ b/phpBB/phpbb/console/command/user/add.php @@ -82,6 +82,7 @@ class add extends \phpbb\console\command\command $this ->setName('user:add') ->setDescription($this->language->lang('CLI_DESCRIPTION_USER_ADD')) + ->setHelp($this->language->lang('CLI_HELP_USER_ADD')) ->addOption( 'username', 'U', From 00bf1ef8b89b8dbae0a22bf3d8b3536d5226f32d Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Tue, 8 Mar 2016 10:46:34 -0800 Subject: [PATCH 22/26] [ticket/12684] Update option help PHPBB3-12684 --- phpBB/language/en/cli.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/language/en/cli.php b/phpBB/language/en/cli.php index 481a417553..6cb516ebfd 100644 --- a/phpBB/language/en/cli.php +++ b/phpBB/language/en/cli.php @@ -86,7 +86,7 @@ $lang = array_merge($lang, array( 'CLI_DESCRIPTION_USER_ADD_OPTION_USERNAME' => 'Username of the new user', 'CLI_DESCRIPTION_USER_ADD_OPTION_PASSWORD' => 'Password of the new user', 'CLI_DESCRIPTION_USER_ADD_OPTION_EMAIL' => 'E-mail address of the new user', - 'CLI_DESCRIPTION_USER_ADD_OPTION_NOTIFY' => 'Send account activation email to the new user', + 'CLI_DESCRIPTION_USER_ADD_OPTION_NOTIFY' => 'Send account activation email to the new user (not sent by default)', 'CLI_EXTENSION_DISABLE_FAILURE' => 'Could not disable extension %s', 'CLI_EXTENSION_DISABLE_SUCCESS' => 'Successfully disabled extension %s', From 693664f278e0d99efef9e81c3848fe3f7edf9e7f Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Wed, 9 Mar 2016 15:16:23 -0800 Subject: [PATCH 23/26] [ticket/12684] Extract interactivity to a method PHPBB3-12684 --- phpBB/phpbb/console/command/user/add.php | 89 ++++++++++++++---------- 1 file changed, 52 insertions(+), 37 deletions(-) diff --git a/phpBB/phpbb/console/command/user/add.php b/phpBB/phpbb/console/command/user/add.php index a0da15915d..8151f3827c 100644 --- a/phpBB/phpbb/console/command/user/add.php +++ b/phpBB/phpbb/console/command/user/add.php @@ -125,43 +125,7 @@ class add extends \phpbb\console\command\command { $io = new SymfonyStyle($input, $output); - $helper = $this->getHelper('question'); - - $data = array( - 'username' => $input->getOption('username'), - 'new_password' => $input->getOption('password'), - 'email' => $input->getOption('email'), - ); - - if (!$data['username']) - { - $question = new Question($this->ask_user('USERNAME')); - $data['username'] = $helper->ask($input, $output, $question); - } - - if (!$data['new_password']) - { - $question = new Question($this->ask_user('PASSWORD')); - $question->setValidator(function ($value) use ($helper, $input, $output) { - $question = new Question($this->ask_user('CONFIRM_PASSWORD')); - $question->setHidden(true); - if ($helper->ask($input, $output, $question) != $value) - { - throw new runtime_exception($this->language->lang('NEW_PASSWORD_ERROR')); - } - return $value; - }); - $question->setHidden(true); - $question->setMaxAttempts(5); - - $data['new_password'] = $helper->ask($input, $output, $question); - } - - if (!$data['email']) - { - $question = new Question($this->ask_user('EMAIL_ADDRESS')); - $data['email'] = $helper->ask($input, $output, $question); - } + $data = $this->interact($input, $output); try { @@ -203,6 +167,57 @@ class add extends \phpbb\console\command\command return 0; } + /** + * Interact with the user to obtain the required options + * + * @param InputInterface $input The input stream used to get the options + * @param OutputInterface $output The output stream, used to print messages + * + * @return array Array of required user options + */ + protected function interact(InputInterface $input, OutputInterface $output) + { + $helper = $this->getHelper('question'); + + $data = array( + 'username' => $input->getOption('username'), + 'new_password' => $input->getOption('password'), + 'email' => $input->getOption('email'), + ); + + if (!$data['username']) + { + $question = new Question($this->ask_user('USERNAME')); + $data['username'] = $helper->ask($input, $output, $question); + } + + if (!$data['new_password']) + { + $question = new Question($this->ask_user('PASSWORD')); + $question->setValidator(function ($value) use ($helper, $input, $output) { + $question = new Question($this->ask_user('CONFIRM_PASSWORD')); + $question->setHidden(true); + if ($helper->ask($input, $output, $question) != $value) + { + throw new runtime_exception($this->language->lang('NEW_PASSWORD_ERROR')); + } + return $value; + }); + $question->setHidden(true); + $question->setMaxAttempts(5); + + $data['new_password'] = $helper->ask($input, $output, $question); + } + + if (!$data['email']) + { + $question = new Question($this->ask_user('EMAIL_ADDRESS')); + $data['email'] = $helper->ask($input, $output, $question); + } + + return $data; + } + /** * Validate the submitted user data * From 5917a8e72ef274b2c7c419abe5349ace9d814d8b Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Wed, 9 Mar 2016 17:40:58 -0800 Subject: [PATCH 24/26] [ticket/12684] Use interactive method correctly PHPBB3-12684 --- phpBB/phpbb/console/command/user/add.php | 55 +++++++++++------------- 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/phpBB/phpbb/console/command/user/add.php b/phpBB/phpbb/console/command/user/add.php index 8151f3827c..3df0473acf 100644 --- a/phpBB/phpbb/console/command/user/add.php +++ b/phpBB/phpbb/console/command/user/add.php @@ -22,6 +22,9 @@ use Symfony\Component\Console\Style\SymfonyStyle; class add extends \phpbb\console\command\command { + /** @var array Array of interactively acquired options */ + protected $data; + /** @var \phpbb\db\driver\driver_interface */ protected $db; @@ -125,11 +128,9 @@ class add extends \phpbb\console\command\command { $io = new SymfonyStyle($input, $output); - $data = $this->interact($input, $output); - try { - $this->validate_user_data($data); + $this->validate_user_data(); $group_id = $this->get_group_id(); } catch (runtime_exception $e) @@ -139,9 +140,9 @@ class add extends \phpbb\console\command\command } $user_row = array( - 'username' => $data['username'], - 'user_password' => $this->password_manager->hash($data['new_password']), - 'user_email' => $data['email'], + 'username' => $this->data['username'], + 'user_password' => $this->password_manager->hash($this->data['new_password']), + 'user_email' => $this->data['email'], 'group_id' => $group_id, 'user_timezone' => $this->config['board_timezone'], 'user_lang' => $this->config['default_lang'], @@ -159,39 +160,37 @@ class add extends \phpbb\console\command\command if ($input->getOption('send-email') && $this->config['email_enable']) { - $this->send_activation_email($user_id, $data); + $this->send_activation_email($user_id); } - $io->success($this->language->lang('CLI_USER_ADD_SUCCESS', $data['username'])); + $io->success($this->language->lang('CLI_USER_ADD_SUCCESS', $this->data['username'])); return 0; } /** - * Interact with the user to obtain the required options + * Interacts with the user. * - * @param InputInterface $input The input stream used to get the options - * @param OutputInterface $output The output stream, used to print messages - * - * @return array Array of required user options + * @param InputInterface $input An InputInterface instance + * @param OutputInterface $output An OutputInterface instance */ protected function interact(InputInterface $input, OutputInterface $output) { $helper = $this->getHelper('question'); - $data = array( + $this->data = array( 'username' => $input->getOption('username'), 'new_password' => $input->getOption('password'), 'email' => $input->getOption('email'), ); - if (!$data['username']) + if (!$this->data['username']) { $question = new Question($this->ask_user('USERNAME')); - $data['username'] = $helper->ask($input, $output, $question); + $this->data['username'] = $helper->ask($input, $output, $question); } - if (!$data['new_password']) + if (!$this->data['new_password']) { $question = new Question($this->ask_user('PASSWORD')); $question->setValidator(function ($value) use ($helper, $input, $output) { @@ -206,33 +205,30 @@ class add extends \phpbb\console\command\command $question->setHidden(true); $question->setMaxAttempts(5); - $data['new_password'] = $helper->ask($input, $output, $question); + $this->data['new_password'] = $helper->ask($input, $output, $question); } - if (!$data['email']) + if (!$this->data['email']) { $question = new Question($this->ask_user('EMAIL_ADDRESS')); - $data['email'] = $helper->ask($input, $output, $question); + $this->data['email'] = $helper->ask($input, $output, $question); } - - return $data; } /** * Validate the submitted user data * - * @param array $data The user data array * @throws runtime_exception if any data fails validation * @return null */ - protected function validate_user_data($data) + protected function validate_user_data() { if (!function_exists('validate_data')) { require($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext); } - $error = validate_data($data, array( + $error = validate_data($this->data, array( 'username' => array( array('string', false, $this->config['min_name_chars'], $this->config['max_name_chars']), array('username', '')), @@ -280,10 +276,9 @@ class add extends \phpbb\console\command\command * Send account activation email * * @param int $user_id The new user's id - * @param array $data The user data array * @return null */ - protected function send_activation_email($user_id, $data) + protected function send_activation_email($user_id) { if ($this->config['require_activation'] == USER_ACTIVATION_SELF) { @@ -308,12 +303,12 @@ class add extends \phpbb\console\command\command $messenger = new \messenger(false); $messenger->template($email_template, $this->user->lang_name); - $messenger->to($data['email'], $data['username']); + $messenger->to($this->data['email'], $this->data['username']); $messenger->anti_abuse_headers($this->config, $this->user); $messenger->assign_vars(array( 'WELCOME_MSG' => htmlspecialchars_decode($this->language->lang('WELCOME_SUBJECT', $this->config['sitename'])), - 'USERNAME' => htmlspecialchars_decode($data['username']), - 'PASSWORD' => htmlspecialchars_decode($data['new_password']), + 'USERNAME' => htmlspecialchars_decode($this->data['username']), + 'PASSWORD' => htmlspecialchars_decode($this->data['new_password']), 'U_ACTIVATE' => generate_board_url() . "/ucp.{$this->php_ext}?mode=activate&u=$user_id&k=$user_actkey") ); From 00c2efca605b4dbd323221a3dab2110c81ae3dcd Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Mon, 21 Mar 2016 13:54:41 -0700 Subject: [PATCH 25/26] [ticket/12684] Refactor a test PHPBB3-12684 --- tests/console/user/add_test.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/console/user/add_test.php b/tests/console/user/add_test.php index de146ef6ec..00134c18cd 100644 --- a/tests/console/user/add_test.php +++ b/tests/console/user/add_test.php @@ -143,7 +143,15 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case public function get_command_tester() { $application = new Application(); - $application->add(new add($this->user, $this->db, $this->config, $this->language, $this->passwords_manager, $this->phpbb_root_path, $this->php_ext)); + $application->add(new add( + $this->user, + $this->db, + $this->config, + $this->language, + $this->passwords_manager, + $this->phpbb_root_path, + $this->php_ext + )); $command = $application->find('user:add'); $this->command_name = $command->getName(); From 5b3b0edd8010ccd4735d4000d284d7717bced897 Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Mon, 21 Mar 2016 13:54:50 -0700 Subject: [PATCH 26/26] [ticket/12684] Use a switch statement for readability PHPBB3-12684 --- phpBB/phpbb/console/command/user/add.php | 26 ++++++++++++------------ 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/phpBB/phpbb/console/command/user/add.php b/phpBB/phpbb/console/command/user/add.php index 3df0473acf..df1f4aa54a 100644 --- a/phpBB/phpbb/console/command/user/add.php +++ b/phpBB/phpbb/console/command/user/add.php @@ -280,20 +280,20 @@ class add extends \phpbb\console\command\command */ protected function send_activation_email($user_id) { - if ($this->config['require_activation'] == USER_ACTIVATION_SELF) + switch ($this->config['require_activation']) { - $email_template = 'user_welcome_inactive'; - $user_actkey = gen_rand_string(mt_rand(6, 10)); - } - else if ($this->config['require_activation'] == USER_ACTIVATION_ADMIN) - { - $email_template = 'admin_welcome_inactive'; - $user_actkey = gen_rand_string(mt_rand(6, 10)); - } - else - { - $email_template = 'user_welcome'; - $user_actkey = ''; + case USER_ACTIVATION_SELF: + $email_template = 'user_welcome_inactive'; + $user_actkey = gen_rand_string(mt_rand(6, 10)); + break; + case USER_ACTIVATION_ADMIN: + $email_template = 'admin_welcome_inactive'; + $user_actkey = gen_rand_string(mt_rand(6, 10)); + break; + default: + $email_template = 'user_welcome'; + $user_actkey = ''; + break; } if (!class_exists('messenger'))