Class: PrxAuth::ResourceMap

Inherits:
Hash
  • 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
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



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/prx_auth/resource_map.rb', line 88

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/prx_auth/resource_map.rb', line 56

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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/prx_auth/resource_map.rb', line 73

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

#[](key) ⇒ Object



40
41
42
# File 'lib/prx_auth/resource_map.rb', line 40

def [](key)
  super(key.to_s)
end

#[]=(key, value) ⇒ Object



44
45
46
# File 'lib/prx_auth/resource_map.rb', line 44

def []=(key, value)
  super(key.to_s, value)
end

#as_json(opts = {}) ⇒ Object



112
113
114
# File 'lib/prx_auth/resource_map.rb', line 112

def as_json(opts={})
  super(opts).merge(@wildcard.length > 0 ? {WILDCARD_KEY => @wildcard}.as_json(opts) : {})
end

#condenseObject



48
49
50
51
52
53
54
# File 'lib/prx_auth/resource_map.rb', line 48

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

Returns:

  • (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



116
117
118
119
120
121
122
123
124
# File 'lib/prx_auth/resource_map.rb', line 116

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