aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/console/command/user/add.php
blob: c60a059251e15e0b547da8b25daf8c32fc6339e8 (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?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\config\config;
use phpbb\console\command\command;
use phpbb\db\driver\driver_interface;
use phpbb\exception\runtime_exception;
use phpbb\language\language;
use phpbb\passwords\manager;
use phpbb\user;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;

class add extends command
{
	/** @var array Array of interactively acquired options */
	protected $data;

	/** @var driver_interface */
	protected $db;

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

	/** @var language */
	protected $language;

	/** @var manager */
	protected $password_manager;

	/**
	 * phpBB root path
	 *
	 * @var string
	 */
	protected $phpbb_root_path;

	/**
	 * PHP extension.
	 *
	 * @var string
	 */
	protected $php_ext;

	/**
	 * Construct method
	 *
	 * @param user             $user
	 * @param driver_interface $db
	 * @param config           $config
	 * @param language         $language
	 * @param manager          $password_manager
	 * @param string           $phpbb_root_path
	 * @param string           $php_ext
	 */
	public function __construct(user $user, driver_interface $db, config $config, language $language, manager $password_manager, $phpbb_root_path, $php_ext)
	{
		$this->db = $db;
		$this->config = $config;
		$this->language = $language;
		$this->password_manager = $password_manager;
		$this->phpbb_root_path = $phpbb_root_path;
		$this->php_ext = $php_ext;

		$this->language->add_lang('ucp');
		parent::__construct($user);
	}

	/**
	 * Sets the command name and description
	 *
	 * @return null
	 */
	protected function configure()
	{
		$this
			->setName('user:add')
			->setDescription($this->language->lang('CLI_DESCRIPTION_USER_ADD'))
			->setHelp($this->language->lang('CLI_HELP_USER_ADD'))
			->addOption(
				'username',
				'U',
				InputOption::VALUE_REQUIRED,
				$this->language->lang('CLI_DESCRIPTION_USER_ADD_OPTION_USERNAME')
			)
			->addOption(
				'password',
				'P',
				InputOption::VALUE_REQUIRED,
				$this->language->lang('CLI_DESCRIPTION_USER_ADD_OPTION_PASSWORD')
			)
			->addOption(
				'email',
				'E',
				InputOption::VALUE_REQUIRED,
				$this->language->lang('CLI_DESCRIPTION_USER_ADD_OPTION_EMAIL')
			)
			->addOption(
				'send-email',
				null,
				InputOption::VALUE_NONE,
				$this->language->lang('CLI_DESCRIPTION_USER_ADD_OPTION_NOTIFY')
			)
		;
	}

	/**
	 * Executes the command user:add
	 *
	 * Adds a new user to the database. If options are not provided, it will ask for the username, password and email.
	 * 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 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)
	{
		$io = new SymfonyStyle($input, $output);

		try
		{
			$this->validate_user_data();
			$group_id = $this->get_group_id();
		}
		catch (runtime_exception $e)
		{
			$io->error($e->getMessage());
			return 1;
		}

		$user_row = array(
			'username'      => $this->data['username'],
			'user_password' => $this->password_manager->hash($this->data['new_password']),
			'user_email'    => $this->data['email'],
			'group_id'      => $group_id,
			'user_timezone' => $this->config['board_timezone'],
			'user_lang'     => $this->config['default_lang'],
			'user_type'     => USER_NORMAL,
			'user_regdate'  => time(),
		);

		$user_id = (int) user_add($user_row);

		if (!$user_id)
		{
			$io->error($this->language->lang('AUTH_NO_PROFILE_CREATED'));
			return 1;
		}

		if ($input->getOption('send-email') && $this->config['email_enable'])
		{
			$this->send_activation_email($user_id);
		}

		$io->success($this->language->lang('CLI_USER_ADD_SUCCESS', $this->data['username']));

		return 0;
	}

	/**
	 * Interacts with the user.
	 *
	 * @param InputInterface  $input  An InputInterface instance
	 * @param OutputInterface $output An OutputInterface instance
	 */
	protected function interact(InputInterface $input, OutputInterface $output)
	{
		$helper = $this->getHelper('question');

		$this->data = array(
			'username'     => $input->getOption('username'),
			'new_password' => $input->getOption('password'),
			'email'        => $input->getOption('email'),
		);

		if (!$this->data['username'])
		{
			$question = new Question($this->ask_user('USERNAME'));
			$this->data['username'] = $helper->ask($input, $output, $question);
		}

		if (!$this->data['new_password'])
		{
			$question = new Question($this->ask_user('PASSWORD'));
			$question->setValidator(function ($value) use ($helper, $input, $output) {
				$question = new Question($this->ask_user('CONFIRM_PASSWORD'));
				$question->setHidden(true);
				if ($helper->ask($input, $output, $question) != $value)
				{
					throw new runtime_exception($this->language->lang('NEW_PASSWORD_ERROR'));
				}
				return $value;
			});
			$question->setHidden(true);
			$question->setMaxAttempts(5);

			$this->data['new_password'] = $helper->ask($input, $output, $question);
		}

		if (!$this->data['email'])
		{
			$question = new Question($this->ask_user('EMAIL_ADDRESS'));
			$this->data['email'] = $helper->ask($input, $output, $question);
		}
	}

	/**
	 * Validate the submitted user data
	 *
	 * @throws runtime_exception if any data fails validation
	 * @return null
	 */
	protected function validate_user_data()
	{
		if (!function_exists('validate_data'))
		{
			require($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext);
		}

		$error = validate_data($this->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->language, '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->language->lang('NO_GROUP'));
		}

		return $row['group_id'];
	}

	/**
	 * Send account activation email
	 *
	 * @param int   $user_id The new user's id
	 * @return null
	 */
	protected function send_activation_email($user_id)
	{
		switch ($this->config['require_activation'])
		{
			case USER_ACTIVATION_SELF:
				$email_template = 'user_welcome_inactive';
				$user_actkey = gen_rand_string(mt_rand(6, 10));
			break;
			case USER_ACTIVATION_ADMIN:
				$email_template = 'admin_welcome_inactive';
				$user_actkey = gen_rand_string(mt_rand(6, 10));
			break;
			default:
				$email_template = 'user_welcome';
				$user_actkey = '';
			break;
		}

		if (!class_exists('messenger'))
		{
			require($this->phpbb_root_path . 'includes/functions_messenger.' . $this->php_ext);
		}

		$messenger = new \messenger(false);
		$messenger->template($email_template, $this->user->lang_name);
		$messenger->to($this->data['email'], $this->data['username']);
		$messenger->anti_abuse_headers($this->config, $this->user);
		$messenger->assign_vars(array(
			'WELCOME_MSG' => htmlspecialchars_decode($this->language->lang('WELCOME_SUBJECT', $this->config['sitename'])),
			'USERNAME'    => htmlspecialchars_decode($this->data['username']),
			'PASSWORD'    => htmlspecialchars_decode($this->data['new_password']),
			'U_ACTIVATE'  => generate_board_url() . "/ucp.{$this->php_ext}?mode=activate&u=$user_id&k=$user_actkey")
		);

		$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') . ' ';
	}
}
1)) { $query .= " $nextCondition profiles.is_enabled = ?"; $nextCondition = 'AND'; push(@bindValues, $is_enabled); } $query .= ' ORDER BY profiles.login_name'; $vars->{'users'} = $dbh->selectall_arrayref($query, {'Slice' => {}}, @bindValues); } if ($matchtype && $matchtype eq 'exact' && scalar(@{$vars->{'users'}}) == 1) { my $match_user_id = $vars->{'users'}[0]->{'userid'}; my $match_user = check_user($match_user_id); edit_processing($match_user); } else { $template->process('admin/users/list.html.tmpl', $vars) || ThrowTemplateError($template->error()); } ########################################################################### } elsif ($action eq 'add') { $editusers || ThrowUserError("auth_failure", {group => "editusers", action => "add", object => "users"}); $vars->{'token'} = issue_session_token('add_user'); $template->process('admin/users/create.html.tmpl', $vars) || ThrowTemplateError($template->error()); ########################################################################### } elsif ($action eq 'new') { $editusers || ThrowUserError("auth_failure", {group => "editusers", action => "add", object => "users"}); check_token_data($token, 'add_user'); # When e.g. the 'Env' auth method is used, the password field # is not displayed. In that case, set the password to *. my $password = $cgi->param('password'); $password = '*' if !defined $password; my $new_user = Bugzilla::User->create({ login_name => scalar $cgi->param('login'), cryptpassword => $password, realname => scalar $cgi->param('name'), disabledtext => scalar $cgi->param('disabledtext'), disable_mail => scalar $cgi->param('disable_mail'), extern_id => scalar $cgi->param('extern_id'), }); userDataToVars($new_user->id); delete_token($token); if ($cgi->param('notify_user')) { $vars->{'new_user'} = $new_user; my $message; $template->process('email/new-user-details.txt.tmpl', $vars, \$message) || ThrowTemplateError($template->error()); MessageToMTA($message); } # We already display the updated page. We have to recreate a token now. $vars->{'token'} = issue_session_token('edit_user'); $vars->{'message'} = 'account_created'; $template->process('admin/users/edit.html.tmpl', $vars) || ThrowTemplateError($template->error()); ########################################################################### } elsif ($action eq 'edit') { my $otherUser = check_user($otherUserID, $otherUserLogin); edit_processing($otherUser); ########################################################################### } elsif ($action eq 'update') { check_token_data($token, 'edit_user'); my $otherUser = check_user($otherUserID, $otherUserLogin); $otherUserID = $otherUser->id; # Lock tables during the check+update session. $dbh->bz_start_transaction(); $editusers || $user->can_see_user($otherUser) || ThrowUserError('auth_failure', {reason => "not_visible", action => "modify", object => "user"}); $vars->{'loginold'} = $otherUser->login; # Update groups my @group_ids = grep { s/group_// } keys %{ Bugzilla->cgi->Vars }; $otherUser->set_groups({ set => \@group_ids }); # Update profiles table entry; silently skip doing this if the user # is not authorized. my $changes = {}; if ($editusers) { $otherUser->set_login($cgi->param('login')); $otherUser->set_name($cgi->param('name')); $otherUser->set_password($cgi->param('password')) if $cgi->param('password'); $otherUser->set_disabledtext($cgi->param('disabledtext')); $otherUser->set_disable_mail($cgi->param('disable_mail')); $otherUser->set_extern_id($cgi->param('extern_id')) if defined($cgi->param('extern_id')); # Update bless groups my @bless_ids = grep { s/bless_// } keys %{ Bugzilla->cgi->Vars }; $otherUser->set_bless_groups({ set => \@bless_ids }); } $changes = $otherUser->update(); $dbh->bz_commit_transaction(); # XXX: userDataToVars may be off when editing ourselves. userDataToVars($otherUserID); delete_token($token); $vars->{'message'} = 'account_updated'; $vars->{'changes'} = \%$changes; # We already display the updated page. We have to recreate a token now. $vars->{'token'} = issue_session_token('edit_user'); $template->process('admin/users/edit.html.tmpl', $vars) || ThrowTemplateError($template->error()); ########################################################################### } elsif ($action eq 'del') { my $otherUser = check_user($otherUserID, $otherUserLogin); $otherUserID = $otherUser->id; Bugzilla->params->{'allowuserdeletion'} || ThrowUserError('users_deletion_disabled'); $editusers || ThrowUserError('auth_failure', {group => "editusers", action => "delete", object => "users"}); $vars->{'otheruser'} = $otherUser; # Find other cross references. $vars->{'attachments'} = $dbh->selectrow_array( 'SELECT COUNT(*) FROM attachments WHERE submitter_id = ?', undef, $otherUserID); $vars->{'assignee_or_qa'} = $dbh->selectrow_array( qq{SELECT COUNT(*) FROM bugs WHERE assigned_to = ? OR qa_contact = ?}, undef, ($otherUserID, $otherUserID)); $vars->{'reporter'} = $dbh->selectrow_array( 'SELECT COUNT(*) FROM bugs WHERE reporter = ?', undef, $otherUserID); $vars->{'cc'} = $dbh->selectrow_array( 'SELECT COUNT(*) FROM cc WHERE who = ?', undef, $otherUserID); $vars->{'bugs_activity'} = $dbh->selectrow_array( 'SELECT COUNT(*) FROM bugs_activity WHERE who = ?', undef, $otherUserID); $vars->{'component_cc'} = $dbh->selectrow_array( 'SELECT COUNT(*) FROM component_cc WHERE user_id = ?', undef, $otherUserID); $vars->{'email_setting'} = $dbh->selectrow_array( 'SELECT COUNT(*) FROM email_setting WHERE user_id = ?', undef, $otherUserID); $vars->{'flags'}{'requestee'} = $dbh->selectrow_array( 'SELECT COUNT(*) FROM flags WHERE requestee_id = ?', undef, $otherUserID); $vars->{'flags'}{'setter'} = $dbh->selectrow_array( 'SELECT COUNT(*) FROM flags WHERE setter_id = ?', undef, $otherUserID); $vars->{'longdescs'} = $dbh->selectrow_array( 'SELECT COUNT(*) FROM longdescs WHERE who = ?', undef, $otherUserID); my $namedquery_ids = $dbh->selectcol_arrayref( 'SELECT id FROM namedqueries WHERE userid = ?', undef, $otherUserID); $vars->{'namedqueries'} = scalar(@$namedquery_ids); if (scalar(@$namedquery_ids)) { $vars->{'namedquery_group_map'} = $dbh->selectrow_array( 'SELECT COUNT(*) FROM namedquery_group_map WHERE namedquery_id IN' . ' (' . join(', ', @$namedquery_ids) . ')'); } else { $vars->{'namedquery_group_map'} = 0; } $vars->{'profile_setting'} = $dbh->selectrow_array( 'SELECT COUNT(*) FROM profile_setting WHERE user_id = ?', undef, $otherUserID); $vars->{'profiles_activity'} = $dbh->selectrow_array( 'SELECT COUNT(*) FROM profiles_activity WHERE who = ? AND userid != ?', undef, ($otherUserID, $otherUserID)); $vars->{'quips'} = $dbh->selectrow_array( 'SELECT COUNT(*) FROM quips WHERE userid = ?', undef, $otherUserID); $vars->{'series'} = $dbh->selectrow_array( 'SELECT COUNT(*) FROM series WHERE creator = ?', undef, $otherUserID); $vars->{'watch'}{'watched'} = $dbh->selectrow_array( 'SELECT COUNT(*) FROM watch WHERE watched = ?', undef, $otherUserID); $vars->{'watch'}{'watcher'} = $dbh->selectrow_array( 'SELECT COUNT(*) FROM watch WHERE watcher = ?', undef, $otherUserID); $vars->{'whine_events'} = $dbh->selectrow_array( 'SELECT COUNT(*) FROM whine_events WHERE owner_userid = ?', undef, $otherUserID); $vars->{'whine_schedules'} = $dbh->selectrow_array( qq{SELECT COUNT(distinct eventid) FROM whine_schedules WHERE mailto = ? AND mailto_type = ? }, undef, ($otherUserID, MAILTO_USER)); $vars->{'token'} = issue_session_token('delete_user'); $template->process('admin/users/confirm-delete.html.tmpl', $vars) || ThrowTemplateError($template->error()); ########################################################################### } elsif ($action eq 'delete') { check_token_data($token, 'delete_user'); my $otherUser = check_user($otherUserID, $otherUserLogin); $otherUserID = $otherUser->id; # Cache for user accounts. my %usercache = (0 => new Bugzilla::User()); my %updatedbugs; # Lock tables during the check+removal session. # XXX: if there was some change on these tables after the deletion # confirmation checks, we may do something here we haven't warned # about. $dbh->bz_start_transaction(); Bugzilla->params->{'allowuserdeletion'} || ThrowUserError('users_deletion_disabled'); $editusers || ThrowUserError('auth_failure', {group => "editusers", action => "delete", object => "users"}); @{$otherUser->product_responsibilities()} && ThrowUserError('user_has_responsibility'); Bugzilla->logout_user($otherUser); # Get the named query list so we can delete namedquery_group_map entries. my $namedqueries_as_string = join(', ', @{$dbh->selectcol_arrayref( 'SELECT id FROM namedqueries WHERE userid = ?', undef, $otherUserID)}); # Get the timestamp for LogActivityEntry. my $timestamp = $dbh->selectrow_array('SELECT NOW()'); # When we update a bug_activity entry, we update the bug timestamp, too. my $sth_set_bug_timestamp = $dbh->prepare('UPDATE bugs SET delta_ts = ? WHERE bug_id = ?'); # Flags my $flag_ids = $dbh->selectcol_arrayref('SELECT id FROM flags WHERE requestee_id = ?', undef, $otherUserID); my $flags = Bugzilla::Flag->new_from_list($flag_ids); $dbh->do('UPDATE flags SET requestee_id = NULL, modification_date = ? WHERE requestee_id = ?', undef, ($timestamp, $otherUserID)); # We want to remove the requestee but leave the requester alone, # so we have to log these changes manually. my %bugs; push(@{$bugs{$_->bug_id}->{$_->attach_id || 0}}, $_) foreach @$flags; foreach my $bug_id (keys %bugs) { foreach my $attach_id (keys %{$bugs{$bug_id}}) { my @old_summaries = Bugzilla::Flag->snapshot($bugs{$bug_id}->{$attach_id}); $_->_set_requestee() foreach @{$bugs{$bug_id}->{$attach_id}}; my @new_summaries = Bugzilla::Flag->snapshot($bugs{$bug_id}->{$attach_id}); my ($removed, $added) = Bugzilla::Flag->update_activity(\@old_summaries, \@new_summaries); LogActivityEntry($bug_id, 'flagtypes.name', $removed, $added, $userid, $timestamp, undef, $attach_id); } $sth_set_bug_timestamp->execute($timestamp, $bug_id); $updatedbugs{$bug_id} = 1; } # Simple deletions in referred tables. $dbh->do('DELETE FROM email_setting WHERE user_id = ?', undef, $otherUserID); $dbh->do('DELETE FROM logincookies WHERE userid = ?', undef, $otherUserID); $dbh->do('DELETE FROM namedqueries WHERE userid = ?', undef, $otherUserID); $dbh->do('DELETE FROM namedqueries_link_in_footer WHERE user_id = ?', undef, $otherUserID); if ($namedqueries_as_string) { $dbh->do('DELETE FROM namedquery_group_map WHERE namedquery_id IN ' . "($namedqueries_as_string)"); } $dbh->do('DELETE FROM profile_setting WHERE user_id = ?', undef, $otherUserID); $dbh->do('DELETE FROM profiles_activity WHERE userid = ? OR who = ?', undef, ($otherUserID, $otherUserID)); $dbh->do('DELETE FROM tokens WHERE userid = ?', undef, $otherUserID); $dbh->do('DELETE FROM user_group_map WHERE user_id = ?', undef, $otherUserID); $dbh->do('DELETE FROM watch WHERE watcher = ? OR watched = ?', undef, ($otherUserID, $otherUserID)); # Deletions in referred tables which need LogActivityEntry. my $buglist = $dbh->selectcol_arrayref('SELECT bug_id FROM cc WHERE who = ?', undef, $otherUserID); $dbh->do('DELETE FROM cc WHERE who = ?', undef, $otherUserID); foreach my $bug_id (@$buglist) { LogActivityEntry($bug_id, 'cc', $otherUser->login, '', $userid, $timestamp); $sth_set_bug_timestamp->execute($timestamp, $bug_id); $updatedbugs{$bug_id} = 1; } # Even more complex deletions in referred tables. my $id; # 1) Series my $sth_seriesid = $dbh->prepare( 'SELECT series_id FROM series WHERE creator = ?'); my $sth_deleteSeries = $dbh->prepare( 'DELETE FROM series WHERE series_id = ?'); my $sth_deleteSeriesData = $dbh->prepare( 'DELETE FROM series_data WHERE series_id = ?'); $sth_seriesid->execute($otherUserID); while ($id = $sth_seriesid->fetchrow_array()) { $sth_deleteSeriesData->execute($id); $sth_deleteSeries->execute($id); } # 2) Whines my $sth_whineidFromEvents = $dbh->prepare( 'SELECT id FROM whine_events WHERE owner_userid = ?'); my $sth_deleteWhineEvent = $dbh->prepare( 'DELETE FROM whine_events WHERE id = ?'); my $sth_deleteWhineQuery = $dbh->prepare( 'DELETE FROM whine_queries WHERE eventid = ?'); my $sth_deleteWhineSchedule = $dbh->prepare( 'DELETE FROM whine_schedules WHERE eventid = ?'); $dbh->do('DELETE FROM whine_schedules WHERE mailto = ? AND mailto_type = ?', undef, ($otherUserID, MAILTO_USER)); $sth_whineidFromEvents->execute($otherUserID); while ($id = $sth_whineidFromEvents->fetchrow_array()) { $sth_deleteWhineQuery->execute($id); $sth_deleteWhineSchedule->execute($id); $sth_deleteWhineEvent->execute($id); } # 3) Bugs # 3.1) fall back to the default assignee $buglist = $dbh->selectall_arrayref( 'SELECT bug_id, initialowner FROM bugs INNER JOIN components ON components.id = bugs.component_id WHERE assigned_to = ?', undef, $otherUserID); my $sth_updateAssignee = $dbh->prepare( 'UPDATE bugs SET assigned_to = ?, delta_ts = ? WHERE bug_id = ?'); foreach my $bug (@$buglist) { my ($bug_id, $default_assignee_id) = @$bug; $sth_updateAssignee->execute($default_assignee_id, $timestamp, $bug_id); $updatedbugs{$bug_id} = 1; $default_assignee_id ||= 0; $usercache{$default_assignee_id} ||= new Bugzilla::User($default_assignee_id); LogActivityEntry($bug_id, 'assigned_to', $otherUser->login, $usercache{$default_assignee_id}->login, $userid, $timestamp); } # 3.2) fall back to the default QA contact $buglist = $dbh->selectall_arrayref( 'SELECT bug_id, initialqacontact FROM bugs INNER JOIN components ON components.id = bugs.component_id WHERE qa_contact = ?', undef, $otherUserID); my $sth_updateQAcontact = $dbh->prepare( 'UPDATE bugs SET qa_contact = ?, delta_ts = ? WHERE bug_id = ?'); foreach my $bug (@$buglist) { my ($bug_id, $default_qa_contact_id) = @$bug; $sth_updateQAcontact->execute($default_qa_contact_id, $timestamp, $bug_id); $updatedbugs{$bug_id} = 1; $default_qa_contact_id ||= 0; $usercache{$default_qa_contact_id} ||= new Bugzilla::User($default_qa_contact_id); LogActivityEntry($bug_id, 'qa_contact', $otherUser->login, $usercache{$default_qa_contact_id}->login, $userid, $timestamp); } # Finally, remove the user account itself. $dbh->do('DELETE FROM profiles WHERE userid = ?', undef, $otherUserID); $dbh->bz_commit_transaction(); delete_token($token); # It's complex to determine which items now need to be flushed from # memcached. As user deletion is expected to be a rare event, we just # flush the entire cache when a user is deleted. Bugzilla->memcached->clear_all(); $vars->{'message'} = 'account_deleted'; $vars->{'otheruser'}{'login'} = $otherUser->login; $vars->{'restrictablegroups'} = $user->bless_groups(); $template->process('admin/users/search.html.tmpl', $vars) || ThrowTemplateError($template->error()); # Send mail about what we've done to bugs. # The deleted user is not notified of the changes. foreach (keys(%updatedbugs)) { Bugzilla::BugMail::Send($_, {'changer' => $user} ); } ########################################################################### } elsif ($action eq 'activity') { my $otherUser = check_user($otherUserID, $otherUserLogin); $vars->{'profile_changes'} = $dbh->selectall_arrayref( "SELECT profiles.login_name AS who, " . $dbh->sql_date_format('profiles_activity.profiles_when') . " AS activity_when, fielddefs.name AS what, profiles_activity.oldvalue AS removed, profiles_activity.newvalue AS added FROM profiles_activity INNER JOIN profiles ON profiles_activity.who = profiles.userid INNER JOIN fielddefs ON fielddefs.id = profiles_activity.fieldid WHERE profiles_activity.userid = ? ORDER BY profiles_activity.profiles_when", {'Slice' => {}}, $otherUser->id); $vars->{'otheruser'} = $otherUser; $template->process("account/profile-activity.html.tmpl", $vars) || ThrowTemplateError($template->error()); ########################################################################### } else { ThrowUserError('unknown_action', {action => $action}); } exit;