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) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #as_json(opts = {}) ⇒ Object
- #condense ⇒ Object
- #contains?(resource, namespace = nil, scope = nil) ⇒ Boolean
- #empty? ⇒ Boolean
- #except(*keys) ⇒ Object
- #except!(*keys) ⇒ Object
-
#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 |
# File 'lib/prx_auth/resource_map.rb', line 5 def initialize(mapped_values) super() do |hash, key| if key == WILDCARD_KEY @wildcard 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) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/prx_auth/resource_map.rb', line 99 def &(other) result = {} other_wildcard = other.list_for_resource(WILDCARD_KEY) (resources + other.resources).uniq.each do |res| left = list_for_resource(res) right = other.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 || other_wildcard.length > 0 result[WILDCARD_KEY] = @wildcard & other_wildcard end ResourceMap.new(result).condense end |
#+(other) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/prx_auth/resource_map.rb', line 67 def +(other) result = {} (resources + other.resources + [WILDCARD_KEY]).uniq.each do |resource| list_a = list_for_resource(resource) list_b = other.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) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/prx_auth/resource_map.rb', line 84 def -(other) result = {} other_wildcard = other.list_for_resource(WILDCARD_KEY) || PrxAuth::ScopeList.new("") resources.each do |resource| result[resource] = list_for_resource(resource) - (other_wildcard + other.list_for_resource(resource)) end if @wildcard.length result[WILDCARD_KEY] = @wildcard - other_wildcard end ResourceMap.new(result) end |
#[](key) ⇒ Object
38 39 40 |
# File 'lib/prx_auth/resource_map.rb', line 38 def [](key) super(key.to_s) end |
#[]=(key, value) ⇒ Object
42 43 44 |
# File 'lib/prx_auth/resource_map.rb', line 42 def []=(key, value) super(key.to_s, value) end |
#as_json(opts = {}) ⇒ Object
123 124 125 |
# File 'lib/prx_auth/resource_map.rb', line 123 def as_json(opts = {}) super.merge((@wildcard.length > 0) ? {WILDCARD_KEY => @wildcard}.as_json(opts) : {}) end |
#condense ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/prx_auth/resource_map.rb', line 59 def condense condensed_wildcard = @wildcard.condense condensed_map = map do |resource, list| [resource, (list - condensed_wildcard).condense] end.to_h ResourceMap.new(condensed_map.merge(WILDCARD_KEY => condensed_wildcard)) end |
#contains?(resource, namespace = nil, scope = nil) ⇒ Boolean
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/prx_auth/resource_map.rb', line 18 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 |
#empty? ⇒ Boolean
55 56 57 |
# File 'lib/prx_auth/resource_map.rb', line 55 def empty? @wildcard.empty? && (super || values.all?(&:empty?)) end |
#except(*keys) ⇒ Object
51 52 53 |
# File 'lib/prx_auth/resource_map.rb', line 51 def except(*keys) dup.except!(*keys) end |
#except!(*keys) ⇒ Object
46 47 48 49 |
# File 'lib/prx_auth/resource_map.rb', line 46 def except!(*keys) keys.each { |key| delete(key.to_s) } self end |
#resources(namespace = nil, scope = nil) ⇒ Object
127 128 129 130 131 132 133 134 135 |
# File 'lib/prx_auth/resource_map.rb', line 127 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 |