mirror of
https://github.com/heartcombo/devise.git
synced 2022-11-09 12:18:31 -05:00
Make use of warden's scoped serialization
This commit is contained in:
parent
22897150d8
commit
c22d755cf4
3 changed files with 35 additions and 41 deletions
|
@ -421,6 +421,17 @@ module Devise
|
||||||
|
|
||||||
Devise.mappings.each_value do |mapping|
|
Devise.mappings.each_value do |mapping|
|
||||||
warden_config.scope_defaults mapping.name, :strategies => mapping.strategies
|
warden_config.scope_defaults mapping.name, :strategies => mapping.strategies
|
||||||
|
|
||||||
|
warden_config.serialize_into_session(mapping.name) do |record|
|
||||||
|
mapping.to.serialize_into_session(record)
|
||||||
|
end
|
||||||
|
|
||||||
|
warden_config.serialize_from_session(mapping.name) do |key|
|
||||||
|
# Previous versions contained an additional entry at the beginning of
|
||||||
|
# key with the record's class name.
|
||||||
|
args = key[-2, 2]
|
||||||
|
mapping.to.serialize_from_session(*args)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@warden_config_block.try :call, Devise.warden_config
|
@@warden_config_block.try :call, Devise.warden_config
|
||||||
|
|
|
@ -12,32 +12,3 @@ module Warden::Mixins::Common
|
||||||
request.cookie_jar
|
request.cookie_jar
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Warden::SessionSerializer
|
|
||||||
def serialize(record)
|
|
||||||
klass = record.class
|
|
||||||
array = klass.serialize_into_session(record)
|
|
||||||
array.unshift(klass.name)
|
|
||||||
end
|
|
||||||
|
|
||||||
def deserialize(keys)
|
|
||||||
klass_name, *args = keys
|
|
||||||
|
|
||||||
begin
|
|
||||||
klass = ActiveSupport::Inflector.constantize(klass_name)
|
|
||||||
if klass.respond_to? :serialize_from_session
|
|
||||||
klass.serialize_from_session(*args)
|
|
||||||
else
|
|
||||||
Rails.logger.warn "[Devise] Stored serialized class #{klass_name} seems not to be Devise enabled anymore. Did you do that on purpose?"
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
rescue NameError => e
|
|
||||||
if e.message =~ /uninitialized constant/
|
|
||||||
Rails.logger.debug "[Devise] Trying to deserialize invalid class #{klass_name}"
|
|
||||||
nil
|
|
||||||
else
|
|
||||||
raise
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
|
@ -333,22 +333,34 @@ class AuthenticationSessionTest < ActionDispatch::IntegrationTest
|
||||||
assert_equal "Cart", @controller.user_session[:cart]
|
assert_equal "Cart", @controller.user_session[:cart]
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'does not explode when invalid user class is stored in session' do
|
test 'does not explode when class name is still stored in session' do
|
||||||
klass = User
|
# In order to test that old sessions do not break with the new scoped
|
||||||
paths = ActiveSupport::Dependencies.autoload_paths.dup
|
# deserialization, we need to serialize the session the old way. This is
|
||||||
|
# done by removing the newly used scoped serialization method
|
||||||
|
# (#user_serialize) and bringing back the old uncsoped #serialize method
|
||||||
|
# that includes the record's class name in the serialization.
|
||||||
begin
|
begin
|
||||||
|
Warden::SessionSerializer.class_eval do
|
||||||
|
alias_method :original_serialize, :serialize
|
||||||
|
alias_method :original_user_serialize, :user_serialize
|
||||||
|
remove_method :user_serialize
|
||||||
|
|
||||||
|
def serialize(record)
|
||||||
|
klass = record.class
|
||||||
|
array = klass.serialize_into_session(record)
|
||||||
|
array.unshift(klass.name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
sign_in_as_user
|
sign_in_as_user
|
||||||
assert warden.authenticated?(:user)
|
assert warden.authenticated?(:user)
|
||||||
|
|
||||||
Object.send :remove_const, :User
|
|
||||||
ActiveSupport::Dependencies.autoload_paths.clear
|
|
||||||
|
|
||||||
visit "/users"
|
|
||||||
assert_not warden.authenticated?(:user)
|
|
||||||
ensure
|
ensure
|
||||||
Object.const_set(:User, klass)
|
Warden::SessionSerializer.class_eval do
|
||||||
ActiveSupport::Dependencies.autoload_paths.replace(paths)
|
alias_method :serialize, :original_serialize
|
||||||
|
remove_method :original_serialize
|
||||||
|
alias_method :user_serialize, :original_user_serialize
|
||||||
|
remove_method :original_user_serialize
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue