forked from bugzilla/extensions-Testopia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tr_tags.cgi
executable file
·243 lines (207 loc) · 7.69 KB
/
tr_tags.cgi
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
#!/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 Testopia System.
#
# The Initial Developer of the Original Code is Greg Hendricks.
# Portions created by Greg Hendricks are Copyright (C) 2006
# Novell. All Rights Reserved.
#
# Contributor(s): Greg Hendricks <ghendricks@novell.com>
use strict;
use lib qw(. lib);
use Bugzilla;
use Bugzilla::Util;
use Bugzilla::User;
use Bugzilla::Config;
use Bugzilla::Constants;
use Bugzilla::Error;
BEGIN { Bugzilla->extensions }
use Bugzilla::Extension::Testopia::Util;
use Bugzilla::Extension::Testopia::TestTag;
use Bugzilla::Extension::Testopia::TestPlan;
use Bugzilla::Extension::Testopia::TestRun;
use Bugzilla::Extension::Testopia::TestCase;
use Bugzilla::Extension::Testopia::Constants;
use JSON;
Bugzilla->login(LOGIN_REQUIRED);
local our $cgi = Bugzilla->cgi;
local our $vars = {};
local our $template = Bugzilla->template;
print $cgi->header;
my $action = $cgi->param('action') || '';
my $type = $cgi->param('type');
$vars->{'type'} = $type;
if ($action eq 'delete'){
my $tag_id = $cgi->param('tagid');
validate_test_id($tag_id, 'tag');
my $tag = Bugzilla::Extension::Testopia::TestTag->new($tag_id);
ThrowUserError("testopia-no-delete", {'object' => $tag}) unless $tag->candelete;
$tag->obliterate;
$vars->{'tr_message'} = "Tag " . $tag->name . " deleted";
display();
}
####################
### Ajax Actions ###
####################
elsif ($action eq 'addtag' || $action eq 'removetag'){
Bugzilla->error_mode(ERROR_MODE_AJAX);
my $obj;
foreach my $id (split(/,/, $cgi->param('id'))){
if ($type eq 'plan'){
$obj = Bugzilla::Extension::Testopia::TestPlan->new($id);
} elsif ($type eq 'case'){
$obj = Bugzilla::Extension::Testopia::TestCase->new($id);
} elsif($type eq 'run'){
$obj = Bugzilla::Extension::Testopia::TestRun->new($id);
}
ThrowUserError("unknown-type", {type => $type}) unless ($obj);
ThrowUserError("testopia-read-only", {'object' => $obj}) unless $obj->canedit;
if ($action eq 'addtag'){
$obj->add_tag($cgi->param('tag'));
}
else {
foreach my $tag (split(',', $cgi->param('tag'))){
trick_taint($tag);
$obj->remove_tag($tag);
}
}
}
if ($cgi->param('method')){
$vars->{'tr_message'} = "Added tag " . $cgi->param('tag') . " To $type " . $obj->id;
$cgi->param($type.'_id', $obj->id);
display();
exit;
}
print "{success: true}";
}
elsif ($action eq 'gettags'){
Bugzilla->error_mode(ERROR_MODE_AJAX);
my $json = new JSON;
my $tags;
if ($cgi->param('type') eq 'user'){
$tags = get_user_tags();
}
elsif ($cgi->param('type') eq 'product'){
my $product = Bugzilla::Extension::Testopia::Product->new($cgi->param('product_id'));
$tags = $product->tags if $product->canedit;
}
else {
my $type = $cgi->param('type');
my $id = $cgi->param('id');
my $obj;
if ($type eq 'plan'){
$obj = Bugzilla::Extension::Testopia::TestPlan->new($id);
} elsif ($type eq 'case'){
$obj = Bugzilla::Extension::Testopia::TestCase->new($id);
} elsif($type eq 'run'){
$obj = Bugzilla::Extension::Testopia::TestRun->new($id);
}
ThrowUserError("unkown-type", {type => $type}) unless ($obj);
ThrowUserError("testopia-permission-denied", {'object' => $obj}) unless $obj->canview;
$tags = $obj->tags;
}
my $out = "{tags:[";
foreach my $tag (@$tags){
$out .= $tag->TO_JSON . ',';
}
chop($out) if scalar @$tags;
print $out ."]}";
}
###################
### Body ###
###################
else {
display();
}
###################
### Subroutines ###
###################
sub display {
my $dbh = Bugzilla->dbh;
my @tags;
if ($cgi->param('action') eq 'show_all' && Bugzilla->user->in_group('admin')){
my $tags = $dbh->selectcol_arrayref(
"SELECT tag_id FROM test_tags
ORDER BY tag_name");
foreach my $t (@{$tags}){
push @tags, Bugzilla::Extension::Testopia::TestTag->new($t);
}
$vars->{'viewall'} = 1;
}
$vars->{'user_tags'} = get_user_tags();
$vars->{'user_name'} = $cgi->param('user') ? $cgi->param('user') : Bugzilla->user->login;
if ($cgi->param('case_id')){
my $case_id = $cgi->param('case_id');
detaint_natural($case_id);
$vars->{'case'} = Bugzilla::Extension::Testopia::TestCase->new($case_id);
}
if ($cgi->param('plan_id')){
my $plan_id = $cgi->param('plan_id');
detaint_natural($plan_id);
$vars->{'plan'} = Bugzilla::Extension::Testopia::TestPlan->new($plan_id);
}
if ($cgi->param('run_id')){
my $run_id = $cgi->param('run_id');
detaint_natural($run_id);
$vars->{'run'} = Bugzilla::Extension::Testopia::TestRun->new($run_id);
}
my @products;
foreach my $id (split(",", $cgi->param('product'))){
my $product = Bugzilla::Extension::Testopia::Product->new($id);
push @products, $product if Bugzilla->user->can_see_product($product->name);
}
$vars->{'products'} = \@products;
my @tagids = split(/[\s,]/, $cgi->param('tag_id'));
foreach my $id (@tagids){
detaint_natural($id);
push @tags, Bugzilla::Extension::Testopia::TestTag->new($id);
}
if ($cgi->param('tag')){
my $name = trim($cgi->param('tag'));
trick_taint($name);
push @tags, Bugzilla::Extension::Testopia::TestTag->new($name);
}
$vars->{'tags'} = \@tags;
$template->process("testopia/tag/show.html.tmpl", $vars)
|| print $template->error();
}
sub get_user_tags {
my $user;
my $userid;
my $dbh = Bugzilla->dbh;
return unless $cgi->param('user');
$userid = login_to_id($cgi->param('user')) if $cgi->param('user');
$user = Bugzilla::User->new($userid);
ThrowUserError("invalid_username", { name => $cgi->param('user') }) unless $userid;
ThrowUserError('auth_failure', {reason => "not_visible",
action => "not_visible",
object => "user"})
unless Bugzilla->user->can_see_user($user);
my $user_tags = $dbh->selectcol_arrayref(
"(SELECT test_tags.tag_id, test_tags.tag_name AS name FROM test_case_tags
INNER JOIN test_tags ON test_case_tags.tag_id = test_tags.tag_id
WHERE userid = ?)
UNION (SELECT test_tags.tag_id, test_tags.tag_name AS name FROM test_plan_tags
INNER JOIN test_tags ON test_plan_tags.tag_id = test_tags.tag_id
WHERE userid = ?)
UNION (SELECT test_tags.tag_id, test_tags.tag_name AS name FROM test_run_tags
INNER JOIN test_tags ON test_run_tags.tag_id = test_tags.tag_id
WHERE userid = ?)
ORDER BY name", undef, ($userid, $userid, $userid));
my @user_tags;
foreach my $id (@$user_tags){
push @user_tags, Bugzilla::Extension::Testopia::TestTag->new($id);
}
return \@user_tags;
}