Sha256: 3bddbe5db826e9ca260e6661ab6c1896001dd1e9843178d2934df82e9bf038ff

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(*args)
        new(*args).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::UnauthorisedResource.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.10 lib/fun_with_json_api/schema_validators/check_resource_is_authorized.rb