blob: 6238b8cbee823e0c8265dc08e1a17aadd3bce8c7 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
begin
require 'active_record'
require 'cgi'
require 'cgi/session'
require 'base64'
# Contributed by Tim Bates
class CGI
class Session
# Active Record database-based session storage class.
#
# Implements session storage in a database using the ActiveRecord ORM library. Assumes that the database
# has a table called +sessions+ with columns +id+ (numeric, primary key), +sessid+ and +data+ (text).
# The session data is stored in the +data+ column in the binary Marshal format; the user is responsible for ensuring that
# only data that can be Marshaled is stored in the session.
#
# Adding +created_at+ or +updated_at+ datetime columns to the sessions table will enable stamping of the data, which can
# be used to clear out old sessions.
#
# It's highly recommended to have an index on the sessid column to improve performance.
class ActiveRecordStore
# The ActiveRecord class which corresponds to the database table.
class Session < ActiveRecord::Base
end
# Create a new ActiveRecordStore instance. This constructor is used internally by CGI::Session.
# The user does not generally need to call it directly.
#
# +session+ is the session for which this instance is being created.
#
# +option+ is currently ignored as no options are recognized.
#
# This session's ActiveRecord database row will be created if it does not exist, or opened if it does.
def initialize(session, option=nil)
ActiveRecord::Base.silence do
@session = Session.find_by_sessid(session.session_id) || Session.new("sessid" => session.session_id, "data" => marshalize({}))
@data = unmarshalize(@session.data)
end
end
# Update and close the session's ActiveRecord object.
def close
return unless @session
update
@session = nil
end
# Close and destroy the session's ActiveRecord object.
def delete
return unless @session
@session.destroy
@session = nil
end
# Restore session state from the session's ActiveRecord object.
def restore
return unless @session
@data = unmarshalize(@session.data)
end
# Save session state in the session's ActiveRecord object.
def update
return unless @session
ActiveRecord::Base.silence { @session.update_attribute "data", marshalize(@data) }
end
private
def unmarshalize(data)
Marshal.load(Base64.decode64(data))
end
def marshalize(data)
Base64.encode64(Marshal.dump(data))
end
end #ActiveRecordStore
end #Session
end #CGI
rescue LoadError
# Couldn't load Active Record, so don't make this store available
end
|