Module: AccessManager::Control::ClassMethods

Defined in:
lib/access_manager/control.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#access_treeObject

Returns the value of attribute access_tree.



16
17
18
# File 'lib/access_manager/control.rb', line 16

def access_tree
  @access_tree
end

Instance Method Details

#access_granted?(controller, action, user_action) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/access_manager/control.rb', line 46

def access_granted?(controller, action, user_action)
  if @access_tree.nil?
    @access_tree = { }
  end

  controller_grants = @access_tree[controller.to_sym]

  if controller_grants.nil?
    false
  elsif controller_grants['@all'] && controller_grants['@all'].include?(user_action.to_sym)
    true
  elsif controller_grants[action.to_sym].nil?
    false
  else
    controller_grants[action.to_sym].include?(user_action.to_sym)
  end
end

#grant_access_with(user_action, args = {}) ⇒ Object



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
# File 'lib/access_manager/control.rb', line 18

def grant_access_with(user_action, args={})
  if @access_tree.nil?
    @access_tree = { }
  end

  args[:to].each do |controller, actions|
    if @access_tree[controller.to_sym].nil?
      @access_tree[controller.to_sym] = { }
    end

    if actions == :all
      if @access_tree[controller.to_sym]['@all'].nil?
        @access_tree[controller.to_sym]['@all'] = []
      end

      @access_tree[controller.to_sym]['@all'] << user_action
    else
      actions.each do |action|
        if @access_tree[controller.to_sym][action.to_sym].nil?
          @access_tree[controller.to_sym][action.to_sym] = []
        end

        @access_tree[controller.to_sym][action.to_sym] << user_action
      end
    end
  end
end