lib/role_requirement_system.rb
# Add this to the top of your controller to require a role in order to access it.
# Example Usage:
# 
#    require_role "contractor"
#    require_role "admin", :only => :destroy # don't allow contractors to destroy
#    require_role "admin", :only => :update, :unless => "current_user.authorized_for_listing?(params[:id]) "
#
# Valid options
#
#  * :only - Only require the role for the given actions
#  * :except - Require the role for everything but 
#  * :if - a Proc or a string to evaluate.  If it evaluates to true, the role is required.
#  * :unless - The inverse of :if
#    
Add this to the top of your controller to require a role in order to access it.
Example Usage.



Valid options

:only - Only require the role for the given actions
:except - Require the role for everything but
:if - a Proc or a string to evaluate.  If it evaluates to true, the role is required.
:unless - The inverse of :if
Add this to the top of your controller to require a role in order to access it . Example Usage . Valid options : only - Only require the role for the given actions : except - Require the role for everything but : if - a Proc or a string to evaluate . If it evaluates to true the role is required . : unless - The inverse of : if
def require_role(roles, options = {})
      options.assert_valid_keys(:if, :unless,
        :for, :only, 
        :for_all_except, :except
      )
      logger.debug "===debug==="
      logger.debug roles
      logger.debug "===debug==="
      unless (@before_action_declared||=false)
        @before_action_declared=true
        before_action :check_roles
      end
      roles = [roles] unless Array===roles
      options[:only] ||= options[:for] if options[:for]
      options[:except] ||= options[:for_all_except] if options[:for_all_except]
      for key in [:only, :except]
        if options.has_key?(key)
          options[key] = [options[key]] unless Array === options[key]
          options[key] = options[key].compact.collect{|v| v.to_sym}
        end 
      end
      self.role_requirements||=[]
      self.role_requirements << {:roles => roles, :options => options }
    end
def require_role ( roles , options = { } ) options . assert_valid_keys ( :if , :unless , :for , :only , :for_all_except , :except ) logger . debug "===debug===" logger . debug roles logger . debug "===debug===" unless ( @before_action_declared ||= false ) @before_action_declared = true before_action :check_roles end roles = [ roles ] unless Array === roles options [ :only ] ||= options [ :for ] if options [ :for ] options [ :except ] ||= options [ :for_all_except ] if options [ :for_all_except ] for key in [ :only , :except ] if options . has_key? ( key ) options [ key ] = [ options [ key ] ] unless Array === options [ key ] options [ key ] = options [ key ] . compact . collect { | v | v . to_sym } end end self . role_requirements ||= [ ] self . role_requirements << { :roles => roles , :options => options } end
Add this to the top of your controller to require a role in order to access it.
Add this to the top of your controller to require a role in order to access it .
# only declare that before filter once # convert to an array if it isn't already # convert any actions into symbols
roles
<not_specific>
options
<not_specific>