diff options
Diffstat (limited to 'Bugzilla/WebService/Util.pm')
-rw-r--r-- | Bugzilla/WebService/Util.pm | 36 |
1 files changed, 25 insertions, 11 deletions
diff --git a/Bugzilla/WebService/Util.pm b/Bugzilla/WebService/Util.pm index dbf5ec593..e94feb490 100644 --- a/Bugzilla/WebService/Util.pm +++ b/Bugzilla/WebService/Util.pm @@ -28,7 +28,8 @@ use base qw(Exporter); require Test::Taint; our @EXPORT_OK = qw( - filter + filter + filter_wants taint_data validate ); @@ -36,21 +37,29 @@ our @EXPORT_OK = qw( sub filter ($$) { my ($params, $hash) = @_; my %newhash = %$hash; - my %include = map { $_ => 1 } @{ $params->{'include_fields'} || [] }; - my %exclude = map { $_ => 1 } @{ $params->{'exclude_fields'} || [] }; foreach my $key (keys %$hash) { - if (defined $params->{include_fields}) { - delete $newhash{$key} if !$include{$key}; - } - if (defined $params->{exclude_fields}) { - delete $newhash{$key} if $exclude{$key}; - } + delete $newhash{$key} if !filter_wants($params, $key); } return \%newhash; } +sub filter_wants ($$) { + my ($params, $field) = @_; + my %include = map { $_ => 1 } @{ $params->{'include_fields'} || [] }; + my %exclude = map { $_ => 1 } @{ $params->{'exclude_fields'} || [] }; + + if (defined $params->{include_fields}) { + return 0 if !$include{$field}; + } + if (defined $params->{exclude_fields}) { + return 0 if $exclude{$field}; + } + + return 1; +} + sub taint_data { my @params = @_; return if !@params; @@ -115,20 +124,25 @@ internally in the WebService code. filter({ include_fields => ['id', 'name'], exclude_fields => ['name'] }, $hash); - + my $wants = filter_wants $params, 'field_name'; validate(@_, 'ids'); =head1 METHODS =over -=item C<filter_fields> +=item C<filter> This helps implement the C<include_fields> and C<exclude_fields> arguments of WebService methods. Given a hash (the second argument to this subroutine), this will remove any keys that are I<not> in C<include_fields> and then remove any keys that I<are> in C<exclude_fields>. +=item C<filter_wants> + +Returns C<1> if a filter would preserve the specified field when passing +a hash to L</filter>, C<0> otherwise. + =item C<validate> This helps in the validation of parameters passed into the WebSerice |