<commit_before>require "unimatrix/keymaker/sdk/version"

module Unimatrix
  module Keymaker
    module Sdk
      class RequiresPolicies

        def initialize( application, resource )
          @application = application
          @resource = resource
        end

        def before( controller )
          access_token = controller.params[ 'access_token' ]
          realm = controller.realm.uuid
          if access_token.present?
            str      = "realm/#{ realm }::#{ @application }::#{ @resource }/*"
            params   = "resource=#{ str }&access_token=#{ access_token }"
            uri      = URI.parse( "#{ ENV[ 'KEYMAKER_URL' ] }/policies?#{ params }" )
            response = Net::HTTP.get( uri ) rescue nil

            if response
              controller.policies = JSON.parse( response )[ 'policies' ] rescue nil
            else
              controller.render_error(
                ApplicationError,
                "The requested policies could not be retrieved from Keymaker."
              )
            end
          else
            controller.render_error( 
              MissingParameterError,
              "The parameter 'access_token' is required."
            ) 
          end
        end
      end

      module ClassMethods

        def requires_policies( application, resource )
          before_filter( RequiresPolicies.new( application, resource ) )
        end

      end

      def self.included( controller )
        controller.extend( ClassMethods )
      end

      def permitted?
        return false if policies.blank?

        policies.each do | policy |
          return true if policy[ 'actions' ].include?( action_name )
        end

        false
      end
    end
  end
end
<commit_msg>Remove #permitted?; checked whether action is permitted within gem<commit_after>require "unimatrix/keymaker/sdk/version"

module Unimatrix
  module Keymaker
    module Sdk
      class RequiresPolicies

        def initialize( application, resource )
          @application = application
          @resource = resource
        end

        def before( controller )
          access_token = controller.params[ 'access_token' ]
          realm = controller.realm.uuid
          if access_token.present?
            str      = "realm/#{ realm }::#{ @application }::#{ @resource }/*"
            params   = "resource=#{ str }&access_token=#{ access_token }"
            uri      = URI.parse( "#{ ENV[ 'KEYMAKER_URL' ] }/policies?#{ params }" )
            response = Net::HTTP.get( uri ) rescue nil

            if response
              policies = JSON.parse( response )[ 'policies' ] rescue nil
              forbidden = true

              policies.each do | policy |
                forbidden = false if policy[ 'actions' ].include?( controller.action_name )
              end

              if forbidden
                controller.render_error(
                  ForbiddenError,
                  "A policy permitting this action was not found."
                )
              end
            else
              controller.render_error(
                ApplicationError,
                "The requested policies could not be retrieved from Keymaker."
              )
            end
          else
            controller.render_error( 
              MissingParameterError,
              "The parameter 'access_token' is required."
            ) 
          end
        end
      end

      module ClassMethods

        def requires_policies( application, resource )
          before_filter( RequiresPolicies.new( application, resource ) )
        end

      end

      def self.included( controller )
        controller.extend( ClassMethods )
      end

    end
  end
end

{'fraction_non_alphanumeric': 0.07727517501346258, 'fraction_numerical': 0.0, 'mean_word_length': 1.6088483146067416, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5499265', 'n_tokens_mistral': 936, 'n_tokens_neox': 925, 'n_words': 259}