class Admission::ResourceArbitration::RulesBuilder

Public Instance Methods

allow(scope, *actions, &block) click to toggle source
# File lib/admission/resource_arbitration.rb, line 63
def allow scope, *actions, &block
  raise "reserved action name #{Admission::ALL_ACTION}" if actions.include? Admission::ALL_ACTION
  raise "invalid scope name" unless scope.respond_to? :to_sym
  add_allowance_rule actions.flatten, (block || true), scope: scope.to_sym
end
allow_all(scope, &block) click to toggle source
# File lib/admission/resource_arbitration.rb, line 69
def allow_all scope, &block
  raise "invalid scope name" unless scope.respond_to? :to_sym
  add_allowance_rule [Admission::ALL_ACTION], (block || true), scope: scope.to_sym
end
allow_resource(resource, *actions, &block) click to toggle source
# File lib/admission/resource_arbitration.rb, line 80
def allow_resource resource, *actions, &block
  raise "reserved action name #{Admission::ALL_ACTION}" if actions.include? Admission::ALL_ACTION
  raise "block not given" unless block
  block.instance_variable_set :@resource_arbiter, true
  scope = case resource
    when Symbol then resource
    when Array then nested_scope(*resource)
    else type_to_scope(resource)
  end
  add_allowance_rule actions.flatten, block, scope: scope
end
create_index() click to toggle source
# File lib/admission/resource_arbitration.rb, line 100
def create_index
  index_instance = @rules.reduce Hash.new do |index, allowance|
    privilege = allowance[:privilege]
    actions = allowance[:actions]
    scope = allowance[:scope]
    arbiter = allowance[:arbiter]

    scope_index = (index[scope] ||= {})

    actions.each do |action|
      action_index = (scope_index[action] ||= {})
      action_index[privilege] = arbiter
    end

    index
  end

  index_instance.values.each do |h|
    h.values.each &:freeze
    h.freeze
  end
  index_instance.freeze
end
forbid(scope, *actions) click to toggle source
# File lib/admission/resource_arbitration.rb, line 74
def forbid scope, *actions
  raise "reserved action name #{Admission::ALL_ACTION}" if actions.include? Admission::ALL_ACTION
  raise "invalid scope name" unless scope.respond_to? :to_sym
  add_allowance_rule actions.flatten, :forbidden, scope: scope.to_sym
end
nested_scope(resource, scope) click to toggle source
# File lib/admission/resource_arbitration.rb, line 96
def nested_scope resource, scope
  Admission::ResourceArbitration.nested_scope resource, scope
end
type_to_scope(resource) click to toggle source
# File lib/admission/resource_arbitration.rb, line 92
def type_to_scope resource
  Admission::ResourceArbitration.type_to_scope resource
end