lib/role_requirement_system.rb
# This is the core of RoleRequirement.  Here is where it discerns if a user can access a controller or not./
This is the core of RoleRequirement.  Here is where it discerns if a user can access a controller or not.
This is the core of RoleRequirement . Here is where it discerns if a user can access a controller or not .
def user_authorized_for?(user, params = {}, binding = self.binding)
      return true unless Array===self.role_requirements
      self.role_requirements.each{| role_requirement|
        roles = role_requirement[:roles]
        options = role_requirement[:options]
        if options.has_key?(:only)
          next unless options[:only].include?( (params[:action]||"index").to_sym )
        end
        if options.has_key?(:except)
          next if options[:except].include?( (params[:action]||"index").to_sym)
        end
        if options.has_key?(:if)
          next unless ( String===options[:if] ? eval(options[:if], binding) : options[:if].call(params) )
        end
        if options.has_key?(:unless)
          next if ( String===options[:unless] ? eval(options[:unless], binding) : options[:unless].call(params) )
        end
        passed = false 
        logger.debug "====roles==="
        roles.each { |role|
          logger.debug "=====has_role====="
          logger.debug user.inspect
          logger.debug role
          logger.debug user.has_role?(role)
          logger.debug "===end has_role==="
          passed = true if user.has_role?(role)
        } unless (! user || user==:false)
        return false unless passed
      }
      return true
    end
def user_authorized_for? ( user , params = { } , binding = self . binding ) return true unless Array === self . role_requirements self . role_requirements . each { | role_requirement | roles = role_requirement [ :roles ] options = role_requirement [ :options ] if options . has_key? ( :only ) next unless options [ :only ] . include? ( ( params [ :action ] || "index" ) . to_sym ) end if options . has_key? ( :except ) next if options [ :except ] . include? ( ( params [ :action ] || "index" ) . to_sym ) end if options . has_key? ( :if ) next unless ( String === options [ :if ] ? eval ( options [ :if ] , binding ) : options [ :if ] . call ( params ) ) end if options . has_key? ( :unless ) next if ( String === options [ :unless ] ? eval ( options [ :unless ] , binding ) : options [ :unless ] . call ( params ) ) end passed = false logger . debug "====roles===" roles . each { | role | logger . debug "=====has_role=====" logger . debug user . inspect logger . debug role logger . debug user . has_role? ( role ) logger . debug "===end has_role===" passed = true if user . has_role? ( role ) } unless ( ! user || user == :false ) return false unless passed } return true end
This is the core of RoleRequirement.
This is the core of RoleRequirement .
# do the options match the params? # check the action # execute the proc.  if the procedure returns false, we don't need to authenticate these roles # execute the proc.  if the procedure returns true, we don't need to authenticate these roles # check to see if they have one of the required roles
user
<not_specific>
params
<not_specific>
binding
<not_specific>