2009-10-19 22:31:33 -04:00
|
|
|
require 'digest/sha1'
|
2009-11-14 21:13:43 -05:00
|
|
|
require 'devise/serializers/rememberable'
|
2009-10-19 22:31:33 -04:00
|
|
|
|
|
|
|
module Devise
|
|
|
|
module Models
|
|
|
|
|
2009-10-19 22:52:31 -04:00
|
|
|
# Rememberable manages generating and clearing token for remember the user
|
|
|
|
# from a saved cookie. Rememberable also has utility methods for dealing
|
|
|
|
# with serializing the user into the cookie and back from the cookie, trying
|
|
|
|
# to lookup the record based on the saved information.
|
|
|
|
# You probably wouldn't use rememberable methods directly, they are used
|
|
|
|
# mostly internally for handling the remember token.
|
2009-10-22 07:49:19 -04:00
|
|
|
#
|
|
|
|
# Configuration:
|
|
|
|
#
|
|
|
|
# remember_for: the time you want the user will be remembered without
|
|
|
|
# asking for credentials. After this time the user will be
|
|
|
|
# blocked and will have to enter his credentials again.
|
|
|
|
# This configuration is also used to calculate the expires
|
|
|
|
# time for the cookie created to remember the user.
|
2009-10-22 07:54:23 -04:00
|
|
|
# By default remember_for is 2.weeks.
|
2009-10-22 07:49:19 -04:00
|
|
|
#
|
2009-10-19 22:52:31 -04:00
|
|
|
# Examples:
|
|
|
|
#
|
|
|
|
# User.find(1).remember_me! # regenerating the token
|
|
|
|
# User.find(1).forget_me! # clearing the token
|
2009-10-20 06:44:21 -04:00
|
|
|
#
|
2009-10-19 22:52:31 -04:00
|
|
|
# # generating info to put into cookies
|
|
|
|
# User.serialize_into_cookie(user)
|
2009-10-20 06:44:21 -04:00
|
|
|
#
|
2009-10-19 22:52:31 -04:00
|
|
|
# # lookup the user based on the incoming cookie information
|
|
|
|
# User.serialize_from_cookie(cookie_string)
|
2009-10-19 22:31:33 -04:00
|
|
|
module Rememberable
|
|
|
|
|
|
|
|
def self.included(base)
|
|
|
|
base.class_eval do
|
|
|
|
extend ClassMethods
|
|
|
|
|
|
|
|
# Remember me option available in after_authentication hook.
|
|
|
|
attr_accessor :remember_me
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Generate a new remember token and save the record without validations.
|
|
|
|
def remember_me!
|
2009-11-18 06:26:47 -05:00
|
|
|
self.remember_token = Devise.friendly_token
|
2009-11-02 20:14:27 -05:00
|
|
|
self.remember_created_at = Time.now.utc
|
2009-10-19 22:31:33 -04:00
|
|
|
save(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Removes the remember token only if it exists, and save the record
|
|
|
|
# without validations.
|
|
|
|
def forget_me!
|
2009-11-21 21:24:34 -05:00
|
|
|
if remember_token
|
2009-10-19 22:31:33 -04:00
|
|
|
self.remember_token = nil
|
2009-10-22 07:09:34 -04:00
|
|
|
self.remember_created_at = nil
|
2009-10-19 22:31:33 -04:00
|
|
|
save(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Checks whether the incoming token matches or not with the record token.
|
|
|
|
def valid_remember_token?(token)
|
2009-11-21 21:24:34 -05:00
|
|
|
remember_token && !remember_expired? && remember_token == token
|
2009-10-22 07:09:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Remember token should be expired if expiration time not overpass now.
|
|
|
|
def remember_expired?
|
2009-11-02 20:14:27 -05:00
|
|
|
remember_expires_at <= Time.now.utc
|
2009-10-22 07:09:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Remember token expires at created time + remember_for configuration
|
|
|
|
def remember_expires_at
|
|
|
|
remember_created_at + remember_for
|
2009-10-19 22:31:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
|
|
|
|
# Create the cookie key using the record id and remember_token
|
2009-10-22 07:09:34 -04:00
|
|
|
def serialize_into_cookie(rememberable)
|
|
|
|
"#{rememberable.id}::#{rememberable.remember_token}"
|
2009-10-19 22:31:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Recreate the user based on the stored cookie
|
|
|
|
def serialize_from_cookie(cookie)
|
2009-10-22 07:09:34 -04:00
|
|
|
rememberable_id, remember_token = cookie.split('::')
|
|
|
|
rememberable = find_by_id(rememberable_id) if rememberable_id
|
|
|
|
rememberable if rememberable.try(:valid_remember_token?, remember_token)
|
2009-10-19 22:31:33 -04:00
|
|
|
end
|
|
|
|
end
|
2009-10-22 17:02:03 -04:00
|
|
|
|
2009-11-09 19:00:44 -05:00
|
|
|
Devise::Models.config(self, :remember_for)
|
2009-10-19 22:31:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|