Class: Aegis::PermissionEvaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/aegis/permission_evaluator.rb

Instance Method Summary collapse

Constructor Details

#initialize(role) ⇒ PermissionEvaluator

Returns a new instance of PermissionEvaluator.



4
5
6
# File 'lib/aegis/permission_evaluator.rb', line 4

def initialize(role)
  @role = role
end

Instance Method Details

#allow(*role_name_or_names, &block) ⇒ Object



16
17
18
# File 'lib/aegis/permission_evaluator.rb', line 16

def allow(*role_name_or_names, &block)
  rule_encountered(role_name_or_names, true, &block)
end

#deny(*role_name_or_names, &block) ⇒ Object



20
21
22
# File 'lib/aegis/permission_evaluator.rb', line 20

def deny(*role_name_or_names, &block)
  rule_encountered(role_name_or_names, false, &block)
end

#evaluate(permissions, rule_args) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/aegis/permission_evaluator.rb', line 8

def evaluate(permissions, rule_args)
  @result = @role.allow_by_default?
  permissions.each do |permission|
	instance_exec(*rule_args, &permission)
  end
  @result
end

#rule_encountered(role_name_or_names, is_allow, &block) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/aegis/permission_evaluator.rb', line 24

def rule_encountered(role_name_or_names, is_allow, &block)
  role_names = Array(role_name_or_names)
  if role_names.include?(@role.name) || role_names.include?(Aegis::Constants::EVERYONE_ROLE_NAME)
	@result = (block ? block.call : true) 
	@result = !@result unless is_allow
  end
end