]> git.openstreetmap.org Git - rails.git/blob - vendor/plugins/sql_session_store/lib/sql_session_store.rb
Revert r16046.
[rails.git] / vendor / plugins / sql_session_store / lib / sql_session_store.rb
1 require 'base64'
2
3 # +SqlSessionStore+ is a stripped down, optimized for speed version of
4 # class +ActiveRecordStore+.
5
6 # Hack for older versions of Rails
7 unless defined?(ActionController::Session::AbstractStore)
8   module ActionController
9     module Session
10       class AbstractStore
11       end
12     end
13   end
14 end
15
16 class SqlSessionStore < ActionController::Session::AbstractStore
17
18   # The class to be used for creating, retrieving and updating sessions.
19   # Defaults to SqlSessionStore::SqlSession, which is derived from +ActiveRecord::Base+.
20   #
21   # In order to achieve acceptable performance you should implement
22   # your own session class, similar to the one provided for Myqsl.
23   #
24   # Only functions +find_session+, +create_session+,
25   # +update_session+ and +destroy+ are required. The best implementations
26   # are +postgresql_session.rb+ and +oracle_session.rb+.
27   cattr_accessor :session_class
28   self.session_class = SqlSession
29
30   # Rack-ism for Rails 2.3.0
31   SESSION_RECORD_KEY = 'rack.session.record'.freeze
32
33   # Backwards-compat indicators (booleans for speed)
34   cattr_accessor :use_rack_session, :use_cgi_session
35   self.use_rack_session = false
36   self.use_cgi_session  = false
37
38   # For Rack compatibility (Rails 2.3.0+)
39   def get_session(env, sid)
40     sid ||= generate_sid
41     #puts "get_session(#{sid})"
42     session = find_or_create_session(sid)
43     env[SESSION_RECORD_KEY] = session
44     [sid, session.data]
45   end
46
47   # For Rack compatibility (Rails 2.3.0+)
48   def set_session(env, sid, session_data)
49     #puts "set_session(#{sid})"
50     session = env[SESSION_RECORD_KEY]
51     session.update_session(session_data)
52     return true # indicate ok to Rack
53   end
54
55   # Create a new SqlSessionStore instance. This method hooks into
56   # the find/create methods of a given driver class.
57   #
58   # +session_id+ is the session ID for which this instance is being created.
59   def find_or_create_session(session_id)
60     if @session = session_class.find_session(session_id)
61       @data = @session.data
62     else
63       @session = session_class.create_session(session_id)
64       @data = {}
65     end
66     @session
67   end
68
69   # Below here is for pre-Rails 2.3.0 and not used in Rack-based servers
70   # The CGI::Session methods are a bit odd in that half are class and half
71   # are instance-based methods
72   # Note that +option+ is currently ignored as no options are recognized.
73   def initialize(session, options={})
74     # This is just some optimization since this is called over and over and over
75     if self.use_rack_session
76       super # MUST call super for Rack sessions
77       return true
78     elsif self.use_cgi_session
79       find_or_create_session(session.session_id)
80     else
81       version ||= Rails.version.split('.')
82       if version[0].to_i == 2 && version[1].to_i < 3
83         find_or_create_session(session.session_id)
84         self.use_cgi_session = true
85       else
86         super # MUST call super for Rack sessions
87         self.use_rack_session = true
88       end
89     end
90   end
91
92   # Update the database and disassociate the session object
93   def close
94     if @session
95       @session.update_session(@data)
96       @session = nil
97     end
98   end
99
100   # Delete the current session, disassociate and destroy session object
101   def delete
102     if @session
103       @session.destroy
104       @session = nil
105     end
106   end
107
108   # Restore session data from the session object
109   def restore
110     if @session
111       @data = @session.data
112     end
113   end
114
115   # Save session data in the session object
116   def update
117     if @session
118       @session.update_session(@data)
119     end
120   end
121   
122   def id
123     @session.id
124   end
125 end
126
127 class CGI::Session
128   def id
129     @dbman.id
130   end
131 end
132 __END__
133
134 # This software is released under the MIT license
135 #
136 # Copyright (c) 2008, 2009 Nate Wiger
137 # Copyright (c) 2005, 2006 Stefan Kaes
138
139 # Permission is hereby granted, free of charge, to any person obtaining
140 # a copy of this software and associated documentation files (the
141 # "Software"), to deal in the Software without restriction, including
142 # without limitation the rights to use, copy, modify, merge, publish,
143 # distribute, sublicense, and/or sell copies of the Software, and to
144 # permit persons to whom the Software is furnished to do so, subject to
145 # the following conditions:
146
147 # The above copyright notice and this permission notice shall be
148 # included in all copies or substantial portions of the Software.
149
150 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
151 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
152 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
153 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
154 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
155 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
156 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
157