Added trackable for sign_in_count, sign_in_at and sign_in_ip.

This commit is contained in:
José Valim
2009-11-24 15:18:42 -02:00
parent 52885725a9
commit f9c5dd6a79
11 changed files with 134 additions and 24 deletions

View File

@@ -1,5 +1,6 @@
module Devise
ALL = [:authenticatable, :confirmable, :recoverable, :rememberable, :timeoutable, :validatable].freeze
ALL = [:authenticatable, :confirmable, :recoverable, :rememberable,
:trackable, :timeoutable, :validatable].freeze
# Maps controller names to devise modules
CONTROLLERS = {

View File

@@ -4,16 +4,13 @@
# record is set, we set the last request time inside it's scoped session to
# verify timeout in the following request.
Warden::Manager.after_set_user do |record, warden, options|
if record && record.respond_to?(:timeout?)
scope = options[:scope]
# Record may have already been logged out by another hook (ie confirmable).
if warden.authenticated?(scope)
last_request_at = warden.session(scope)['last_request_at']
if record.timeout?(last_request_at)
warden.logout(scope)
throw :warden, :scope => scope, :message => :timeout
end
warden.session(scope)['last_request_at'] = Time.now.utc
scope = options[:scope]
if record && record.respond_to?(:timeout?) && warden.authenticated?(scope)
last_request_at = warden.session(scope)['last_request_at']
if record.timeout?(last_request_at)
warden.logout(scope)
throw :warden, :scope => scope, :message => :timeout
end
warden.session(scope)['last_request_at'] = Time.now.utc
end
end

View File

@@ -0,0 +1,18 @@
# After each sign in, update sign in time, sign in count and sign in IP.
Warden::Manager.after_authentication do |record, warden, options|
scope = options[:scope]
if Devise.mappings[scope].try(:trackable?) && warden.authenticated?(scope)
old_current, new_current = record.current_sign_in_at, Time.now
record.last_sign_in_at = old_current || new_current
record.current_sign_in_at = new_current
old_current, new_current = record.current_sign_in_ip, warden.request.remote_ip
record.last_sign_in_ip = old_current || new_current
record.current_sign_in_ip = new_current
record.sign_in_count ||= 0
record.sign_in_count += 1
record.save(false)
end
end

View File

@@ -75,6 +75,7 @@ module Devise
options = modules.extract_options!
modules = Devise.all if modules.include?(:all)
modules -= Array(options.delete(:except))
modules = Devise::ALL & modules
if !modules.include?(:authenticatable)
modules = [:authenticatable] | modules

View File

@@ -0,0 +1,16 @@
require 'devise/hooks/trackable'
module Devise
module Models
# Track information about your user sign in. It tracks the following columns:
#
# * sign_in_count - Increased every time a sign in is made (by form, openid, oauth)
# * current_sign_in_at - A tiemstamp updated when the user signs in
# * last_sign_in_at - Holds the timestamp of the previous sign in
# * current_sign_in_ip - The remote ip updated when the user sign in
# * last_sign_in_at - Holds the remote ip of the previous sign in
#
module Trackable
end
end
end

View File

@@ -41,6 +41,16 @@ module Devise
apply_schema :remember_created_at, DateTime
end
# Creates sign_in_count, current_sign_in_at, last_sign_in_at,
# current_sign_in_ip, last_sign_in_in.
def trackable
apply_schema :sign_in_count, Integer
apply_schema :current_sign_in_at, DateTime
apply_schema :last_sign_in_at, DateTime
apply_schema :current_sign_in_ip, String
apply_schema :last_sign_in_ip, String
end
# Overwrite with specific modification to create your own schema.
def apply_schema(name, type, options={})
raise NotImplementedError