Module: Bizside::Acl::AvailableHelper

Included in:
ControllerHelper
Defined in:
lib/bizside/acl/available_helper.rb

Instance Method Summary collapse

Instance Method Details

#available_for(controller_name, action_name = nil, params = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
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
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bizside/acl/available_helper.rb', line 5

def available_for(controller_name, action_name = nil, params = {})
  controller_name = controller_name[1..-1] if controller_name.start_with?('/')
  action_name ||= 'index'
  message = "ACL: #{controller_name}##{action_name} => "

  Bizside::Acl::AccessControlUtils::role_keys.each do |role_key|
    access_control = Bizside::Acl::AccessControlUtils::get_access_control(role_key)

    # コントローラの定義を取得
    controller_value = access_control[controller_name]
    next unless controller_value.present?

    # アクションの定義を取得
    if controller_value.is_a?(String)
      action_value = controller_value
    elsif controller_value.is_a?(Array)
      action_value = controller_value
    elsif controller_value.is_a?(TrueClass)
      action_value = true
    elsif controller_value.is_a?(FalseClass)
      action_value = false
    else
      action_value = controller_value[action_name]
      action_value = controller_value['index'] if action_value.nil?
      if action_value.is_a?(Hash)
        action_value = controller_value[action_value['same_as']] if action_value['same_as'].present?
      end
    end

    next if action_value.nil?

    # アクセス定義を確認
    role = eval(role_key)
    if role
      if action_value.is_a?(String)
        ret = role.instance_eval(action_value)
      elsif action_value.is_a?(Array)
        action_value.each do |judge|
          ret = role.instance_eval(judge)
          break unless ret
        end
      elsif action_value.is_a?(FalseClass)
        ret = false
      else
        ret = true
      end
    else
      ret = false
    end

    unless ret      
      Bizside.logger.debug message + 'false'
      return false
    end

  end

  Bizside.logger.debug message + 'true'
  true
end