diff --git a/phpBB/config/default/container/services_console.yml b/phpBB/config/default/container/services_console.yml index 0a28c0ed1f..3ada9d1639 100644 --- a/phpBB/config/default/container/services_console.yml +++ b/phpBB/config/default/container/services_console.yml @@ -232,3 +232,15 @@ services: - '%core.php_ext%' tags: - { name: console.command } + + console.command.user.delete: + class: phpbb\console\command\user\delete + arguments: + - '@user' + - '@dbal.conn' + - '@language' + - '@log' + - '%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 6cb516ebfd..bb7baf67f7 100644 --- a/phpBB/language/en/cli.php +++ b/phpBB/language/en/cli.php @@ -87,6 +87,9 @@ $lang = array_merge($lang, array( '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 (not sent by default)', + 'CLI_DESCRIPTION_USER_DELETE' => 'Delete a user account.', + 'CLI_DESCRIPTION_USER_DELETE_USERNAME' => 'Username of the user to delete', + 'CLI_DESCRIPTION_USER_DELETE_OPTION_POSTS' => 'Delete all posts by the user. Without this option, the user’s posts will be retained.', 'CLI_EXTENSION_DISABLE_FAILURE' => 'Could not disable extension %s', 'CLI_EXTENSION_DISABLE_SUCCESS' => 'Successfully disabled extension %s', @@ -126,6 +129,7 @@ $lang = array_merge($lang, array( 'CLI_THUMBNAIL_NOTHING_TO_DELETE' => 'No thumbnails to delete.', 'CLI_USER_ADD_SUCCESS' => 'Successfully added user %s.', + 'CLI_USER_DELETE_CONFIRM' => 'Are you sure you want to delete ‘%s’? [y/N]', )); // Additional help for commands. diff --git a/phpBB/phpbb/console/command/user/delete.php b/phpBB/phpbb/console/command/user/delete.php new file mode 100644 index 0000000000..360b119e17 --- /dev/null +++ b/phpBB/phpbb/console/command/user/delete.php @@ -0,0 +1,175 @@ + + * @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; + +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Question\ConfirmationQuestion; +use Symfony\Component\Console\Question\Question; +use Symfony\Component\Console\Style\SymfonyStyle; + +class delete extends \phpbb\console\command\command +{ + /** @var \phpbb\db\driver\driver_interface */ + protected $db; + + /** @var \phpbb\language\language */ + protected $language; + + /** @var \phpbb\log\log_interface */ + protected $log; + + /** + * phpBB root path + * + * @var string + */ + protected $phpbb_root_path; + + /** + * PHP extension. + * + * @var string + */ + protected $php_ext; + + /** + * Construct method + * + * @param \phpbb\user $user + * @param \phpbb\db\driver\driver_interface $db + * @param \phpbb\language\language $language + * @param \phpbb\log\log_interface $log + * @param string $phpbb_root_path + * @param string $php_ext + */ + public function __construct(\phpbb\user $user, \phpbb\db\driver\driver_interface $db, \phpbb\language\language $language, \phpbb\log\log_interface $log, $phpbb_root_path, $php_ext) + { + $this->db = $db; + $this->language = $language; + $this->log = $log; + $this->phpbb_root_path = $phpbb_root_path; + $this->php_ext = $php_ext; + + $this->language->add_lang('acp/users'); + parent::__construct($user); + } + + /** + * Sets the command name and description + * + * @return null + */ + protected function configure() + { + $this + ->setName('user:delete') + ->setDescription($this->language->lang('CLI_DESCRIPTION_USER_DELETE')) + ->addArgument( + 'username', + InputArgument::REQUIRED, + $this->language->lang('CLI_DESCRIPTION_USER_DELETE_USERNAME') + ) + ->addOption( + 'delete-posts', + null, + InputOption::VALUE_NONE, + $this->language->lang('CLI_DESCRIPTION_USER_DELETE_OPTION_POSTS') + ) + ; + } + + /** + * Executes the command user:delete + * + * Deletes a user from the database. An option to delete the user's posts + * is available, by default posts will be retained. + * + * @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) + { + $name = $input->getArgument('username'); + $mode = ($input->getOption('delete-posts')) ? 'remove' : 'retain'; + + if ($name) + { + $io = new SymfonyStyle($input, $output); + + if (!$user_row = $this->get_user_data($name)) + { + $io->error($this->language->lang('NO_USER')); + return 1; + } + + if (!function_exists('user_delete')) + { + require($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext); + } + + user_delete($mode, $user_row['user_id'], $user_row['username']); + + $this->log->add('admin', ANONYMOUS, '', 'LOG_USER_DELETED', false, array($user_row['username'])); + + $io->success($this->language->lang('USER_DELETED')); + } + + return 0; + } + + /** + * Interacts with the user. + * Confirm they really want to delete the account...last chance! + * + * @param InputInterface $input An InputInterface instance + * @param OutputInterface $output An OutputInterface instance + */ + protected function interact(InputInterface $input, OutputInterface $output) + { + $helper = $this->getHelper('question'); + + $question = new ConfirmationQuestion( + $this->language->lang('CLI_USER_DELETE_CONFIRM', $input->getArgument('username')), + false + ); + + if (!$helper->ask($input, $output, $question)) + { + $input->setArgument('username', false); + } + } + + /** + * Get the user's data from the database + * + * @param string $name A user name + * @return mixed The user's id and username if they exist, false otherwise. + */ + protected function get_user_data($name) + { + $sql = 'SELECT user_id, username + FROM ' . USERS_TABLE . " + WHERE username_clean = '" . $this->db->sql_escape(utf8_clean_string($name)) . "'"; + $result = $this->db->sql_query_limit($sql, 1); + $user_row = $this->db->sql_fetchrow($result); + $this->db->sql_freeresult($result); + + return $user_row; + } +} diff --git a/tests/console/user/delete_test.php b/tests/console/user/delete_test.php new file mode 100644 index 0000000000..5162358713 --- /dev/null +++ b/tests/console/user/delete_test.php @@ -0,0 +1,169 @@ + +* @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\delete; + +class phpbb_console_command_user_delete_test extends phpbb_database_test_case +{ + protected $db; + protected $user; + protected $language; + protected $log; + protected $command_name; + protected $question; + protected $phpbb_root_path; + protected $php_ext; + + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/config.xml'); + } + + public function setUp() + { + 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 = new \phpbb\config\config(array()); + + $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( + $this->language, + '\phpbb\datetime' + )); + + $this->log = $this->getMockBuilder('\phpbb\log\log') + ->disableOriginalConstructor() + ->getMock(); + + $phpbb_container->set('auth.provider.db', new phpbb_mock_auth_provider()); + $provider_collection = new \phpbb\auth\provider_collection($phpbb_container, $config); + $provider_collection->add('auth.provider.db'); + $phpbb_container->set( + 'auth.provider_collection', + $provider_collection + ); + + $this->phpbb_root_path = $phpbb_root_path; + $this->php_ext = $phpEx; + + parent::setUp(); + } + + public function test_delete() + { + $command_tester = $this->get_command_tester(); + + $this->assertEquals(3, $this->get_user_id('Test')); + + $this->question->setInputStream($this->getInputStream("yes\n")); + + $command_tester->execute(array( + 'command' => $this->command_name, + 'username' => 'Test', + '--delete-posts' => false, + )); + + $this->assertNull($this->get_user_id('Test')); + $this->assertContains('USER_DELETED', $command_tester->getDisplay()); + } + + public function test_delete_non_user() + { + $command_tester = $this->get_command_tester(); + + $this->assertNull($this->get_user_id('Foo')); + + $this->question->setInputStream($this->getInputStream("yes\n")); + + $command_tester->execute(array( + 'command' => $this->command_name, + 'username' => 'Foo', + '--delete-posts' => false, + )); + + $this->assertContains('NO_USER', $command_tester->getDisplay()); + } + + public function test_delete_cancel() + { + $command_tester = $this->get_command_tester(); + + $this->assertEquals(3, $this->get_user_id('Test')); + + $this->question->setInputStream($this->getInputStream("no\n")); + + $command_tester->execute(array( + 'command' => $this->command_name, + 'username' => 'Test', + '--delete-posts' => false, + )); + + $this->assertNotNull($this->get_user_id('Test')); + } + + public function get_command_tester() + { + $application = new Application(); + $application->add(new delete( + $this->user, + $this->db, + $this->language, + $this->log, + $this->phpbb_root_path, + $this->php_ext + )); + + $command = $application->find('user:delete'); + $this->command_name = $command->getName(); + $this->question = $command->getHelper('question'); + + 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; + } +}