Sha256: bc9e97299ec5783ce2cbb6ff4ea51d8b27ccaf964c520ccfc8ef6708d2402bc0

Contents?: true

Size: 1.6 KB

Versions: 13

Compression:

Stored size: 1.6 KB

Contents

module Shamu
  module Security

    # A rule capturing the permitted actions and resources for {Policy}
    # permissions.
    class PolicyRule

      # ============================================================================
      # @!group Attributes
      #

      # @!attribute
      # @return [Object] the value to return as the result of a {Policy#permit?}
      #     call if the rule matches the request.
        attr_reader :result

      #
      # @!endgroup Attributes

      def initialize( actions, resource, result, block )
        @actions  = actions
        @resource = resource
        @result   = result
        @block    = block
      end

      # Determines if the rule matches the request action permission on the
      # given resource.
      #
      # @param [Symbol] action to be performed.
      # @param [Object] resource the action will be performed on.
      # @param [Object] additional context offered to {Policy#permit?}.
      #
      # @return [Boolean] true if the rule is a match.
      def match?( action, resource, additional_context )
        return false unless actions.include? action
        return false unless resource_match?( resource )

        if block && !resource.is_a?( Module )
          block.call( resource, additional_context )
        else
          true
        end
      end

      private

        attr_reader :actions
        attr_reader :resource
        attr_reader :block

        def resource_match?( candidate )
          return true if resource == candidate
          return true if resource.is_a?( Module ) && candidate.is_a?( resource )
        end

    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
shamu-0.0.18 lib/shamu/security/policy_rule.rb
shamu-0.0.17 lib/shamu/security/policy_rule.rb
shamu-0.0.15 lib/shamu/security/policy_rule.rb
shamu-0.0.14 lib/shamu/security/policy_rule.rb
shamu-0.0.13 lib/shamu/security/policy_rule.rb
shamu-0.0.11 lib/shamu/security/policy_rule.rb
shamu-0.0.9 lib/shamu/security/policy_rule.rb
shamu-0.0.8 lib/shamu/security/policy_rule.rb
shamu-0.0.7 lib/shamu/security/policy_rule.rb
shamu-0.0.5 lib/shamu/security/policy_rule.rb
shamu-0.0.4 lib/shamu/security/policy_rule.rb
shamu-0.0.3 lib/shamu/security/policy_rule.rb
shamu-0.0.2 lib/shamu/security/policy_rule.rb