Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.65% covered (success)
95.65%
44 / 46
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
delete
95.65% covered (success)
95.65%
44 / 46
50.00% covered (danger)
50.00%
2 / 4
10
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 configure
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 execute
93.33% covered (success)
93.33%
14 / 15
0.00% covered (danger)
0.00%
0 / 1
5.01
 interact
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
3.01
1<?php
2/**
3 *
4 * This file is part of the phpBB Forum Software package.
5 *
6 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7 * @license GNU General Public License, version 2 (GPL-2.0)
8 *
9 * For full copyright and license information, please see
10 * the docs/CREDITS.txt file.
11 *
12 */
13
14namespace phpbb\console\command\user;
15
16use phpbb\console\command\command;
17use phpbb\language\language;
18use phpbb\log\log_interface;
19use phpbb\user;
20use phpbb\user_loader;
21use Symfony\Component\Console\Command\Command as symfony_command;
22use Symfony\Component\Console\Helper\QuestionHelper;
23use Symfony\Component\Console\Input\InputArgument;
24use Symfony\Component\Console\Input\InputInterface;
25use Symfony\Component\Console\Input\InputOption;
26use Symfony\Component\Console\Output\OutputInterface;
27use Symfony\Component\Console\Question\ConfirmationQuestion;
28use Symfony\Component\Console\Style\SymfonyStyle;
29
30class delete extends command
31{
32    /** @var language */
33    protected $language;
34
35    /** @var log_interface */
36    protected $log;
37
38    /** @var user_loader */
39    protected $user_loader;
40
41    /**
42     * phpBB root path
43     *
44     * @var string
45     */
46    protected $phpbb_root_path;
47
48    /**
49     * PHP extension.
50     *
51     * @var string
52     */
53    protected $php_ext;
54
55    /**
56     * Construct method
57     *
58     * @param user             $user
59     * @param language         $language
60     * @param log_interface    $log
61     * @param user_loader      $user_loader
62     * @param string           $phpbb_root_path
63     * @param string           $php_ext
64     */
65    public function __construct(user $user, language $language, log_interface $log, user_loader $user_loader, $phpbb_root_path, $php_ext)
66    {
67        $this->language = $language;
68        $this->log = $log;
69        $this->user_loader = $user_loader;
70        $this->phpbb_root_path = $phpbb_root_path;
71        $this->php_ext = $php_ext;
72
73        $this->language->add_lang('acp/users');
74        parent::__construct($user);
75    }
76
77    /**
78     * Sets the command name and description
79     *
80     * @return void
81     */
82    protected function configure()
83    {
84        $this
85            ->setName('user:delete')
86            ->setDescription($this->language->lang('CLI_DESCRIPTION_USER_DELETE'))
87            ->addArgument(
88                'username',
89                InputArgument::REQUIRED,
90                $this->language->lang('CLI_DESCRIPTION_USER_DELETE_USERNAME')
91            )
92            ->addOption(
93                'delete-posts',
94                null,
95                InputOption::VALUE_NONE,
96                $this->language->lang('CLI_DESCRIPTION_USER_DELETE_OPTION_POSTS')
97            )
98        ;
99    }
100
101    /**
102     * Executes the command user:delete
103     *
104     * Deletes a user from the database. An option to delete the user's posts
105     * is available, by default posts will be retained.
106     *
107     * @param InputInterface  $input  The input stream used to get the options
108     * @param OutputInterface $output The output stream, used to print messages
109     *
110     * @return int 0 if all is well, 1 if any errors occurred
111     */
112    protected function execute(InputInterface $input, OutputInterface $output)
113    {
114        $name = $input->getArgument('username');
115        $mode = ($input->getOption('delete-posts')) ? 'remove' : 'retain';
116
117        if ($name)
118        {
119            $io = new SymfonyStyle($input, $output);
120
121            $user_id  = $this->user_loader->load_user_by_username($name);
122            $user_row = $this->user_loader->get_user($user_id);
123
124            if ($user_row['user_id'] == ANONYMOUS)
125            {
126                $io->error($this->language->lang('NO_USER'));
127                return symfony_command::FAILURE;
128            }
129
130            if (!function_exists('user_delete'))
131            {
132                require($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext);
133            }
134
135            user_delete($mode, $user_row['user_id'], $user_row['username']);
136
137            $this->log->add('admin', ANONYMOUS, '', 'LOG_USER_DELETED', false, array($user_row['username']));
138
139            $io->success($this->language->lang('USER_DELETED'));
140        }
141
142        return symfony_command::SUCCESS;
143    }
144
145    /**
146     * Interacts with the user.
147     * Confirm they really want to delete the account...last chance!
148     *
149     * @param InputInterface  $input  An InputInterface instance
150     * @param OutputInterface $output An OutputInterface instance
151     */
152    protected function interact(InputInterface $input, OutputInterface $output)
153    {
154        $helper = $this->getHelper('question');
155        if (!$helper instanceof QuestionHelper)
156        {
157            return;
158        }
159
160        $question = new ConfirmationQuestion(
161            $this->language->lang('CLI_USER_DELETE_CONFIRM', $input->getArgument('username')),
162            false
163        );
164
165        if (!$helper->ask($input, $output, $question))
166        {
167            $input->setArgument('username', false);
168        }
169    }
170}