Adding a column to an Object in an Extension

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Adding a column to an Object in an Extension

Ali Irfan Ustek
Hi all,

I have added a column to Bugzilla::Field::Choice in my extension,
update works but creating a new value doesn't create the new column.
When I added warn messages to code it seems that
Bugzilla::Field::Choice::create runs before "object_before_create"
hook.

How can I make sure that group_id field is added to object?

<pre>
sub install_update_db {
    my ($self, $args) = @_;

    my $dbh = Bugzilla->dbh;
    my @standard_fields =
        qw(bug_status resolution priority bug_severity op_sys
rep_platform);
    my $custom_fields = $dbh->selectcol_arrayref(
        'SELECT name FROM fielddefs WHERE custom = 1 AND type
IN(?,?)',
        undef, FIELD_TYPE_SINGLE_SELECT, FIELD_TYPE_MULTI_SELECT);
    foreach my $field_name (@standard_fields, @$custom_fields) {
    if (!$dbh->bz_column_info($field_name, 'group_id')){
       my $field = new Bugzilla::Field({'name' => $field_name});
                my $group = Bugzilla::Field::ChoiceGroup->create({
                        name   => "Default",
        field => $field,
                        sortkey => "0",
                    });
        $dbh->bz_add_column($field_name, 'group_id',
        {TYPE => 'INT2'},
        $group->id);
                $dbh->bz_add_index($field_name, "${field_name}
_group_id_idx",
                           ['group_id']);
                }
    }
}

#############################
#                  Objects
#
#############################
sub object_columns {
    my ($self, $args) = @_;
    my ($class, $columns) = @$args{qw(class columns)};

    if ($class->isa('Bugzilla::Field::Choice')) {
        push(@$columns, 'group_id');
    }
}

sub object_before_create {
    my ($self, $args) = @_;
    my ($class, $params) = @$args{qw(class params)};
    if ($class->isa('Bugzilla::Field::Choice')) {
       warn "Creating CHOICE";
        my $input = Bugzilla->input_params;
        my $group_id = $input->{'group_id'};
        trick_taint($group_id);
        $params->{group_id}   = $group_id;
        warn "GID: ".$params->{group_id};
    }
}

sub object_validators {
    my ($self, $args) = @_;
    my ($class, $validators) = @$args{qw(class validators)};

    if ($class->isa('Bugzilla::Field::Choice')) {
        $validators->{group_id} = \&_check_group_id;
    }
}

sub object_end_of_create {
    my ($self, $args) = @_;

    my $class  = $args->{'class'};
    my $object = $args->{'object'};

        if ($class->isa('Bugzilla::Field')) {
                my $created_group = Bugzilla::Extension::FieldGroups::Group-
>create({
            name   => "Default",
            field => $class,
            sortkey => "0",
    });
        }
}

sub object_update_columns {
    my ($self, $args) = @_;
    my ($object, $columns) = @$args{qw(object columns)};

    if ($object->isa('Bugzilla::Field::Choice')) {
        push(@$columns, 'group_id');
    }
}

sub object_end_of_set {
    my ($self, $args) = @_;

    my ($object, $field) = @$args{qw(object field)};

    if ($object->isa('Bugzilla::Field::Choice')) {
        my $input = Bugzilla->input_params;
        if ($object->{group_id} != $input->{'group_id'}) {
            my $group_id = $input->{'group_id'};
            trick_taint($group_id);
        $object->{group_id} = $group_id;
        }
    }
}

sub object_end_of_set_all {
    my ($self, $args) = @_;
    my ($object) = $args->{object};
    warn "FINISHED SET ALL";
    if ($object->isa('Bugzilla::Field::Choice')) {
        my $input = Bugzilla->input_params;
        $object->set('group_id',   $input->{'group_id'});
    }
}
</pre>
_______________________________________________
dev-apps-bugzilla mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-apps-bugzilla
-
To view or change your list settings, click here:
<http://bugzilla.org/cgi-bin/mj_wwwusr?user=lists@...>
Reply | Threaded
Open this post in threaded view
|

Re: Adding a column to an Object in an Extension

Max Kanat-Alexander
>          my $input = Bugzilla->input_params;
>          my $group_id = $input->{'group_id'};
>          trick_taint($group_id);
>          $params->{group_id}   = $group_id;

        FWIW, you should not be untainting group id here, you should be
untainting it in a validator, if untainting is even required.

>      if ($object->isa('Bugzilla::Field::Choice')) {
>          my $input = Bugzilla->input_params;
>          if ($object->{group_id} != $input->{'group_id'}) {
>              my $group_id = $input->{'group_id'};
>              trick_taint($group_id);
>           $object->{group_id} = $group_id;
>          }
>      }

        This code should not be in object_end_of_set. In fact, as far as I can
see, you shouldn't have *any* code in object_end_of_set. This looks like
code that belongs in a validator instead, and this isn't even a valid
validation. I could pass in any number here and it would take it.

        Also, it looks like your object_validators hook references some
function called _check_group_id, but I don't see that here.

> sub object_end_of_set_all {

        It's possible that editvalues.cgi isn't using set_all, and that it
should be.

        -Max
--
Max Kanat-Alexander
Chief Architect, Community Lead, and Release Manager
Bugzilla Project
http://www.bugzilla.org/
-
To view or change your list settings, click here:
<http://bugzilla.org/cgi-bin/mj_wwwusr?user=lists@...>
Reply | Threaded
Open this post in threaded view
|

Re: Adding a column to an Object in an Extension

Ali Irfan Ustek
My mistake was that in _check_group_id an invalid check and returning undef.

Also, I changed untaint to

my $group = Bugzilla::Extension::FieldGroups::Group->new($input->{'group_id'});

to make sure the id passed is valid.

Thanks
-
To view or change your list settings, click here:
<http://bugzilla.org/cgi-bin/mj_wwwusr?user=lists@...>
Reply | Threaded
Open this post in threaded view
|

Re: Adding a column to an Object in an Extension

Max Kanat-Alexander
On 09/14/2011 01:58 AM, Robin Alan wrote:
> Also, I changed untaint to
>
> my $group = Bugzilla::Extension::FieldGroups::Group->new($input->{'group_id'});
>
> to make sure the id passed is valid.

        Okay. However, validation should be done in the validator and nowhere else.

        -Max
--
Max Kanat-Alexander
Chief Architect, Community Lead, and Release Manager
Bugzilla Project
http://www.bugzilla.org/
-
To view or change your list settings, click here:
<http://bugzilla.org/cgi-bin/mj_wwwusr?user=lists@...>