Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
97.14% |
68 / 70 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
delete_id | |
97.14% |
68 / 70 |
|
60.00% |
3 / 5 |
17 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
1 | |||
configure | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
96.43% |
27 / 28 |
|
0.00% |
0 / 1 |
9 | |||
interact | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
4.01 | |||
delete_bot_user | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 |
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 | |
14 | namespace phpbb\console\command\user; |
15 | |
16 | use phpbb\console\command\command; |
17 | use phpbb\db\driver\driver_interface; |
18 | use phpbb\language\language; |
19 | use phpbb\log\log_interface; |
20 | use phpbb\user; |
21 | use phpbb\user_loader; |
22 | use Symfony\Component\Console\Helper\QuestionHelper; |
23 | use Symfony\Component\Console\Input\InputArgument; |
24 | use Symfony\Component\Console\Input\InputInterface; |
25 | use Symfony\Component\Console\Input\InputOption; |
26 | use Symfony\Component\Console\Output\OutputInterface; |
27 | use Symfony\Component\Console\Question\ConfirmationQuestion; |
28 | use Symfony\Component\Console\Style\SymfonyStyle; |
29 | |
30 | class delete_id extends command |
31 | { |
32 | /** @var driver_interface */ |
33 | protected $db; |
34 | |
35 | /** @var language */ |
36 | protected $language; |
37 | |
38 | /** @var log_interface */ |
39 | protected $log; |
40 | |
41 | /** @var user_loader */ |
42 | protected $user_loader; |
43 | |
44 | /** @var string Bots table */ |
45 | protected $bots_table; |
46 | |
47 | /** @var string User group table */ |
48 | protected $user_group_table; |
49 | |
50 | /** @var string Users table */ |
51 | protected $users_table; |
52 | |
53 | /** @var string phpBB root path */ |
54 | protected $phpbb_root_path; |
55 | |
56 | /** @var string PHP extension */ |
57 | protected $php_ext; |
58 | |
59 | /** |
60 | * Construct method |
61 | * |
62 | * @param driver_interface $db |
63 | * @param language $language |
64 | * @param log_interface $log |
65 | * @param user $user |
66 | * @param user_loader $user_loader |
67 | * @param string $bots_table |
68 | * @param string $user_group_table |
69 | * @param string $users_table |
70 | * @param string $phpbb_root_path |
71 | * @param string $php_ext |
72 | */ |
73 | public function __construct(driver_interface $db, language $language, log_interface $log, user $user, user_loader $user_loader, |
74 | string $bots_table, string $user_group_table, string $users_table, string $phpbb_root_path, string $php_ext) |
75 | { |
76 | $this->db = $db; |
77 | $this->language = $language; |
78 | $this->log = $log; |
79 | $this->user_loader = $user_loader; |
80 | $this->bots_table = $bots_table; |
81 | $this->user_group_table = $user_group_table; |
82 | $this->users_table = $users_table; |
83 | $this->phpbb_root_path = $phpbb_root_path; |
84 | $this->php_ext = $php_ext; |
85 | |
86 | $this->language->add_lang('acp/users'); |
87 | parent::__construct($user); |
88 | } |
89 | |
90 | /** |
91 | * Sets the command name and description |
92 | * |
93 | * @return void |
94 | */ |
95 | protected function configure(): void |
96 | { |
97 | $this |
98 | ->setName('user:delete_id') |
99 | ->setDescription($this->language->lang('CLI_DESCRIPTION_USER_DELETE_ID')) |
100 | ->addArgument( |
101 | 'user_ids', |
102 | InputArgument::REQUIRED | InputArgument::IS_ARRAY, |
103 | $this->language->lang('CLI_DESCRIPTION_USER_DELETE_ID_OPTION_ID') |
104 | ) |
105 | ->addOption( |
106 | 'delete-posts', |
107 | null, |
108 | InputOption::VALUE_NONE, |
109 | $this->language->lang('CLI_DESCRIPTION_USER_DELETE_OPTION_POSTS') |
110 | ) |
111 | ; |
112 | } |
113 | |
114 | /** |
115 | * Executes the command user:delete_ids |
116 | * |
117 | * Deletes a list of user ids from the database. An option to delete the users' posts |
118 | * is available, by default posts will be retained. |
119 | * |
120 | * @param InputInterface $input The input stream used to get the options |
121 | * @param OutputInterface $output The output stream, used to print messages |
122 | * |
123 | * @return int 0 if all is well, 1 if any errors occurred |
124 | */ |
125 | protected function execute(InputInterface $input, OutputInterface $output): int |
126 | { |
127 | $user_ids = $input->getArgument('user_ids'); |
128 | $mode = ($input->getOption('delete-posts')) ? 'remove' : 'retain'; |
129 | $deleted_users = 0; |
130 | $io = new SymfonyStyle($input, $output); |
131 | |
132 | if (count($user_ids) > 0) |
133 | { |
134 | $this->user_loader->load_users($user_ids); |
135 | |
136 | $progress = $this->create_progress_bar(count($user_ids), $io, $output); |
137 | $progress->setMessage($this->language->lang('CLI_USER_DELETE_ID_START')); |
138 | $progress->start(); |
139 | |
140 | foreach ($user_ids as $user_id) |
141 | { |
142 | $user_row = $this->user_loader->get_user($user_id); |
143 | |
144 | // Skip anonymous user |
145 | if ($user_row['user_id'] == ANONYMOUS) |
146 | { |
147 | $progress->advance(); |
148 | continue; |
149 | } |
150 | else if ($user_row['user_type'] == USER_IGNORE) |
151 | { |
152 | $this->delete_bot_user($user_row); |
153 | } |
154 | else |
155 | { |
156 | if (!function_exists('user_delete')) |
157 | { |
158 | require($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext); |
159 | } |
160 | |
161 | user_delete($mode, $user_row['user_id'], $user_row['username']); |
162 | |
163 | $this->log->add('admin', ANONYMOUS, '', 'LOG_USER_DELETED', false, array($user_row['username'])); |
164 | } |
165 | |
166 | $progress->advance(); |
167 | $deleted_users++; |
168 | } |
169 | |
170 | $progress->finish(); |
171 | |
172 | if ($deleted_users > 0) |
173 | { |
174 | $io->success($this->language->lang('CLI_USER_DELETE_ID_SUCCESS')); |
175 | } |
176 | } |
177 | |
178 | if (!$deleted_users) |
179 | { |
180 | $io->note($this->language->lang('CLI_USER_DELETE_NONE')); |
181 | } |
182 | |
183 | return 0; |
184 | } |
185 | |
186 | /** |
187 | * Interacts with the user. |
188 | * Confirm they really want to delete the account...last chance! |
189 | * |
190 | * @param InputInterface $input An InputInterface instance |
191 | * @param OutputInterface $output An OutputInterface instance |
192 | */ |
193 | protected function interact(InputInterface $input, OutputInterface $output): void |
194 | { |
195 | $helper = $this->getHelper('question'); |
196 | if (!$helper instanceof QuestionHelper) |
197 | { |
198 | return; |
199 | } |
200 | |
201 | $user_ids = $input->getArgument('user_ids'); |
202 | if (count($user_ids) > 0) |
203 | { |
204 | $question = new ConfirmationQuestion( |
205 | $this->language->lang('CLI_USER_DELETE_ID_CONFIRM', implode(',', $user_ids)), |
206 | false |
207 | ); |
208 | |
209 | if (!$helper->ask($input, $output, $question)) |
210 | { |
211 | $input->setArgument('user_ids', []); |
212 | } |
213 | } |
214 | } |
215 | |
216 | /** |
217 | * Deletes a bot user |
218 | * |
219 | * @param array $user_row |
220 | * @return void |
221 | */ |
222 | protected function delete_bot_user(array $user_row): void |
223 | { |
224 | $delete_tables = [$this->bots_table, $this->user_group_table, $this->users_table]; |
225 | foreach ($delete_tables as $table) |
226 | { |
227 | $sql = "DELETE FROM $table |
228 | WHERE user_id = " . (int) $user_row['user_id']; |
229 | $this->db->sql_query($sql); |
230 | } |
231 | } |
232 | } |