class Can4::SubjectRule

Rule class representing actions performable on a subject. @!visibility private

Public Class Methods

new() click to toggle source
# File lib/can4/rule.rb, line 5
def initialize
  @actions = {}
end

Public Instance Methods

add_grant(action, block) click to toggle source

Add a granting ACL for a particular action.

@param action [symbol] The action. @param block An optional block for granularity.

# File lib/can4/rule.rb, line 13
def add_grant(action, block)
  @actions[action] = block || true
end
authorized?(action, subject, args) click to toggle source

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.

# File lib/can4/rule.rb, line 24
def authorized?(action, subject, args)
  block = @actions[:manage] || @actions[action]

  return false unless block
  return true if block == true

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