Class: SessionManager
- Inherits:
-
Object
- Object
- SessionManager
- Defined in:
- app/models/session_manager.rb
Class Attribute Summary collapse
-
.identity_lifetime ⇒ Object
Returns the value of attribute identity_lifetime.
Instance Method Summary collapse
-
#account ⇒ Object
Retrieves the user account from the database through an identity.
- #created_at ⇒ Object
-
#identity ⇒ Object
Retrieves the identity from using the identity id and creation time stored in the session.
-
#identity=(identity) ⇒ Object
Sets into the session the identity with which the user has logged in.
-
#initialize(request_session) ⇒ SessionManager
constructor
A new instance of SessionManager.
- #last_announcements_seen! ⇒ Object
- #last_announcements_seen_on ⇒ Object
-
#previous_page ⇒ Object
Provides the url to redirect to for a login.
-
#remember_me! ⇒ Object
Sets the login expiration to one year from now.
-
#remember_page(url) ⇒ Object
Stores the url in the user session to provide a redirect after the login.
-
#sign_out ⇒ Object
Deletes the user login information for the session.
-
#signed_in? ⇒ Boolean
Returns true if the user is logged in, otherwise returns false.
Constructor Details
#initialize(request_session) ⇒ SessionManager
Returns a new instance of SessionManager.
14 15 16 17 18 19 |
# File 'app/models/session_manager.rb', line 14 def initialize(request_session) @session = request_session refresh! @session[:created_at] ||= Time.now @session[:previous_page] = @session[:last_page] end |
Class Attribute Details
.identity_lifetime ⇒ Object
Returns the value of attribute identity_lifetime.
4 5 6 |
# File 'app/models/session_manager.rb', line 4 def identity_lifetime @identity_lifetime end |
Instance Method Details
#account ⇒ Object
Retrieves the user account from the database through an identity.
75 76 77 |
# File 'app/models/session_manager.rb', line 75 def account identity ? identity.account : User::Guest.find_or_create_from_session(@session) end |
#created_at ⇒ Object
21 22 23 |
# File 'app/models/session_manager.rb', line 21 def created_at @session[:created_at] end |
#identity ⇒ Object
Retrieves the identity from using the identity id and creation time stored in the session
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'app/models/session_manager.rb', line 60 def identity serialized_identity = @session[:identity] if serialized_identity && serialized_identity['expires_at'] > Time.now begin @identity ||= User::Identity.deserialize!(serialized_identity['id_token']) rescue nil end else nil end end |
#identity=(identity) ⇒ Object
Sets into the session the identity with which the user has logged in.
44 45 46 47 48 49 50 51 52 |
# File 'app/models/session_manager.rb', line 44 def identity=(identity) unless identity.nil? @session[:identity] = {'id_token' => identity.serialize, 'expires_at' => SessionManager.identity_lifetime.from_now} else @session[:identity] = nil end @identity = identity end |
#last_announcements_seen! ⇒ Object
39 40 41 |
# File 'app/models/session_manager.rb', line 39 def last_announcements_seen! @session[:last_announcements_seen_on] = Time.now end |
#last_announcements_seen_on ⇒ Object
35 36 37 |
# File 'app/models/session_manager.rb', line 35 def last_announcements_seen_on @session[:last_announcements_seen_on] ||= 1 end |
#previous_page ⇒ Object
Provides the url to redirect to for a login.
31 32 33 |
# File 'app/models/session_manager.rb', line 31 def previous_page @session[:previous_page] end |
#remember_me! ⇒ Object
Sets the login expiration to one year from now.
85 86 87 |
# File 'app/models/session_manager.rb', line 85 def remember_me! @session[:identity]['expires_at'] = 1.year.from_now end |
#remember_page(url) ⇒ Object
Stores the url in the user session to provide a redirect after the login.
26 27 28 |
# File 'app/models/session_manager.rb', line 26 def remember_page(url) @session[:last_page] = url end |
#sign_out ⇒ Object
Deletes the user login information for the session.
55 56 57 |
# File 'app/models/session_manager.rb', line 55 def sign_out self.identity = nil end |
#signed_in? ⇒ Boolean
Returns true if the user is logged in, otherwise returns false.
80 81 82 |
# File 'app/models/session_manager.rb', line 80 def signed_in? identity ? true : false end |