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
|
#!/usr/bin/perl -wT
# 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.
#
# Contributor(s): Max Kanat-Alexander <mkanat@bugzilla.org>
# Frédéric Buclin <LpSolit@gmail.com>
# This is a script to edit the values of fields that have drop-down
# or select boxes. It is largely a copy of editmilestones.cgi, but
# with some cleanup.
use strict;
use lib qw(. lib);
use Bugzilla;
use Bugzilla::Util;
use Bugzilla::Error;
use Bugzilla::Constants;
use Bugzilla::Config qw(:admin);
use Bugzilla::Token;
use Bugzilla::Field;
use Bugzilla::Bug;
use Bugzilla::Status qw(SPECIAL_STATUS_WORKFLOW_ACTIONS);
# List of different tables that contain the changeable field values
# (the old "enums.") Keep them in alphabetical order by their
# English name from field-descs.html.tmpl.
# Format: Array of valid field names.
our @valid_fields = ('op_sys', 'rep_platform', 'priority', 'bug_severity',
'bug_status', 'resolution');
# Add custom select fields.
my @custom_fields = Bugzilla->get_fields({custom => 1,
type => FIELD_TYPE_SINGLE_SELECT});
push(@custom_fields, Bugzilla->get_fields({custom => 1,
type => FIELD_TYPE_MULTI_SELECT}));
push(@valid_fields, map { $_->name } @custom_fields);
######################################################################
# Subroutines
######################################################################
# Returns whether or not the specified table exists in the @tables array.
sub FieldExists {
my ($field) = @_;
return lsearch(\@valid_fields, $field) >= 0;
}
# Same as FieldExists, but emits and error and dies if it fails.
sub FieldMustExist {
my ($field)= @_;
$field ||
ThrowUserError('fieldname_not_specified');
# Is it a valid field to be editing?
FieldExists($field) ||
ThrowUserError('fieldname_invalid', {'field' => $field});
return new Bugzilla::Field({name => $field});
}
# Returns if the specified value exists for the field specified.
sub ValueExists {
my ($field, $value) = @_;
# Value is safe because it's being passed only to a SELECT
# statement via a placeholder.
trick_taint($value);
my $dbh = Bugzilla->dbh;
my $value_count =
$dbh->selectrow_array("SELECT COUNT(*) FROM $field "
. " WHERE value = ?", undef, $value);
return $value_count;
}
# Same check as ValueExists, emits an error text and dies if it fails.
sub ValueMustExist {
my ($field, $value)= @_;
# Values may not be empty (it's very difficult to deal
# with empty values in the admin interface).
trim($value) || ThrowUserError('fieldvalue_not_specified');
# Does it exist in the DB?
ValueExists($field, $value) ||
ThrowUserError('fieldvalue_doesnt_exist', {'value' => $value,
'field' => $field});
}
######################################################################
# Main Body Execution
######################################################################
# require the user to have logged in
Bugzilla->login(LOGIN_REQUIRED);
my $dbh = Bugzilla->dbh;
my $cgi = Bugzilla->cgi;
my $template = Bugzilla->template;
local our $vars = {};
# Replace this entry by separate entries in templates when
# the documentation about legal values becomes bigger.
$vars->{'doc_section'} = 'edit-values.html';
print $cgi->header();
Bugzilla->user->in_group('admin') ||
ThrowUserError('auth_failure', {group => "admin",
action => "edit",
object => "field_values"});
#
# often-used variables
#
my $field = trim($cgi->param('field') || '');
my $value = trim($cgi->param('value') || '');
my $sortkey = trim($cgi->param('sortkey') || '0');
my $action = trim($cgi->param('action') || '');
my $token = $cgi->param('token');
# Gives the name of the parameter associated with the field
# and representing its default value.
local our %defaults;
$defaults{'op_sys'} = 'defaultopsys';
$defaults{'rep_platform'} = 'defaultplatform';
$defaults{'priority'} = 'defaultpriority';
$defaults{'bug_severity'} = 'defaultseverity';
# Alternatively, a list of non-editable values can be specified.
# In this case, only the sortkey can be altered.
local our %static;
$static{'bug_status'} = ['UNCONFIRMED', Bugzilla->params->{'duplicate_or_move_bug_status'}];
$static{'resolution'} = ['', 'FIXED', 'MOVED', 'DUPLICATE'];
$static{$_->name} = ['---'] foreach (@custom_fields);
#
# field = '' -> Show nice list of fields
#
unless ($field) {
# Convert @valid_fields into the format that select-field wants.
my @field_list = ();
foreach my $field_name (@valid_fields) {
push(@field_list, {name => $field_name});
}
$vars->{'fields'} = \@field_list;
$template->process("admin/fieldvalues/select-field.html.tmpl",
$vars)
|| ThrowTemplateError($template->error());
exit;
}
# At this point, the field is defined.
my $field_obj = FieldMustExist($field);
$vars->{'field'} = $field_obj;
trick_taint($field);
sub display_field_values {
my $template = Bugzilla->template;
my $field = $vars->{'field'}->name;
$vars->{'values'} = $vars->{'field'}->legal_values;
$vars->{'default'} = Bugzilla->params->{$defaults{$field}} if defined $defaults{$field};
$vars->{'static'} = $static{$field} if exists $static{$field};
$template->process("admin/fieldvalues/list.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
#
# action='' -> Show nice list of values.
#
display_field_values() unless $action;
#
# action='add' -> show form for adding new field value.
# (next action will be 'new')
#
if ($action eq 'add') {
$vars->{'value'} = $value;
$vars->{'token'} = issue_session_token('add_field_value');
$template->process("admin/fieldvalues/create.html.tmpl",
$vars)
|| ThrowTemplateError($template->error());
exit;
}
#
# action='new' -> add field value entered in the 'action=add' screen
#
if ($action eq 'new') {
check_token_data($token, 'add_field_value');
my $created_value = Bugzilla::Field::Choice->type($field_obj)->create(
{ value => $value, sortkey => $sortkey,
is_open => scalar $cgi->param('is_open') });
delete_token($token);
$vars->{'message'} = 'field_value_created';
$vars->{'value'} = $created_value->name;
display_field_values();
}
#
# action='del' -> ask if user really wants to delete
# (next action would be 'delete')
#
if ($action eq 'del') {
ValueMustExist($field, $value);
trick_taint($value);
# See if any bugs are still using this value.
if ($field_obj->type != FIELD_TYPE_MULTI_SELECT) {
$vars->{'bug_count'} =
$dbh->selectrow_array("SELECT COUNT(*) FROM bugs WHERE $field = ?",
undef, $value);
}
else {
$vars->{'bug_count'} =
$dbh->selectrow_array("SELECT COUNT(*) FROM bug_$field WHERE value = ?",
undef, $value);
}
$vars->{'value_count'} =
$dbh->selectrow_array("SELECT COUNT(*) FROM $field");
$vars->{'value'} = $value;
$vars->{'param_name'} = $defaults{$field};
# If the value cannot be deleted, throw an error.
if (lsearch($static{$field}, $value) >= 0) {
ThrowUserError('fieldvalue_not_deletable', $vars);
}
$vars->{'token'} = issue_session_token('delete_field_value');
$template->process("admin/fieldvalues/confirm-delete.html.tmpl",
$vars)
|| ThrowTemplateError($template->error());
exit;
}
#
# action='delete' -> really delete the field value
#
if ($action eq 'delete') {
check_token_data($token, 'delete_field_value');
ValueMustExist($field, $value);
$vars->{'value'} = $value;
$vars->{'param_name'} = $defaults{$field};
if (defined $defaults{$field}
&& ($value eq Bugzilla->params->{$defaults{$field}}))
{
ThrowUserError('fieldvalue_is_default', $vars);
}
# If the value cannot be deleted, throw an error.
if (lsearch($static{$field}, $value) >= 0) {
ThrowUserError('fieldvalue_not_deletable', $vars);
}
trick_taint($value);
$dbh->bz_start_transaction();
# Check if there are any bugs that still have this value.
my $bug_count;
if ($field_obj->type != FIELD_TYPE_MULTI_SELECT) {
$bug_count =
$dbh->selectrow_array("SELECT COUNT(*) FROM bugs WHERE $field = ?",
undef, $value);
}
else {
$bug_count =
$dbh->selectrow_array("SELECT COUNT(*) FROM bug_$field WHERE value = ?",
undef, $value);
}
if ($bug_count) {
# You tried to delete a field that bugs are still using.
# You can't just delete the bugs. That's ridiculous.
ThrowUserError("fieldvalue_still_has_bugs",
{ field => $field, value => $value,
count => $bug_count });
}
if ($field eq 'bug_status') {
my $status_id = $dbh->selectrow_arrayref('SELECT id FROM bug_status
WHERE value = ?', undef, $value);
$dbh->do('DELETE FROM status_workflow
WHERE old_status = ? OR new_status = ?',
undef, ($status_id, $status_id));
}
$dbh->do("DELETE FROM $field WHERE value = ?", undef, $value);
$dbh->bz_commit_transaction();
delete_token($token);
$vars->{'message'} = 'field_value_deleted';
$vars->{'no_edit_link'} = 1;
display_field_values();
}
#
# action='edit' -> present the edit-value form
# (next action would be 'update')
#
if ($action eq 'edit') {
ValueMustExist($field, $value);
trick_taint($value);
$vars->{'sortkey'} = $dbh->selectrow_array(
"SELECT sortkey FROM $field WHERE value = ?", undef, $value) || 0;
$vars->{'value'} = $value;
$vars->{'is_static'} = (lsearch($static{$field}, $value) >= 0) ? 1 : 0;
$vars->{'token'} = issue_session_token('edit_field_value');
if ($field eq 'bug_status') {
$vars->{'is_open'} = $dbh->selectrow_array('SELECT is_open FROM bug_status
WHERE value = ?', undef, $value);
}
$template->process("admin/fieldvalues/edit.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
#
# action='update' -> update the field value
#
if ($action eq 'update') {
check_token_data($token, 'edit_field_value');
$vars->{'value'} = $cgi->param('valueold');
my $value_obj = Bugzilla::Field::Choice->type($field_obj)
->check($cgi->param('valueold'));
$value_obj->set_name($value);
$value_obj->set_sortkey($sortkey);
$vars->{'changes'} = $value_obj->update();
delete_token($token);
$vars->{'value_obj'} = $value_obj;
$vars->{'message'} = 'field_value_updated';
display_field_values();
}
#
# No valid action found
#
# We can't get here without $field being defined --
# See the unless($field) block at the top.
ThrowUserError('no_valid_action', { field => $field } );
|