Class: Admission::ResourceArbitration::RulesBuilder

Inherits:
Arbitration::RulesBuilder show all
Defined in:
lib/admission/resource_arbitration.rb

Instance Attribute Summary

Attributes inherited from Arbitration::RulesBuilder

#privilege_order

Instance Method Summary collapse

Methods inherited from Arbitration::RulesBuilder

#add_allowance_rule, #initialize, #privilege

Constructor Details

This class inherits a constructor from Admission::Arbitration::RulesBuilder

Instance Method Details

#allow(scope, *actions, &block) ⇒ Object



63
64
65
66
67
# 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) ⇒ Object



69
70
71
72
# 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) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# 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_indexObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# 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) ⇒ Object



74
75
76
77
78
# 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) ⇒ Object



96
97
98
# File 'lib/admission/resource_arbitration.rb', line 96

def nested_scope resource, scope
  Admission::ResourceArbitration.nested_scope resource, scope
end

#type_to_scope(resource) ⇒ Object



92
93
94
# File 'lib/admission/resource_arbitration.rb', line 92

def type_to_scope resource
  Admission::ResourceArbitration.type_to_scope resource
end