Class: Thwart::RoleRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/thwart/role_registry.rb

Instance Method Summary collapse

Constructor Details

#initialize(role_creator = nil) ⇒ RoleRegistry

Returns a new instance of RoleRegistry.



64
65
66
67
# File 'lib/thwart/role_registry.rb', line 64

def initialize(role_creator = nil)
  self.monitor_builder(role_creator) if !role_creator.nil?
  self
end

Instance Method Details

#add(role) ⇒ Object

Raises:



11
12
13
14
# File 'lib/thwart/role_registry.rb', line 11

def add(role)
  raise DuplicateRoleError, "Role #{role} already exists in the role registry!" if self.has_role?(role)
  @roles << role
end

#find_actor_role(actor) ⇒ Object



57
58
59
60
61
62
# File 'lib/thwart/role_registry.rb', line 57

def find_actor_role(actor)
  r = actor.thwart_role if actor.respond_to?(:thwart_role)
  r = r.to_sym if r.respond_to?(:to_sym)
  r = find_role(r) if r.is_a?(Symbol)
  r
end

#find_role(name) ⇒ Object



81
82
83
# File 'lib/thwart/role_registry.rb', line 81

def find_role(name)
  self.roles.find {|a| a.name == name}
end

#has_role?(role) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/thwart/role_registry.rb', line 53

def has_role?(role)
  self.roles.include?(role)
end

#monitor_builder(role_creator) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/thwart/role_registry.rb', line 69

def monitor_builder(role_creator)
  registry = self
  unless role_creator.nil?
    role_creator.class.set_callback :build_role, :after do |object|
      registry.add(object.last_built_role)
    end
  else
    @role_creator.class.reset_callbacks(:build_role)
  end
  @role_creator = role_creator
end

#query(actor, resource, action) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/thwart/role_registry.rb', line 16

def query(actor, resource, action)    
  role = self.find_actor_role(actor)
  if Thwart.log_query_path
    Thwart.last_query_path = [] 
    Thwart.last_query_path.push({:actor => actor, :resource => resource, :action => action})
    name = resource.thwart_name if resource.respond_to?(:thwart_name)
    name ||= resource
    Thwart.last_query_path.push({:actor_role => role.name, :resource_name => resource})
  end
  
  if role.nil? || !self.has_role?(role) 
    raise MissingRoleError, "Role #{role} could not be found in the registry!" if Thwart.actor_must_play_role
  else
    q = [role]
    while r = q.shift
      resp = r.query(actor, resource, action)
      
      if Thwart.log_query_path
        Thwart.last_query_path.push("Querying #{r.name}")
        Thwart.last_query_path.push(r)
        Thwart.last_query_path.push("Response: #{resp}")
      end
      
      if resp != nil
        return resp # positive/negative response from the role, a rule governs the role on this query
      else
        q = q | r.parents.map do |a| 
          a = self.find_role(a) if a.is_a?(Symbol)
          a 
        end # add this roles parents to the query queue
      end
    end
  end

  Thwart.default_query_response # return was not called above, return the default
end

#rolesObject



6
7
8
9
# File 'lib/thwart/role_registry.rb', line 6

def roles
  @roles ||= []
  @roles
end