Quack, quack, quack. Use duck typing instead of hardcoding everything, closes #1281.

This commit is contained in:
José Valim 2011-08-29 14:40:10 +02:00
parent 673c707085
commit 7396c6911d
3 changed files with 17 additions and 10 deletions

View File

@ -1,3 +1,6 @@
* enhancements
* Use serialize_into_session and serialize_from_session in Warden serialize to improve extensibility
* bug fix * bug fix
* Properly deprecate setup_mail * Properly deprecate setup_mail
* Fix encoding issues with email regexp * Fix encoding issues with email regexp

View File

@ -82,6 +82,15 @@ module Devise
module ClassMethods module ClassMethods
Devise::Models.config(self, :authentication_keys, :request_keys, :strip_whitespace_keys, :case_insensitive_keys, :http_authenticatable, :params_authenticatable) Devise::Models.config(self, :authentication_keys, :request_keys, :strip_whitespace_keys, :case_insensitive_keys, :http_authenticatable, :params_authenticatable)
def serialize_into_session(record)
[record.to_key, record.authenticatable_salt]
end
def serialize_from_session(key, salt)
record = to_adapter.get(key)
record if record && record.authenticatable_salt == salt
end
def params_authenticatable?(strategy) def params_authenticatable?(strategy)
params_authenticatable.is_a?(Array) ? params_authenticatable.is_a?(Array) ?
params_authenticatable.include?(strategy) : params_authenticatable params_authenticatable.include?(strategy) : params_authenticatable

View File

@ -15,21 +15,16 @@ end
class Warden::SessionSerializer class Warden::SessionSerializer
def serialize(record) def serialize(record)
[record.class.name, record.to_key, record.authenticatable_salt] klass = record.class
array = klass.serialize_into_session(record)
array.unshift(klass.name)
end end
def deserialize(keys) def deserialize(keys)
if keys.size == 2 klass, *args = keys
raise "Devise changed how it stores objects in session. If you are seeing this message, " <<
"you can fix it by changing one character in your secret_token or cleaning up your " <<
"database sessions if you are using a db store."
end
klass, id, salt = keys
begin begin
record = ActiveSupport::Inflector.constantize(klass).to_adapter.get(id) ActiveSupport::Inflector.constantize(klass).serialize_from_session(*args)
record if record && record.authenticatable_salt == salt
rescue NameError => e rescue NameError => e
if e.message =~ /uninitialized constant/ if e.message =~ /uninitialized constant/
Rails.logger.debug "[Devise] Trying to deserialize invalid class #{klass}" Rails.logger.debug "[Devise] Trying to deserialize invalid class #{klass}"