Class: RabbitCage::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/rabbitcage/filter.rb

Class Method Summary collapse

Class Method Details

.buildObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rabbitcage/filter.rb', line 66

def self.build
  require 'erb'
  method = ERB.new(%q[
  def filter(frame)
<%- @rules[:any].each do |rule| -%>
  return :<%= rule[:permission]  %> if <%= rule[:properties] %>
<%- end -%>
<%- @rules.delete(:any) -%>
return :allow if (frame.class == AMQP::Protocol::Header || frame.class == String)
<%- if @rules.any? -%>
  
  case frame  # 4
    <%- @rules.each_pair do |klass, r|-%>
      <%-  next if klass == :any -%>
      when <%= klass %>
        <%- r.each do |rule| -%>
          return :<%= rule[:permission]  %> if <%= rule[:properties] %>
        <%- end -%>
    <%- end -%>
  end
<%- end -%>
:<%= @default %>
  end
  ].gsub!(/^  /,''), nil, '>-%').result(binding)  
LOGGER.debug "Generated filter method:\n#{method}"
RabbitCage::ClientConnection.class_eval method
  
end

.generate_properties(properties, object) ⇒ Object



58
59
60
# File 'lib/rabbitcage/filter.rb', line 58

def self.generate_properties(properties, object)
  properties
end

.register(permission, user, action, object, properties) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rabbitcage/filter.rb', line 20

def self.register permission, user, action, object, properties
  available_actions = {:queue => ['Declare', 'Bind', 'Unbind', 'Delete', 'Purge'],
                       :connection => ['Start', 'StartOk', 'Open', 'Tune', 'TuneOk', 'Close', 'Redirect', 'Secure'],
                       :channel => ['Open', 'Flow', 'Alert', 'Close', 'CloseOk'],
                       :exchange => ['Declare', 'Delete'],
                       :access => ['Request'],
                       :basic => ['Qos', 'Consume', 'Cancel', 'Publish', 'Deliver', 'Get', 'Ack', 'Reject']
                     }
  klass = []
  if action.is_a? Array
    klass = action.collect { |a| "AMQP::Protocol::#{object.camelize}::#{a.camelize}" }.join(',')
  else
    if action == :all && object != :all
      klass = available_actions[object].collect { |x| "AMQP::Protocol::#{object.camelize}::#{x}" }
    elsif action == :all && object == :all
      klass = :any
    elsif action != :all && object == :all
      raise "This acl format is currently not supported"
      exit
    else
      klass = "AMQP::Protocol::#{object.camelize}::#{action.camelize}"
    end
  end
  klass = klass.join(', ') if klass.class == Array
  @rules[klass] = [] unless @rules[klass].class == Array
  
  name = properties.delete(:name)
  properties[object] = name if name
  cond = []
  cond << ('@user =' << (user.is_a?(Regexp) ? "~ #{user.inspect}" : "= '#{user}'"))  if user != :all
    
  properties.each_pair do |p, value|
    cond << ("frame.#{p} =" << (value.is_a?(Regexp) ? "~ #{value.inspect}" : "= '#{value}'"))
  end
  
  @rules[klass] << {:permission => permission, :user => user, :properties => cond.join(' and ') }
end

.set_default(action) ⇒ Object



62
63
64
# File 'lib/rabbitcage/filter.rb', line 62

def self.set_default action
  @default = action
end