summaryrefslogtreecommitdiffstats
path: root/Bugzilla/BugUrl/Bugzilla/Local.pm
blob: 233acbe6696a7a2c59268a32d382098491cfd81d (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
# -*- 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 Tiago Mello.
# Portions created by Tiago Mello are Copyright (C) 2010
# Tiago Mello. All Rights Reserved.
#
# Contributor(s): Tiago Mello <timello@linux.vnet.ibm.com>

package Bugzilla::BugUrl::Bugzilla::Local;
use strict;
use base qw(Bugzilla::BugUrl::Bugzilla);

use Bugzilla::Error;
use Bugzilla::Util;

###############################
####    Initialization     ####
###############################

use constant VALIDATOR_DEPENDENCIES => {
    value => ['bug_id'],
};

###############################
####        Methods        ####
###############################

sub ref_bug_url {
    my $self = shift;

    if (!exists $self->{ref_bug_url}) {
        my $ref_bug_id = new URI($self->name)->query_param('id');
        my $ref_value = $self->local_uri($self->bug_id);
        $self->{ref_bug_url} =
            new Bugzilla::BugUrl::Bugzilla::Local({ bug_id => $ref_bug_id,
                                                    value => $ref_value });
    }
    return $self->{ref_bug_url};
}

sub insert_create_data {
    my ($class, $field_values) = @_;

    my $ref_bug = delete $field_values->{ref_bug};
    my $bug_url = $class->SUPER::insert_create_data($field_values);
    my $url = $class->local_uri($bug_url->bug_id);

    # Check if the ref bug has already the url and then,
    # update the ref bug to point to the current bug.
    if (!grep { $_->name eq $url } @{ $ref_bug->see_also }) {
        $class->SUPER::insert_create_data({ value  => $url,
                                            bug_id => $ref_bug->id,
                                            class  => ref($class) || $class });
    }

    return $bug_url;
}

sub remove_from_db {
    my $self = shift;

    my $dbh = Bugzilla->dbh;
    my $ref_bug_url = $self->ref_bug_url;

    $dbh->bz_start_transaction();

    # We remove the current see also first so then we
    # avoid infinite loop later.
    $self->SUPER::remove_from_db();

    # We also remove the referenced bug url.
    if (defined $ref_bug_url) {
        my $ref_bug = Bugzilla::Bug->check($ref_bug_url->bug_id);
        my $product = $ref_bug->product_obj;
        if (Bugzilla->user->can_edit_product($product->id)) {
            $ref_bug_url->remove_from_db();
        }
    }

    $dbh->bz_commit_transaction();
}

sub should_handle {
    my ($class, $uri) = @_;

    return $uri->as_string =~ m/^\w+$/ ? 1 : 0;

    my $canonical_local = URI->new($class->local_uri)->canonical;

    # Treating the domain case-insensitively and ignoring http(s)://
    return ($canonical_local->authority eq $uri->canonical->authority
            and $canonical_local->path eq $uri->canonical->path) ? 1 : 0;
}

sub _check_value {
    my ($class, $uri, undef, $params) = @_;

    # At this point we are going to treat any word as a
    # bug id/alias to the local Bugzilla.
    my $value = $uri->as_string;
    if ($value =~ m/^\w+$/) {
        $uri = new URI($class->local_uri($value));
    } else {
        # It's not a word, then we have to check
        # if it's a valid Bugzilla url.
        $uri = $class->SUPER::_check_value($uri);
    }

    my $ref_bug_id  = $uri->query_param('id');
    my $ref_bug     = Bugzilla::Bug->check($ref_bug_id);
    my $self_bug_id = $params->{bug_id};
    $params->{ref_bug} = $ref_bug;

    if ($ref_bug->id == $self_bug_id) {
        ThrowUserError('see_also_self_reference');
    }
 
    my $product = $ref_bug->product_obj;
    if (!Bugzilla->user->can_edit_product($product->id)) {
        ThrowUserError("product_edit_denied",
                       { product => $product->name });
    }

    return $uri;
}

sub local_uri {
    my ($self, $bug_id) = @_;
    $bug_id ||= '';
    return correct_urlbase() . "show_bug.cgi?id=$bug_id";
}

1;