Class: ActionDispatch::Session::CodeIgniterStore

Inherits:
AbstractStore
  • Object
show all
Defined in:
lib/action_dispatch/session/code_igniter_store.rb

Overview

A session store for Rails to handle Pyro sessions.

Constant Summary collapse

SESSION_RECORD_KEY =
'rack.session.record'

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ CodeIgniterStore

Returns a new instance of CodeIgniterStore.

Parameters:

  • app (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :key (String) — default: 'default_pyrocms'

    The session cookie name.



15
16
17
# File 'lib/action_dispatch/session/code_igniter_store.rb', line 15

def initialize(app, options={})
  super(app, { key: 'default_pyrocms' }.merge(options))
end

Instance Method Details

#commit_session?(req, session, options) ⇒ Boolean

Note:

This is called from Rack::Session::Abstract::Persisted#commit_session.

Should the session be persisted?

Parameters:

  • req (ActionDispatch::Request)
  • session (Hash)
  • options (Hash)

Returns:

  • (Boolean)

    when true #write_session will be called

See Also:



54
55
56
57
# File 'lib/action_dispatch/session/code_igniter_store.rb', line 54

def commit_session?(req, session, options)
  # If session_filter returns true then let super decide if we commit the session.
  Firebug.config.session_filter.call(req) ? super : false
end

#delete_session(req, sid, options) ⇒ String

Deletes then creates a new session in the database.

Parameters:

  • req (ActionDispatch::Request)
  • sid (String)
  • options (Hash)

Returns:

  • (String)

    the new session id

See Also:



97
98
99
100
101
102
103
104
105
106
# File 'lib/action_dispatch/session/code_igniter_store.rb', line 97

def delete_session(req, sid, options)
  silence_logger(req) do
    # Get the current database record for this session then delete it.
    find_session_model(req, sid).delete
    return if options[:drop]
    req.env[SESSION_RECORD_KEY] = nil
    # Generate a new one and return it's ID
    find_session_model(req).tap { |s| s.save if options[:renew] }.session_id
  end
end

#extract_session_id(req) ⇒ String?

Tries to find the session ID in the requests cookies.

Parameters:

  • req (ActionDispatch::Request)

Returns:

  • (String, nil)

See Also:



116
117
118
119
120
121
122
123
# File 'lib/action_dispatch/session/code_igniter_store.rb', line 116

def extract_session_id(req)
  sid = req.cookies[@key]
  # the request didn't have the session cookie so create a new session ID.
  return generate_sid if sid.nil?
  # sometimes the cookie contains just the session ID.
  return sid if sid.size <= 32
  Firebug.decrypt_cookie(sid)[:session_id]
end

#find_session(req, sid) ⇒ Array<String, Hash>

Finds an existing session or creates a new one.

Parameters:

  • req (ActionDispatch::Request)
  • sid (String)

Returns:

  • (Array<String, Hash>)

See Also:



30
31
32
33
34
35
36
37
38
# File 'lib/action_dispatch/session/code_igniter_store.rb', line 30

def find_session(req, sid)
  silence_logger(req) do
    model = find_session_model(req, sid)
    req.env[SESSION_RECORD_KEY] = model
    # +Rack::Session::Abstract::Persisted#load_session+ expects this to return an Array with the first value being
    # the session ID and the second the actual session data.
    [model.session_id, model.user_data]
  end
end

#write_session(req, sid, session, _options) ⇒ String

Writes the session information to the database.

Parameters:

  • req (ActionDispatch::Request)
  • sid (String)
  • session (Hash)
  • _options (Hash)

Returns:

  • (String)

    encrypted and base64 encoded string of the session data.

See Also:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/action_dispatch/session/code_igniter_store.rb', line 70

def write_session(req, sid, session, _options)
  silence_logger(req) do
    model = find_session_model(req, sid)
    model_params = {
      session_id: model.session_id,
      user_agent: req.user_agent || '', # user_agent can't be null
      ip_address: req.remote_ip || '',  # ip_address can't be null
      user_data: session
    }
    # Returning false will cause Rack to output a warning.
    return false unless model.update(model_params)
    req.env[SESSION_RECORD_KEY] = model
    # Return the encrypted cookie format of the data. Rack sets this value as the cookie in the response
    model.cookie_data
  end
end