Sha256: 3e801f3be7c9383b30b411c4e909b33573654cae3c540c9e2aefdf211963a7e2

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

module Can4
  # Rule class representing actions performable on a subject.
  # @!visibility private
  class SubjectRule
    def initialize
      @actions = {}
    end

    # Add a granting ACL for a particular action.
    #
    # @param action [symbol] The action.
    # @param block An optional block for granularity.
    def add_grant(action, block)
      @actions[action] = block || true
    end

    # Return whether or not an object can perform a particular action on a
    # subject.
    #
    # @param action [Symbol] The action.
    # @param subject [Object] The subject.
    # @param args [Hash] Variable arguments for more granular matching.
    # @return [Boolean] True or false.
    def authorized?(action, subject, args)
      block = @actions[:manage] || @actions[action]

      return false unless block
      return true if block == true

      !!block.call(subject, *args)
    end
  end

  # Fake rule representing nothing matched a subject when looking up its
  # ability.
  #
  # @!visibility private
  class NullRule
    def self.authorized?(*)
      false
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
can4-1.0.2 lib/can4/rule.rb