2013-03-13 12:37:54 -04:00
|
|
|
module Devise
|
2013-04-10 11:33:50 -04:00
|
|
|
class BaseSanitizer
|
2013-04-14 02:21:46 -04:00
|
|
|
attr_reader :params, :resource_name, :resource_class
|
2013-03-13 12:37:54 -04:00
|
|
|
|
2013-04-14 02:21:46 -04:00
|
|
|
def initialize(resource_class, resource_name, params)
|
|
|
|
@resource_class = resource_class
|
|
|
|
@resource_name = resource_name
|
|
|
|
@params = params
|
|
|
|
@blocks = Hash.new
|
2013-04-10 11:33:50 -04:00
|
|
|
end
|
|
|
|
|
2013-04-14 02:21:46 -04:00
|
|
|
def for(kind, &block)
|
|
|
|
if block_given?
|
|
|
|
@blocks[kind] = block
|
|
|
|
else
|
|
|
|
block = @blocks[kind]
|
|
|
|
block ? block.call(default_params) : fallback_for(kind)
|
|
|
|
end
|
2013-04-10 11:33:50 -04:00
|
|
|
end
|
|
|
|
|
2013-04-14 02:21:46 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def fallback_for(kind)
|
2013-04-10 11:33:50 -04:00
|
|
|
default_params
|
|
|
|
end
|
2013-04-14 02:21:46 -04:00
|
|
|
|
|
|
|
def default_params
|
|
|
|
params.fetch(resource_name, {})
|
|
|
|
end
|
2013-04-10 11:33:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class ParameterSanitizer < BaseSanitizer
|
2013-08-11 14:47:18 -04:00
|
|
|
|
|
|
|
class PermittedParameters
|
|
|
|
|
|
|
|
def initialize(resource_class)
|
|
|
|
@resource_class = resource_class
|
|
|
|
@for = { :sign_in => sign_in, :sign_up => sign_up, :account_update => account_update }
|
|
|
|
end
|
|
|
|
|
|
|
|
def sign_in
|
|
|
|
auth_keys + [:password, :remember_me]
|
|
|
|
end
|
|
|
|
|
|
|
|
def sign_up
|
|
|
|
auth_keys + [:password, :password_confirmation]
|
|
|
|
end
|
|
|
|
|
|
|
|
def account_update
|
|
|
|
auth_keys + [:password, :password_confirmation, :current_password]
|
|
|
|
end
|
|
|
|
|
|
|
|
def auth_keys
|
|
|
|
@resource_class.authentication_keys.respond_to?(:keys) ? @resource_class.authentication_keys.keys : @resource_class.authentication_keys
|
|
|
|
end
|
|
|
|
|
|
|
|
def for(kind)
|
|
|
|
@for[kind]
|
|
|
|
end
|
|
|
|
|
|
|
|
def add(*params)
|
|
|
|
@for.each { |action, permitted| permitted.push *params }
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove(*params)
|
|
|
|
@for.each do |action, permitted|
|
|
|
|
permitted.delete_if { |param| params.include? param }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def permitted_parameters
|
|
|
|
@permitted_parameters ||= PermittedParameters.new(@resource_class)
|
|
|
|
end
|
|
|
|
|
2013-04-14 02:21:46 -04:00
|
|
|
private
|
2013-04-10 11:33:50 -04:00
|
|
|
|
2013-04-14 02:21:46 -04:00
|
|
|
def fallback_for(kind)
|
|
|
|
if respond_to?(kind, true)
|
|
|
|
send(kind)
|
2013-08-11 14:47:18 -04:00
|
|
|
elsif (permitted = permitted_parameters.for(kind))
|
|
|
|
default_params.permit permitted
|
2013-04-14 02:21:46 -04:00
|
|
|
else
|
|
|
|
raise NotImplementedError, "Devise Parameter Sanitizer doesn't know how to sanitize parameters for #{kind}"
|
|
|
|
end
|
2013-03-13 12:37:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|