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 =

The key name used to store the session model in the request env.

'rack.session.record'
ACTION_DISPATCH_LOGGER_KEY =

The request env hash key has the logger instance.

'action_dispatch.logger'

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.



18
19
20
# File 'lib/action_dispatch/session/code_igniter_store.rb', line 18

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:



57
58
59
60
# File 'lib/action_dispatch/session/code_igniter_store.rb', line 57

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, nil)

    the new session id or nil if options[:drop].

See Also:



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/action_dispatch/session/code_igniter_store.rb', line 102

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 its 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:



122
123
124
125
126
127
128
129
130
# File 'lib/action_dispatch/session/code_igniter_store.rb', line 122

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, Array>

Finds an existing session or creates a new one.

Parameters:

  • req (ActionDispatch::Request)
  • sid (String, Rack::Session::SessionId)

Returns:

  • (Array<String, Hash, Array>)

See Also:



33
34
35
36
37
38
39
40
41
# File 'lib/action_dispatch/session/code_igniter_store.rb', line 33

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, FalseClass

Writes the session information to the database.

Parameters:

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

Returns:

  • (String, FalseClass)

    encrypted and base64 encoded string of the session data or false if the session could not be saved.

See Also:



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/action_dispatch/session/code_igniter_store.rb', line 74

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