summaryrefslogtreecommitdiffstats
path: root/extensions/EditTable/Extension.pm
blob: 675b3fe90234567fb35f6256372d3ae0bd6e97f8 (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
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.


# this is a quick and dirty table editor, designed to allow admins to quickly
# maintain tables.
#
# each table must be defined via the editable_tables hook
#
# this extension doesn't currently provide any ability to modify or validate
# values.  use with caution!

package Bugzilla::Extension::EditTable;

use strict;
use warnings;

use base qw(Bugzilla::Extension);

use Bugzilla::Error;
use Bugzilla::Hook;
use Bugzilla::Token;
use Bugzilla::Util qw(trick_taint);
use JSON;
use Storable qw(dclone);

our $VERSION = '1';

# definitions for tables which we can edit with the quick-and-dirty editor
#
# $table_name => {
#   id_field    => name of the "id" field
#   order_by    => the field to sort rows by (optional, defaults to the id_field)
#   blurb       => text which describes the table
#   group       => group required to edit this table (optional, defaults to "admin")
# }
#
# example:
# 'antispam_domain_blocklist' => {
#     id_field   => 'id',
#     order_by   => 'domain',
#     blurb      => 'List of fully qualified domain names to block at account creation time.',
#     group      => 'can_configure_antispam',
# },

sub EDITABLE_TABLES {
    my $tables = {};
    Bugzilla::Hook::process("editable_tables", { tables => $tables });
    return $tables;
}

sub page_before_template {
    my ($self, $args) = @_;
    my ($vars, $page) = @$args{qw(vars page_id)};
    return unless $page eq 'edit_table.html';
    my $input = Bugzilla->input_params;

    # we only support editing a particular set of tables
    my $table_name = $input->{table};
    exists $self->EDITABLE_TABLES()->{$table_name}
        || ThrowUserError('edittable_unsupported', { table => $table_name } );
    my $table = $self->EDITABLE_TABLES()->{$table_name};
    my $id_field = $table->{id_field};
    my $order_by = $table->{order_by} || $id_field;
    my $group    = $table->{group} || 'admin';
    trick_taint($table_name);

    Bugzilla->user->in_group($group)
        || ThrowUserError('auth_failure', { group  => $group,
                                            action => 'edit',
                                            object => 'tables' });

    # load columns
    my $dbh = Bugzilla->dbh;
    my @fields = sort
                 grep { $_ ne $id_field && $_ ne $order_by; }
                 $dbh->bz_table_columns($table_name);
    if ($order_by ne $id_field) {
        unshift @fields, $order_by;
    }

    # update table
    my $data = $input->{table_data};
    my $edits = [];
    if ($data) {
        check_hash_token($input->{token}, [$table_name]);
        delete_token($input->{token});

        $data = from_json($data)->{data};
        $edits = dclone($data);
        eval {
            $dbh->bz_start_transaction;

            foreach my $row (@$data) {
                map { trick_taint($_) } @$row;
                if ($row->[0] eq '-') {
                    # add
                    shift @$row;
                    next unless grep { $_ ne '' } @$row;
                    my $placeholders = join(',', split(//, '?' x scalar(@fields)));
                    $dbh->do(
                        "INSERT INTO $table_name(" . join(',', @fields) . ") " .
                        "VALUES ($placeholders)",
                        undef,
                        @$row
                    );
                }
                elsif ($row->[0] < 0) {
                    # delete
                    $dbh->do(
                        "DELETE FROM $table_name WHERE $id_field=?",
                        undef,
                        -$row->[0]
                    );
                }
                else {
                    # update
                    my $id = shift @$row;
                    $dbh->do(
                        "UPDATE $table_name " .
                        "SET " . join(',', map { "$_ = ?" } @fields) . " " .
                        "WHERE $id_field = ?",
                        undef,
                        @$row, $id
                    );
                }
            }

            $dbh->bz_commit_transaction;
            $vars->{updated} = 1;
            $edits = [];
        };
        if ($@) {
            my $error = $@;
            $error =~ s/^DBD::[^:]+::db do failed: //;
            $error =~ s/^(.+) \[for Statement ".+$/$1/s;
            $vars->{error} = $error;
            $dbh->bz_rollback_transaction;
        }
    }

    # load data from table
    unshift @fields, $id_field;
    $data = $dbh->selectall_arrayref(
        "SELECT " . join(',', @fields) . " FROM $table_name ORDER BY $order_by"
    );

    # we don't support nulls currently
    foreach my $row (@$data) {
        if (grep { !defined($_) } @$row) {
            ThrowUserError('edittable_nulls', { table => $table_name } );
        }
    }

    # apply failed edits
    foreach my $edit (@$edits) {
        if ($edit->[0] eq '-') {
            push @$data, $edit;
        }
        else {
            my $id = $edit->[0];
            foreach my $row (@$data) {
                if ($row->[0] == $id) {
                    @$row = @$edit;
                    last;
                }
            }
        }
    }

    $vars->{table_name} = $table_name;
    $vars->{blurb}      = $table->{blurb};
    $vars->{token}      = issue_hash_token([$table_name]);
    $vars->{table_data} = to_json({
        fields   => \@fields,
        id_field => $id_field,
        data     => $data,
    });
}

__PACKAGE__->NAME;