Class: PrxAuth::ResourceMap

Inherits:
Object
  • Object
show all
Defined in:
lib/prx_auth/resource_map.rb

Constant Summary collapse

WILDCARD_KEY =
'*'

Instance Method Summary collapse

Constructor Details

#initialize(mapped_values) ⇒ ResourceMap

Returns a new instance of ResourceMap.



5
6
7
8
9
10
11
# File 'lib/prx_auth/resource_map.rb', line 5

def initialize(mapped_values)
  input = mapped_values.clone
  @wildcard = ScopeList.new(input.delete(WILDCARD_KEY)||'')
  @map = Hash[input.map do |(key, values)|
    [key, ScopeList.new(values)]
  end]
end

Instance Method Details

#contains?(resource, namespace = nil, scope = nil) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/prx_auth/resource_map.rb', line 13

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 = @map[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

#freezeObject



33
34
35
36
37
# File 'lib/prx_auth/resource_map.rb', line 33

def freeze
  @map.freeze
  @wildcard.freeze
  self
end

#resources(namespace = nil, scope = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/prx_auth/resource_map.rb', line 39

def resources(namespace=nil, scope=nil)
  if namespace.nil?
    @map.keys
  else
    @map.select do |name, list|
      list.contains?(namespace, scope)
    end.map(&:first)
  end
end