request.cgi 13 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 22 23 24 25 26 27 28 29 30
# -*- 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>

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

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

use lib qw(.);
31
require "globals.pl";
32
use Bugzilla;
33 34 35 36 37
use Bugzilla::Flag;
use Bugzilla::FlagType;
use Bugzilla::User;

# Make sure the user is logged in.
38 39 40 41
my $user = Bugzilla->login();
my $userid = $user->id;

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

45 46 47 48 49

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

50 51 52 53 54 55 56 57 58 59 60 61
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);

62 63 64 65 66 67 68 69
queue();
exit;

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

sub queue {
70
    my $cgi = Bugzilla->cgi;
71
    my $dbh = Bugzilla->dbh;
72 73 74
    
    validateStatus($cgi->param('status'));
    validateGroup($cgi->param('group'));
75 76 77
    
    my $attach_join_clause = "flags.attach_id = attachments.attach_id";
    if (Param("insidergroup") && !UserInGroup(Param("insidergroup"))) {
78
        $attach_join_clause .= " AND attachments.isprivate < 1";
79 80 81 82 83 84 85 86 87 88 89 90
    }

    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,
91
    " . $dbh->sql_date_format('flags.creation_date', '%Y.%m.%d %H:%i') .
92 93 94 95
    # 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
96 97 98 99 100
    # table to help us weed out secure bugs to which the user should not have
    # access.
    "
      FROM           flags 
           LEFT JOIN attachments
101
                  ON ($attach_join_clause)
102
          INNER JOIN flagtypes
103
                  ON flags.type_id = flagtypes.id
104
          INNER JOIN profiles AS requesters
105
                  ON flags.setter_id = requesters.userid
106
           LEFT JOIN profiles AS requestees
107
                  ON flags.requestee_id  = requestees.userid
108
          INNER JOIN bugs
109
                  ON flags.bug_id = bugs.bug_id
110
          INNER JOIN products
111
                  ON bugs.product_id = products.id
112
          INNER JOIN components
113
                  ON bugs.component_id = components.id
114
           LEFT JOIN bug_group_map AS bgmap
115
                  ON bgmap.bug_id = bugs.bug_id
116
                 AND bgmap.group_id NOT IN (" .
117
                     join(', ', (-1, values(%{$user->groups}))) . ")
118
           LEFT JOIN cc AS ccmap
119 120
                  ON ccmap.who = $userid
                 AND ccmap.bug_id = bugs.bug_id
121 122 123 124 125
    " .

    # 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
126
                 (bugs.reporter = $userid AND bugs.reporter_accessible = 1) OR
127 128 129
                 (bugs.assigned_to = $userid) " .
                 (Param('useqacontact') ? "OR
                 (bugs.qa_contact = $userid))" : ")");
130
    
131
    # Non-deleted flags only
132
    $query .= " AND flags.is_active = 1 ";
133
    
134
    # Limit query to pending requests.
135
    $query .= " AND flags.status = '?' " unless $cgi->param('status');
136 137 138 139

    # The set of criteria by which we filter records to display in the queue.
    my @criteria = ();
    
140 141 142 143 144 145 146 147 148
    # 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").
149 150
    if ($cgi->param('status')) {
        if ($cgi->param('status') eq "+-") {
151
            push(@criteria, "flags.status IN ('+', '-')");
152
            push(@excluded_columns, 'status') unless $cgi->param('do_union');
153
        }
154 155 156
        elsif ($cgi->param('status') ne "all") {
            push(@criteria, "flags.status = '" . $cgi->param('status') . "'");
            push(@excluded_columns, 'status') unless $cgi->param('do_union');
157
        }
158 159 160
    }
    
    # Filter results by exact email address of requester or requestee.
161
    if (defined $cgi->param('requester') && $cgi->param('requester') ne "") {
162 163 164
        my $requester = $dbh->quote($cgi->param('requester'));
        trick_taint($requester); # Quoted above
        push(@criteria, $dbh->sql_istrcmp('requesters.login_name', $requester));
165
        push(@excluded_columns, 'requester') unless $cgi->param('do_union');
166
    }
167
    if (defined $cgi->param('requestee') && $cgi->param('requestee') ne "") {
168
        if ($cgi->param('requestee') ne "-") {
169 170
            my $requestee = $dbh->quote($cgi->param('requestee'));
            trick_taint($requestee); # Quoted above
171
            push(@criteria, $dbh->sql_istrcmp('requestees.login_name',
172
                            $requestee));
173 174
        }
        else { push(@criteria, "flags.requestee_id IS NULL") }
175
        push(@excluded_columns, 'requestee') unless $cgi->param('do_union');
176 177 178
    }
    
    # Filter results by exact product or component.
179 180
    if (defined $cgi->param('product') && $cgi->param('product') ne "") {
        my $product_id = get_product_id($cgi->param('product'));
181
        if ($product_id) {
182
            push(@criteria, "bugs.product_id = $product_id");
183 184 185
            push(@excluded_columns, 'product') unless $cgi->param('do_union');
            if (defined $cgi->param('component') && $cgi->param('component') ne "") {
                my $component_id = get_component_id($product_id, $cgi->param('component'));
186
                if ($component_id) {
187
                    push(@criteria, "bugs.component_id = $component_id");
188
                    push(@excluded_columns, 'component') unless $cgi->param('do_union');
189
                }
190 191
                else { ThrowUserError("component_not_valid", { 'product' => $cgi->param('product'),
                                                               'name' => $cgi->param('component') }) }
192 193
            }
        }
194
        else { ThrowUserError("product_doesnt_exist", { 'product' => $cgi->param('product') }) }
195 196 197
    }
    
    # Filter results by flag types.
198 199
    my $form_type = $cgi->param('type');
    if (defined $form_type && !grep($form_type eq $_, ("", "all"))) {
200 201
        # Check if any matching types are for attachments.  If not, don't show
        # the attachment column in the report.
202
        my $types = Bugzilla::FlagType::match({ 'name' => $form_type });
203 204 205 206 207 208 209 210
        my $has_attachment_type = 0;
        foreach my $type (@$types) {
            if ($type->{'target_type'} eq "attachment") {
                $has_attachment_type = 1;
                last;
            }
        }
        if (!$has_attachment_type) { push(@excluded_columns, 'attachment') }
211 212 213 214

        my $quoted_form_type = $dbh->quote($form_type);
        trick_taint($quoted_form_type); # Already SQL quoted
        push(@criteria, "flagtypes.name = " . $quoted_form_type);
215
        push(@excluded_columns, 'type') unless $cgi->param('do_union');
216 217
    }
    
218 219 220
    # 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.
221
    my $and_or = $cgi->param('do_union') ? " OR " : " AND ";
222 223
    $query .= " AND (" . join($and_or, @criteria) . ") " if scalar(@criteria);
    
224 225 226
    # 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.
227 228 229 230 231 232 233 234
    $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,
                requestees.login_name, flags.creation_date,
                cclist_accessible, bugs.reporter, bugs.reporter_accessible,
                bugs.assigned_to');
235 236 237 238

    # 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.
239 240 241 242

    my $form_group = $cgi->param('group');
    $form_group ||= "requestee";
    if ($form_group eq "requester") {
243 244
        $query .= " ORDER BY requesters.realname, requesters.login_name";
    }
245
    elsif ($form_group eq "requestee") {
246 247
        $query .= " ORDER BY requestees.realname, requestees.login_name";
    }
248
    elsif ($form_group eq "category") {
249 250
        $query .= " ORDER BY products.name, components.name";
    }
251
    elsif ($form_group eq "type") {
252 253 254 255 256 257 258 259
        $query .= " ORDER BY flagtypes.name";
    }

    # Order the records (within each group).
    $query .= " , flags.creation_date";
    
    # Pass the query to the template for use when debugging this script.
    $vars->{'query'} = $query;
260
    $vars->{'debug'} = $cgi->param('debug') ? 1 : 0;
261
    
262
    my $results = $dbh->selectall_arrayref($query);
263
    my @requests = ();
264 265
    foreach my $result (@$results) {
        my @data = @$result;
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
        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");
284 285 286
    my $flagtypes = $dbh->selectcol_arrayref(
                         "SELECT DISTINCT(name) FROM flagtypes ORDER BY name");
    push(@types, @$flagtypes);
287
    
288
    $vars->{'products'} = $user->get_selectable_products;
289
    $vars->{'excluded_columns'} = \@excluded_columns;
290
    $vars->{'group_field'} = $form_group;
291 292 293 294
    $vars->{'requests'} = \@requests;
    $vars->{'types'} = \@types;

    # Return the appropriate HTTP response headers.
295
    print Bugzilla->cgi->header();
296 297 298 299 300 301 302 303 304 305 306

    # 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 {
307 308
    my $status = $_[0];
    return if !defined $status;
309
    
310
    grep($status eq $_, qw(? +- + - all))
311
      || ThrowCodeError("flag_status_invalid",
312
                        { status => $status });
313 314 315
}

sub validateGroup {
316 317
    my $group = $_[0];
    return if !defined $group;
318
    
319
    grep($group eq $_, qw(requester requestee category type))
320
      || ThrowCodeError("request_queue_group_invalid", 
321
                        { group => $group });
322 323
}