Commit b06a5f44 authored by Dave Lawrence's avatar Dave Lawrence

Bug 972881 - Add hooks to Bugzilla/WebService/Server/REST.pm to allow for…

Bug 972881 - Add hooks to Bugzilla/WebService/Server/REST.pm to allow for extensions to extend and alter REST data r=glob,a=justdave
parent 3ff1cbe6
......@@ -1623,6 +1623,89 @@ See L<Bugzilla::WebService::Constants/WS_ERROR_CODE> for an example.
=back
=head2 webservice_fix_credentials
This hook allows for altering the credential parameters provided by the client
before authentication actually occurs. For example, this can be used to allow mapping
of custom parameters to the standard Bugzilla_login and Bugzilla_password parameters.
Params:
=over
=item C<params>
A hash ref containing the parameters passed into the webservice after
they have been obtained from the URL or body of the request.
=back
=head2 webservice_rest_request
This hook allows for altering any of the parameters provided by the client
after authentication has occured. You are able to change things like renaming
of keys, removing values, or adding additional information.
Params:
=over
=item C<params>
A hash ref containing the parameters passed into the webservice after
they have been obtained from the URL or body of the request.
=item C<rpc>
The current JSONRPC, XMLRPC, or REST object.
=back
=head2 webservice_rest_resources
This hook allows for altering of the REST resources data allowing you to
add additional paths to perform additional operations or to override the
resources already provided by the webservice modules.
Params:
=over
=item C<resources>
A hash returned from each module loaded that is used to determine
which code handler to use based on a regex match of the CGI path.
=item C<rpc>
The current JSONRPC, XMLRPC, or REST object.
=back
=head2 webservice_rest_response
This hook allows for altering the result data or response object
that is being returned by the current REST webservice call.
Params:
=over
=item C<response>
The HTTP response object generated by JSON-RPC library. You can use this
to add headers, etc.
=item C<result>
A reference to a hash that contains the result data.
=item C<rpc>
The current JSONRPC, XMLRPC, or REST object.
=back
=head1 SEE ALSO
L<Bugzilla::Extension>
......@@ -15,9 +15,10 @@ use parent qw(Bugzilla::WebService::Server::JSONRPC);
use Bugzilla;
use Bugzilla::Constants;
use Bugzilla::Error;
use Bugzilla::Hook;
use Bugzilla::Util qw(correct_urlbase html_quote);
use Bugzilla::WebService::Constants;
use Bugzilla::WebService::Util qw(taint_data fix_credentials);
use Bugzilla::Util qw(correct_urlbase html_quote);
# Load resource modules
use Bugzilla::WebService::Server::REST::Resources::Bug;
......@@ -124,6 +125,9 @@ sub response {
$result = $json_data->{result};
}
Bugzilla::Hook::process('webservice_rest_response',
{ rpc => $self, result => \$result, response => $response });
# Access Control
$response->header("Access-Control-Allow-Origin", "*");
$response->header("Access-Control-Allow-Headers", "origin, content-type, accept");
......@@ -226,8 +230,6 @@ sub _argument_type_check {
taint_data($params);
Bugzilla->input_params($params);
# Now, convert dateTime fields on input.
my $method = $self->bz_method_name;
my $pkg = $self->{dispatch_path}->{$self->path_info};
......@@ -262,6 +264,10 @@ sub _argument_type_check {
eval "package $new_class;$isa_string;";
bless $self, $new_class;
# Allow extensions to modify the params post login
Bugzilla::Hook::process('webservice_rest_request',
{ rpc => $self, params => $params });
if ($params_is_array) {
$params = [$params];
}
......@@ -377,6 +383,9 @@ sub _find_resource {
$resources->{$module} = $module->rest_resources;
}
Bugzilla::Hook::process('webservice_rest_resources',
{ rpc => $self, resources => $resources });
# Use the resources hash from each module loaded earlier to determine
# which handler to use based on a regex match of the CGI path.
# Also any matches found in the regex will be passed in later to the
......
......@@ -242,6 +242,9 @@ sub fix_credentials {
if (exists $params->{'token'}) {
$params->{'Bugzilla_token'} = $params->{'token'};
}
# Allow extensions to modify the credential data before login
Bugzilla::Hook::process('webservice_fix_credentials', { params => $params });
}
__END__
......
......@@ -949,5 +949,57 @@ sub webservice_error_codes {
$error_map->{'example_my_error'} = 10001;
}
sub webservice_fix_credentials {
my ($self, $args) = @_;
my $rpc = $args->{'rpc'};
my $params = $args->{'params'};
# Allow user to pass in username=foo&password=bar
if (exists $params->{'username'} && exists $params->{'password'}) {
$params->{'Bugzilla_login'} = $params->{'username'};
$params->{'Bugzilla_password'} = $params->{'password'};
}
}
sub webservice_rest_request {
my ($self, $args) = @_;
my $rpc = $args->{'rpc'};
my $params = $args->{'params'};
# Internally we may have a field called 'cf_test_field' but we allow users
# to use the shorter 'test_field' name.
if (exists $params->{'test_field'}) {
$params->{'test_field'} = delete $params->{'cf_test_field'};
}
}
sub webservice_rest_resources {
my ($self, $args) = @_;
my $rpc = $args->{'rpc'};
my $resources = $args->{'resources'};
# Add a new resource that allows for /rest/example/hello
# to call Example.hello
$resources->{'Bugzilla::Extension::Example::WebService'} = [
qr{^/example/hello$}, {
GET => {
method => 'hello',
}
}
];
}
sub webservice_rest_response {
my ($self, $args) = @_;
my $rpc = $args->{'rpc'};
my $result = $args->{'result'};
my $response = $args->{'response'};
# Convert a list of bug hashes to a single bug hash if only one is
# being returned.
if (ref $$result eq 'HASH'
&& exists $$result->{'bugs'}
&& scalar @{ $$result->{'bugs'} } == 1)
{
$$result = $$result->{'bugs'}->[0];
}
}
# This must be the last line of your extension.
__PACKAGE__->NAME;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment