Module: AccessManager::Rules

Defined in:
lib/access_manager/rules.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#access_treeObject

Returns the value of attribute access_tree.



3
4
5
# File 'lib/access_manager/rules.rb', line 3

def access_tree
  @access_tree
end

Instance Method Details

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

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/access_manager/rules.rb', line 33

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

  if @access_tree[controller.to_sym].nil?
    true
  elsif ((!@access_tree[controller.to_sym]['@all'].nil?) && @access_tree[controller.to_sym]['@all'].any?)
    @access_tree[controller.to_sym]['@all'].include?(user_action.to_sym)
  elsif @access_tree[controller.to_sym][action.to_sym].nil?
    true
  else
    @access_tree[controller.to_sym][action.to_sym].include?(user_action.to_sym)
  end
end

#grant_access_with(user_action, args = {}) ⇒ 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
# File 'lib/access_manager/rules.rb', line 5

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