]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Bug 972881 - Add hooks to Bugzilla/WebService/Server/REST.pm to allow for extensions...
authorDave Lawrence <dlawrence@mozilla.com>
Tue, 25 Feb 2014 21:43:57 +0000 (21:43 +0000)
committerDave Lawrence <dlawrence@mozilla.com>
Tue, 25 Feb 2014 21:43:57 +0000 (21:43 +0000)
r=glob,a=justdave

Bugzilla/Hook.pm
Bugzilla/WebService/Server/REST.pm
Bugzilla/WebService/Util.pm
extensions/Example/Extension.pm

index e6a0ba2838bffe8f137c124e8e97867121811d15..811e858d794398fa7023b3a96e9907320caf4d99 100644 (file)
@@ -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>
index f1e59873cf1050b15c8f99b6c67cde6512d249a7..f4bc47c91c4bc2e5bf0b5f9bb2717a48af9e5997 100644 (file)
@@ -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
index 3647832165fb6ef88c46d40f25db35694c5ec201..7f6d14b597ca36b8915ebf420f4c1061a1391e6b 100644 (file)
@@ -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__
index e3e9b55f65761d09bfd81eab7457f9ef5a034c26..790231b89e382cef1ca5828a6e4d0ac83fa50f91 100644 (file)
@@ -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;