Commit aca1246073ec1b1d5c1a2e13a1ae58b8c742d145

Authored by Braulio Bhavamitra
2 parents 97489673 becf0ab7

Merge branch 'ai3139' into 'master'

Register last login date

See merge request !465
app/models/user.rb
@@ -114,6 +114,10 @@ class User < ActiveRecord::Base @@ -114,6 +114,10 @@ class User < ActiveRecord::Base
114 u && u.authenticated?(password) ? u : nil 114 u && u.authenticated?(password) ? u : nil
115 end 115 end
116 116
  117 + def register_login
  118 + self.update_attribute :last_login_at, Time.now
  119 + end
  120 +
117 # Activates the user in the database. 121 # Activates the user in the database.
118 def activate 122 def activate
119 return false unless self.person 123 return false unless self.person
db/migrate/20140519113821_add_last_login_at_to_user.rb 0 → 100644
@@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
  1 +class AddLastLoginAtToUser < ActiveRecord::Migration
  2 + def self.up
  3 + add_column :users, :last_login_at, :datetime
  4 + end
  5 +
  6 + def self.down
  7 + remove_column :users, :last_login_at
  8 + end
  9 +end
lib/authenticated_system.rb
@@ -5,22 +5,23 @@ module AuthenticatedSystem @@ -5,22 +5,23 @@ module AuthenticatedSystem
5 def logged_in? 5 def logged_in?
6 current_user != nil 6 current_user != nil
7 end 7 end
8 - 8 +
9 # Accesses the current user from the session. 9 # Accesses the current user from the session.
10 def current_user 10 def current_user
11 @current_user ||= (session[:user] && User.find_by_id(session[:user])) || nil 11 @current_user ||= (session[:user] && User.find_by_id(session[:user])) || nil
12 end 12 end
13 - 13 +
14 # Store the given user in the session. 14 # Store the given user in the session.
15 def current_user=(new_user) 15 def current_user=(new_user)
16 if new_user.nil? 16 if new_user.nil?
17 session.delete(:user) 17 session.delete(:user)
18 else 18 else
19 session[:user] = new_user.id 19 session[:user] = new_user.id
  20 + new_user.register_login
20 end 21 end
21 @current_user = new_user 22 @current_user = new_user
22 end 23 end
23 - 24 +
24 # Check if the user is authorized. 25 # Check if the user is authorized.
25 # 26 #
26 # Override this method in your controllers if you want to restrict access 27 # Override this method in your controllers if you want to restrict access
@@ -62,7 +63,7 @@ module AuthenticatedSystem @@ -62,7 +63,7 @@ module AuthenticatedSystem
62 access_denied 63 access_denied
63 end 64 end
64 end 65 end
65 - 66 +
66 # Redirect as appropriate when an access request fails. 67 # Redirect as appropriate when an access request fails.
67 # 68 #
68 # The default action is to redirect to the login screen. 69 # The default action is to redirect to the login screen.
@@ -88,15 +89,15 @@ module AuthenticatedSystem @@ -88,15 +89,15 @@ module AuthenticatedSystem
88 end 89 end
89 end 90 end
90 false 91 false
91 - end  
92 - 92 + end
  93 +
93 # Store the URI of the current request in the session. 94 # Store the URI of the current request in the session.
94 # 95 #
95 # We can return to this location by calling #redirect_back_or_default. 96 # We can return to this location by calling #redirect_back_or_default.
96 def store_location(location = request.url) 97 def store_location(location = request.url)
97 session[:return_to] = location 98 session[:return_to] = location
98 end 99 end
99 - 100 +
100 # Redirect to the URI stored by the most recent store_location call or 101 # Redirect to the URI stored by the most recent store_location call or
101 # to the passed default. 102 # to the passed default.
102 def redirect_back_or_default(default) 103 def redirect_back_or_default(default)
@@ -106,7 +107,7 @@ module AuthenticatedSystem @@ -106,7 +107,7 @@ module AuthenticatedSystem
106 redirect_to(default) 107 redirect_to(default)
107 end 108 end
108 end 109 end
109 - 110 +
110 # Inclusion hook to make #current_user and #logged_in? 111 # Inclusion hook to make #current_user and #logged_in?
111 # available as ActionView helper methods. 112 # available as ActionView helper methods.
112 def self.included(base) 113 def self.included(base)
@@ -132,6 +133,6 @@ module AuthenticatedSystem @@ -132,6 +133,6 @@ module AuthenticatedSystem
132 def get_auth_data 133 def get_auth_data
133 auth_key = @@http_auth_headers.detect { |h| request.env.has_key?(h) } 134 auth_key = @@http_auth_headers.detect { |h| request.env.has_key?(h) }
134 auth_data = request.env[auth_key].to_s.split unless auth_key.blank? 135 auth_data = request.env[auth_key].to_s.split unless auth_key.blank?
135 - return auth_data && auth_data[0] == 'Basic' ? Base64.decode64(auth_data[1]).split(':')[0..1] : [nil, nil] 136 + return auth_data && auth_data[0] == 'Basic' ? Base64.decode64(auth_data[1]).split(':')[0..1] : [nil, nil]
136 end 137 end
137 end 138 end