class RightOn::RightAllowed
Constants
- APPLICABLE_RIGHTS
- CHANGE_ACTIONS
- VIEW_ACTIONS
Attributes
rights[RW]
Public Class Methods
[](name)
click to toggle source
# File lib/right_on/right_allowed.rb, line 59 def self.[](name) @rights = cache.read('Right.all') || calculate_and_write_cache @rights[name] end
cache()
click to toggle source
# File lib/right_on/right_allowed.rb, line 46 def self.cache @@cache ||= Rails.cache end
cache=(cache)
click to toggle source
# File lib/right_on/right_allowed.rb, line 50 def self.cache=(cache) @@cache = cache end
clear_cache()
click to toggle source
# File lib/right_on/right_allowed.rb, line 54 def self.clear_cache cache.delete('Right.all') end
new(controller, action)
click to toggle source
# File lib/right_on/right_allowed.rb, line 3 def initialize(controller, action) @controller = controller @action = action end
Private Class Methods
calculate_and_write_cache()
click to toggle source
# File lib/right_on/right_allowed.rb, line 65 def self.calculate_and_write_cache right_cache = Hash[RightOn::Right.all.map{|r|[r.name, r.id]}] cache.write('Right.all', right_cache) or raise RuntimeError, "Could not cache rights" right_cache end
Public Instance Methods
action_permitted?(action)
click to toggle source
# File lib/right_on/right_allowed.rb, line 35 def action_permitted?(action) case action.to_sym when :change CHANGE_ACTIONS.include?(@action) when :view VIEW_ACTIONS.include?(@action) else action == @action end end
allowed?(right)
click to toggle source
# File lib/right_on/right_allowed.rb, line 8 def allowed?(right) return false unless right.controller == @controller if right.action action_permitted?(right.action) else # right without action works if no specific right exists # e.g. can't edit if there's a edit or change right defined # as you must used that specific right specific_rights = Array(APPLICABLE_RIGHTS[@action.to_sym]) + [@action] specific_rights.all?{|action| self.class["#{@controller}##{action}"].nil?} end end