Class: EY::GateKeeper::AccessControlList

Inherits:
Object
  • Object
show all
Defined in:
lib/ey_gatekeeper/access_control_list.rb

Overview

A list of AccessControl objects. Can check a request method and path against all controls in the list. Example: AccessControlList.new({ ‘xdna://foobars’ => [‘GET’, ‘PUT’, ‘DELETE’], ‘xdna://foobazes’ => [] })

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(list) ⇒ AccessControlList

Returns a new instance of AccessControlList.



61
62
63
64
65
66
67
68
# File 'lib/ey_gatekeeper/access_control_list.rb', line 61

def initialize(list)
  @list = case list
            when Hash
              list.map {|path, methods| AccessControl.new(path, methods) }
            when Array
              list
            end
end

Instance Attribute Details

#listObject (readonly)

Returns the value of attribute list.



59
60
61
# File 'lib/ey_gatekeeper/access_control_list.rb', line 59

def list
  @list
end

Instance Method Details

#&(other_list) ⇒ Object

Intersect the two lists



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ey_gatekeeper/access_control_list.rb', line 93

def &(other_list)
  new_list = (paths + other_list.paths).uniq.map do |path|
    if self[path] && other_list[path]
      # If both lists have the path, do a simple control AND
      self[path] & other_list[path]
    elsif self[path]
      # If only one side has the path, see if there's a less specific control we can AND with
      pare_down(self[path], other_list)
    elsif other_list[path]
      # Same for here, just opposite
      pare_down(other_list[path], self)
    end
  end.compact

  AccessControlList.new(new_list)
end

#[](path) ⇒ Object



88
89
90
# File 'lib/ey_gatekeeper/access_control_list.rb', line 88

def [](path)
  list.detect {|c| c.path == path }
end

#allow?(method, path) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
# File 'lib/ey_gatekeeper/access_control_list.rb', line 70

def allow?(method, path)
  control = control_for(path)
  control && control.allow?(method.to_s.upcase)
end

#control_for(path) ⇒ Object



79
80
81
82
# File 'lib/ey_gatekeeper/access_control_list.rb', line 79

def control_for(path)
  matches = list.find_all {|c| c.suitable_for?(path) }
  matches.sort_by {|c| c.path.size }.last
end

#deny?(method, path) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/ey_gatekeeper/access_control_list.rb', line 75

def deny?(method, path)
  !allow?(method, path)
end

#pathsObject



84
85
86
# File 'lib/ey_gatekeeper/access_control_list.rb', line 84

def paths
  list.map {|c| c.path }
end

#to_hashObject



110
111
112
113
114
115
116
# File 'lib/ey_gatekeeper/access_control_list.rb', line 110

def to_hash
  {}.tap do |result|
    list.each do |control|
      result.update(control.path => control.methods)
    end
  end
end