Sha256: c175f20f93164a166aecc41c199d0d289b250d3226a79441d4a70a4658c1ff8e

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

module Robotnik
  module Authorization
    class Status
      
      include Robotnik::DslEval
        
        def can action, resource, conditions=nil, &block
          init_rule_for resource
          rules[resource][action] = conditions || block || true
        end
        
        def cannot action, resource
          init_rule_for resource
          rules[resource][action] = false
        end
        
        def can? action, resource, subject
          begin
            condition = rules[resource][action]
            case condition
              when true, false then condition
              else
                return condition.call(subject) if condition.respond_to?(:call)
                deliberation = true
                deliberation = deliberation && condition[:if].call(subject) if condition.has_key?(:if)
                deliberation = deliberation && (! condition[:unless].call(subject)) if deliberation && condition.has_key?(:unless)
                deliberation
            end
          rescue NoMethodError
            false
          end
        end
        
      private
        
        def rules
          @rules ||= {}.with_indifferent_access
        end
        
        def init_rule_for resource
          rules[resource] ||= {}.with_indifferent_access
        end
        
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stasi-0.0.1.alpha lib/stasi/authorization/status.rb