Module: Bali::Statics::Authorizer::HelperFunctions

Extended by:
HelperFunctions
Included in:
HelperFunctions
Defined in:
lib/bali/statics/authorizer.rb

Instance Method Summary collapse

Instance Method Details

#check(term, obj, arg1, arg2, arg3) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/bali/statics/authorizer.rb', line 37

def check(term, obj, arg1, arg2, arg3)
  # try to infer current user if only passing one arg
  if arg2.nil? && arg3.nil? && obj.respond_to?(:current_user)
    arg2 = arg1
    arg1 = obj.current_user
  elsif arg3.nil? && obj.respond_to?(:current_user)
    arg3 = arg2
    arg2 = arg1
    arg1 = obj.current_user
  end

  arg3 = HelperFunctions.determine_model_class! obj, arg1, arg2, arg3

  actor = HelperFunctions.find_actor(arg1, arg2, arg3)
  operation = HelperFunctions.find_operation(arg1, arg2, arg3)
  record = HelperFunctions.find_record(arg1, arg2, arg3)

  Bali::Judge.check(term, actor, operation, record)
end

#determine_model_class!(obj, arg1, arg2, arg3) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/bali/statics/authorizer.rb', line 29

def determine_model_class!(obj, arg1, arg2, arg3)
  if arg2.nil? && arg3.nil? && !obj.respond_to?(:model_class)
    raise Bali::Error, "Cannot perform checking when the actor is not known"
  end
  arg3 = obj.model_class if (arg2.nil? || arg1.nil?) && arg3.nil?
  arg3
end

#find_actor(actor, operation, record = nil) ⇒ Object



9
10
11
# File 'lib/bali/statics/authorizer.rb', line 9

def find_actor(actor, operation, record = nil)
  return actor unless not_true_actor?(actor)
end

#find_operation(actor, operation, record = nil) ⇒ Object



13
14
15
16
17
# File 'lib/bali/statics/authorizer.rb', line 13

def find_operation(actor, operation, record = nil)
  not_true_actor?(actor) ?
    actor :
    operation
end

#find_record(actor, operation, record = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/bali/statics/authorizer.rb', line 19

def find_record(actor, operation, record = nil)
  if not_true_actor?(actor) && record.nil?
    operation
  elsif actor.is_a?(ActiveRecord::Base) && record.nil?
    actor.class
  else
    record
  end
end

#not_true_actor?(actor) ⇒ Boolean



5
6
7
# File 'lib/bali/statics/authorizer.rb', line 5

def not_true_actor?(actor)
  Symbol === actor || String === actor
end