Skip to content

Commit

Permalink
Quack, quack, quack. Use duck typing instead of hardcoding everything,
Browse files Browse the repository at this point in the history
…closes #1281.
  • Loading branch information
josevalim committed Aug 29, 2011
1 parent 673c707 commit 7396c69
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rdoc
@@ -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
Expand Down
9 changes: 9 additions & 0 deletions lib/devise/models/authenticatable.rb
Expand Up @@ -82,6 +82,15 @@ def authenticatable_salt
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
Expand Down
15 changes: 5 additions & 10 deletions lib/devise/rails/warden_compat.rb
Expand Up @@ -15,21 +15,16 @@ def cookies

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}"
Expand Down

0 comments on commit 7396c69

Please sign in to comment.