mongoid_sessions 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.1
1
+ 1.1.0
@@ -3,6 +3,9 @@ require 'action_dispatch/middleware/session/abstract_store'
3
3
  module ActionDispatch
4
4
  module Session
5
5
 
6
+ class SessionExpiredException < Exception
7
+ end
8
+
6
9
  class MongoidSessionStore < ActionDispatch::Session::AbstractStore
7
10
 
8
11
  SESSION_RECORD_KEY = 'rack.session.record'
@@ -15,10 +18,7 @@ module ActionDispatch
15
18
  end
16
19
 
17
20
  def get_session(env, sid)
18
- unless sid and session = session_class.find_by_session_id(sid)
19
- sid = generate_sid
20
- session = session_class.new(:session_id => sid, :data => {})
21
- end
21
+ return nil unless sid and session = session_class.find_by_session_id(sid)
22
22
  env[SESSION_RECORD_KEY] = session
23
23
  [sid, session.data]
24
24
  end
@@ -9,22 +9,47 @@ module Mongoid
9
9
 
10
10
  field :session_id
11
11
  field :raw_data
12
+ field :expires_at, :type => DateTime
13
+
14
+ DEFAULT_SESSION_EXPIRY = 60
12
15
 
13
16
  store_in :collection => :sessions, :database => 'mongoid_session_store'
14
17
 
18
+ before_update :set_expires_at
19
+
15
20
  def self.find_by_session_id(session_id)
16
21
  where(:session_id => session_id).last
17
22
  end
18
23
 
24
+ def self.session_expiry
25
+ ENV['SESSION_TIMEOUT'].to_i.minutes || DEFAULT_SESSION_EXPIRY.minutes
26
+ end
27
+
19
28
  def data
20
- self.raw_data.present? && Marshal.load(Base64.decode64(self.raw_data)) || {}
29
+ self.raw_data.present? && Marshal.load(Base64.decode64(self.raw_data)).merge(:expires_at => self.expires_at) || {}
21
30
  end
22
31
 
23
- def data=(raw)
24
- raw ||= {}
32
+ def data=(raw={})
25
33
  self.raw_data = Base64.encode64(Marshal.dump(raw))
26
34
  end
27
35
 
36
+ def set_expires_at
37
+ if current?
38
+ self.expires_at = Time.now + Session.session_expiry
39
+ else
40
+ self.expires_at = nil
41
+ end
42
+ end
43
+
44
+ def current?
45
+ return true unless self.expires_at.present?
46
+ self.expires_at.in_time_zone > Time.now.in_time_zone
47
+ end
48
+
49
+ def expired?
50
+ ! current?
51
+ end
52
+
28
53
  def loaded?
29
54
  self.raw_data.present?
30
55
  end
@@ -1,3 +1,3 @@
1
1
  module MongoidSessions
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_sessions
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-31 00:00:00.000000000 Z
12
+ date: 2013-08-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongo