request.cgi 13.5 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
my $dbh = Bugzilla->dbh;
46 47 48 49
my $template = Bugzilla->template;
my $action = $cgi->param('action') || '';

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

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

55 56 57 58 59 60 61 62 63 64 65 66
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';
}

Bugzilla::User::match_field($cgi, $fields);

67 68 69 70
if ($action eq 'queue') {
    queue();
}
else {
71 72 73 74 75 76 77 78 79
    my $flagtypes = $dbh->selectcol_arrayref('SELECT DISTINCT(name) FROM flagtypes
                                              ORDER BY name');
    my @types = ('all', @$flagtypes);

    my $vars = {};
    $vars->{'products'} = $user->get_selectable_products;
    $vars->{'types'} = \@types;
    $vars->{'requests'} = {};
    $template->process('request/queue.html.tmpl', $vars)
80 81
      || ThrowTemplateError($template->error());
}
82 83 84 85 86 87 88
exit;

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

sub queue {
89
    my $cgi = Bugzilla->cgi;
90
    # There are some user privilege checks to do. We do them against the main DB.
91
    my $dbh = Bugzilla->dbh;
92 93 94
    my $template = Bugzilla->template;
    my $user = Bugzilla->user;
    my $userid = $user->id;
95
    my $vars = {};
96 97 98 99

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

100 101 102 103 104 105 106 107 108 109
    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,
                requestees.realname, requestees.login_name,
110
    " . $dbh->sql_date_format('flags.modification_date', '%Y.%m.%d %H:%i') .
111 112 113 114
    # 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
115 116 117 118 119
    # table to help us weed out secure bugs to which the user should not have
    # access.
    "
      FROM           flags 
           LEFT JOIN attachments
120
                  ON flags.attach_id = attachments.attach_id
121
          INNER JOIN flagtypes
122
                  ON flags.type_id = flagtypes.id
123
          INNER JOIN profiles AS requesters
124
                  ON flags.setter_id = requesters.userid
125
           LEFT JOIN profiles AS requestees
126
                  ON flags.requestee_id  = requestees.userid
127
          INNER JOIN bugs
128
                  ON flags.bug_id = bugs.bug_id
129
          INNER JOIN products
130
                  ON bugs.product_id = products.id
131
          INNER JOIN components
132
                  ON bugs.component_id = components.id
133
           LEFT JOIN bug_group_map AS bgmap
134
                  ON bgmap.bug_id = bugs.bug_id
135
                 AND bgmap.group_id NOT IN (" .
136
                     join(', ', (-1, values(%{$user->groups}))) . ")
137
           LEFT JOIN cc AS ccmap
138 139
                  ON ccmap.who = $userid
                 AND ccmap.bug_id = bugs.bug_id
140 141 142 143 144
    " .

    # 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
145
                 (bugs.reporter = $userid AND bugs.reporter_accessible = 1) OR
146
                 (bugs.assigned_to = $userid) " .
147
                 (Bugzilla->params->{'useqacontact'} ? "OR
148
                 (bugs.qa_contact = $userid))" : ")");
149 150 151 152 153 154 155

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

156
    # Limit query to pending requests.
157
    $query .= " AND flags.status = '?' " unless $status;
158 159

    # The set of criteria by which we filter records to display in the queue.
160
    # We now move to the shadow DB to query the DB.
161
    my @criteria = ();
162 163
    $dbh = Bugzilla->switch_to_shadow_db;

164 165 166 167 168 169 170 171 172
    # 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").
173 174
    if ($status) {
        if ($status eq "+-") {
175
            push(@criteria, "flags.status IN ('+', '-')");
176
            push(@excluded_columns, 'status') unless $cgi->param('do_union');
177
        }
178 179
        elsif ($status ne "all") {
            push(@criteria, "flags.status = '$status'");
180
            push(@excluded_columns, 'status') unless $cgi->param('do_union');
181
        }
182 183 184
    }
    
    # Filter results by exact email address of requester or requestee.
185
    if (defined $cgi->param('requester') && $cgi->param('requester') ne "") {
186 187 188
        my $requester = $dbh->quote($cgi->param('requester'));
        trick_taint($requester); # Quoted above
        push(@criteria, $dbh->sql_istrcmp('requesters.login_name', $requester));
189
        push(@excluded_columns, 'requester') unless $cgi->param('do_union');
190
    }
191
    if (defined $cgi->param('requestee') && $cgi->param('requestee') ne "") {
192
        if ($cgi->param('requestee') ne "-") {
193 194
            my $requestee = $dbh->quote($cgi->param('requestee'));
            trick_taint($requestee); # Quoted above
195
            push(@criteria, $dbh->sql_istrcmp('requestees.login_name',
196
                            $requestee));
197 198
        }
        else { push(@criteria, "flags.requestee_id IS NULL") }
199
        push(@excluded_columns, 'requestee') unless $cgi->param('do_union');
200 201 202
    }
    
    # Filter results by exact product or component.
203
    if (defined $cgi->param('product') && $cgi->param('product') ne "") {
204 205 206 207
        my $product = Bugzilla::Product::check_product(scalar $cgi->param('product'));
        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 "") {
208 209
            my $component = Bugzilla::Component->check({ product => $product,
                                                         name => scalar $cgi->param('component') });
210 211
            push(@criteria, "bugs.component_id = " . $component->id);
            push(@excluded_columns, 'component') unless $cgi->param('do_union');
212 213
        }
    }
214

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

224
        if (!$has_attachment_type) { push(@excluded_columns, 'attachment') }
225 226 227 228

        my $quoted_form_type = $dbh->quote($form_type);
        trick_taint($quoted_form_type); # Already SQL quoted
        push(@criteria, "flagtypes.name = " . $quoted_form_type);
229
        push(@excluded_columns, 'type') unless $cgi->param('do_union');
230 231
    }
    
232 233 234
    # 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.
235
    my $and_or = $cgi->param('do_union') ? " OR " : " AND ";
236 237
    $query .= " AND (" . join($and_or, @criteria) . ") " if scalar(@criteria);
    
238 239 240
    # 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.
241 242 243 244 245
    $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,
246
                requestees.login_name, flags.modification_date,
247 248
                cclist_accessible, bugs.reporter, bugs.reporter_accessible,
                bugs.assigned_to');
249 250 251 252

    # 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.
253 254 255

    $form_group ||= "requestee";
    if ($form_group eq "requester") {
256 257
        $query .= " ORDER BY requesters.realname, requesters.login_name";
    }
258
    elsif ($form_group eq "requestee") {
259 260
        $query .= " ORDER BY requestees.realname, requestees.login_name";
    }
261
    elsif ($form_group eq "category") {
262 263
        $query .= " ORDER BY products.name, components.name";
    }
264
    elsif ($form_group eq "type") {
265 266 267 268
        $query .= " ORDER BY flagtypes.name";
    }

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

271 272
    # Pass the query to the template for use when debugging this script.
    $vars->{'query'} = $query;
273
    $vars->{'debug'} = $cgi->param('debug') ? 1 : 0;
274
    
275
    my $results = $dbh->selectall_arrayref($query);
276
    my @requests = ();
277 278
    foreach my $result (@$results) {
        my @data = @$result;
279 280 281 282 283 284 285 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]) , 
          'created'         => $data[13]
        };
        push(@requests, $request);
    }

    # Get a list of request type names to use in the filter form.
    my @types = ("all");
297 298 299
    my $flagtypes = $dbh->selectcol_arrayref(
                         "SELECT DISTINCT(name) FROM flagtypes ORDER BY name");
    push(@types, @$flagtypes);
300 301 302 303

    # We move back to the main DB to get the list of products the user can see.
    $dbh = Bugzilla->switch_to_main_db;

304
    $vars->{'products'} = $user->get_selectable_products;
305
    $vars->{'excluded_columns'} = \@excluded_columns;
306
    $vars->{'group_field'} = $form_group;
307 308 309 310 311 312 313 314 315 316 317 318 319
    $vars->{'requests'} = \@requests;
    $vars->{'types'} = \@types;

    # 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 {
320
    my $status = shift;
321
    return if !defined $status;
322

323
    grep($status eq $_, qw(? +- + - all))
324
      || ThrowCodeError("flag_status_invalid",
325
                        { status => $status });
326 327
    trick_taint($status);
    return $status;
328 329 330
}

sub validateGroup {
331
    my $group = shift;
332
    return if !defined $group;
333

334
    grep($group eq $_, qw(requester requestee category type))
335
      || ThrowCodeError("request_queue_group_invalid", 
336
                        { group => $group });
337 338
    trick_taint($group);
    return $group;
339 340
}