summaryrefslogtreecommitdiff
blob: c28a8624049d0128abfce95726d43c101ddb14f0 (plain)
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
#!/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::Error;
use Bugzilla::Util;
use Bugzilla::Field;
use Bugzilla::Token;
use Bugzilla::Group;

my $cgi      = Bugzilla->cgi;
my $template = Bugzilla->template;
my $vars     = {};

# Make sure the user is logged in.
my $user = Bugzilla->login(LOGIN_REQUIRED);

my $action = trim($cgi->param('action') || '');
my $token  = $cgi->param('token');

print $cgi->header();

# List all existing custom fields if no action is given.
if (!$action) {
  # Have to be able to admin a CF to do this
  $user->can_admin_any_cf()
  || ThrowUserError('auth_failure',
    {action => 'admin', object => 'custom_fields'});

  $template->process('admin/custom_fields/list.html.tmpl', $vars)
    || ThrowTemplateError($template->error());
}

# Interface to add a new custom field.
elsif ($action eq 'add') {
  # Only admin can add
  $user->in_group('admin')
  || ThrowUserError('auth_failure',
  {group => 'admin', action => 'edit', object => 'custom_fields'});

  $vars->{'token'} = issue_session_token('add_field');
  @{$vars->{groups}} = Bugzilla::Group->get_all();

  $template->process('admin/custom_fields/create.html.tmpl', $vars)
    || ThrowTemplateError($template->error());
}
elsif ($action eq 'new') {
  # Only admin can add
  $user->in_group('admin')
  || ThrowUserError('auth_failure',
  {group => 'admin', action => 'edit', object => 'custom_fields'});

  check_token_data($token, 'add_field');
  $vars->{'field'} = Bugzilla::Field->create({
    name                => scalar $cgi->param('name'),
    description         => scalar $cgi->param('desc'),
    long_desc           => scalar $cgi->param('long_desc'),
    type                => scalar $cgi->param('type'),
    sortkey             => scalar $cgi->param('sortkey'),
    mailhead            => scalar $cgi->param('new_bugmail'),
    enter_bug           => scalar $cgi->param('enter_bug'),
    obsolete            => scalar $cgi->param('obsolete'),
    custom              => 1,
    buglist             => 1,
    visibility_field_id => scalar $cgi->param('visibility_field_id'),
    visibility_values   => [$cgi->param('visibility_values')],
    value_field_id      => scalar $cgi->param('value_field_id'),
    reverse_desc        => scalar $cgi->param('reverse_desc'),
    is_mandatory        => scalar $cgi->param('is_mandatory'),
  });

  $vars->{'field'}->set_view_groups([$cgi->param('view_groups')]);
  $vars->{'field'}->set_edit_groups([$cgi->param('edit_groups')]);
  $vars->{'field'}->set_admin_groups([$cgi->param('admin_groups')]);

  delete_token($token);

  $vars->{'message'} = 'custom_field_created';

  $template->process('admin/custom_fields/list.html.tmpl', $vars)
    || ThrowTemplateError($template->error());
}
elsif ($action eq 'edit') {
  my $name = $cgi->param('name') || ThrowUserError('field_missing_name');

  # Custom field names must start with "cf_".
  if ($name !~ /^cf_/) {
    $name = 'cf_' . $name;
  }
  my $field = new Bugzilla::Field({'name' => $name});
  $field || ThrowUserError('customfield_nonexistent', {'name' => $name});

  # Have to be able to admin a CF to do this
  $field->user_can_admin($user)
  || ThrowUserError('auth_failure',
  {groups => $field->edit_groups(), action => 'modify', object => 'custom_fields'});

  $vars->{'field'} = $field;
  $vars->{'token'} = issue_session_token('edit_field');

  @{$vars->{groups}} = Bugzilla::Group->get_all();

  $template->process('admin/custom_fields/edit.html.tmpl', $vars)
    || ThrowTemplateError($template->error());
}
elsif ($action eq 'update') {
  check_token_data($token, 'edit_field');
  my $name = $cgi->param('name');

  # Validate fields.
  $name || ThrowUserError('field_missing_name');

  # Custom field names must start with "cf_".
  if ($name !~ /^cf_/) {
    $name = 'cf_' . $name;
  }
  my $field = new Bugzilla::Field({'name' => $name});
  $field || ThrowUserError('customfield_nonexistent', {'name' => $name});

  # Have to be able to admin a CF to do this
  $field->user_can_admin($user)
  || ThrowUserError('auth_failure',
    {groups => $field->edit_groups(), action => 'modify', object => 'custom_fields'});

  $field->set_description($cgi->param('desc'));
  $field->set_long_desc($cgi->param('long_desc'));
  $field->set_sortkey($cgi->param('sortkey'));
  $field->set_in_new_bugmail($cgi->param('new_bugmail'));
  $field->set_enter_bug($cgi->param('enter_bug'));
  $field->set_obsolete($cgi->param('obsolete'));
  $field->set_is_mandatory($cgi->param('is_mandatory'));
  $field->set_visibility_field($cgi->param('visibility_field_id'));
  $field->set_visibility_values([$cgi->param('visibility_values')]);
  $field->set_value_field($cgi->param('value_field_id'));
  $field->set_reverse_desc($cgi->param('reverse_desc'));

  $field->set_view_groups([$cgi->param('view_groups')]);
  $field->set_edit_groups([$cgi->param('edit_groups')]);
  $field->set_admin_groups([$cgi->param('admin_groups')]);

  $field->update();

  delete_token($token);

  $vars->{'field'}   = $field;
  $vars->{'message'} = 'custom_field_updated';

  $template->process('admin/custom_fields/list.html.tmpl', $vars)
    || ThrowTemplateError($template->error());
}
elsif ($action eq 'del') {
  # Only admin can add
  $user->in_group('admin')
  || ThrowUserError('auth_failure',
  {group => 'admin', action => 'edit', object => 'custom_fields'});

  my $name = $cgi->param('name');

  $name || ThrowUserError('field_missing_name');

  # Custom field names must start with "cf_".
  if ($name !~ /^cf_/) {
    $name = 'cf_' . $name;
  }
  my $field = new Bugzilla::Field({'name' => $name});
  $field || ThrowUserError('customfield_nonexistent', {'name' => $name});

  $vars->{'field'} = $field;
  $vars->{'token'} = issue_session_token('delete_field');

  $template->process('admin/custom_fields/confirm-delete.html.tmpl', $vars)
    || ThrowTemplateError($template->error());
}
elsif ($action eq 'delete') {
  # Only admin can add
  $user->in_group('admin')
  || ThrowUserError('auth_failure',
  {group => 'admin', action => 'edit', object => 'custom_fields'});

  check_token_data($token, 'delete_field');
  my $name = $cgi->param('name');

  # Validate fields.
  $name || ThrowUserError('field_missing_name');

  # Custom field names must start with "cf_".
  if ($name !~ /^cf_/) {
    $name = 'cf_' . $name;
  }
  my $field = new Bugzilla::Field({'name' => $name});
  $field || ThrowUserError('customfield_nonexistent', {'name' => $name});

  # Calling remove_from_db will check if field can be deleted.
  # If the field cannot be deleted, it will throw an error.
  $field->remove_from_db();

  $vars->{'field'}   = $field;
  $vars->{'message'} = 'custom_field_deleted';

  delete_token($token);

  $template->process('admin/custom_fields/list.html.tmpl', $vars)
    || ThrowTemplateError($template->error());
}
else {
  ThrowUserError('unknown_action', {action => $action});
}