mirror of
https://github.com/heartcombo/devise.git
synced 2026-04-28 03:00:29 -04:00
Added trackable for sign_in_count, sign_in_at and sign_in_ip.
This commit is contained in:
@@ -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 = {
|
||||
|
||||
@@ -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
|
||||
|
||||
18
lib/devise/hooks/trackable.rb
Normal file
18
lib/devise/hooks/trackable.rb
Normal 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
|
||||
@@ -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
|
||||
|
||||
16
lib/devise/models/trackable.rb
Normal file
16
lib/devise/models/trackable.rb
Normal 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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user