diff options
Diffstat (limited to 'actionpack/lib')
-rw-r--r-- | actionpack/lib/action_dispatch/middleware/session/abstract_store.rb | 69 |
1 files changed, 56 insertions, 13 deletions
diff --git a/actionpack/lib/action_dispatch/middleware/session/abstract_store.rb b/actionpack/lib/action_dispatch/middleware/session/abstract_store.rb index e82132b445..56c71b962a 100644 --- a/actionpack/lib/action_dispatch/middleware/session/abstract_store.rb +++ b/actionpack/lib/action_dispatch/middleware/session/abstract_store.rb @@ -18,19 +18,6 @@ module ActionDispatch end end - module DestroyableSession - def destroy - clear - options = @env[Rack::Session::Abstract::ENV_SESSION_OPTIONS_KEY] if @env - options ||= {} - @by.send(:destroy_session, @env, options[:id], options) if @by - options[:id] = nil - @loaded = false - end - end - - ::Rack::Session::Abstract::SessionHash.send :include, DestroyableSession - module Compatibility def initialize(app, options = {}) options[:key] ||= '_session_id' @@ -81,12 +68,68 @@ module ActionDispatch include Compatibility include StaleSessionCheck + ENV_SESSION_KEY = Rack::Session::Abstract::ENV_SESSION_KEY # :nodoc: + ENV_SESSION_OPTIONS_KEY = Rack::Session::Abstract::ENV_SESSION_OPTIONS_KEY # :nodoc: + private + module DestroyableSession + def destroy + clear + options = @env[Rack::Session::Abstract::ENV_SESSION_OPTIONS_KEY] if @env + options ||= {} + @by.send(:destroy_session, @env, options[:id], options) if @by + options[:id] = nil + @loaded = false + end + end + + ::Rack::Session::Abstract::SessionHash.send :include, DestroyableSession + + def prepare_session(env) + session_was = env[ENV_SESSION_KEY] + env[ENV_SESSION_KEY] = Rack::Session::Abstract::SessionHash.new(self, env) + env[ENV_SESSION_OPTIONS_KEY] = Request::Session::Options.new(self, env, @default_options) + env[ENV_SESSION_KEY].merge! session_was if session_was + end + def set_cookie(env, session_id, cookie) request = ActionDispatch::Request.new(env) request.cookie_jar[key] = cookie end end end + + class Request + # SessionHash is responsible to lazily load the session from store. + class Session + class Options #:nodoc: + def initialize(by, env, default_options) + @by = by + @env = env + @session_id_loaded = false + @delegate = default_options + end + + def [](key) + load_session_id! if key == :id && session_id_not_loaded? + @delegate[key] + end + + def []=(k,v); @delegate[k] = v; end + def to_hash; @delegate.dup; end + def values_at(*args) @delegate.values_at(*args); end + + private + def session_id_not_loaded? + !(@session_id_loaded || @delegate.key?(:id)) + end + + def load_session_id! + @delegate[:id] = @by.send(:extract_session_id, @env) + @session_id_loaded = true + end + end + end + end end |