[ticket/12684] Update to use non-deprecated methods

PHPBB3-12684
This commit is contained in:
Matt Friedman 2016-02-29 13:28:42 -08:00
parent f32b4c0547
commit 637b02690d
2 changed files with 154 additions and 160 deletions

View file

@ -175,18 +175,6 @@ services:
tags: tags:
- { name: console.command } - { 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: console.command.reparser.list:
class: phpbb\console\command\reparser\list_all class: phpbb\console\command\reparser\list_all
arguments: arguments:
@ -231,3 +219,16 @@ services:
- '@user' - '@user'
tags: tags:
- { name: console.command } - { 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 }

View file

@ -1,15 +1,15 @@
<?php <?php
/** /**
* *
* This file is part of the phpBB Forum Software package. * This file is part of the phpBB Forum Software package.
* *
* @copyright (c) phpBB Limited <https://www.phpbb.com> * @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0) * @license GNU General Public License, version 2 (GPL-2.0)
* *
* For full copyright and license information, please see * For full copyright and license information, please see
* the docs/CREDITS.txt file. * the docs/CREDITS.txt file.
* *
*/ */
namespace phpbb\console\command\user; 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\InputInterface;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Console\Style\SymfonyStyle;
class add extends \phpbb\console\command\command class add extends \phpbb\console\command\command
@ -27,11 +28,15 @@ class add extends \phpbb\console\command\command
/** @var \phpbb\config\config */ /** @var \phpbb\config\config */
protected $config; protected $config;
/** @var \phpbb\language\language */
protected $language;
/** @var \phpbb\passwords\manager */ /** @var \phpbb\passwords\manager */
protected $password_manager; protected $password_manager;
/** /**
* phpBB root path * phpBB root path
*
* @var string * @var string
*/ */
protected $phpbb_root_path; protected $phpbb_root_path;
@ -46,22 +51,24 @@ class add extends \phpbb\console\command\command
/** /**
* Construct method * Construct method
* *
* @param \phpbb\user $user The user object used for language information * @param \phpbb\user $user
* @param \phpbb\db\driver\driver_interface $db The database in wich will be inserted the user * @param \phpbb\db\driver\driver_interface $db
* @param \phpbb\config\config $config The config object used to get default language and timezone * @param \phpbb\config\config $config
* @param \phpbb\passwords\manager $password_manager The password manager used to store the user's password * @param \phpbb\language\language $language
* @param string $phpbb_root_path Root path * @param \phpbb\passwords\manager $password_manager
* @param string $php_ext PHP extension * @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\passwords\manager $password_manager, $phpbb_root_path, $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->db = $db;
$this->config = $config; $this->config = $config;
$this->language = $language;
$this->password_manager = $password_manager; $this->password_manager = $password_manager;
$this->phpbb_root_path = $phpbb_root_path; $this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext; $this->php_ext = $php_ext;
$user->add_lang('ucp'); $language->add_lang('ucp');
parent::__construct($user); parent::__construct($user);
} }
@ -74,62 +81,72 @@ class add extends \phpbb\console\command\command
{ {
$this $this
->setName('user:add') ->setName('user:add')
->setDescription($this->user->lang('CLI_DESCRIPTION_USER_ADD')) ->setDescription($this->language->lang('CLI_DESCRIPTION_USER_ADD'))
->addOption('username', null, InputOption::VALUE_REQUIRED, $this->user->lang('CLI_DESCRIPTION_USER_ADD_OPTION_USERNAME')) ->addOption('username', null, InputOption::VALUE_REQUIRED, $this->language->lang('CLI_DESCRIPTION_USER_ADD_OPTION_USERNAME'))
->addOption('password', null, InputOption::VALUE_REQUIRED, $this->user->lang('CLI_DESCRIPTION_USER_ADD_OPTION_PASSWORD')) ->addOption('password', null, InputOption::VALUE_REQUIRED, $this->language->lang('CLI_DESCRIPTION_USER_ADD_OPTION_PASSWORD'))
->addOption('email', null, InputOption::VALUE_REQUIRED, $this->user->lang('CLI_DESCRIPTION_USER_ADD_OPTION_EMAIL')) ->addOption('email', null, InputOption::VALUE_REQUIRED, $this->language->lang('CLI_DESCRIPTION_USER_ADD_OPTION_EMAIL'))
->addOption('send-email', null, InputOption::VALUE_NONE, $this->user->lang('CLI_CONFIG_PRINT_WITHOUT_NEWLINE')) ->addOption('send-email', null, InputOption::VALUE_NONE, $this->language->lang('CLI_CONFIG_PRINT_WITHOUT_NEWLINE'))
; ;
} }
/** /**
* Executes the command user:add * Executes the command user:add
* *
* If not given in option, asks the username, password and email. * Adds a new user to the database. If options are not provided, it will ask for 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. * 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 InputInterface $input The input stream used to get the options
* @param OutputInterface $output The output stream, used to print messages * @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 * @return int 0 if all is well, 1 if any errors occurred
*/ */
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
$io = new SymfonyStyle($input, $output); $io = new SymfonyStyle($input, $output);
$dialog = $this->getHelperSet()->get('dialog'); $helper = $this->getHelper('question');
$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
);
}
$data = array( $data = array(
'username' => $username, 'username' => $input->getOption('username'),
'new_password' => $password, 'new_password' => $input->getOption('password'),
'email' => $email, '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 try
{ {
$this->validate_user_data($data); $this->validate_user_data($data);
@ -151,9 +168,9 @@ class add extends \phpbb\console\command\command
} }
$user_row = array( $user_row = array(
'username' => $username, 'username' => $data['username'],
'user_password' => $this->password_manager->hash($password), 'user_password' => $this->password_manager->hash($data['new_password']),
'user_email' => $email, 'user_email' => $data['email'],
'group_id' => $group_id, 'group_id' => $group_id,
'user_timezone' => $this->config['board_timezone'], 'user_timezone' => $this->config['board_timezone'],
'user_lang' => $this->config['default_lang'], 'user_lang' => $this->config['default_lang'],
@ -166,53 +183,18 @@ class add extends \phpbb\console\command\command
require($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext); 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']) if ($input->getOption('send-email') && $this->config['email_enable'])
{ {
$this->send_activation_email($user_id, $data); $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; 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 * Validate the submitted user data
* *
@ -265,7 +247,7 @@ class add extends \phpbb\console\command\command
if (!$row || !$row['group_id']) 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']; return $row['group_id'];
@ -310,7 +292,7 @@ class add extends \phpbb\console\command\command
$messenger->anti_abuse_headers($this->config, $this->user); $messenger->anti_abuse_headers($this->config, $this->user);
$messenger->assign_vars(array( $messenger->assign_vars(array(
'WELCOME_MSG' => htmlspecialchars_decode($this->user->lang('WELCOME_SUBJECT', $this->config['sitename'])), 'WELCOME_MSG' => htmlspecialchars_decode($this->language->lang('WELCOME_SUBJECT', $this->config['sitename'])),
'USERNAME' => htmlspecialchars_decode($data['username']), 'USERNAME' => htmlspecialchars_decode($data['username']),
'PASSWORD' => htmlspecialchars_decode($data['new_password']), 'PASSWORD' => htmlspecialchars_decode($data['new_password']),
'U_ACTIVATE' => generate_board_url() . "/ucp.{$this->php_ext}?mode=activate&u=$user_id&k=$user_actkey") 'U_ACTIVATE' => generate_board_url() . "/ucp.{$this->php_ext}?mode=activate&u=$user_id&k=$user_actkey")
@ -318,4 +300,15 @@ class add extends \phpbb\console\command\command
$messenger->send(NOTIFY_EMAIL); $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') . ' ';
}
} }