2012-05-03 17:28:11 -04:00
|
|
|
|
require 'rack/session/abstract/id'
|
|
|
|
|
|
|
|
|
|
module ActionDispatch
|
|
|
|
|
class Request < Rack::Request
|
2012-08-31 14:19:53 -04:00
|
|
|
|
# Session is responsible for lazily loading the session from store.
|
2012-05-03 17:28:11 -04:00
|
|
|
|
class Session # :nodoc:
|
2015-08-20 16:43:57 -04:00
|
|
|
|
ENV_SESSION_KEY = Rack::RACK_SESSION # :nodoc:
|
|
|
|
|
ENV_SESSION_OPTIONS_KEY = Rack::RACK_SESSION_OPTIONS # :nodoc:
|
2012-05-03 17:28:11 -04:00
|
|
|
|
|
2013-12-11 16:29:33 -05:00
|
|
|
|
# Singleton object used to determine if an optional param wasn't specified
|
|
|
|
|
Unspecified = Object.new
|
2015-04-06 21:23:51 -04:00
|
|
|
|
|
|
|
|
|
# Creates a session hash, merging the properties of the previous session if any
|
2015-08-22 20:57:45 -04:00
|
|
|
|
def self.create(store, req, default_options)
|
|
|
|
|
session_was = find req
|
|
|
|
|
session = Request::Session.new(store, req)
|
2012-05-03 17:28:11 -04:00
|
|
|
|
session.merge! session_was if session_was
|
|
|
|
|
|
2015-08-22 20:57:45 -04:00
|
|
|
|
set(req, session)
|
|
|
|
|
Options.set(req, Request::Session::Options.new(store, default_options))
|
2012-05-03 17:28:11 -04:00
|
|
|
|
session
|
|
|
|
|
end
|
|
|
|
|
|
2015-08-22 20:57:45 -04:00
|
|
|
|
def self.find(req)
|
|
|
|
|
req.get_header ENV_SESSION_KEY
|
2012-05-03 17:28:11 -04:00
|
|
|
|
end
|
|
|
|
|
|
2015-08-22 20:57:45 -04:00
|
|
|
|
def self.set(req, session)
|
|
|
|
|
req.set_header ENV_SESSION_KEY, session
|
2012-05-03 17:28:11 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
class Options #:nodoc:
|
2015-08-22 20:57:45 -04:00
|
|
|
|
def self.set(req, options)
|
|
|
|
|
req.set_header ENV_SESSION_OPTIONS_KEY, options
|
2012-05-03 17:28:11 -04:00
|
|
|
|
end
|
|
|
|
|
|
2015-08-22 20:57:45 -04:00
|
|
|
|
def self.find(req)
|
|
|
|
|
req.get_header ENV_SESSION_OPTIONS_KEY
|
2012-05-03 17:28:11 -04:00
|
|
|
|
end
|
|
|
|
|
|
2015-06-13 17:12:44 -04:00
|
|
|
|
def initialize(by, default_options)
|
2012-05-03 17:28:11 -04:00
|
|
|
|
@by = by
|
2012-05-04 18:55:36 -04:00
|
|
|
|
@delegate = default_options.dup
|
2012-05-03 17:28:11 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def [](key)
|
2015-06-13 17:12:44 -04:00
|
|
|
|
@delegate[key]
|
|
|
|
|
end
|
|
|
|
|
|
2015-08-22 20:57:45 -04:00
|
|
|
|
def id(req)
|
2015-06-13 17:12:44 -04:00
|
|
|
|
@delegate.fetch(:id) {
|
2015-08-22 20:57:45 -04:00
|
|
|
|
@by.send(:extract_session_id, req)
|
2015-06-13 17:12:44 -04:00
|
|
|
|
}
|
2012-05-03 17:28:11 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def []=(k,v); @delegate[k] = v; end
|
|
|
|
|
def to_hash; @delegate.dup; end
|
|
|
|
|
def values_at(*args); @delegate.values_at(*args); end
|
|
|
|
|
end
|
|
|
|
|
|
2015-08-22 20:57:45 -04:00
|
|
|
|
def initialize(by, req)
|
2012-05-03 17:28:11 -04:00
|
|
|
|
@by = by
|
2015-08-22 20:57:45 -04:00
|
|
|
|
@req = req
|
2012-05-03 17:28:11 -04:00
|
|
|
|
@delegate = {}
|
|
|
|
|
@loaded = false
|
|
|
|
|
@exists = nil # we haven't checked yet
|
|
|
|
|
end
|
|
|
|
|
|
2013-01-10 22:34:52 -05:00
|
|
|
|
def id
|
2015-08-22 20:57:45 -04:00
|
|
|
|
options.id(@req)
|
2013-01-10 22:34:52 -05:00
|
|
|
|
end
|
|
|
|
|
|
2012-05-03 17:28:11 -04:00
|
|
|
|
def options
|
2015-08-22 20:57:45 -04:00
|
|
|
|
Options.find @req
|
2012-05-03 17:28:11 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
|
clear
|
|
|
|
|
options = self.options || {}
|
2015-08-22 20:57:45 -04:00
|
|
|
|
@by.send(:destroy_session, @req, options.id(@req), options)
|
2012-09-01 17:41:23 -04:00
|
|
|
|
|
|
|
|
|
# Load the new sid to be written with the response
|
2012-05-03 17:28:11 -04:00
|
|
|
|
@loaded = false
|
2012-09-01 17:41:23 -04:00
|
|
|
|
load_for_write!
|
2012-05-03 17:28:11 -04:00
|
|
|
|
end
|
|
|
|
|
|
2015-06-07 16:58:16 -04:00
|
|
|
|
# Returns value of the key stored in the session or
|
|
|
|
|
# nil if the given key is not found in the session.
|
2012-05-03 17:28:11 -04:00
|
|
|
|
def [](key)
|
|
|
|
|
load_for_read!
|
|
|
|
|
@delegate[key.to_s]
|
|
|
|
|
end
|
|
|
|
|
|
2015-06-07 16:58:16 -04:00
|
|
|
|
# Returns true if the session has the given key or false.
|
2012-05-03 17:28:11 -04:00
|
|
|
|
def has_key?(key)
|
|
|
|
|
load_for_read!
|
|
|
|
|
@delegate.key?(key.to_s)
|
|
|
|
|
end
|
|
|
|
|
alias :key? :has_key?
|
|
|
|
|
alias :include? :has_key?
|
|
|
|
|
|
2015-06-07 16:58:16 -04:00
|
|
|
|
# Returns keys of the session as Array.
|
2012-05-22 22:24:08 -04:00
|
|
|
|
def keys
|
|
|
|
|
@delegate.keys
|
|
|
|
|
end
|
|
|
|
|
|
2015-06-07 16:58:16 -04:00
|
|
|
|
# Returns values of the session as Array.
|
2012-05-22 22:24:08 -04:00
|
|
|
|
def values
|
|
|
|
|
@delegate.values
|
|
|
|
|
end
|
|
|
|
|
|
2015-06-07 16:58:16 -04:00
|
|
|
|
# Writes given value to given key of the session.
|
2012-05-03 17:28:11 -04:00
|
|
|
|
def []=(key, value)
|
|
|
|
|
load_for_write!
|
|
|
|
|
@delegate[key.to_s] = value
|
|
|
|
|
end
|
|
|
|
|
|
2015-06-07 16:58:16 -04:00
|
|
|
|
# Clears the session.
|
2012-05-03 17:28:11 -04:00
|
|
|
|
def clear
|
|
|
|
|
load_for_write!
|
|
|
|
|
@delegate.clear
|
|
|
|
|
end
|
|
|
|
|
|
2015-06-07 16:58:16 -04:00
|
|
|
|
# Returns the session as Hash.
|
2012-05-03 17:28:11 -04:00
|
|
|
|
def to_hash
|
|
|
|
|
load_for_read!
|
|
|
|
|
@delegate.dup.delete_if { |_,v| v.nil? }
|
|
|
|
|
end
|
|
|
|
|
|
2015-06-07 16:58:16 -04:00
|
|
|
|
# Updates the session with given Hash.
|
|
|
|
|
#
|
|
|
|
|
# session.to_hash
|
|
|
|
|
# # => {"session_id"=>"e29b9ea315edf98aad94cc78c34cc9b2"}
|
|
|
|
|
#
|
|
|
|
|
# session.update({ "foo" => "bar" })
|
|
|
|
|
# # => {"session_id"=>"e29b9ea315edf98aad94cc78c34cc9b2", "foo" => "bar"}
|
|
|
|
|
#
|
|
|
|
|
# session.to_hash
|
|
|
|
|
# # => {"session_id"=>"e29b9ea315edf98aad94cc78c34cc9b2", "foo" => "bar"}
|
2012-05-03 17:28:11 -04:00
|
|
|
|
def update(hash)
|
|
|
|
|
load_for_write!
|
|
|
|
|
@delegate.update stringify_keys(hash)
|
|
|
|
|
end
|
|
|
|
|
|
2015-06-07 16:58:16 -04:00
|
|
|
|
# Deletes given key from the session.
|
2012-05-03 17:28:11 -04:00
|
|
|
|
def delete(key)
|
|
|
|
|
load_for_write!
|
|
|
|
|
@delegate.delete key.to_s
|
|
|
|
|
end
|
|
|
|
|
|
2015-06-07 16:58:16 -04:00
|
|
|
|
# Returns value of given key from the session, or raises +KeyError+
|
|
|
|
|
# if can't find given key in case of not setted dafault value.
|
|
|
|
|
# Returns default value if specified.
|
|
|
|
|
#
|
|
|
|
|
# session.fetch(:foo)
|
|
|
|
|
# # => KeyError: key not found: "foo"
|
|
|
|
|
#
|
|
|
|
|
# session.fetch(:foo, :bar)
|
|
|
|
|
# # => :bar
|
|
|
|
|
#
|
|
|
|
|
# session.fetch(:foo) do
|
|
|
|
|
# :bar
|
|
|
|
|
# end
|
|
|
|
|
# # => :bar
|
2013-12-11 16:29:33 -05:00
|
|
|
|
def fetch(key, default=Unspecified, &block)
|
|
|
|
|
load_for_read!
|
|
|
|
|
if default == Unspecified
|
|
|
|
|
@delegate.fetch(key.to_s, &block)
|
2013-10-29 12:00:45 -04:00
|
|
|
|
else
|
2013-12-11 16:29:33 -05:00
|
|
|
|
@delegate.fetch(key.to_s, default, &block)
|
2013-10-29 12:00:45 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2012-05-03 17:28:11 -04:00
|
|
|
|
def inspect
|
|
|
|
|
if loaded?
|
|
|
|
|
super
|
|
|
|
|
else
|
|
|
|
|
"#<#{self.class}:0x#{(object_id << 1).to_s(16)} not yet loaded>"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def exists?
|
|
|
|
|
return @exists unless @exists.nil?
|
2015-08-22 20:57:45 -04:00
|
|
|
|
@exists = @by.send(:session_exists?, @req)
|
2012-05-03 17:28:11 -04:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def loaded?
|
|
|
|
|
@loaded
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def empty?
|
|
|
|
|
load_for_read!
|
|
|
|
|
@delegate.empty?
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def merge!(other)
|
|
|
|
|
load_for_write!
|
|
|
|
|
@delegate.merge!(other)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def load_for_read!
|
|
|
|
|
load! if !loaded? && exists?
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def load_for_write!
|
|
|
|
|
load! unless loaded?
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def load!
|
2015-08-22 20:57:45 -04:00
|
|
|
|
id, session = @by.load_session @req
|
2012-05-03 17:28:11 -04:00
|
|
|
|
options[:id] = id
|
|
|
|
|
@delegate.replace(stringify_keys(session))
|
|
|
|
|
@loaded = true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def stringify_keys(other)
|
|
|
|
|
other.each_with_object({}) { |(key, value), hash|
|
|
|
|
|
hash[key.to_s] = value
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|