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