Sha256: 5596fd5c18830efca1f1ca8521a414ed07164fa1e7454e3cef3e51381a4f3a39

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

require 'fun_with_json_api/exception'

module FunWithJsonApi
  module SchemaValidators
    class CheckResourceIsAuthorised
      def self.call(...)
        new(...).call
      end

      attr_reader :resource
      attr_reader :resource_id
      attr_reader :deserializer
      attr_reader :resource_pointer

      def initialize(resource, resource_id, deserializer, resource_pointer: '/data')
        @resource = resource
        @resource_id = resource_id
        @deserializer = deserializer
        @resource_pointer = resource_pointer
      end

      def call
        unless deserializer.resource_authorizer.call(resource)
          raise Exceptions::UnauthorizedResource.new(
            "resource_authorizer method for '#{deserializer.type}' returned a false value",
            ExceptionPayload.new(
              pointer: resource_pointer,
              detail: unauthorized_resource_message
            )
          )
        end
      end

      def resource_type
        deserializer.type
      end

      private

      def unauthorized_resource_message
        I18n.t(
          :unauthorized_resource,
          resource: resource_type,
          resource_id: resource_id,
          scope: 'fun_with_json_api.find_resource_from_document'
        )
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fun_with_json_api-0.0.14 lib/fun_with_json_api/schema_validators/check_resource_is_authorized.rb