diff --git a/lib/pact_broker/api/resources/authorization.rb b/lib/pact_broker/api/resources/authorization.rb new file mode 100644 index 000000000..6419d0f07 --- /dev/null +++ b/lib/pact_broker/api/resources/authorization.rb @@ -0,0 +1,37 @@ +module PactBroker + module Api + module Resources + module Authorization + def action + if read_methods.include?(request.method) + :read + elsif update_methods.include?(request.method) + :update + elsif create_methods.include?(request.method) + :create + elsif delete_methods.include?(request.method) + :delete + else + raise "Cannot map #{request.method} to an action" + end + end + + def read_methods + %w{GET HEAD OPTIONS} + end + + def update_methods + %w{PUT PATCH} + end + + def create_methods + %w{POST PUT} + end + + def delete_methods + %w{DELETE} + end + end + end + end +end diff --git a/lib/pact_broker/api/resources/default_base_resource.rb b/lib/pact_broker/api/resources/default_base_resource.rb index 2bfe11bcb..fac94cc41 100644 --- a/lib/pact_broker/api/resources/default_base_resource.rb +++ b/lib/pact_broker/api/resources/default_base_resource.rb @@ -7,6 +7,7 @@ require 'pact_broker/json' require 'pact_broker/pacts/pact_params' require 'pact_broker/api/resources/authentication' +require 'pact_broker/api/resources/authorization' require 'pact_broker/errors' module PactBroker @@ -18,6 +19,8 @@ class DefaultBaseResource < Webmachine::Resource include PactBroker::Services include PactBroker::Api::PactBrokerUrls include PactBroker::Api::Resources::Authentication + include PactBroker::Api::Resources::Authorization + include PactBroker::Logging attr_accessor :user @@ -261,6 +264,10 @@ def validation_errors_for_schema?(schema_to_use = schema, params_to_validate = p false end end + + def malformed_request_for_json_with_schema?(schema_to_use = schema, params_to_validate = params) + invalid_json? || validation_errors_for_schema?(schema_to_use, params_to_validate) + end end end end