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
* Properly deprecate setup_mail
* Fix encoding issues with email regexp

View File

@ -82,6 +82,15 @@ module Devise
module ClassMethods
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)
params_authenticatable.is_a?(Array) ?
params_authenticatable.include?(strategy) : params_authenticatable

View File

@ -15,21 +15,16 @@ end
class Warden::SessionSerializer
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
def deserialize(keys)
if keys.size == 2
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
klass, *args = keys
begin
record = ActiveSupport::Inflector.constantize(klass).to_adapter.get(id)
record if record && record.authenticatable_salt == salt
ActiveSupport::Inflector.constantize(klass).serialize_from_session(*args)
rescue NameError => e
if e.message =~ /uninitialized constant/
Rails.logger.debug "[Devise] Trying to deserialize invalid class #{klass}"