Class: PrxAuth::ResourceMap
- Inherits:
-
Hash
- Object
- Hash
- PrxAuth::ResourceMap
- Defined in:
- lib/prx_auth/resource_map.rb
Constant Summary collapse
- WILDCARD_KEY =
'*'
Instance Method Summary collapse
- #&(other_map) ⇒ Object
- #+(other_map) ⇒ Object
- #-(other_map) ⇒ Object
- #as_json(opts = {}) ⇒ Object
- #condense ⇒ Object
- #contains?(resource, namespace = nil, scope = nil) ⇒ Boolean
-
#initialize(mapped_values) ⇒ ResourceMap
constructor
A new instance of ResourceMap.
- #resources(namespace = nil, scope = nil) ⇒ Object
Constructor Details
#initialize(mapped_values) ⇒ ResourceMap
Returns a new instance of ResourceMap.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/prx_auth/resource_map.rb', line 5 def initialize(mapped_values) super() do |hash, key| if key == WILDCARD_KEY @wildcard else nil end end input = mapped_values.clone @wildcard = ScopeList.new(input.delete(WILDCARD_KEY)||'') input.each do |(key, values)| self[key.to_s] = ScopeList.new(values) end end |
Instance Method Details
#&(other_map) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/prx_auth/resource_map.rb', line 80 def &(other_map) result = {} other_wildcard = other_map.list_for_resource(WILDCARD_KEY) (resources + other_map.resources).uniq.each do |res| left = list_for_resource(res) right = other_map.list_for_resource(res) result[res] = if left.nil? right & @wildcard elsif right.nil? left & other_wildcard else (left + @wildcard) & (right + other_wildcard) end end if @wildcard.length > 0 result[WILDCARD_KEY] = @wildcard - (@wildcard - other_wildcard) end ResourceMap.new(result).condense end |
#+(other_map) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/prx_auth/resource_map.rb', line 48 def +(other_map) result = {} (resources + other_map.resources + [WILDCARD_KEY]).uniq.each do |resource| list_a = list_for_resource(resource) list_b = other_map.list_for_resource(resource) result[resource] = if list_a.nil? list_b elsif list_b.nil? list_a else list_a + list_b end end ResourceMap.new(result).condense end |
#-(other_map) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/prx_auth/resource_map.rb', line 65 def -(other_map) result = {} other_wildcard = other_map.list_for_resource(WILDCARD_KEY) || PrxAuth::ScopeList.new('') resources.each do |resource| result[resource] = list_for_resource(resource) - (other_wildcard + other_map.list_for_resource(resource)) end if @wildcard.length result[WILDCARD_KEY] = @wildcard - other_wildcard end ResourceMap.new(result) end |
#as_json(opts = {}) ⇒ Object
104 105 106 |
# File 'lib/prx_auth/resource_map.rb', line 104 def as_json(opts={}) super(opts).merge(@wildcard.length > 0 ? {WILDCARD_KEY => @wildcard}.as_json(opts) : {}) end |
#condense ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/prx_auth/resource_map.rb', line 40 def condense condensed_wildcard = @wildcard.condense condensed_map = Hash[map do |resource, list| [resource, (list - condensed_wildcard).condense] end] ResourceMap.new(condensed_map.merge(WILDCARD_KEY => condensed_wildcard)) end |
#contains?(resource, namespace = nil, scope = nil) ⇒ Boolean
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/prx_auth/resource_map.rb', line 20 def contains?(resource, namespace=nil, scope=nil) resource = resource.to_s if resource == WILDCARD_KEY raise ArgumentError if namespace.nil? @wildcard.contains?(namespace, scope) else mapped_resource = self[resource] if mapped_resource && !namespace.nil? mapped_resource.contains?(namespace, scope) || @wildcard.contains?(namespace, scope) elsif !namespace.nil? @wildcard.contains?(namespace, scope) else !!mapped_resource end end end |
#resources(namespace = nil, scope = nil) ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/prx_auth/resource_map.rb', line 108 def resources(namespace=nil, scope=nil) if namespace.nil? keys else select do |name, list| list.contains?(namespace, scope) || @wildcard.contains?(namespace, scope) end.map(&:first) end end |