request.cgi 14.1 KB
Newer Older
1
#!/usr/bin/perl -wT
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# -*- 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>
22
#                 Frédéric Buclin <LpSolit@gmail.com>
23 24 25 26 27 28 29 30

################################################################################
# Script Initialization
################################################################################

# Make it harder for us to do dangerous things in Perl.
use strict;

31
use lib qw(. lib);
32

33
use Bugzilla;
34 35
use Bugzilla::Util;
use Bugzilla::Error;
36 37 38
use Bugzilla::Flag;
use Bugzilla::FlagType;
use Bugzilla::User;
39 40
use Bugzilla::Product;
use Bugzilla::Component;
41 42

# Make sure the user is logged in.
43 44
my $user = Bugzilla->login();
my $cgi = Bugzilla->cgi;
45 46
# Force the script to run against the shadow DB. We already validated credentials.
Bugzilla->switch_to_shadow_db;
47 48 49 50
my $template = Bugzilla->template;
my $action = $cgi->param('action') || '';

print $cgi->header();
51 52 53 54 55

################################################################################
# Main Body Execution
################################################################################

56 57 58 59 60 61 62 63 64 65
my $fields;
$fields->{'requester'}->{'type'} = 'single';
# If the user doesn't restrict his search to requests from the wind
# (requestee ne '-'), include the requestee for completion.
unless (defined $cgi->param('requestee')
        && $cgi->param('requestee') eq '-')
{
    $fields->{'requestee'}->{'type'} = 'single';
}

66
Bugzilla::User::match_field($fields);
67

68 69 70 71
if ($action eq 'queue') {
    queue();
}
else {
72
    my $flagtypes = get_flag_types();
73 74 75 76 77
    my @types = ('all', @$flagtypes);

    my $vars = {};
    $vars->{'types'} = \@types;
    $vars->{'requests'} = {};
78 79

    my %components;
80
    foreach my $prod (@{$user->get_selectable_products}) {
81 82 83 84 85 86
        foreach my $comp (@{$prod->components}) {
            $components{$comp->name} = 1;
        }
    }
    $vars->{'components'} = [ sort { $a cmp $b } keys %components ];

87
    $template->process('request/queue.html.tmpl', $vars)
88 89
      || ThrowTemplateError($template->error());
}
90 91 92 93 94 95 96
exit;

################################################################################
# Functions
################################################################################

sub queue {
97
    my $cgi = Bugzilla->cgi;
98
    my $dbh = Bugzilla->dbh;
99 100 101
    my $template = Bugzilla->template;
    my $user = Bugzilla->user;
    my $userid = $user->id;
102
    my $vars = {};
103 104 105 106

    my $status = validateStatus($cgi->param('status'));
    my $form_group = validateGroup($cgi->param('group'));

107 108 109 110 111 112 113 114 115
    my $query = 
    # Select columns describing each flag, the bug/attachment on which
    # it has been set, who set it, and of whom they are requesting it.
    " SELECT    flags.id, flagtypes.name,
                flags.status,
                flags.bug_id, bugs.short_desc,
                products.name, components.name,
                flags.attach_id, attachments.description,
                requesters.realname, requesters.login_name,
116
                requestees.realname, requestees.login_name, COUNT(privs.group_id),
117
    " . $dbh->sql_date_format('flags.modification_date', '%Y.%m.%d %H:%i') .
118 119 120 121
    # Use the flags and flagtypes tables for information about the flags,
    # the bugs and attachments tables for target info, the profiles tables
    # for setter and requestee info, the products/components tables
    # so we can display product and component names, and the bug_group_map
122 123 124 125 126
    # table to help us weed out secure bugs to which the user should not have
    # access.
    "
      FROM           flags 
           LEFT JOIN attachments
127
                  ON flags.attach_id = attachments.attach_id
128
          INNER JOIN flagtypes
129
                  ON flags.type_id = flagtypes.id
130
          INNER JOIN profiles AS requesters
131
                  ON flags.setter_id = requesters.userid
132
           LEFT JOIN profiles AS requestees
133
                  ON flags.requestee_id  = requestees.userid
134
          INNER JOIN bugs
135
                  ON flags.bug_id = bugs.bug_id
136
          INNER JOIN products
137
                  ON bugs.product_id = products.id
138
          INNER JOIN components
139
                  ON bugs.component_id = components.id
140
           LEFT JOIN bug_group_map AS bgmap
141
                  ON bgmap.bug_id = bugs.bug_id
142
                 AND bgmap.group_id NOT IN (" .
143
                     $user->groups_as_string . ")
144 145
           LEFT JOIN bug_group_map AS privs
                  ON privs.bug_id = bugs.bug_id
146
           LEFT JOIN cc AS ccmap
147 148
                  ON ccmap.who = $userid
                 AND ccmap.bug_id = bugs.bug_id
149 150 151 152 153
    " .

    # Weed out bug the user does not have access to
    " WHERE     ((bgmap.group_id IS NULL) OR
                 (ccmap.who IS NOT NULL AND cclist_accessible = 1) OR
154
                 (bugs.reporter = $userid AND bugs.reporter_accessible = 1) OR
155
                 (bugs.assigned_to = $userid) " .
156
                 (Bugzilla->params->{'useqacontact'} ? "OR
157
                 (bugs.qa_contact = $userid))" : ")");
158 159 160 161 162 163 164

    unless ($user->is_insider) {
        $query .= " AND (attachments.attach_id IS NULL
                         OR attachments.isprivate = 0
                         OR attachments.submitter_id = $userid)";
    }

165
    # Limit query to pending requests.
166
    $query .= " AND flags.status = '?' " unless $status;
167 168 169

    # The set of criteria by which we filter records to display in the queue.
    my @criteria = ();
170

171 172 173 174 175 176 177 178 179
    # A list of columns to exclude from the report because the report conditions
    # limit the data being displayed to exact matches for those columns.
    # In other words, if we are only displaying "pending" , we don't
    # need to display a "status" column in the report because the value for that
    # column will always be the same.
    my @excluded_columns = ();
    
    # Filter requests by status: "pending", "granted", "denied", "all" 
    # (which means any), or "fulfilled" (which means "granted" or "denied").
180 181
    if ($status) {
        if ($status eq "+-") {
182
            push(@criteria, "flags.status IN ('+', '-')");
183
            push(@excluded_columns, 'status') unless $cgi->param('do_union');
184
        }
185 186
        elsif ($status ne "all") {
            push(@criteria, "flags.status = '$status'");
187
            push(@excluded_columns, 'status') unless $cgi->param('do_union');
188
        }
189 190 191
    }
    
    # Filter results by exact email address of requester or requestee.
192
    if (defined $cgi->param('requester') && $cgi->param('requester') ne "") {
193 194 195
        my $requester = $dbh->quote($cgi->param('requester'));
        trick_taint($requester); # Quoted above
        push(@criteria, $dbh->sql_istrcmp('requesters.login_name', $requester));
196
        push(@excluded_columns, 'requester') unless $cgi->param('do_union');
197
    }
198
    if (defined $cgi->param('requestee') && $cgi->param('requestee') ne "") {
199
        if ($cgi->param('requestee') ne "-") {
200 201
            my $requestee = $dbh->quote($cgi->param('requestee'));
            trick_taint($requestee); # Quoted above
202
            push(@criteria, $dbh->sql_istrcmp('requestees.login_name',
203
                            $requestee));
204 205
        }
        else { push(@criteria, "flags.requestee_id IS NULL") }
206
        push(@excluded_columns, 'requestee') unless $cgi->param('do_union');
207 208 209
    }
    
    # Filter results by exact product or component.
210
    if (defined $cgi->param('product') && $cgi->param('product') ne "") {
211
        my $product = Bugzilla::Product->check(scalar $cgi->param('product'));
212 213 214
        push(@criteria, "bugs.product_id = " . $product->id);
        push(@excluded_columns, 'product') unless $cgi->param('do_union');
        if (defined $cgi->param('component') && $cgi->param('component') ne "") {
215 216
            my $component = Bugzilla::Component->check({ product => $product,
                                                         name => scalar $cgi->param('component') });
217 218
            push(@criteria, "bugs.component_id = " . $component->id);
            push(@excluded_columns, 'component') unless $cgi->param('do_union');
219 220
        }
    }
221

222
    # Filter results by flag types.
223 224
    my $form_type = $cgi->param('type');
    if (defined $form_type && !grep($form_type eq $_, ("", "all"))) {
225 226
        # Check if any matching types are for attachments.  If not, don't show
        # the attachment column in the report.
227 228 229 230
        my $has_attachment_type =
            Bugzilla::FlagType::count({ 'name' => $form_type,
                                        'target_type' => 'attachment' });

231
        if (!$has_attachment_type) { push(@excluded_columns, 'attachment') }
232 233 234 235

        my $quoted_form_type = $dbh->quote($form_type);
        trick_taint($quoted_form_type); # Already SQL quoted
        push(@criteria, "flagtypes.name = " . $quoted_form_type);
236
        push(@excluded_columns, 'type') unless $cgi->param('do_union');
237 238
    }
    
239 240 241
    # Add the criteria to the query.  We do an intersection by default 
    # but do a union if the "do_union" URL parameter (for which there is no UI 
    # because it's an advanced feature that people won't usually want) is true.
242
    my $and_or = $cgi->param('do_union') ? " OR " : " AND ";
243 244
    $query .= " AND (" . join($and_or, @criteria) . ") " if scalar(@criteria);
    
245 246 247
    # Group the records by flag ID so we don't get multiple rows of data
    # for each flag.  This is only necessary because of the code that
    # removes flags on bugs the user is unauthorized to access.
248 249 250 251 252
    $query .= ' ' . $dbh->sql_group_by('flags.id',
               'flagtypes.name, flags.status, flags.bug_id, bugs.short_desc,
                products.name, components.name, flags.attach_id,
                attachments.description, requesters.realname,
                requesters.login_name, requestees.realname,
253
                requestees.login_name, flags.modification_date,
254 255
                cclist_accessible, bugs.reporter, bugs.reporter_accessible,
                bugs.assigned_to');
256 257 258 259

    # Group the records, in other words order them by the group column
    # so the loop in the display template can break them up into separate
    # tables every time the value in the group column changes.
260 261 262

    $form_group ||= "requestee";
    if ($form_group eq "requester") {
263 264
        $query .= " ORDER BY requesters.realname, requesters.login_name";
    }
265
    elsif ($form_group eq "requestee") {
266 267
        $query .= " ORDER BY requestees.realname, requestees.login_name";
    }
268
    elsif ($form_group eq "category") {
269 270
        $query .= " ORDER BY products.name, components.name";
    }
271
    elsif ($form_group eq "type") {
272 273 274 275
        $query .= " ORDER BY flagtypes.name";
    }

    # Order the records (within each group).
276 277
    $query .= " , flags.modification_date";

278 279
    # Pass the query to the template for use when debugging this script.
    $vars->{'query'} = $query;
280
    $vars->{'debug'} = $cgi->param('debug') ? 1 : 0;
281
    
282
    my $results = $dbh->selectall_arrayref($query);
283
    my @requests = ();
284 285
    foreach my $result (@$results) {
        my @data = @$result;
286 287 288 289 290 291 292 293 294 295 296
        my $request = {
          'id'              => $data[0] , 
          'type'            => $data[1] , 
          'status'          => $data[2] , 
          'bug_id'          => $data[3] , 
          'bug_summary'     => $data[4] , 
          'category'        => "$data[5]: $data[6]" , 
          'attach_id'       => $data[7] , 
          'attach_summary'  => $data[8] ,
          'requester'       => ($data[9] ? "$data[9] <$data[10]>" : $data[10]) , 
          'requestee'       => ($data[11] ? "$data[11] <$data[12]>" : $data[12]) , 
297 298
          'restricted'      => $data[13] ? 1 : 0,
          'created'         => $data[14]
299 300 301 302 303 304
        };
        push(@requests, $request);
    }

    # Get a list of request type names to use in the filter form.
    my @types = ("all");
305
    my $flagtypes = get_flag_types();
306
    push(@types, @$flagtypes);
307

308
    $vars->{'excluded_columns'} = \@excluded_columns;
309
    $vars->{'group_field'} = $form_group;
310 311 312
    $vars->{'requests'} = \@requests;
    $vars->{'types'} = \@types;

313
    my %components;
314
    foreach my $prod (@{$user->get_selectable_products}) {
315 316 317 318 319 320
        foreach my $comp (@{$prod->components}) {
            $components{$comp->name} = 1;
        }
    }
    $vars->{'components'} = [ sort { $a cmp $b } keys %components ];

321 322 323 324 325 326 327 328 329 330
    # Generate and return the UI (HTML page) from the appropriate template.
    $template->process("request/queue.html.tmpl", $vars)
      || ThrowTemplateError($template->error());
}

################################################################################
# Data Validation / Security Authorization
################################################################################

sub validateStatus {
331
    my $status = shift;
332
    return if !defined $status;
333

334
    grep($status eq $_, qw(? +- + - all))
335
      || ThrowUserError("flag_status_invalid", { status => $status });
336 337
    trick_taint($status);
    return $status;
338 339 340
}

sub validateGroup {
341
    my $group = shift;
342
    return if !defined $group;
343

344
    grep($group eq $_, qw(requester requestee category type))
345
      || ThrowUserError("request_queue_group_invalid", { group => $group });
346 347
    trick_taint($group);
    return $group;
348 349
}

350 351 352 353 354 355 356 357 358 359 360
# Returns all flag types which have at least one flag of this type.
# If a flag type is inactive but still has flags, we want it.
sub get_flag_types {
    my $dbh = Bugzilla->dbh;
    my $flag_types = $dbh->selectcol_arrayref('SELECT DISTINCT name
                                                 FROM flagtypes
                                                WHERE flagtypes.id IN
                                                      (SELECT DISTINCT type_id FROM flags)
                                             ORDER BY name');
    return $flag_types;
}