Skip to content

Commit

Permalink
Merge pull request rails#204 from TobiasBales/allow-subclassing-sessi…
Browse files Browse the repository at this point in the history
…on-store

Allow subclasses of session store to override session_class
  • Loading branch information
byroot committed Aug 15, 2023
2 parents 2082e1d + 35cffd2 commit 0fa232b
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/action_dispatch/session/active_record_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ module Session
class ActiveRecordStore < ActionDispatch::Session::AbstractSecureStore
# The class used for session storage. Defaults to
# ActiveRecord::SessionStore::Session
cattr_accessor :session_class
class_attribute :session_class

SESSION_RECORD_KEY = 'rack.session.record'
ENV_SESSION_OPTIONS_KEY = Rack::RACK_SESSION_OPTIONS
Expand All @@ -67,7 +67,7 @@ def get_session(request, sid)
# If the sid was nil or if there is no pre-existing session under the sid,
# force the generation of a new sid and associate a new session associated with the new sid
sid = generate_sid
session = @@session_class.new(:session_id => sid.private_id, :data => {})
session = session_class.new(:session_id => sid.private_id, :data => {})
end
request.env[SESSION_RECORD_KEY] = session
[sid, session.data]
Expand Down Expand Up @@ -106,7 +106,7 @@ def delete_session(request, session_id, options)
new_sid = generate_sid

if options[:renew]
new_model = @@session_class.new(:session_id => new_sid.private_id, :data => data)
new_model = session_class.new(:session_id => new_sid.private_id, :data => data)
new_model.save
request.env[SESSION_RECORD_KEY] = new_model
end
Expand All @@ -120,7 +120,7 @@ def get_session_model(request, id)
model = get_session_with_fallback(id)
unless model
id = generate_sid
model = @@session_class.new(:session_id => id.private_id, :data => {})
model = session_class.new(:session_id => id.private_id, :data => {})
model.save
end
if request.env[ENV_SESSION_OPTIONS_KEY][:id].nil?
Expand All @@ -134,9 +134,9 @@ def get_session_model(request, id)

def get_session_with_fallback(sid)
if sid && !self.class.private_session_id?(sid.public_id)
if (secure_session = @@session_class.find_by_session_id(sid.private_id))
if (secure_session = session_class.find_by_session_id(sid.private_id))
secure_session
elsif (insecure_session = @@session_class.find_by_session_id(sid.public_id))
elsif (insecure_session = session_class.find_by_session_id(sid.public_id))
insecure_session.session_id = sid.private_id # this causes the session to be secured
insecure_session
end
Expand Down

0 comments on commit 0fa232b

Please sign in to comment.