Class: Users::Session

Inherits:
Object show all
Defined in:
lib/users/session.rb

Overview

Sessions are created on user logins and exist until the user logs out or the expiration time passes between activies.

Upon logging in, the user may set the session id as a json-rpc/rjr header and pass it to the Registry on subsequent requests to determine in the logged in user has the necessary privileges to perform the requested operations.

Constant Summary collapse

SESSION_EXPIRATION =

Number of seconds which inactivity is allowed before invalidating the session.

TODO make configurable

6000

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Session

Session initializer

Options Hash (args):

  • :user,'user' (Users::User)

    user owner of the session

  • :id,'id' (String)

    id to assign to session

  • :refreshed_time,'refreshed_time' (Time)

    time session was established


43
44
45
46
47
48
49
50
51
52
# File 'lib/users/session.rb', line 43

def initialize(args = {})
  attr_from_args args,
                 :user => nil,
                 :id   => Motel::gen_uuid,
                 :refreshed_time => Time.now,
                 :endpoint_id => nil

  @refreshed_time =
    Time.parse(@refreshed_time) if @refreshed_time.is_a?(String)
end

Instance Attribute Details

#endpoint_idObject

ID of the rjr node which this session was established on


31
32
33
# File 'lib/users/session.rb', line 31

def endpoint_id
  @endpoint_id
end

#idObject

Unique identifier of the session


22
23
24
# File 'lib/users/session.rb', line 22

def id
  @id
end

#refreshed_timeObject

Time the session was last refreshed


25
26
27
# File 'lib/users/session.rb', line 25

def refreshed_time
  @refreshed_time
end

#userObject

Handle to the user which established the session


28
29
30
# File 'lib/users/session.rb', line 28

def user
  @user
end

Class Method Details

.json_create(o) ⇒ Object

Create new session from json representation


87
88
89
90
# File 'lib/users/session.rb', line 87

def self.json_create(o)
  session = new(o['data'])
  return session
end

Instance Method Details

#timed_out?true, false

Return boolean indicating if this session is no longer valid.

Returns true if SESSION_EXPIRATION seconds have passed since the session was created or this method was last invoked, else return false.


61
62
63
64
65
66
67
68
# File 'lib/users/session.rb', line 61

def timed_out?
  ct = Time.now
  return true if ct - @refreshed_time > SESSION_EXPIRATION &&
                 !@user.permenant

  @refreshed_time = ct
  return false
end

#to_json(*a) ⇒ Object

Convert session to json representation and return it


76
77
78
79
80
81
82
83
84
# File 'lib/users/session.rb', line 76

def to_json(*a)
  {
    'json_class' => self.class.name,
    'data'       => {:user           => user,
                     :id             => id,
                     :refreshed_time => refreshed_time,
                     :endpoint_id    => endpoint_id}
  }.to_json(*a)
end

#to_sObject

Convert session to human readable string and return it


71
72
73
# File 'lib/users/session.rb', line 71

def to_s
  "session-#{@id}(#{@user})"
end