Sha256: 675396634f7c71cd05f2e7aa8e56ec79529b52d2edce16436ad58c2cc7d434d2

Contents?: true

Size: 1.17 KB

Versions: 57

Compression:

Stored size: 1.17 KB

Contents

module Devise
  class ParameterFilter
    def initialize(case_insensitive_keys, strip_whitespace_keys)
      @case_insensitive_keys = case_insensitive_keys || []
      @strip_whitespace_keys = strip_whitespace_keys || []
    end

    def filter(conditions)
      conditions = stringify_params(conditions.dup)

      conditions.merge!(filtered_hash_by_method_for_given_keys(conditions.dup, :downcase, @case_insensitive_keys))
      conditions.merge!(filtered_hash_by_method_for_given_keys(conditions.dup, :strip, @strip_whitespace_keys))

      conditions
    end

    def filtered_hash_by_method_for_given_keys(conditions, method, condition_keys)
      condition_keys.each do |k|
        value = conditions[k]
        conditions[k] = value.send(method) if value.respond_to?(method)
      end

      conditions
    end

    # Force keys to be string to avoid injection on mongoid related database.
    def stringify_params(conditions)
      return conditions unless conditions.is_a?(Hash)
      conditions.each do |k, v|
        conditions[k] = v.to_s if param_requires_string_conversion?(v)
      end
    end

    private

    def param_requires_string_conversion?(value)
      true
    end
  end
end

Version data entries

57 entries across 57 versions & 9 rubygems

Version Path
devise-4.3.0 lib/devise/parameter_filter.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/devise-4.2.1/lib/devise/parameter_filter.rb
devise-4.2.1 lib/devise/parameter_filter.rb
devise-4.2.0 lib/devise/parameter_filter.rb
devise-3.5.10 lib/devise/parameter_filter.rb
devise-4.0.3 lib/devise/parameter_filter.rb
devise-4.1.1 lib/devise/parameter_filter.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/devise-4.1.0/lib/devise/parameter_filter.rb
devise-4.1.0 lib/devise/parameter_filter.rb
devise-3.5.9 lib/devise/parameter_filter.rb
devise-4.0.2 lib/devise/parameter_filter.rb
devise-4.0.1 lib/devise/parameter_filter.rb
devise-3.5.8 lib/devise/parameter_filter.rb
devise-3.5.7 lib/devise/parameter_filter.rb
devise-4.0.0 lib/devise/parameter_filter.rb
devise-4.0.0.rc2 lib/devise/parameter_filter.rb
devise-4.0.0.rc1 lib/devise/parameter_filter.rb
devise-3.5.6 lib/devise/parameter_filter.rb
devise-3.5.5 lib/devise/parameter_filter.rb
devise-3.5.4 lib/devise/parameter_filter.rb