Class: Isomorfeus::Empowerment::Session

Inherits:
LucidObject::Base
  • Object
show all
Defined in:
lib/isomorfeus/empowerment/session.rb

Class Method Summary collapse

Class Method Details

.add(session_id:, cookie:, user:, accessor:) ⇒ Object



13
14
15
16
# File 'lib/isomorfeus/empowerment/session.rb', line 13

def add(session_id:, cookie:, user:, accessor:)
  t = Time.now
  self.create(key: session_id, attributes: { user_class_name: user.class.name, user_key: user.key, cookie: cookie, accessor: accessor, ctime: t, atime: t })
end

.get_user(session_id:) ⇒ Object



38
39
40
41
42
43
# File 'lib/isomorfeus/empowerment/session.rb', line 38

def get_user(session_id:)
  s = touch(session_id: session_id)
  s.user_class_name.constantize.load(key: s.user_key) if s
rescue
  nil
end

.remove(session_id:) ⇒ Object



57
58
59
# File 'lib/isomorfeus/empowerment/session.rb', line 57

def remove(session_id:)
  self.destroy(key: session_id)
end


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/isomorfeus/empowerment/session.rb', line 18

def take_cookie(accessor:)
  s = self.search(:accessor, accessor).first
  if s
    cookie = s.cookie
    if cookie
      session_info = cookie.split('; ').first
      session_id = session_info.split('=').last.strip
      s.cookie = nil
      s.save
      cookie
    else
      # asked for the same cookie a second time
      # can probably only be due to session hijacking
      # so delete session associated with that accessor
      s.destroy
      nil
    end
  end
end

.touch(session_id:) ⇒ Object Also known as: get_session



45
46
47
48
49
50
51
52
53
54
# File 'lib/isomorfeus/empowerment/session.rb', line 45

def touch(session_id:)
  s = self.load(key: session_id)
  return nil unless s
  t = Time.now
  if (t - s.atime) > 600
    s.atime = t
    s.save
  end
  s
end