aboutsummaryrefslogtreecommitdiff
blob: db06037947ba801f57bfe49173823c82d3a5c481 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @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 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
{
	/** @var \phpbb\db\driver\driver_interface */
	protected $db;

	/** @var \phpbb\config\config */
	protected $config;

	/** @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
	*
	* @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)
	{
		$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);
	}

	/**
	* 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'))
		;
	}

	/**
	* 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)
	{
		$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
			);
		}

		$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();
		}
		catch (runtime_exception $e)
		{
			$io->error($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'			=> $this->config['board_timezone'],
			'user_lang'				=> $this->config['default_lang'],
			'user_type'				=> USER_NORMAL,
			'user_regdate'			=> time(),
		);

		if (!function_exists('user_add'))
		{
			require($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext);
		}

		$user_id = user_add($user_row);
		$io->success($this->user->lang('SUCCESS_ADD_USER', $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
	*/
	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
	*
	* 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
			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 || !$row['group_id'])
		{
			throw new runtime_exception($this->user->lang('NO_GROUP'));
		}

		return $row['group_id'];
	}
}