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
|
#!/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>
# Frédéric Buclin <LpSolit@gmail.com>
################################################################################
# Script Initialization
################################################################################
# Make it harder for us to do dangerous things in Perl.
use strict;
use lib qw(. lib);
# Use Bugzilla's flag modules for handling flag types.
use Bugzilla;
use Bugzilla::Constants;
use Bugzilla::Flag;
use Bugzilla::FlagType;
use Bugzilla::Group;
use Bugzilla::Util;
use Bugzilla::Error;
use Bugzilla::Product;
use Bugzilla::Component;
use Bugzilla::Token;
# Make sure the user is logged in and has the right privileges.
my $user = Bugzilla->login(LOGIN_REQUIRED);
my $cgi = Bugzilla->cgi;
my $template = Bugzilla->template;
# We need this everywhere.
my $vars = get_products_and_components();
print $cgi->header();
$user->in_group('editcomponents')
|| ThrowUserError("auth_failure", {group => "editcomponents",
action => "edit",
object => "flagtypes"});
my $action = $cgi->param('action') || 'list';
my $token = $cgi->param('token');
my $product = $cgi->param('product');
my $component = $cgi->param('component');
my $flag_id = $cgi->param('id');
if ($product) {
$product = Bugzilla::Product->check({ name => $product, allow_inaccessible => 1 });
}
if ($component) {
($product && $product->id)
|| ThrowUserError('flag_type_component_without_product');
$component = Bugzilla::Component->check({ product => $product, name => $component });
}
# If 'categoryAction' is set, it has priority over 'action'.
if (my ($category_action) = grep { $_ =~ /^categoryAction-(?:\w+)$/ } $cgi->param()) {
$category_action =~ s/^categoryAction-//;
my @inclusions = $cgi->param('inclusions');
my @exclusions = $cgi->param('exclusions');
if ($category_action eq 'include') {
my $category = ($product ? $product->id : 0) . ":" .
($component ? $component->id : 0);
push(@inclusions, $category) unless grep($_ eq $category, @inclusions);
}
elsif ($category_action eq 'exclude') {
my $category = ($product ? $product->id : 0) . ":" .
($component ? $component->id : 0);
push(@exclusions, $category) unless grep($_ eq $category, @exclusions);
}
elsif ($category_action eq 'removeInclusion') {
my @inclusion_to_remove = $cgi->param('inclusion_to_remove');
foreach my $remove (@inclusion_to_remove) {
@inclusions = grep { $_ ne $remove } @inclusions;
}
}
elsif ($category_action eq 'removeExclusion') {
my @exclusion_to_remove = $cgi->param('exclusion_to_remove');
foreach my $remove (@exclusion_to_remove) {
@exclusions = grep { $_ ne $remove } @exclusions;
}
}
# Convert the array @clusions('prod_ID:comp_ID') back to a hash of
# the form %clusions{'prod_name:comp_name'} = 'prod_ID:comp_ID'
my %inclusions = clusion_array_to_hash(\@inclusions);
my %exclusions = clusion_array_to_hash(\@exclusions);
$vars->{'groups'} = [Bugzilla::Group->get_all];
$vars->{'action'} = $action;
my $type = {};
$type->{$_} = $cgi->param($_) foreach $cgi->param();
# Make sure boolean fields are defined, else they fall back to 1.
foreach my $boolean qw(is_active is_requestable is_requesteeble is_multiplicable) {
$type->{$boolean} ||= 0;
}
# That's what I call a big hack. The template expects to see a group object.
$type->{'grant_group'} = {};
$type->{'grant_group'}->{'name'} = $cgi->param('grant_group');
$type->{'request_group'} = {};
$type->{'request_group'}->{'name'} = $cgi->param('request_group');
$type->{'inclusions'} = \%inclusions;
$type->{'exclusions'} = \%exclusions;
$vars->{'type'} = $type;
$vars->{'token'} = $token;
$vars->{'check_clusions'} = 1;
$template->process("admin/flag-type/edit.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
if ($action eq 'list') {
my $product_id = $product ? $product->id : 0;
my $component_id = $component ? $component->id : 0;
my $show_flag_counts = $cgi->param('show_flag_counts') ? 1 : 0;
my $group_id = $cgi->param('group');
my $bug_flagtypes;
my $attach_flagtypes;
# If a component is given, restrict the list to flag types available
# for this component.
if ($component) {
$bug_flagtypes = $component->flag_types->{'bug'};
$attach_flagtypes = $component->flag_types->{'attachment'};
# Filter flag types if a group ID is given.
$bug_flagtypes = filter_group($bug_flagtypes, $group_id);
$attach_flagtypes = filter_group($attach_flagtypes, $group_id);
}
# If only a product is specified but no component, then restrict the list
# to flag types available in at least one component of that product.
elsif ($product) {
$bug_flagtypes = $product->flag_types->{'bug'};
$attach_flagtypes = $product->flag_types->{'attachment'};
# Filter flag types if a group ID is given.
$bug_flagtypes = filter_group($bug_flagtypes, $group_id);
$attach_flagtypes = filter_group($attach_flagtypes, $group_id);
}
# If no product is given, then show all flag types available.
else {
my $flagtypes = Bugzilla::FlagType::match({ group => $group_id });
$bug_flagtypes = [grep { $_->target_type eq 'bug' } @$flagtypes];
$attach_flagtypes = [grep { $_->target_type eq 'attachment' } @$flagtypes];
}
if ($show_flag_counts) {
my %bug_lists;
my %map = ('+' => 'granted', '-' => 'denied', '?' => 'pending');
foreach my $flagtype (@$bug_flagtypes, @$attach_flagtypes) {
$bug_lists{$flagtype->id} = {};
my $flags = Bugzilla::Flag->match({type_id => $flagtype->id});
# Build lists of bugs, triaged by flag status.
push(@{$bug_lists{$flagtype->id}->{$map{$_->status}}}, $_->bug_id) foreach @$flags;
}
$vars->{'bug_lists'} = \%bug_lists;
$vars->{'show_flag_counts'} = 1;
}
$vars->{'selected_product'} = $product ? $product->name : '';
$vars->{'selected_component'} = $component ? $component->name : '';
$vars->{'bug_types'} = $bug_flagtypes;
$vars->{'attachment_types'} = $attach_flagtypes;
$template->process("admin/flag-type/list.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
if ($action eq 'enter') {
my $type = $cgi->param('target_type');
($type eq 'bug' || $type eq 'attachment')
|| ThrowCodeError('flag_type_target_type_invalid', { target_type => $type });
$vars->{'action'} = 'insert';
$vars->{'token'} = issue_session_token('add_flagtype');
$vars->{'type'} = { 'target_type' => $type,
'inclusions' => { '__Any__:__Any__' => '0:0' } };
# Get a list of groups available to restrict this flag type against.
$vars->{'groups'} = [Bugzilla::Group->get_all];
$template->process("admin/flag-type/edit.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
if ($action eq 'edit' || $action eq 'copy') {
$vars->{'type'} = Bugzilla::FlagType->check({ id => $flag_id });
if ($action eq 'copy') {
$vars->{'action'} = "insert";
$vars->{'token'} = issue_session_token('add_flagtype');
}
else {
$vars->{'action'} = "update";
$vars->{'token'} = issue_session_token('edit_flagtype');
}
# Get a list of groups available to restrict this flag type against.
$vars->{'groups'} = [Bugzilla::Group->get_all];
$template->process("admin/flag-type/edit.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
if ($action eq 'insert') {
check_token_data($token, 'add_flagtype');
my $name = $cgi->param('name');
my $description = $cgi->param('description');
my $target_type = $cgi->param('target_type');
my $cc_list = $cgi->param('cc_list');
my $sortkey = $cgi->param('sortkey');
my $is_active = $cgi->param('is_active');
my $is_requestable = $cgi->param('is_requestable');
my $is_specifically = $cgi->param('is_requesteeble');
my $is_multiplicable = $cgi->param('is_multiplicable');
my $grant_group = $cgi->param('grant_group');
my $request_group = $cgi->param('request_group');
my @inclusions = $cgi->param('inclusions');
my @exclusions = $cgi->param('exclusions');
my $flagtype = Bugzilla::FlagType->create({
name => $name,
description => $description,
target_type => $target_type,
cc_list => $cc_list,
sortkey => $sortkey,
is_active => $is_active,
is_requestable => $is_requestable,
is_requesteeble => $is_specifically,
is_multiplicable => $is_multiplicable,
grant_group => $grant_group,
request_group => $request_group,
inclusions => \@inclusions,
exclusions => \@exclusions
});
delete_token($token);
$vars->{'name'} = $flagtype->name;
$vars->{'message'} = "flag_type_created";
my @flagtypes = Bugzilla::FlagType->get_all;
$vars->{'bug_types'} = [grep { $_->target_type eq 'bug' } @flagtypes];
$vars->{'attachment_types'} = [grep { $_->target_type eq 'attachment' } @flagtypes];
$template->process("admin/flag-type/list.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
if ($action eq 'update') {
check_token_data($token, 'edit_flagtype');
my $name = $cgi->param('name');
my $description = $cgi->param('description');
my $cc_list = $cgi->param('cc_list');
my $sortkey = $cgi->param('sortkey');
my $is_active = $cgi->param('is_active');
my $is_requestable = $cgi->param('is_requestable');
my $is_specifically = $cgi->param('is_requesteeble');
my $is_multiplicable = $cgi->param('is_multiplicable');
my $grant_group = $cgi->param('grant_group');
my $request_group = $cgi->param('request_group');
my @inclusions = $cgi->param('inclusions');
my @exclusions = $cgi->param('exclusions');
my $flagtype = Bugzilla::FlagType->check({ id => $flag_id });
$flagtype->set_name($name);
$flagtype->set_description($description);
$flagtype->set_cc_list($cc_list);
$flagtype->set_sortkey($sortkey);
$flagtype->set_is_active($is_active);
$flagtype->set_is_requestable($is_requestable);
$flagtype->set_is_specifically_requestable($is_specifically);
$flagtype->set_is_multiplicable($is_multiplicable);
$flagtype->set_grant_group($grant_group);
$flagtype->set_request_group($request_group);
$flagtype->set_clusions({ inclusions => \@inclusions, exclusions => \@exclusions})
if $cgi->param('check_clusions');
my $changes = $flagtype->update();
delete_token($token);
$vars->{'flagtype'} = $flagtype;
$vars->{'changes'} = $changes;
$vars->{'message'} = 'flag_type_updated';
my @flagtypes = Bugzilla::FlagType->get_all;
$vars->{'bug_types'} = [grep { $_->target_type eq 'bug' } @flagtypes];
$vars->{'attachment_types'} = [grep { $_->target_type eq 'attachment' } @flagtypes];
$template->process("admin/flag-type/list.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
if ($action eq 'confirmdelete') {
$vars->{'flag_type'} = Bugzilla::FlagType->check({ id => $flag_id });
$vars->{'token'} = issue_session_token('delete_flagtype');
$template->process("admin/flag-type/confirm-delete.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
if ($action eq 'delete') {
check_token_data($token, 'delete_flagtype');
my $flagtype = Bugzilla::FlagType->check({ id => $flag_id });
$flagtype->remove_from_db();
delete_token($token);
$vars->{'name'} = $flagtype->name;
$vars->{'message'} = "flag_type_deleted";
my @flagtypes = Bugzilla::FlagType->get_all;
$vars->{'bug_types'} = [grep { $_->target_type eq 'bug' } @flagtypes];
$vars->{'attachment_types'} = [grep { $_->target_type eq 'attachment' } @flagtypes];
$template->process("admin/flag-type/list.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
if ($action eq 'deactivate') {
check_token_data($token, 'delete_flagtype');
my $flagtype = Bugzilla::FlagType->check({ id => $flag_id });
$flagtype->set_is_active(0);
$flagtype->update();
delete_token($token);
$vars->{'message'} = "flag_type_deactivated";
$vars->{'flag_type'} = $flagtype;
my @flagtypes = Bugzilla::FlagType->get_all;
$vars->{'bug_types'} = [grep { $_->target_type eq 'bug' } @flagtypes];
$vars->{'attachment_types'} = [grep { $_->target_type eq 'attachment' } @flagtypes];
$template->process("admin/flag-type/list.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
ThrowUserError('unknown_action', {action => $action});
#####################
# Helper subroutines
#####################
sub get_products_and_components {
my $vars = {};
my @products = Bugzilla::Product->get_all;
# We require all unique component names.
my %components;
foreach my $product (@products) {
foreach my $component (@{$product->components}) {
$components{$component->name} = 1;
}
}
$vars->{'products'} = \@products;
$vars->{'components'} = [sort(keys %components)];
return $vars;
}
sub filter_group {
my ($flag_types, $gid) = @_;
return $flag_types unless $gid;
my @flag_types = grep {($_->grant_group && $_->grant_group->id == $gid)
|| ($_->request_group && $_->request_group->id == $gid)} @$flag_types;
return \@flag_types;
}
# Convert the array @clusions('prod_ID:comp_ID') back to a hash of
# the form %clusions{'prod_name:comp_name'} = 'prod_ID:comp_ID'
sub clusion_array_to_hash {
my $array = shift;
my %hash;
my %products;
my %components;
foreach my $ids (@$array) {
trick_taint($ids);
my ($product_id, $component_id) = split(":", $ids);
my $product_name = "__Any__";
if ($product_id) {
$products{$product_id} ||= new Bugzilla::Product($product_id);
$product_name = $products{$product_id}->name if $products{$product_id};
}
my $component_name = "__Any__";
if ($component_id) {
$components{$component_id} ||= new Bugzilla::Component($component_id);
$component_name = $components{$component_id}->name if $components{$component_id};
}
$hash{"$product_name:$component_name"} = $ids;
}
return %hash;
}
|