2012-10-12 09:37:39 +00:00
|
|
|
module Draper
|
|
|
|
class Security
|
|
|
|
def initialize
|
|
|
|
@allowed = []
|
|
|
|
@denied = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def denies(*methods)
|
2012-11-13 20:57:20 +00:00
|
|
|
raise ArgumentError, "Specify at least one method to blacklist when using denies" if methods.empty?
|
2012-10-12 09:37:39 +00:00
|
|
|
self.strategy = :denies
|
2012-11-13 20:57:20 +00:00
|
|
|
@denied += methods.map(&:to_sym)
|
2012-10-12 09:37:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def denies_all
|
|
|
|
self.strategy = :denies_all
|
|
|
|
end
|
|
|
|
|
|
|
|
def allows(*methods)
|
2012-11-13 20:57:20 +00:00
|
|
|
raise ArgumentError, "Specify at least one method to whitelist when using allows" if methods.empty?
|
2012-10-12 09:37:39 +00:00
|
|
|
self.strategy = :allows
|
2012-11-13 20:57:20 +00:00
|
|
|
@allowed += methods.map(&:to_sym)
|
2012-10-12 09:37:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def allow?(method)
|
|
|
|
case strategy
|
|
|
|
when :allows
|
|
|
|
allowed.include?(method)
|
|
|
|
when :denies, nil
|
|
|
|
!denied.include?(method)
|
|
|
|
when :denies_all
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :allowed, :denied, :strategy
|
|
|
|
|
|
|
|
def strategy=(strategy)
|
|
|
|
@strategy ||= strategy
|
|
|
|
raise ArgumentError, "Use only one of 'allows', 'denies', or 'denies_all'." unless @strategy == strategy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|