RSS
To get this branch, use:
bzr branch /bugzilla/4.2
3135 by mkanat%kerio.com
Bug 281876: New Admin Interface to manage old Enum fields (Field Values)
1
#!/usr/bin/perl -wT
2
# The contents of this file are subject to the Mozilla Public
3
# License Version 1.1 (the "License"); you may not use this file
4
# except in compliance with the License. You may obtain a copy of
5
# the License at http://www.mozilla.org/MPL/
6
#
7
# Software distributed under the License is distributed on an "AS
8
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9
# implied. See the License for the specific language governing
10
# rights and limitations under the License.
11
#
12
# The Original Code is the Bugzilla Bug Tracking System.
13
#
3434 by mkanat%kerio.com
Bug 293501: Add me to files where I belong and fix kerio.com emails
14
# Contributor(s): Max Kanat-Alexander <mkanat@bugzilla.org>
3756 by lpsolit%gmail.com
Bug 298688: Forbid the deletion of values used by default for the priority, severity, platform and OS and update data/params when editing them - Patch by Frédéric Buclin <LpSolit@gmail.com> r=GavinS a=justdave
15
#                 Frédéric Buclin <LpSolit@gmail.com>
3135 by mkanat%kerio.com
Bug 281876: New Admin Interface to manage old Enum fields (Field Values)
16
17
# This is a script to edit the values of fields that have drop-down
18
# or select boxes. It is largely a copy of editmilestones.cgi, but 
19
# with some cleanup.
20
21
use strict;
5304 by mkanat%bugzilla.org
Bug 399954: Make Bugzilla able to hold its dependencies in a local directory
22
use lib qw(. lib);
3135 by mkanat%kerio.com
Bug 281876: New Admin Interface to manage old Enum fields (Field Values)
23
24
use Bugzilla;
25
use Bugzilla::Util;
26
use Bugzilla::Error;
27
use Bugzilla::Constants;
4612 by lpsolit%gmail.com
Bug 281181: [SECURITY] It's way too easy to delete versions/components/milestones etc... - Patch by Frédéric Buclin <LpSolit@gmail.com> r=mkanat a=myk
28
use Bugzilla::Token;
5068 by lpsolit%gmail.com
Bug 369987: Display real field names in editvalues.cgi - Patch by Frédéric Buclin <LpSolit@gmail.com> r=ghendricks a=LpSolit
29
use Bugzilla::Field;
6258 by mkanat%bugzilla.org
Bug 456922: Now that Bugzilla::Field::Choice is complete, clean up editvalues.cgi and error messages
30
use Bugzilla::Field::Choice;
3135 by mkanat%kerio.com
Bug 281876: New Admin Interface to manage old Enum fields (Field Values)
31
6258 by mkanat%bugzilla.org
Bug 456922: Now that Bugzilla::Field::Choice is complete, clean up editvalues.cgi and error messages
32
###############
33
# Subroutines #
34
###############
35
36
sub display_field_values {
37
    my $vars = shift;
38
    my $template = Bugzilla->template;
39
    $vars->{'values'} = $vars->{'field'}->legal_values;
40
    $template->process("admin/fieldvalues/list.html.tmpl", $vars)
41
      || ThrowTemplateError($template->error());
42
    exit;
3135 by mkanat%kerio.com
Bug 281876: New Admin Interface to manage old Enum fields (Field Values)
43
}
44
45
######################################################################
46
# Main Body Execution
47
######################################################################
48
49
# require the user to have logged in
50
Bugzilla->login(LOGIN_REQUIRED);
51
52
my $dbh      = Bugzilla->dbh;
53
my $cgi      = Bugzilla->cgi;
54
my $template = Bugzilla->template;
6258 by mkanat%bugzilla.org
Bug 456922: Now that Bugzilla::Field::Choice is complete, clean up editvalues.cgi and error messages
55
my $vars = {};
3135 by mkanat%kerio.com
Bug 281876: New Admin Interface to manage old Enum fields (Field Values)
56
5322 by lpsolit%gmail.com
Part 3 of bug 182083: add Help for more admin pages - Patch by me, r=Colin
57
# Replace this entry by separate entries in templates when
58
# the documentation about legal values becomes bigger.
59
$vars->{'doc_section'} = 'edit-values.html';
60
3135 by mkanat%kerio.com
Bug 281876: New Admin Interface to manage old Enum fields (Field Values)
61
print $cgi->header();
62
6153 by mkanat%bugzilla.org
Bug 442031: Make Bugzilla::User::groups return an arrayref of Bugzilla::Group objects (instead of a hashref of group ids and names).
63
Bugzilla->user->in_group('admin') ||
4579 by lpsolit%gmail.com
Bug 353768: "editcomponents" group is not appropriate for editvalues.cgi (Field Values) - Patch by Frédéric Buclin <LpSolit@gmail.com> r=mkanat a=justdave
64
    ThrowUserError('auth_failure', {group  => "admin",
3135 by mkanat%kerio.com
Bug 281876: New Admin Interface to manage old Enum fields (Field Values)
65
                                    action => "edit",
4578 by lpsolit%gmail.com
Bug 353965: object => "field values" doesn't exist in user-error.html.tmpl - Patch by Frédéric Buclin <LpSolit@gmail.com> r=mkanat a=justdave
66
                                    object => "field_values"});
3135 by mkanat%kerio.com
Bug 281876: New Admin Interface to manage old Enum fields (Field Values)
67
68
#
69
# often-used variables
70
#
6258 by mkanat%bugzilla.org
Bug 456922: Now that Bugzilla::Field::Choice is complete, clean up editvalues.cgi and error messages
71
my $action = trim($cgi->param('action')  || '');
72
my $token  = $cgi->param('token');
4343 by lpsolit%gmail.com
Bug 94534: Customised resolutions - Patch by Frédéric Buclin <LpSolit@gmail.com> r=mkanat a=myk
73
3135 by mkanat%kerio.com
Bug 281876: New Admin Interface to manage old Enum fields (Field Values)
74
#
75
# field = '' -> Show nice list of fields
76
#
6258 by mkanat%bugzilla.org
Bug 456922: Now that Bugzilla::Field::Choice is complete, clean up editvalues.cgi and error messages
77
if (!$cgi->param('field')) {
7444 by Tiago Mello
Bug 586871: Convert all Bugzilla->get_fields calls to Bugzilla->fields
78
    my @field_list =
79
        @{ Bugzilla->fields({ is_select => 1, is_abnormal => 0 }) };
3135 by mkanat%kerio.com
Bug 281876: New Admin Interface to manage old Enum fields (Field Values)
80
81
    $vars->{'fields'} = \@field_list;
6258 by mkanat%bugzilla.org
Bug 456922: Now that Bugzilla::Field::Choice is complete, clean up editvalues.cgi and error messages
82
    $template->process("admin/fieldvalues/select-field.html.tmpl", $vars)
83
      || ThrowTemplateError($template->error());
84
    exit;
85
}
86
87
# At this point, the field must be defined.
88
my $field = Bugzilla::Field->check($cgi->param('field'));
6955 by Max Kanat-Alexander
Bug 487508: Allow restricting the visibility of custom fields and values by component
89
if (!$field->is_select || $field->is_abnormal) {
6258 by mkanat%bugzilla.org
Bug 456922: Now that Bugzilla::Field::Choice is complete, clean up editvalues.cgi and error messages
90
    ThrowUserError('fieldname_invalid', { field => $field });
91
}
92
$vars->{'field'} = $field;
3135 by mkanat%kerio.com
Bug 281876: New Admin Interface to manage old Enum fields (Field Values)
93
5441 by lpsolit%gmail.com
Bug 325487: Admin pages should require less "clicks" (remove useless confirmation pages) - Patch by Frédéric Buclin <LpSolit@gmail.com> r=justdave r=mkanat a=LpSolit
94
#
95
# action='' -> Show nice list of values.
96
#
6258 by mkanat%bugzilla.org
Bug 456922: Now that Bugzilla::Field::Choice is complete, clean up editvalues.cgi and error messages
97
display_field_values($vars) unless $action;
3135 by mkanat%kerio.com
Bug 281876: New Admin Interface to manage old Enum fields (Field Values)
98
99
#
100
# action='add' -> show form for adding new field value.
101
# (next action will be 'new')
102
#
103
if ($action eq 'add') {
4612 by lpsolit%gmail.com
Bug 281181: [SECURITY] It's way too easy to delete versions/components/milestones etc... - Patch by Frédéric Buclin <LpSolit@gmail.com> r=mkanat a=myk
104
    $vars->{'token'} = issue_session_token('add_field_value');
6258 by mkanat%bugzilla.org
Bug 456922: Now that Bugzilla::Field::Choice is complete, clean up editvalues.cgi and error messages
105
    $template->process("admin/fieldvalues/create.html.tmpl", $vars)
3135 by mkanat%kerio.com
Bug 281876: New Admin Interface to manage old Enum fields (Field Values)
106
      || ThrowTemplateError($template->error());
107
    exit;
108
}
109
110
#
111
# action='new' -> add field value entered in the 'action=add' screen
112
#
113
if ($action eq 'new') {
4612 by lpsolit%gmail.com
Bug 281181: [SECURITY] It's way too easy to delete versions/components/milestones etc... - Patch by Frédéric Buclin <LpSolit@gmail.com> r=mkanat a=myk
114
    check_token_data($token, 'add_field_value');
3135 by mkanat%kerio.com
Bug 281876: New Admin Interface to manage old Enum fields (Field Values)
115
6312 by mkanat%bugzilla.org
Bug 308253: Ability to add select (enum) fields to a bug whose list of values depends on the value of another field
116
    my $created_value = Bugzilla::Field::Choice->type($field)->create({
117
        value   => scalar $cgi->param('value'), 
118
        sortkey => scalar $cgi->param('sortkey'),
119
        is_open => scalar $cgi->param('is_open'),
120
        visibility_value_id => scalar $cgi->param('visibility_value_id'),
121
    });
3135 by mkanat%kerio.com
Bug 281876: New Admin Interface to manage old Enum fields (Field Values)
122
4612 by lpsolit%gmail.com
Bug 281181: [SECURITY] It's way too easy to delete versions/components/milestones etc... - Patch by Frédéric Buclin <LpSolit@gmail.com> r=mkanat a=myk
123
    delete_token($token);
124
5441 by lpsolit%gmail.com
Bug 325487: Admin pages should require less "clicks" (remove useless confirmation pages) - Patch by Frédéric Buclin <LpSolit@gmail.com> r=justdave r=mkanat a=LpSolit
125
    $vars->{'message'} = 'field_value_created';
6258 by mkanat%bugzilla.org
Bug 456922: Now that Bugzilla::Field::Choice is complete, clean up editvalues.cgi and error messages
126
    $vars->{'value'} = $created_value;
127
    display_field_values($vars);
3135 by mkanat%kerio.com
Bug 281876: New Admin Interface to manage old Enum fields (Field Values)
128
}
129
6258 by mkanat%bugzilla.org
Bug 456922: Now that Bugzilla::Field::Choice is complete, clean up editvalues.cgi and error messages
130
# After this, we always have a value
131
my $value = Bugzilla::Field::Choice->type($field)->check($cgi->param('value'));
132
$vars->{'value'} = $value;
3135 by mkanat%kerio.com
Bug 281876: New Admin Interface to manage old Enum fields (Field Values)
133
134
#
135
# action='del' -> ask if user really wants to delete
136
# (next action would be 'delete')
137
#
138
if ($action eq 'del') {
4343 by lpsolit%gmail.com
Bug 94534: Customised resolutions - Patch by Frédéric Buclin <LpSolit@gmail.com> r=mkanat a=myk
139
    # If the value cannot be deleted, throw an error.
6258 by mkanat%bugzilla.org
Bug 456922: Now that Bugzilla::Field::Choice is complete, clean up editvalues.cgi and error messages
140
    if ($value->is_static) {
4343 by lpsolit%gmail.com
Bug 94534: Customised resolutions - Patch by Frédéric Buclin <LpSolit@gmail.com> r=mkanat a=myk
141
        ThrowUserError('fieldvalue_not_deletable', $vars);
142
    }
4612 by lpsolit%gmail.com
Bug 281181: [SECURITY] It's way too easy to delete versions/components/milestones etc... - Patch by Frédéric Buclin <LpSolit@gmail.com> r=mkanat a=myk
143
    $vars->{'token'} = issue_session_token('delete_field_value');
4343 by lpsolit%gmail.com
Bug 94534: Customised resolutions - Patch by Frédéric Buclin <LpSolit@gmail.com> r=mkanat a=myk
144
6258 by mkanat%bugzilla.org
Bug 456922: Now that Bugzilla::Field::Choice is complete, clean up editvalues.cgi and error messages
145
    $template->process("admin/fieldvalues/confirm-delete.html.tmpl", $vars)
3135 by mkanat%kerio.com
Bug 281876: New Admin Interface to manage old Enum fields (Field Values)
146
      || ThrowTemplateError($template->error());
147
148
    exit;
149
}
150
151
152
#
153
# action='delete' -> really delete the field value
154
#
155
if ($action eq 'delete') {
4612 by lpsolit%gmail.com
Bug 281181: [SECURITY] It's way too easy to delete versions/components/milestones etc... - Patch by Frédéric Buclin <LpSolit@gmail.com> r=mkanat a=myk
156
    check_token_data($token, 'delete_field_value');
6258 by mkanat%bugzilla.org
Bug 456922: Now that Bugzilla::Field::Choice is complete, clean up editvalues.cgi and error messages
157
    $value->remove_from_db();
4612 by lpsolit%gmail.com
Bug 281181: [SECURITY] It's way too easy to delete versions/components/milestones etc... - Patch by Frédéric Buclin <LpSolit@gmail.com> r=mkanat a=myk
158
    delete_token($token);
5441 by lpsolit%gmail.com
Bug 325487: Admin pages should require less "clicks" (remove useless confirmation pages) - Patch by Frédéric Buclin <LpSolit@gmail.com> r=justdave r=mkanat a=LpSolit
159
    $vars->{'message'} = 'field_value_deleted';
160
    $vars->{'no_edit_link'} = 1;
6258 by mkanat%bugzilla.org
Bug 456922: Now that Bugzilla::Field::Choice is complete, clean up editvalues.cgi and error messages
161
    display_field_values($vars);
3135 by mkanat%kerio.com
Bug 281876: New Admin Interface to manage old Enum fields (Field Values)
162
}
163
164
165
#
166
# action='edit' -> present the edit-value form
167
# (next action would be 'update')
168
#
169
if ($action eq 'edit') {
4612 by lpsolit%gmail.com
Bug 281181: [SECURITY] It's way too easy to delete versions/components/milestones etc... - Patch by Frédéric Buclin <LpSolit@gmail.com> r=mkanat a=myk
170
    $vars->{'token'} = issue_session_token('edit_field_value');
4343 by lpsolit%gmail.com
Bug 94534: Customised resolutions - Patch by Frédéric Buclin <LpSolit@gmail.com> r=mkanat a=myk
171
    $template->process("admin/fieldvalues/edit.html.tmpl", $vars)
3135 by mkanat%kerio.com
Bug 281876: New Admin Interface to manage old Enum fields (Field Values)
172
      || ThrowTemplateError($template->error());
173
174
    exit;
175
}
176
177
178
#
179
# action='update' -> update the field value
180
#
181
if ($action eq 'update') {
4612 by lpsolit%gmail.com
Bug 281181: [SECURITY] It's way too easy to delete versions/components/milestones etc... - Patch by Frédéric Buclin <LpSolit@gmail.com> r=mkanat a=myk
182
    check_token_data($token, 'edit_field_value');
6258 by mkanat%bugzilla.org
Bug 456922: Now that Bugzilla::Field::Choice is complete, clean up editvalues.cgi and error messages
183
    $vars->{'value_old'} = $value->name;
7170 by Max Kanat-Alexander
Bug 561296: A fix allowing updating a field value's name when it is
184
    if ($cgi->should_set('is_active')) {
185
        $value->set_is_active($cgi->param('is_active'));
186
    }
6258 by mkanat%bugzilla.org
Bug 456922: Now that Bugzilla::Field::Choice is complete, clean up editvalues.cgi and error messages
187
    $value->set_name($cgi->param('value_new'));
188
    $value->set_sortkey($cgi->param('sortkey'));
6312 by mkanat%bugzilla.org
Bug 308253: Ability to add select (enum) fields to a bug whose list of values depends on the value of another field
189
    $value->set_visibility_value($cgi->param('visibility_value_id'));
6258 by mkanat%bugzilla.org
Bug 456922: Now that Bugzilla::Field::Choice is complete, clean up editvalues.cgi and error messages
190
    $vars->{'changes'} = $value->update();
4612 by lpsolit%gmail.com
Bug 281181: [SECURITY] It's way too easy to delete versions/components/milestones etc... - Patch by Frédéric Buclin <LpSolit@gmail.com> r=mkanat a=myk
191
    delete_token($token);
5441 by lpsolit%gmail.com
Bug 325487: Admin pages should require less "clicks" (remove useless confirmation pages) - Patch by Frédéric Buclin <LpSolit@gmail.com> r=justdave r=mkanat a=LpSolit
192
    $vars->{'message'} = 'field_value_updated';
6258 by mkanat%bugzilla.org
Bug 456922: Now that Bugzilla::Field::Choice is complete, clean up editvalues.cgi and error messages
193
    display_field_values($vars);
3135 by mkanat%kerio.com
Bug 281876: New Admin Interface to manage old Enum fields (Field Values)
194
}
195
196
# No valid action found
7188 by Frédéric Buclin
Bug 565879: Merge ThrowCodeError("action_unrecognized"), ThrowUserError("no_valid_action") and ThrowCodeError("unknown_action")
197
ThrowUserError('unknown_action', {action => $action});