2
# -*- Mode: perl; indent-tabs-mode: nil -*-
4
# The contents of this file are subject to the Mozilla Public
5
# License Version 1.1 (the "License"); you may not use this file
6
# except in compliance with the License. You may obtain a copy of
7
# the License at http://www.mozilla.org/MPL/
9
# Software distributed under the License is distributed on an "AS
10
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11
# implied. See the License for the specific language governing
12
# rights and limitations under the License.
14
# The Original Code is mozilla.org code.
16
# The Initial Developer of the Original Code is Holger
17
# Schurig. Portions created by Holger Schurig are
18
# Copyright (C) 1999 Holger Schurig. All
21
# Contributor(s): Holger Schurig <holgerschurig@nikocity.de>
22
# Terry Weissman <terry@mozilla.org>
23
# Frédéric Buclin <LpSolit@gmail.com>
24
# Akamai Technologies <bugzilla-dev@akamai.com>
26
# Direct any questions on this source code to
28
# Holger Schurig <holgerschurig@nikocity.de>
34
use Bugzilla::Constants;
39
use Bugzilla::Component;
47
# Takes an arrayref of login names and returns an arrayref of user ids.
48
sub check_initial_cc {
49
my ($user_names) = @_;
52
foreach my $cc (@$user_names) {
53
my $id = login_to_id($cc, THROW_ERROR);
56
return [keys %cc_ids];
63
my $cgi = Bugzilla->cgi;
64
my $dbh = Bugzilla->dbh;
65
my $template = Bugzilla->template;
72
my $user = Bugzilla->login(LOGIN_REQUIRED);
73
my $whoid = $user->id;
77
$user->in_group('editcomponents')
78
|| scalar(@{$user->get_products_by_permission('editcomponents')})
79
|| ThrowUserError("auth_failure", {group => "editcomponents",
81
object => "components"});
84
# often used variables
86
my $product_name = trim($cgi->param('product') || '');
87
my $comp_name = trim($cgi->param('component') || '');
88
my $action = trim($cgi->param('action') || '');
89
my $showbugcounts = (defined $cgi->param('showbugcounts'));
90
my $token = $cgi->param('token');
93
# product = '' -> Show nice list of products
96
unless ($product_name) {
97
my $selectable_products = $user->get_selectable_products;
98
# If the user has editcomponents privs for some products only,
99
# we have to restrict the list of products to display.
100
unless ($user->in_group('editcomponents')) {
101
$selectable_products = $user->get_products_by_permission('editcomponents');
103
$vars->{'products'} = $selectable_products;
104
$vars->{'showbugcounts'} = $showbugcounts;
106
$template->process("admin/components/select-product.html.tmpl", $vars)
107
|| ThrowTemplateError($template->error());
111
my $product = $user->check_can_admin_product($product_name);
114
# action='' -> Show nice list of components
119
$vars->{'showbugcounts'} = $showbugcounts;
120
$vars->{'product'} = $product;
121
$template->process("admin/components/list.html.tmpl", $vars)
122
|| ThrowTemplateError($template->error());
129
# action='add' -> present form for parameters for new component
131
# (next action will be 'new')
134
if ($action eq 'add') {
135
$vars->{'token'} = issue_session_token('add_component');
136
$vars->{'product'} = $product;
137
$template->process("admin/components/create.html.tmpl", $vars)
138
|| ThrowTemplateError($template->error());
146
# action='new' -> add component entered in the 'action=add' screen
149
if ($action eq 'new') {
150
check_token_data($token, 'add_component');
151
# Do the user matching
152
Bugzilla::User::match_field ($cgi, {
153
'initialowner' => { 'type' => 'single' },
154
'initialqacontact' => { 'type' => 'single' },
155
'initialcc' => { 'type' => 'multi' },
158
my $default_assignee = trim($cgi->param('initialowner') || '');
159
my $default_qa_contact = trim($cgi->param('initialqacontact') || '');
160
my $description = trim($cgi->param('description') || '');
161
my @initial_cc = $cgi->param('initialcc');
163
$comp_name || ThrowUserError('component_blank_name');
165
if (length($comp_name) > 64) {
166
ThrowUserError('component_name_too_long',
167
{'name' => $comp_name});
171
new Bugzilla::Component({product => $product,
172
name => $comp_name});
175
ThrowUserError('component_already_exists',
176
{'name' => $component->name});
179
$description || ThrowUserError('component_blank_description',
180
{name => $comp_name});
182
$default_assignee || ThrowUserError('component_need_initialowner',
183
{name => $comp_name});
185
my $default_assignee_id = login_to_id($default_assignee);
186
my $default_qa_contact_id = Bugzilla->params->{'useqacontact'} ?
187
(login_to_id($default_qa_contact) || undef) : undef;
189
my $initial_cc_ids = check_initial_cc(\@initial_cc);
191
trick_taint($comp_name);
192
trick_taint($description);
194
$dbh->bz_lock_tables('components WRITE', 'component_cc WRITE');
196
$dbh->do("INSERT INTO components
197
(product_id, name, description, initialowner,
199
VALUES (?, ?, ?, ?, ?)", undef,
200
($product->id, $comp_name, $description,
201
$default_assignee_id, $default_qa_contact_id));
203
$component = new Bugzilla::Component({ product => $product,
204
name => $comp_name });
206
my $sth = $dbh->prepare("INSERT INTO component_cc
207
(user_id, component_id) VALUES (?, ?)");
208
foreach my $user_id (@$initial_cc_ids) {
209
$sth->execute($user_id, $component->id);
212
$dbh->bz_unlock_tables;
214
# Insert default charting queries for this product.
215
# If they aren't using charting, this won't do any harm.
218
my $prodcomp = "&product=" . url_quote($product->name) .
219
"&component=" . url_quote($comp_name);
221
# For localization reasons, we get the title of the queries from the
223
my $open_name = $cgi->param('open_name');
224
my $nonopen_name = $cgi->param('nonopen_name');
225
my $open_query = "field0-0-0=resolution&type0-0-0=notregexp&value0-0-0=." .
227
my $nonopen_query = "field0-0-0=resolution&type0-0-0=regexp&value0-0-0=." .
230
# trick_taint is ok here, as these variables aren't used as a command
232
trick_taint($open_name);
233
trick_taint($nonopen_name);
234
trick_taint($open_query);
235
trick_taint($nonopen_query);
237
push(@series, [$open_name, $open_query]);
238
push(@series, [$nonopen_name, $nonopen_query]);
240
foreach my $sdata (@series) {
241
my $series = new Bugzilla::Series(undef, $product->name,
242
$comp_name, $sdata->[0],
243
$whoid, 1, $sdata->[1], 1);
244
$series->writeToDatabase();
247
$vars->{'comp'} = $component;
248
$vars->{'product'} = $product;
249
delete_token($token);
251
$template->process("admin/components/created.html.tmpl",
253
|| ThrowTemplateError($template->error());
261
# action='del' -> ask if user really wants to delete
263
# (next action would be 'delete')
266
if ($action eq 'del') {
267
$vars->{'token'} = issue_session_token('delete_component');
269
Bugzilla::Component::check_component($product, $comp_name);
271
$vars->{'product'} = $product;
273
$template->process("admin/components/confirm-delete.html.tmpl", $vars)
274
|| ThrowTemplateError($template->error());
282
# action='delete' -> really delete the component
285
if ($action eq 'delete') {
286
check_token_data($token, 'delete_component');
288
Bugzilla::Component::check_component($product, $comp_name);
290
if ($component->bug_count) {
291
if (Bugzilla->params->{"allowbugdeletion"}) {
292
foreach my $bug_id (@{$component->bug_ids}) {
293
# Note: We allow admins to delete bugs even if they can't
294
# see them, as long as they can see the product.
295
my $bug = new Bugzilla::Bug($bug_id);
296
$bug->remove_from_db();
299
ThrowUserError("component_has_bugs",
300
{nb => $component->bug_count });
304
$dbh->bz_lock_tables('components WRITE', 'component_cc WRITE',
305
'flaginclusions WRITE', 'flagexclusions WRITE');
307
$dbh->do("DELETE FROM flaginclusions WHERE component_id = ?",
308
undef, $component->id);
309
$dbh->do("DELETE FROM flagexclusions WHERE component_id = ?",
310
undef, $component->id);
311
$dbh->do("DELETE FROM component_cc WHERE component_id = ?",
312
undef, $component->id);
313
$dbh->do("DELETE FROM components WHERE id = ?",
314
undef, $component->id);
316
$dbh->bz_unlock_tables();
318
$vars->{'comp'} = $component;
319
$vars->{'product'} = $product;
320
delete_token($token);
322
$template->process("admin/components/deleted.html.tmpl", $vars)
323
|| ThrowTemplateError($template->error());
330
# action='edit' -> present the edit component form
332
# (next action would be 'update')
335
if ($action eq 'edit') {
336
$vars->{'token'} = issue_session_token('edit_component');
338
Bugzilla::Component::check_component($product, $comp_name);
339
$vars->{'comp'} = $component;
341
$vars->{'initial_cc_names'} =
342
join(', ', map($_->login, @{$component->initial_cc}));
344
$vars->{'product'} = $product;
346
$template->process("admin/components/edit.html.tmpl",
348
|| ThrowTemplateError($template->error());
356
# action='update' -> update the component
359
if ($action eq 'update') {
360
check_token_data($token, 'edit_component');
361
# Do the user matching
362
Bugzilla::User::match_field ($cgi, {
363
'initialowner' => { 'type' => 'single' },
364
'initialqacontact' => { 'type' => 'single' },
365
'initialcc' => { 'type' => 'multi' },
368
my $comp_old_name = trim($cgi->param('componentold') || '');
369
my $default_assignee = trim($cgi->param('initialowner') || '');
370
my $default_qa_contact = trim($cgi->param('initialqacontact') || '');
371
my $description = trim($cgi->param('description') || '');
372
my @initial_cc = $cgi->param('initialcc');
375
Bugzilla::Component::check_component($product, $comp_old_name);
377
$comp_name || ThrowUserError('component_blank_name');
379
if (length($comp_name) > 64) {
380
ThrowUserError('component_name_too_long',
381
{'name' => $comp_name});
384
if ($comp_name ne $component_old->name) {
386
new Bugzilla::Component({product => $product,
387
name => $comp_name});
389
ThrowUserError('component_already_exists',
390
{'name' => $component->name});
394
$description || ThrowUserError('component_blank_description',
395
{'name' => $component_old->name});
397
$default_assignee || ThrowUserError('component_need_initialowner',
398
{name => $comp_name});
400
my $default_assignee_id = login_to_id($default_assignee);
401
my $default_qa_contact_id = login_to_id($default_qa_contact) || undef;
403
my $initial_cc_ids = check_initial_cc(\@initial_cc);
405
$dbh->bz_lock_tables('components WRITE', 'component_cc WRITE',
408
if ($comp_name ne $component_old->name) {
410
trick_taint($comp_name);
411
$dbh->do("UPDATE components SET name = ? WHERE id = ?",
412
undef, ($comp_name, $component_old->id));
414
$vars->{'updated_name'} = 1;
418
if ($description ne $component_old->description) {
420
trick_taint($description);
421
$dbh->do("UPDATE components SET description = ? WHERE id = ?",
422
undef, ($description, $component_old->id));
424
$vars->{'updated_description'} = 1;
427
if ($default_assignee ne $component_old->default_assignee->login) {
429
$dbh->do("UPDATE components SET initialowner = ? WHERE id = ?",
430
undef, ($default_assignee_id, $component_old->id));
432
$vars->{'updated_initialowner'} = 1;
435
if (Bugzilla->params->{'useqacontact'}
436
&& $default_qa_contact ne $component_old->default_qa_contact->login) {
437
$dbh->do("UPDATE components SET initialqacontact = ?
438
WHERE id = ?", undef,
439
($default_qa_contact_id, $component_old->id));
441
$vars->{'updated_initialqacontact'} = 1;
444
my @initial_cc_old = map($_->id, @{$component_old->initial_cc});
445
my ($removed, $added) = diff_arrays(\@initial_cc_old, $initial_cc_ids);
447
foreach my $user_id (@$removed) {
448
$dbh->do('DELETE FROM component_cc
449
WHERE component_id = ? AND user_id = ?', undef,
450
$component_old->id, $user_id);
451
$vars->{'updated_initialcc'} = 1;
454
foreach my $user_id (@$added) {
455
$dbh->do("INSERT INTO component_cc (user_id, component_id)
456
VALUES (?, ?)", undef, $user_id, $component_old->id);
457
$vars->{'updated_initialcc'} = 1;
460
$dbh->bz_unlock_tables();
462
my $component = new Bugzilla::Component($component_old->id);
464
$vars->{'comp'} = $component;
465
$vars->{'initial_cc_names'} =
466
join(', ', map($_->login, @{$component->initial_cc}));
467
$vars->{'product'} = $product;
468
delete_token($token);
470
$template->process("admin/components/updated.html.tmpl",
472
|| ThrowTemplateError($template->error());
478
# No valid action found
480
ThrowUserError('no_valid_action', {'field' => "component"});