Module: AccessManager::Control
- Defined in:
- lib/access_manager/control.rb
Instance Method Summary collapse
- #access_granted?(controller, action, user_action) ⇒ Boolean
- #can?(action) ⇒ Boolean
- #can_access?(controller, action) ⇒ Boolean
- #grant_access_with(user_action, args = {}) ⇒ Object
Instance Method Details
#access_granted?(controller, action, user_action) ⇒ Boolean
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/control.rb', line 11 def access_granted?(controller, action, user_action) if @access_tree.nil? set_grants! end if @access_tree.nil? || @access_tree.empty? return false 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 |
#can?(action) ⇒ Boolean
7 8 9 |
# File 'lib/access_manager/control.rb', line 7 def can?(action) action_codes.map(&:to_s).include?(action.to_s) end |
#can_access?(controller, action) ⇒ Boolean
3 4 5 |
# File 'lib/access_manager/control.rb', line 3 def can_access?(controller, action) action_codes.any? { |user_action| access_granted?(controller, action, user_action) } end |
#grant_access_with(user_action, args = {}) ⇒ Object
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 |
# File 'lib/access_manager/control.rb', line 33 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 |