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
|
#!/usr/bin/perl -wT
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Bugzilla Bug Tracking System.
#
# The Initial Developer of the Original Code is Netscape Communications
# Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
# Contributor(s): Myk Melez <myk@mozilla.org>
################################################################################
# Script Initialization
################################################################################
# Make it harder for us to do dangerous things in Perl.
use strict;
use lib ".";
# Include the Bugzilla CGI and general utility library.
require "CGI.pl";
# Use Bugzilla's flag modules for handling flag types.
use Bugzilla;
use Bugzilla::Constants;
use Bugzilla::Flag;
use Bugzilla::FlagType;
use vars qw( $template $vars );
# Make sure the user is logged in and is an administrator.
Bugzilla->login(LOGIN_REQUIRED);
UserInGroup("editcomponents")
|| ThrowUserError("authorization_failure",
{ action => "administer flag types" });
# Suppress "used only once" warnings.
use vars qw(@legal_product @legal_components %components);
my $product_id;
my $component_id;
################################################################################
# Main Body Execution
################################################################################
# All calls to this script should contain an "action" variable whose value
# determines what the user wants to do. The code below checks the value of
# that variable and runs the appropriate code.
# Determine whether to use the action specified by the user or the default.
my $action = $::FORM{'action'} || 'list';
my @categoryActions;
if (@categoryActions = grep(/^categoryAction-.+/, keys(%::FORM))) {
$categoryActions[0] =~ s/^categoryAction-//;
processCategoryChange($categoryActions[0]);
exit;
}
if ($action eq 'list') { list(); }
elsif ($action eq 'enter') { edit(); }
elsif ($action eq 'copy') { edit(); }
elsif ($action eq 'edit') { edit(); }
elsif ($action eq 'insert') { insert(); }
elsif ($action eq 'update') { update(); }
elsif ($action eq 'confirmdelete') { confirmDelete(); }
elsif ($action eq 'delete') { deleteType(); }
elsif ($action eq 'deactivate') { deactivate(); }
else {
ThrowCodeError("action_unrecognized", { action => $action });
}
exit;
################################################################################
# Functions
################################################################################
sub list {
# Define the variables and functions that will be passed to the UI template.
$vars->{'bug_types'} =
Bugzilla::FlagType::match({ 'target_type' => 'bug',
'group' => $::FORM{'group'} }, 1);
$vars->{'attachment_types'} =
Bugzilla::FlagType::match({ 'target_type' => 'attachment',
'group' => $::FORM{'group'} }, 1);
# Return the appropriate HTTP response headers.
print Bugzilla->cgi->header();
# Generate and return the UI (HTML page) from the appropriate template.
$template->process("admin/flag-type/list.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
}
sub edit {
$action eq 'enter' ? validateTargetType() : validateID();
# Get this installation's products and components.
GetVersionTable();
# products and components and the function used to modify the components
# menu when the products menu changes; used by the template to populate
# the menus and keep the components menu consistent with the products menu
$vars->{'products'} = \@::legal_product;
$vars->{'components'} = \@::legal_components;
$vars->{'components_by_product'} = \%::components;
$vars->{'last_action'} = $::FORM{'action'};
if ($::FORM{'action'} eq 'enter' || $::FORM{'action'} eq 'copy') {
$vars->{'action'} = "insert";
}
else {
$vars->{'action'} = "update";
}
# If copying or editing an existing flag type, retrieve it.
if ($::FORM{'action'} eq 'copy' || $::FORM{'action'} eq 'edit') {
$vars->{'type'} = Bugzilla::FlagType::get($::FORM{'id'});
$vars->{'type'}->{'inclusions'} = Bugzilla::FlagType::get_inclusions($::FORM{'id'});
$vars->{'type'}->{'exclusions'} = Bugzilla::FlagType::get_exclusions($::FORM{'id'});
# Users want to see group names, not IDs
foreach my $group ("grant_gid", "request_gid") {
my $gid = $vars->{'type'}->{$group};
next if (!$gid);
SendSQL("SELECT name FROM groups WHERE id = $gid");
$vars->{'type'}->{$group} = FetchOneColumn();
}
}
# Otherwise set the target type (the minimal information about the type
# that the template needs to know) from the URL parameter and default
# the list of inclusions to all categories.
else {
$vars->{'type'} = { 'target_type' => $::FORM{'target_type'} ,
'inclusions' => ["__Any__:__Any__"] };
}
# Return the appropriate HTTP response headers.
print Bugzilla->cgi->header();
# Generate and return the UI (HTML page) from the appropriate template.
$template->process("admin/flag-type/edit.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
}
sub processCategoryChange {
my $categoryAction = shift;
validateIsActive();
validateIsRequestable();
validateIsRequesteeble();
validateAllowMultiple();
my @inclusions = $::MFORM{'inclusions'} ? @{$::MFORM{'inclusions'}} : ();
my @exclusions = $::MFORM{'exclusions'} ? @{$::MFORM{'exclusions'}} : ();
if ($categoryAction eq 'include') {
validateProduct();
validateComponent();
my $category = ($::FORM{'product'} || "__Any__") . ":" . ($::FORM{'component'} || "__Any__");
push(@inclusions, $category) unless grep($_ eq $category, @inclusions);
}
elsif ($categoryAction eq 'exclude') {
validateProduct();
validateComponent();
my $category = ($::FORM{'product'} || "__Any__") . ":" . ($::FORM{'component'} || "__Any__");
push(@exclusions, $category) unless grep($_ eq $category, @exclusions);
}
elsif ($categoryAction eq 'removeInclusion') {
@inclusions = map(($_ eq $::FORM{'inclusion_to_remove'} ? () : $_), @inclusions);
}
elsif ($categoryAction eq 'removeExclusion') {
@exclusions = map(($_ eq $::FORM{'exclusion_to_remove'} ? () : $_), @exclusions);
}
# Get this installation's products and components.
GetVersionTable();
# products and components; used by the template to populate the menus
# and keep the components menu consistent with the products menu
$vars->{'products'} = \@::legal_product;
$vars->{'components'} = \@::legal_components;
$vars->{'components_by_product'} = \%::components;
$vars->{'action'} = $::FORM{'action'};
my $type = {};
foreach my $key (keys %::FORM) { $type->{$key} = $::FORM{$key} }
$type->{'inclusions'} = \@inclusions;
$type->{'exclusions'} = \@exclusions;
$vars->{'type'} = $type;
# Return the appropriate HTTP response headers.
print Bugzilla->cgi->header();
# Generate and return the UI (HTML page) from the appropriate template.
$template->process("admin/flag-type/edit.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
}
sub insert {
validateName();
validateDescription();
validateCCList();
validateTargetType();
validateSortKey();
validateIsActive();
validateIsRequestable();
validateIsRequesteeble();
validateAllowMultiple();
validateGroups();
my $name = SqlQuote($::FORM{'name'});
my $description = SqlQuote($::FORM{'description'});
my $cc_list = SqlQuote($::FORM{'cc_list'});
my $target_type = $::FORM{'target_type'} eq "bug" ? "b" : "a";
SendSQL("LOCK TABLES flagtypes WRITE, products READ, components READ, " .
"flaginclusions WRITE, flagexclusions WRITE");
# Determine the new flag type's unique identifier.
SendSQL("SELECT MAX(id) FROM flagtypes");
my $id = FetchSQLData() + 1;
# Insert a record for the new flag type into the database.
SendSQL("INSERT INTO flagtypes (id, name, description, cc_list,
target_type, sortkey, is_active, is_requestable,
is_requesteeble, is_multiplicable,
grant_group_id, request_group_id)
VALUES ($id, $name, $description, $cc_list, '$target_type',
$::FORM{'sortkey'}, $::FORM{'is_active'},
$::FORM{'is_requestable'}, $::FORM{'is_requesteeble'},
$::FORM{'is_multiplicable'}, $::FORM{'grant_gid'},
$::FORM{'request_gid'})");
# Populate the list of inclusions/exclusions for this flag type.
foreach my $category_type ("inclusions", "exclusions") {
foreach my $category (@{$::MFORM{$category_type}}) {
my ($product, $component) = split(/:/, $category);
my $product_id = get_product_id($product) || "NULL";
my $component_id =
get_component_id($product_id, $component) || "NULL";
SendSQL("INSERT INTO flag$category_type (type_id, product_id, " .
"component_id) VALUES ($id, $product_id, $component_id)");
}
}
SendSQL("UNLOCK TABLES");
$vars->{'name'} = $::FORM{'name'};
$vars->{'message'} = "flag_type_created";
# Return the appropriate HTTP response headers.
print Bugzilla->cgi->header();
# Generate and return the UI (HTML page) from the appropriate template.
$template->process("global/message.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
}
sub update {
validateID();
validateName();
validateDescription();
validateCCList();
validateTargetType();
validateSortKey();
validateIsActive();
validateIsRequestable();
validateIsRequesteeble();
validateAllowMultiple();
validateGroups();
my $name = SqlQuote($::FORM{'name'});
my $description = SqlQuote($::FORM{'description'});
my $cc_list = SqlQuote($::FORM{'cc_list'});
SendSQL("LOCK TABLES flagtypes WRITE, products READ, components READ, " .
"flaginclusions WRITE, flagexclusions WRITE");
SendSQL("UPDATE flagtypes
SET name = $name ,
description = $description ,
cc_list = $cc_list ,
sortkey = $::FORM{'sortkey'} ,
is_active = $::FORM{'is_active'} ,
is_requestable = $::FORM{'is_requestable'} ,
is_requesteeble = $::FORM{'is_requesteeble'} ,
is_multiplicable = $::FORM{'is_multiplicable'} ,
grant_group_id = $::FORM{'grant_gid'} ,
request_group_id = $::FORM{'request_gid'}
WHERE id = $::FORM{'id'}");
# Update the list of inclusions/exclusions for this flag type.
foreach my $category_type ("inclusions", "exclusions") {
SendSQL("DELETE FROM flag$category_type WHERE type_id = $::FORM{'id'}");
foreach my $category (@{$::MFORM{$category_type}}) {
my ($product, $component) = split(/:/, $category);
my $product_id = get_product_id($product) || "NULL";
my $component_id =
get_component_id($product_id, $component) || "NULL";
SendSQL("INSERT INTO flag$category_type (type_id, product_id, " .
"component_id) VALUES ($::FORM{'id'}, $product_id, " .
"$component_id)");
}
}
SendSQL("UNLOCK TABLES");
# Clear existing flags for bugs/attachments in categories no longer on
# the list of inclusions or that have been added to the list of exclusions.
SendSQL("
SELECT flags.id
FROM flags, bugs LEFT OUTER JOIN flaginclusions AS i
ON (flags.type_id = i.type_id
AND (bugs.product_id = i.product_id OR i.product_id IS NULL)
AND (bugs.component_id = i.component_id OR i.component_id IS NULL))
WHERE flags.type_id = $::FORM{'id'}
AND flags.bug_id = bugs.bug_id
AND flags.is_active = 1
AND i.type_id IS NULL
");
Bugzilla::Flag::clear(FetchOneColumn()) while MoreSQLData();
SendSQL("
SELECT flags.id
FROM flags, bugs, flagexclusions AS e
WHERE flags.type_id = $::FORM{'id'}
AND flags.bug_id = bugs.bug_id
AND flags.type_id = e.type_id
AND flags.is_active = 1
AND (bugs.product_id = e.product_id OR e.product_id IS NULL)
AND (bugs.component_id = e.component_id OR e.component_id IS NULL)
");
Bugzilla::Flag::clear(FetchOneColumn()) while MoreSQLData();
$vars->{'name'} = $::FORM{'name'};
$vars->{'message'} = "flag_type_changes_saved";
# Return the appropriate HTTP response headers.
print Bugzilla->cgi->header();
# Generate and return the UI (HTML page) from the appropriate template.
$template->process("global/message.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
}
sub confirmDelete
{
validateID();
# check if we need confirmation to delete:
my $count = Bugzilla::Flag::count({ 'type_id' => $::FORM{'id'},
'is_active' => 1 });
if ($count > 0) {
$vars->{'flag_type'} = Bugzilla::FlagType::get($::FORM{'id'});
$vars->{'flag_count'} = scalar($count);
# Return the appropriate HTTP response headers.
print Bugzilla->cgi->header();
# Generate and return the UI (HTML page) from the appropriate template.
$template->process("admin/flag-type/confirm-delete.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
}
else {
deleteType();
}
}
sub deleteType {
validateID();
SendSQL("LOCK TABLES flagtypes WRITE, flags WRITE, " .
"flaginclusions WRITE, flagexclusions WRITE");
# Get the name of the flag type so we can tell users
# what was deleted.
SendSQL("SELECT name FROM flagtypes WHERE id = $::FORM{'id'}");
$vars->{'name'} = FetchOneColumn();
SendSQL("DELETE FROM flags WHERE type_id = $::FORM{'id'}");
SendSQL("DELETE FROM flaginclusions WHERE type_id = $::FORM{'id'}");
SendSQL("DELETE FROM flagexclusions WHERE type_id = $::FORM{'id'}");
SendSQL("DELETE FROM flagtypes WHERE id = $::FORM{'id'}");
SendSQL("UNLOCK TABLES");
$vars->{'message'} = "flag_type_deleted";
# Return the appropriate HTTP response headers.
print Bugzilla->cgi->header();
# Generate and return the UI (HTML page) from the appropriate template.
$template->process("global/message.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
}
sub deactivate {
validateID();
validateIsActive();
SendSQL("LOCK TABLES flagtypes WRITE");
SendSQL("UPDATE flagtypes SET is_active = 0 WHERE id = $::FORM{'id'}");
SendSQL("UNLOCK TABLES");
$vars->{'message'} = "flag_type_deactivated";
$vars->{'flag_type'} = Bugzilla::FlagType::get($::FORM{'id'});
# Return the appropriate HTTP response headers.
print Bugzilla->cgi->header();
# Generate and return the UI (HTML page) from the appropriate template.
$template->process("global/message.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
}
################################################################################
# Data Validation / Security Authorization
################################################################################
sub validateID {
detaint_natural($::FORM{'id'})
|| ThrowCodeError("flag_type_id_invalid", { id => $::FORM{'id'} });
SendSQL("SELECT 1 FROM flagtypes WHERE id = $::FORM{'id'}");
FetchOneColumn()
|| ThrowCodeError("flag_type_nonexistent", { id => $::FORM{'id'} });
}
sub validateName {
$::FORM{'name'}
&& length($::FORM{'name'}) <= 50
|| ThrowUserError("flag_type_name_invalid", { name => $::FORM{'name'} });
}
sub validateDescription {
length($::FORM{'description'}) < 2**16-1
|| ThrowUserError("flag_type_description_invalid");
}
sub validateCCList {
length($::FORM{'cc_list'}) <= 200
|| ThrowUserError("flag_type_cc_list_invalid",
{ cc_list => $::FORM{'cc_list'} });
my @addresses = split(/[, ]+/, $::FORM{'cc_list'});
foreach my $address (@addresses) { CheckEmailSyntax($address) }
}
sub validateProduct {
return if !$::FORM{'product'};
$product_id = get_product_id($::FORM{'product'});
defined($product_id)
|| ThrowCodeError("flag_type_product_nonexistent",
{ product => $::FORM{'product'} });
}
sub validateComponent {
return if !$::FORM{'component'};
$product_id
|| ThrowCodeError("flag_type_component_without_product");
$component_id = get_component_id($product_id, $::FORM{'component'});
defined($component_id)
|| ThrowCodeError("flag_type_component_nonexistent",
{ product => $::FORM{'product'},
component => $::FORM{'component'} });
}
sub validateSortKey {
detaint_natural($::FORM{'sortkey'})
&& $::FORM{'sortkey'} < 32768
|| ThrowUserError("flag_type_sortkey_invalid",
{ sortkey => $::FORM{'sortkey'} });
}
sub validateTargetType {
grep($::FORM{'target_type'} eq $_, ("bug", "attachment"))
|| ThrowCodeError("flag_type_target_type_invalid",
{ target_type => $::FORM{'target_type'} });
}
sub validateIsActive {
$::FORM{'is_active'} = $::FORM{'is_active'} ? 1 : 0;
}
sub validateIsRequestable {
$::FORM{'is_requestable'} = $::FORM{'is_requestable'} ? 1 : 0;
}
sub validateIsRequesteeble {
$::FORM{'is_requesteeble'} = $::FORM{'is_requesteeble'} ? 1 : 0;
}
sub validateAllowMultiple {
$::FORM{'is_multiplicable'} = $::FORM{'is_multiplicable'} ? 1 : 0;
}
sub validateGroups {
# Convert group names to group IDs
foreach my $col ("grant_gid", "request_gid") {
my $name = $::FORM{$col};
$::FORM{$col} ||= "NULL";
next if (!$name);
SendSQL("SELECT id FROM groups WHERE name = " . SqlQuote($name));
$::FORM{$col} = FetchOneColumn();
if (!$::FORM{$col}) {
ThrowUserError("group_unknown", { name => $name });
}
}
}
|