From 0ee4621e7828a205189368aa9b8a515574d9c030 Mon Sep 17 00:00:00 2001 From: "wurblzap%gmail.com" <> Date: Sun, 20 Aug 2006 00:20:23 +0000 Subject: Bug 224577: Bugzilla could use a web services interface. Patch by Marc Schumann ; r=mkanat; a=myk --- Bugzilla/Auth/Login/CGI.pm | 7 +++++ Bugzilla/Constants.pm | 20 +++++++++++++++ Bugzilla/Error.pm | 20 ++++++++++----- Bugzilla/Install/Requirements.pm | 14 ++++++++++ Bugzilla/Util.pm | 13 +++++----- Bugzilla/WebService.pm | 47 ++++++++++++++++++++++++++++++++++ Bugzilla/WebService/Bug.pm | 36 ++++++++++++++++++++++++++ Bugzilla/WebService/Bugzilla.pm | 27 ++++++++++++++++++++ Bugzilla/WebService/Constants.pm | 32 +++++++++++++++++++++++ Bugzilla/WebService/Product.pm | 36 ++++++++++++++++++++++++++ Bugzilla/WebService/User.pm | 55 ++++++++++++++++++++++++++++++++++++++++ 11 files changed, 295 insertions(+), 12 deletions(-) create mode 100755 Bugzilla/WebService.pm create mode 100755 Bugzilla/WebService/Bug.pm create mode 100755 Bugzilla/WebService/Bugzilla.pm create mode 100755 Bugzilla/WebService/Constants.pm create mode 100755 Bugzilla/WebService/Product.pm create mode 100755 Bugzilla/WebService/User.pm (limited to 'Bugzilla') diff --git a/Bugzilla/Auth/Login/CGI.pm b/Bugzilla/Auth/Login/CGI.pm index 033cb992b..2a61a54f7 100644 --- a/Bugzilla/Auth/Login/CGI.pm +++ b/Bugzilla/Auth/Login/CGI.pm @@ -34,6 +34,7 @@ use base qw(Bugzilla::Auth::Login); use constant user_can_create_account => 1; use Bugzilla::Constants; +use Bugzilla::WebService::Constants; use Bugzilla::Util; use Bugzilla::Error; @@ -58,6 +59,12 @@ sub fail_nodata { my $cgi = Bugzilla->cgi; my $template = Bugzilla->template; + if (Bugzilla->error_mode == Bugzilla::Constants::ERROR_MODE_DIE_SOAP_FAULT) { + die SOAP::Fault + ->faultcode(ERROR_AUTH_NODATA) + ->faultstring('Login Required'); + } + # Redirect to SSL if required if (Bugzilla->params->{'sslbase'} ne '' and Bugzilla->params->{'ssl'} ne 'never') diff --git a/Bugzilla/Constants.pm b/Bugzilla/Constants.pm index 197c1d70a..9493ea400 100644 --- a/Bugzilla/Constants.pm +++ b/Bugzilla/Constants.pm @@ -27,6 +27,7 @@ # Christopher Aillon # Shane H. W. Travis # Max Kanat-Alexander +# Marc Schumann package Bugzilla::Constants; use strict; @@ -108,6 +109,14 @@ use File::Basename; BUG_STATE_OPEN + USAGE_MODE_BROWSER + USAGE_MODE_CMDLINE + USAGE_MODE_WEBSERVICE + + ERROR_MODE_WEBPAGE + ERROR_MODE_DIE + ERROR_MODE_DIE_SOAP_FAULT + DB_MODULE ROOT_USER ON_WINDOWS @@ -290,6 +299,17 @@ use constant FIELD_TYPE_FREETEXT => 1; use constant BUG_STATE_OPEN => ('NEW', 'REOPENED', 'ASSIGNED', 'UNCONFIRMED'); +# Usage modes. Default USAGE_MODE_BROWSER. Use with Bugzilla->usage_mode. +use constant USAGE_MODE_BROWSER => 0; +use constant USAGE_MODE_CMDLINE => 1; +use constant USAGE_MODE_WEBSERVICE => 2; + +# Error modes. Default set by Bugzilla->usage_mode (so ERROR_MODE_WEBPAGE +# usually). Use with Bugzilla->error_mode. +use constant ERROR_MODE_WEBPAGE => 0; +use constant ERROR_MODE_DIE => 1; +use constant ERROR_MODE_DIE_SOAP_FAULT => 2; + # Data about what we require for different databases. use constant DB_MODULE => { 'mysql' => {db => 'Bugzilla::DB::Mysql', db_version => '4.0.14', diff --git a/Bugzilla/Error.pm b/Bugzilla/Error.pm index b88c4eeb8..1bb0556af 100644 --- a/Bugzilla/Error.pm +++ b/Bugzilla/Error.pm @@ -18,6 +18,7 @@ # Rights Reserved. # # Contributor(s): Bradley Baetz +# Marc Schumann package Bugzilla::Error; @@ -27,6 +28,7 @@ use base qw(Exporter); @Bugzilla::Error::EXPORT = qw(ThrowCodeError ThrowTemplateError ThrowUserError); use Bugzilla::Constants; +use Bugzilla::WebService::Constants; use Bugzilla::Util; use Date::Format; @@ -74,15 +76,21 @@ sub _throw_error { } my $template = Bugzilla->template; - if (Bugzilla->batch) { + if (Bugzilla->error_mode == ERROR_MODE_WEBPAGE) { + print Bugzilla->cgi->header(); + $template->process($name, $vars) + || ThrowTemplateError($template->error()); + } + elsif (Bugzilla->error_mode == ERROR_MODE_DIE) { my $message; $template->process($name, $vars, \$message) || ThrowTemplateError($template->error()); die("$message\n"); - } else { - print Bugzilla->cgi->header(); - $template->process($name, $vars) - || ThrowTemplateError($template->error()); + } + elsif (Bugzilla->error_mode == ERROR_MODE_DIE_SOAP_FAULT) { + die SOAP::Fault + ->faultcode(ERROR_GENERAL) + ->faultstring($error); } exit; } @@ -103,7 +111,7 @@ sub ThrowTemplateError { Bugzilla->dbh->bz_unlock_tables(UNLOCK_ABORT); my $vars = {}; - if (Bugzilla->batch) { + if (Bugzilla->error_mode == ERROR_MODE_DIE) { die("error: template error: $template_err"); } diff --git a/Bugzilla/Install/Requirements.pm b/Bugzilla/Install/Requirements.pm index e521f6e11..3a27bb2e4 100644 --- a/Bugzilla/Install/Requirements.pm +++ b/Bugzilla/Install/Requirements.pm @@ -13,6 +13,7 @@ # The Original Code is the Bugzilla Bug Tracking System. # # Contributor(s): Max Kanat-Alexander +# Marc Schumann package Bugzilla::Install::Requirements; @@ -117,6 +118,10 @@ use constant OPTIONAL_MODULES => [ name => 'Net::LDAP', version => 0 }, + { + name => 'SOAP::Lite', + version => 0 + }, ]; # These are only required if you want to use Bugzilla with @@ -252,6 +257,15 @@ sub check_requirements { " " . install_command("Image::Magick") . "\n\n"; } + # Web Services + if (!$have_mod{'SOAP::Lite'}) { + print "If you want your Bugzilla installation to be accessible\n", + "via its Web Service interface, you will need to install\n", + "the SOAP::Lite module by running (as $root):\n\n"; + print " SOAP::Lite: " . + install_command("SOAP::Lite") . "\n\n"; + } + # Graphical Reports if (!$have_mod{'GD'} || !$have_mod{'GD::Graph'} || !$have_mod{'GD::Text::Align'} diff --git a/Bugzilla/Util.pm b/Bugzilla/Util.pm index 24debb1d6..8821a6c66 100644 --- a/Bugzilla/Util.pm +++ b/Bugzilla/Util.pm @@ -24,6 +24,7 @@ # Christopher Aillon # Max Kanat-Alexander # Frédéric Buclin +# Marc Schumann package Bugzilla::Util; @@ -63,20 +64,20 @@ sub is_tainted { sub trick_taint { require Carp; Carp::confess("Undef to trick_taint") unless defined $_[0]; - my ($match) = $_[0] =~ /^(.*)$/s; - $_[0] = $match; + my $match = $_[0] =~ /^(.*)$/s; + $_[0] = $match ? $1 : undef; return (defined($_[0])); } sub detaint_natural { - my ($match) = $_[0] =~ /^(\d+)$/; - $_[0] = $match; + my $match = $_[0] =~ /^(\d+)$/; + $_[0] = $match ? $1 : undef; return (defined($_[0])); } sub detaint_signed { - my ($match) = $_[0] =~ /^([-+]?\d+)$/; - $_[0] = $match; + my $match = $_[0] =~ /^([-+]?\d+)$/; + $_[0] = $match ? $1 : undef; # Remove any leading plus sign. if (defined($_[0]) && $_[0] =~ /^\+(\d+)$/) { $_[0] = $1; diff --git a/Bugzilla/WebService.pm b/Bugzilla/WebService.pm new file mode 100755 index 000000000..9e100c12a --- /dev/null +++ b/Bugzilla/WebService.pm @@ -0,0 +1,47 @@ +# -*- Mode: perl; indent-tabs-mode: nil -*- +# +# The contents of this file are subject to the Mozilla Public +# License Version 1.1 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of +# the License at http://www.mozilla.org/MPL/ +# +# Software distributed under the License is distributed on an "AS +# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or +# implied. See the License for the specific language governing +# rights and limitations under the License. +# +# The Original Code is the Bugzilla Bug Tracking System. +# +# Contributor(s): Marc Schumann + +package Bugzilla::WebService; + +use strict; +use Bugzilla::WebService::Constants; + +sub fail_unimplemented { + my $this = shift; + + die SOAP::Fault + ->faultcode(ERROR_UNIMPLEMENTED) + ->faultstring('Service Unimplemented'); +} + +package Bugzilla::WebService::XMLRPC::Transport::HTTP::CGI; + +use strict; +eval 'use base qw(XMLRPC::Transport::HTTP::CGI)'; + +sub make_response { + my $self = shift; + + $self->SUPER::make_response(@_); + + # XMLRPC::Transport::HTTP::CGI doesn't know about Bugzilla carrying around + # its cookies in Bugzilla::CGI, so we need to copy them over. + foreach (@{Bugzilla->cgi->{'Bugzilla_cookie_list'}}) { + $self->response->headers->push_header('Set-Cookie', $_); + } +} + +1; diff --git a/Bugzilla/WebService/Bug.pm b/Bugzilla/WebService/Bug.pm new file mode 100755 index 000000000..6698fdc97 --- /dev/null +++ b/Bugzilla/WebService/Bug.pm @@ -0,0 +1,36 @@ +# -*- Mode: perl; indent-tabs-mode: nil -*- +# +# The contents of this file are subject to the Mozilla Public +# License Version 1.1 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of +# the License at http://www.mozilla.org/MPL/ +# +# Software distributed under the License is distributed on an "AS +# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or +# implied. See the License for the specific language governing +# rights and limitations under the License. +# +# The Original Code is the Bugzilla Bug Tracking System. +# +# Contributor(s): Marc Schumann + +package Bugzilla::WebService::Bug; + +use strict; +use base qw(Bugzilla::WebService); + +use Bugzilla::WebService::Constants; +use Bugzilla::Util qw(detaint_natural); +use Bugzilla::Bug; + +sub get_bug { + my $self = shift; + my ($bug_id) = @_; + + Bugzilla->login; + + ValidateBugID($bug_id); + return new Bugzilla::Bug($bug_id); +} + +1; diff --git a/Bugzilla/WebService/Bugzilla.pm b/Bugzilla/WebService/Bugzilla.pm new file mode 100755 index 000000000..0caf5fab2 --- /dev/null +++ b/Bugzilla/WebService/Bugzilla.pm @@ -0,0 +1,27 @@ +# -*- Mode: perl; indent-tabs-mode: nil -*- +# +# The contents of this file are subject to the Mozilla Public +# License Version 1.1 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of +# the License at http://www.mozilla.org/MPL/ +# +# Software distributed under the License is distributed on an "AS +# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or +# implied. See the License for the specific language governing +# rights and limitations under the License. +# +# The Original Code is the Bugzilla Bug Tracking System. +# +# Contributor(s): Marc Schumann + +package Bugzilla::WebService::Bugzilla; + +use strict; +use base qw(Bugzilla::WebService); +use Bugzilla::Constants; + +sub get_version { + return BUGZILLA_VERSION; +} + +1; diff --git a/Bugzilla/WebService/Constants.pm b/Bugzilla/WebService/Constants.pm new file mode 100755 index 000000000..2e9457add --- /dev/null +++ b/Bugzilla/WebService/Constants.pm @@ -0,0 +1,32 @@ +# -*- Mode: perl; indent-tabs-mode: nil -*- +# +# The contents of this file are subject to the Mozilla Public +# License Version 1.1 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of +# the License at http://www.mozilla.org/MPL/ +# +# Software distributed under the License is distributed on an "AS +# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or +# implied. See the License for the specific language governing +# rights and limitations under the License. +# +# The Original Code is the Bugzilla Bug Tracking System. +# +# Contributor(s): Marc Schumann + +package Bugzilla::WebService::Constants; + +use strict; +use base qw(Exporter); + +@Bugzilla::WebService::Constants::EXPORT = qw( + ERROR_AUTH_NODATA + ERROR_UNIMPLEMENTED + ERROR_GENERAL +); + +use constant ERROR_AUTH_NODATA => 410; +use constant ERROR_UNIMPLEMENTED => 910; +use constant ERROR_GENERAL => 999; + +1; diff --git a/Bugzilla/WebService/Product.pm b/Bugzilla/WebService/Product.pm new file mode 100755 index 000000000..b56abb588 --- /dev/null +++ b/Bugzilla/WebService/Product.pm @@ -0,0 +1,36 @@ +# -*- Mode: perl; indent-tabs-mode: nil -*- +# +# The contents of this file are subject to the Mozilla Public +# License Version 1.1 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of +# the License at http://www.mozilla.org/MPL/ +# +# Software distributed under the License is distributed on an "AS +# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or +# implied. See the License for the specific language governing +# rights and limitations under the License. +# +# The Original Code is the Bugzilla Bug Tracking System. +# +# Contributor(s): Marc Schumann + +package Bugzilla::WebService::Product; + +use strict; +use base qw(Bugzilla::WebService); +use Bugzilla::Product; + +sub get_product { + my $self = shift; + my ($product_name) = @_; + + Bugzilla->login; + + # Bugzilla::Product doesn't do permissions checks, so we can't do the call + # to Bugzilla::Product::new until a permissions check happens here. + $self->fail_unimplemented(); + + return new Bugzilla::Product({'name' => $product_name}); +} + +1; diff --git a/Bugzilla/WebService/User.pm b/Bugzilla/WebService/User.pm new file mode 100755 index 000000000..813b2fc2a --- /dev/null +++ b/Bugzilla/WebService/User.pm @@ -0,0 +1,55 @@ +# -*- Mode: perl; indent-tabs-mode: nil -*- +# +# The contents of this file are subject to the Mozilla Public +# License Version 1.1 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of +# the License at http://www.mozilla.org/MPL/ +# +# Software distributed under the License is distributed on an "AS +# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or +# implied. See the License for the specific language governing +# rights and limitations under the License. +# +# The Original Code is the Bugzilla Bug Tracking System. +# +# Contributor(s): Marc Schumann + +package Bugzilla::WebService::User; + +use strict; +use base qw(Bugzilla::WebService); +use Bugzilla; +use Bugzilla::Constants; + +sub login { + my $self = shift; + my ($login, $password, $remember) = @_; + + # Convert $remember from a boolean 0/1 value to a CGI-compatible one. + if (defined($remember)) { + $remember = $remember? 'on': ''; + } + else { + # Use Bugzilla's default if $remember is not supplied. + $remember = + Bugzilla->params->{'rememberlogin'} eq 'defaulton'? 'on': ''; + } + + # Make sure the CGI user info class works if necessary. + my $cgi = Bugzilla->cgi; + $cgi->param('Bugzilla_login', $login); + $cgi->param('Bugzilla_password', $password); + $cgi->param('Bugzilla_remember', $remember); + + Bugzilla->login; + return Bugzilla->user->id; +} + +sub logout { + my $self = shift; + + Bugzilla->login(LOGIN_OPTIONAL); + Bugzilla->logout; +} + +1; -- cgit v1.2.3-24-g4f1b