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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
|
#!/usr/bin/perl -T
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
use 5.10.1;
use strict;
use warnings;
use lib qw(. lib);
use Bugzilla;
use Bugzilla::Constants;
use Bugzilla::Util;
use Bugzilla::Error;
use Bugzilla::Group;
use Bugzilla::Product;
use Bugzilla::Component;
use Bugzilla::Classification;
use Bugzilla::Token;
#
# Preliminary checks:
#
my $user = Bugzilla->login(LOGIN_REQUIRED);
my $whoid = $user->id;
my $dbh = Bugzilla->dbh;
my $cgi = Bugzilla->cgi;
my $template = Bugzilla->template;
my $vars = {};
# Remove this as soon as the documentation about products has been
# improved and each action has its own section.
$vars->{'doc_section'} = 'administering/categorization.html#products';
print $cgi->header();
$user->in_group('editcomponents')
|| scalar(@{$user->get_products_by_permission('editcomponents')})
|| ThrowUserError("auth_failure",
{group => "editcomponents", action => "edit", object => "products"});
#
# often used variables
#
my $classification_name = trim($cgi->param('classification') || '');
my $product_name = trim($cgi->param('product') || '');
my $action = trim($cgi->param('action') || '');
my $token = $cgi->param('token');
#
# product = '' -> Show nice list of classifications (if
# classifications enabled)
#
if ( Bugzilla->params->{'useclassification'}
&& !$classification_name
&& !$product_name)
{
my $class;
if ($user->in_group('editcomponents')) {
$class = [Bugzilla::Classification->get_all];
}
else {
# Only keep classifications containing at least one product
# which you can administer.
my $products = $user->get_products_by_permission('editcomponents');
my %class_ids = map { $_->classification_id => 1 } @$products;
$class = Bugzilla::Classification->new_from_list([keys %class_ids]);
}
$vars->{'classifications'} = $class;
$template->process("admin/products/list-classifications.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
#
# action = '' -> Show a nice list of products, unless a product
# is already specified (then edit it)
#
if (!$action && !$product_name) {
my $classification;
my $products;
if (Bugzilla->params->{'useclassification'}) {
$classification = Bugzilla::Classification->check($classification_name);
$products = $user->get_selectable_products($classification->id);
$vars->{'classification'} = $classification;
}
else {
$products = $user->get_selectable_products;
}
# If the user has editcomponents privs for some products only,
# we have to restrict the list of products to display.
unless ($user->in_group('editcomponents')) {
$products = $user->get_products_by_permission('editcomponents');
if (Bugzilla->params->{'useclassification'}) {
@$products = grep { $_->classification_id == $classification->id } @$products;
}
}
$vars->{'products'} = $products;
$vars->{'showbugcounts'} = $cgi->param('showbugcounts') ? 1 : 0;
$template->process("admin/products/list.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
#
# action='add' -> present form for parameters for new product
#
# (next action will be 'new')
#
if ($action eq 'add') {
# The user must have the global editcomponents privs to add
# new products.
$user->in_group('editcomponents')
|| ThrowUserError("auth_failure",
{group => "editcomponents", action => "add", object => "products"});
if (Bugzilla->params->{'useclassification'}) {
my $classification = Bugzilla::Classification->check($classification_name);
$vars->{'classification'} = $classification;
}
$vars->{'token'} = issue_session_token('add_product');
## REDHAT EXTENSION START 1312306 1656675
$vars->{'groups'} = Bugzilla::Group->match();
## REDHAT EXTENSION END 1312306 1656675
$template->process("admin/products/create.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
#
# action='new' -> add product entered in the 'action=add' screen
#
if ($action eq 'new') {
# The user must have the global editcomponents privs to add
# new products.
$user->in_group('editcomponents')
|| ThrowUserError("auth_failure",
{group => "editcomponents", action => "add", object => "products"});
check_token_data($token, 'add_product');
Bugzilla::User::match_field({
'initialowner' => {'type' => 'single'},
'initialqacontact' => {'type' => 'single'},
## REDHAT EXTENSION 876015: Add initialdocscontact
'initialdocscontact' => {'type' => 'single'},
'initialcc' => {'type' => 'multi'},
});
my %product_create_params = (
classification => $classification_name,
name => $product_name,
description => scalar $cgi->param('description'),
version => scalar $cgi->param('version'),
defaultmilestone => scalar $cgi->param('defaultmilestone'),
defaultrelease => scalar $cgi->param('defaultrelease'),
isactive => scalar $cgi->param('is_active'),
create_series => scalar $cgi->param('createseries'),
allows_unconfirmed => scalar $cgi->param('allows_unconfirmed'),
);
$dbh->bz_start_transaction();
my $product = Bugzilla::Product->create(\%product_create_params);
my @initial_cc = $cgi->param('initialcc');
my %component_create_params = (
product => $product,
name => trim($cgi->param('component') || ''),
description => scalar $cgi->param('comp_desc'),
initialowner => scalar $cgi->param('initialowner'),
initialqacontact => scalar $cgi->param('initialqacontact'),
## REDHAT EXTENSION 876015: Add initialdocscontact
initialdocscontact => scalar $cgi->param('initialdocscontact'),
initial_cc => \@initial_cc,
create_series => scalar $cgi->param('createseries'),
);
Bugzilla::Component->create(\%component_create_params);
## REDHAT EXTENSION START 1312306 1656675
my $changes;
Bugzilla::Hook::process('update_product_add_vars',
{product => $product, vars => \%{$cgi->Vars}, changes => $changes});
## REDHAT EXTENSION END 1312306 1656675
$dbh->bz_commit_transaction();
delete_token($token);
$vars->{'message'} = 'product_created';
$vars->{'product'} = $product;
if (Bugzilla->params->{'useclassification'}) {
$vars->{'classification'}
= new Bugzilla::Classification($product->classification_id);
}
$vars->{'token'} = issue_session_token('edit_product');
## REDHAT EXTENSION START 1312306 1656675
$vars->{'groups'} = Bugzilla::Group->match();
## REDHAT EXTENSION END 1312306 1656675
$template->process("admin/products/edit.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
#
# action='del' -> ask if user really wants to delete
#
# (next action would be 'delete')
#
if ($action eq 'del') {
my $product = $user->check_can_admin_product($product_name);
if (Bugzilla->params->{'useclassification'}) {
$vars->{'classification'}
= new Bugzilla::Classification($product->classification_id);
}
$vars->{'product'} = $product;
$vars->{'token'} = issue_session_token('delete_product');
Bugzilla::Hook::process('product_confirm_delete', {vars => $vars});
$template->process("admin/products/confirm-delete.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
#
# action='delete' -> really delete the product
#
if ($action eq 'delete') {
my $product = $user->check_can_admin_product($product_name);
check_token_data($token, 'delete_product');
$product->remove_from_db(
{delete_series => scalar $cgi->param('delete_series')});
delete_token($token);
$vars->{'message'} = 'product_deleted';
$vars->{'product'} = $product;
$vars->{'no_edit_product_link'} = 1;
if (Bugzilla->params->{'useclassification'}) {
$vars->{'classifications'}
= $user->in_group('editcomponents')
? [Bugzilla::Classification->get_all]
: $user->get_selectable_classifications;
$template->process("admin/products/list-classifications.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
}
else {
my $products = $user->get_selectable_products;
# If the user has editcomponents privs for some products only,
# we have to restrict the list of products to display.
unless ($user->in_group('editcomponents')) {
$products = $user->get_products_by_permission('editcomponents');
}
$vars->{'products'} = $products;
$template->process("admin/products/list.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
}
exit;
}
#
# action='edit' -> present the 'edit product' form
# If a product is given with no action associated with it, then edit it.
#
# (next action would be 'update')
#
if ($action eq 'edit' || (!$action && $product_name)) {
my $product = $user->check_can_admin_product($product_name);
if (Bugzilla->params->{'useclassification'}) {
$vars->{'classification'}
= new Bugzilla::Classification($product->classification_id);
}
$vars->{'product'} = $product;
$vars->{'token'} = issue_session_token('edit_product');
## REDHAT EXTENSION START 1312306
$vars->{'groups'} = Bugzilla::Group->match();
## REDHAT EXTENSION END 1312306
$template->process("admin/products/edit.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
#
# action='update' -> update the product
#
if ($action eq 'update') {
check_token_data($token, 'edit_product');
my $product_old_name = trim($cgi->param('product_old_name') || '');
my $product = $user->check_can_admin_product($product_old_name);
## REDHAT EXTENSION START 951975
# If the product is 'always private' but it has public bugs, we need
# to catch this and offer a new group restriction
if (scalar $cgi->param('always_private')) {
if (my $group = scalar $cgi->param('always_private_group')) {
$product->private_product_set($group);
}
if ($product->private_product_check()) {
my $gc = $product->group_controls;
my @group_ids
= grep { $gc->{$_}{membercontrol} || $gc->{$_}{othercontrol} } keys %$gc;
my @product_groups = map { $gc->{$_}{group} } @group_ids;
my $template = Bugzilla->template;
$template->process(
'admin/products/private-group.html.tmpl',
{product_groups => \@product_groups}
) || ThrowTemplateError($template->error());
exit;
}
}
## REDHAT EXTENSION END 951975
$product->set_all({
name => $product_name,
description => scalar $cgi->param('description'),
is_active => scalar $cgi->param('is_active'),
allows_unconfirmed => scalar $cgi->param('allows_unconfirmed'),
default_milestone => scalar $cgi->param('defaultmilestone'),
agile_team => scalar $cgi->param('agile_team'), ## RED HAT EXTENSION 1622371
});
$product->set_default_release(scalar $cgi->param('defaultrelease'));
## REDHAT EXTENSION BEGIN 706784
$product->set_multiple_components(scalar $cgi->param('multiple_components'));
$product->set_multiple_target_releases(
scalar $cgi->param('multiple_target_releases'));
$product->set_multiple_versions(scalar $cgi->param('multiple_versions'));
## REDHAT EXTENSION END 706784
## REDHAT EXTENSION BEGIN 829154
$product->set_always_private(scalar $cgi->param('always_private'));
## REDHAT EXTENSION END 829154
## REDHAT EXTENSION BEGIN 1326559
$product->set_allows_on_dev(scalar $cgi->param('allows_on_dev'));
$product->set_allows_no_clear_acks(scalar $cgi->param('allows_no_clear_acks'));
## REDHAT EXTENSION END 1326559
my $changes = $product->update();
Bugzilla::Hook::process('update_product_add_vars',
{product => $product, vars => \%{$cgi->Vars}, changes => $changes});
delete_token($token);
if (Bugzilla->params->{'useclassification'}) {
$vars->{'classification'}
= new Bugzilla::Classification($product->classification_id);
}
$vars->{'product'} = $product;
$vars->{'changes'} = $changes;
$template->process("admin/products/updated.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
#
# action='editgroupcontrols' -> display product group controls
#
if ($action eq 'editgroupcontrols') {
my $product = $user->check_can_admin_product($product_name);
$vars->{'product'} = $product;
$vars->{'token'} = issue_session_token('edit_group_controls');
$template->process("admin/products/groupcontrol/edit.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
#
# action='updategroupcontrols' -> update product group controls
#
if ($action eq 'updategroupcontrols') {
my $product = $user->check_can_admin_product($product_name);
check_token_data($token, 'edit_group_controls');
my @now_na = ();
my @now_mandatory = ();
foreach my $f ($cgi->param()) {
if ($f =~ /^membercontrol_(\d+)$/) {
my $id = $1;
if ($cgi->param($f) == CONTROLMAPNA) {
push @now_na, $id;
}
elsif ($cgi->param($f) == CONTROLMAPMANDATORY) {
push @now_mandatory, $id;
}
}
}
if (!defined $cgi->param('confirmed')) {
my $na_groups;
if (@now_na) {
$na_groups = $dbh->selectall_arrayref(
'SELECT groups.name, COUNT(bugs.bug_id) AS count
FROM bugs
INNER JOIN bug_group_map
ON bug_group_map.bug_id = bugs.bug_id
INNER JOIN groups
ON bug_group_map.group_id = groups.id
WHERE groups.id IN (' . join(', ', @now_na) . ')
AND bugs.product_id = ? ' . $dbh->sql_group_by('groups.name'),
{'Slice' => {}}, $product->id
);
}
# return the mandatory groups which need to have bug entries
# added to the bug_group_map and the corresponding bug count
my $mandatory_groups;
if (@now_mandatory) {
$mandatory_groups = $dbh->selectall_arrayref(
'SELECT groups.name,
(SELECT COUNT(bugs.bug_id)
FROM bugs
WHERE bugs.product_id = ?
AND bugs.bug_id NOT IN
(SELECT bug_group_map.bug_id FROM bug_group_map
WHERE bug_group_map.group_id = groups.id))
AS count
FROM groups
WHERE groups.id IN (' . join(', ', @now_mandatory) . ')
ORDER BY groups.name', {'Slice' => {}}, $product->id
);
# remove zero counts
@$mandatory_groups = grep { $_->{count} } @$mandatory_groups;
}
if ( ($na_groups && scalar(@$na_groups))
|| ($mandatory_groups && scalar(@$mandatory_groups)))
{
$vars->{'product'} = $product;
$vars->{'na_groups'} = $na_groups;
$vars->{'mandatory_groups'} = $mandatory_groups;
$template->process("admin/products/groupcontrol/confirm-edit.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
}
my $groups = Bugzilla::Group->match({isactive => 1, isbuggroup => 1});
foreach my $group (@$groups) {
my $group_id = $group->id;
$product->set_group_controls(
$group,
{
entry => scalar $cgi->param("entry_$group_id") || 0,
membercontrol => scalar $cgi->param("membercontrol_$group_id") || CONTROLMAPNA,
othercontrol => scalar $cgi->param("othercontrol_$group_id") || CONTROLMAPNA,
canedit => scalar $cgi->param("canedit_$group_id") || 0,
editcomponents => scalar $cgi->param("editcomponents_$group_id") || 0,
editbugs => scalar $cgi->param("editbugs_$group_id") || 0,
canconfirm => scalar $cgi->param("canconfirm_$group_id") || 0
}
);
}
## REDHAT EXTENSION START 951975
# If the product is 'always private' but removing a group would
# expose a bug, we need to catch this and offer a new group restriction
if ($product->always_private) {
if (my $group = scalar $cgi->param('always_private_group')) {
$product->private_product_set($group);
}
if ($product->private_product_check()) {
my $gc = $product->group_controls;
my @group_ids
= grep { $gc->{$_}{membercontrol} || $gc->{$_}{othercontrol} } keys %$gc;
my @product_groups = map { $gc->{$_}{group} } @group_ids;
my $template = Bugzilla->template;
$template->process(
'admin/products/private-group.html.tmpl',
{product_groups => \@product_groups}
) || ThrowTemplateError($template->error());
exit;
}
}
## REDHAT EXTENSION END 951975
my $changes = $product->update;
delete_token($token);
$vars->{'product'} = $product;
$vars->{'changes'} = $changes;
$template->process("admin/products/groupcontrol/updated.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
# No valid action found
ThrowUserError('unknown_action', {action => $action});
|