2009-10-22 17:02:03 -04:00
|
|
|
require 'devise/hooks/confirmable'
|
|
|
|
|
2009-09-17 10:06:46 -04:00
|
|
|
module Devise
|
2009-10-09 07:30:25 -04:00
|
|
|
module Models
|
2009-10-09 08:27:44 -04:00
|
|
|
|
|
|
|
# Confirmable is responsible to verify if an account is already confirmed to
|
2009-10-13 16:01:42 -04:00
|
|
|
# sign in, and to send emails with confirmation instructions.
|
2009-10-09 08:27:44 -04:00
|
|
|
# Confirmation instructions are sent to the user email after creating a
|
|
|
|
# record, after updating it's email and also when manually requested by
|
|
|
|
# a new confirmation instruction request.
|
|
|
|
# Whenever the user update it's email, his account is automatically unconfirmed,
|
|
|
|
# it means it won't be able to sign in again without confirming the account
|
|
|
|
# again through the email that was sent.
|
2009-10-22 07:49:19 -04:00
|
|
|
#
|
|
|
|
# Configuration:
|
|
|
|
#
|
2009-10-30 05:23:47 -04:00
|
|
|
# confirm_within: the time you want the user will have to confirm it's account
|
|
|
|
# without blocking his access. When confirm_within is zero, the
|
|
|
|
# user won't be able to sign in without confirming. You can
|
|
|
|
# use this to let your user access some features of your
|
|
|
|
# application without confirming the account, but blocking it
|
|
|
|
# after a certain period (ie 7 days). By default confirm_within is
|
|
|
|
# zero, it means users always have to confirm to sign in.
|
2009-10-22 07:49:19 -04:00
|
|
|
#
|
2009-10-09 08:27:44 -04:00
|
|
|
# Examples:
|
|
|
|
#
|
|
|
|
# User.find(1).confirm! # returns true unless it's already confirmed
|
|
|
|
# User.find(1).confirmed? # true/false
|
|
|
|
# User.find(1).send_confirmation_instructions # manually send instructions
|
|
|
|
# User.find(1).reset_confirmation! # reset confirmation status and send instructions
|
2009-10-09 07:30:25 -04:00
|
|
|
module Confirmable
|
2009-09-17 10:06:46 -04:00
|
|
|
|
2009-10-09 07:30:25 -04:00
|
|
|
def self.included(base)
|
|
|
|
base.class_eval do
|
|
|
|
extend ClassMethods
|
2009-09-17 10:06:46 -04:00
|
|
|
|
2009-10-30 05:49:18 -04:00
|
|
|
before_create :generate_confirmation_token
|
|
|
|
after_create :send_confirmation_instructions
|
2009-10-09 07:30:25 -04:00
|
|
|
end
|
2009-09-17 10:06:46 -04:00
|
|
|
end
|
|
|
|
|
2009-10-09 07:30:25 -04:00
|
|
|
# Confirm a user by setting it's confirmed_at to actual time. If the user
|
|
|
|
# is already confirmed, add en error to email field
|
|
|
|
def confirm!
|
2009-10-15 15:43:30 -04:00
|
|
|
unless_confirmed do
|
2009-10-22 22:54:42 -04:00
|
|
|
self.confirmation_token = nil
|
|
|
|
self.confirmed_at = Time.now
|
|
|
|
save(false)
|
2009-10-09 07:30:25 -04:00
|
|
|
end
|
2009-09-17 10:06:46 -04:00
|
|
|
end
|
2009-10-08 18:50:46 -04:00
|
|
|
|
2009-10-09 07:30:25 -04:00
|
|
|
# Verifies whether a user is confirmed or not
|
|
|
|
def confirmed?
|
2009-11-21 21:24:34 -05:00
|
|
|
!new_record? && !confirmed_at.nil?
|
2009-10-08 18:50:46 -04:00
|
|
|
end
|
|
|
|
|
2009-10-09 07:30:25 -04:00
|
|
|
# Send confirmation instructions by email
|
|
|
|
def send_confirmation_instructions
|
2009-11-02 20:14:27 -05:00
|
|
|
::DeviseMailer.deliver_confirmation_instructions(self)
|
2009-09-17 19:03:38 -04:00
|
|
|
end
|
2009-09-17 18:54:19 -04:00
|
|
|
|
2009-10-09 07:30:25 -04:00
|
|
|
# Remove confirmation date and send confirmation instructions, to ensure
|
|
|
|
# after sending these instructions the user won't be able to sign in without
|
|
|
|
# confirming it's account
|
|
|
|
def reset_confirmation!
|
2009-10-15 15:54:04 -04:00
|
|
|
unless_confirmed do
|
2009-10-30 05:49:18 -04:00
|
|
|
generate_confirmation_token
|
2009-10-15 16:05:46 -04:00
|
|
|
save(false)
|
2009-10-15 15:54:04 -04:00
|
|
|
send_confirmation_instructions
|
|
|
|
end
|
2009-10-07 23:53:04 -04:00
|
|
|
end
|
|
|
|
|
2009-10-20 19:32:30 -04:00
|
|
|
# Verify whether a user is active to sign in or not. If the user is
|
|
|
|
# already confirmed, it should never be blocked. Otherwise we need to
|
|
|
|
# calculate if the confirm time has not expired for this user, in other
|
|
|
|
# words, if the confirmation is still valid.
|
|
|
|
def active?
|
|
|
|
confirmed? || confirmation_period_valid?
|
|
|
|
end
|
|
|
|
|
2009-10-20 09:08:40 -04:00
|
|
|
protected
|
2009-10-09 07:30:25 -04:00
|
|
|
|
2009-10-20 19:32:30 -04:00
|
|
|
# Checks if the confirmation for the user is within the limit time.
|
|
|
|
# We do this by calculating if the difference between today and the
|
|
|
|
# confirmation sent date does not exceed the confirm in time configured.
|
|
|
|
# Confirm_in is a model configuration, must always be an integer value.
|
2009-10-22 22:54:42 -04:00
|
|
|
#
|
2009-10-20 19:32:30 -04:00
|
|
|
# Example:
|
2009-10-22 22:54:42 -04:00
|
|
|
#
|
2009-10-30 05:23:47 -04:00
|
|
|
# # confirm_within = 1.day and confirmation_sent_at = today
|
2009-10-20 19:32:30 -04:00
|
|
|
# confirmation_period_valid? # returns true
|
2009-10-22 22:54:42 -04:00
|
|
|
#
|
2009-10-30 05:23:47 -04:00
|
|
|
# # confirm_within = 5.days and confirmation_sent_at = 4.days.ago
|
2009-10-20 19:32:30 -04:00
|
|
|
# confirmation_period_valid? # returns true
|
2009-10-22 22:54:42 -04:00
|
|
|
#
|
2009-10-30 05:23:47 -04:00
|
|
|
# # confirm_within = 5.days and confirmation_sent_at = 5.days.ago
|
2009-10-20 19:32:30 -04:00
|
|
|
# confirmation_period_valid? # returns false
|
2009-10-22 22:54:42 -04:00
|
|
|
#
|
2009-10-30 05:23:47 -04:00
|
|
|
# # confirm_within = 0.days
|
2009-10-20 19:32:30 -04:00
|
|
|
# confirmation_period_valid? # will always return false
|
2009-10-22 22:54:42 -04:00
|
|
|
#
|
2009-10-20 19:32:30 -04:00
|
|
|
def confirmation_period_valid?
|
2009-11-24 08:00:35 -05:00
|
|
|
confirmation_sent_at && confirmation_sent_at.utc >= self.class.confirm_within.ago
|
2009-10-09 07:30:25 -04:00
|
|
|
end
|
|
|
|
|
2009-10-18 07:14:52 -04:00
|
|
|
# Checks whether the record is confirmed or not, yielding to the block
|
|
|
|
# if it's already confirmed, otherwise adds an error to email.
|
2009-10-15 15:43:30 -04:00
|
|
|
def unless_confirmed
|
|
|
|
unless confirmed?
|
|
|
|
yield
|
|
|
|
else
|
|
|
|
errors.add(:email, :already_confirmed, :default => 'already confirmed')
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-18 07:14:52 -04:00
|
|
|
# Generates a new random token for confirmation, and stores the time
|
|
|
|
# this token is being generated
|
|
|
|
def generate_confirmation_token
|
2009-10-30 05:49:18 -04:00
|
|
|
self.confirmed_at = nil
|
2009-11-18 06:26:47 -05:00
|
|
|
self.confirmation_token = Devise.friendly_token
|
2009-10-18 07:14:52 -04:00
|
|
|
self.confirmation_sent_at = Time.now.utc
|
|
|
|
end
|
|
|
|
|
2009-10-09 07:30:25 -04:00
|
|
|
module ClassMethods
|
|
|
|
# Attempt to find a user by it's email. If a record is found, send new
|
|
|
|
# confirmation instructions to it. If not user is found, returns a new user
|
|
|
|
# with an email not found error.
|
|
|
|
# Options must contain the user email
|
2009-10-10 15:20:23 -04:00
|
|
|
def send_confirmation_instructions(attributes={})
|
|
|
|
confirmable = find_or_initialize_with_error_by_email(attributes[:email])
|
2009-10-09 07:30:25 -04:00
|
|
|
confirmable.reset_confirmation! unless confirmable.new_record?
|
|
|
|
confirmable
|
|
|
|
end
|
|
|
|
|
|
|
|
# Find a user by it's confirmation token and try to confirm it.
|
2009-10-18 08:16:30 -04:00
|
|
|
# If no user is found, returns a new user with an error.
|
2009-10-09 07:30:25 -04:00
|
|
|
# If the user is already confirmed, create an error for the user
|
2009-10-18 08:16:30 -04:00
|
|
|
# Options must have the confirmation_token
|
2009-10-10 15:20:23 -04:00
|
|
|
def confirm!(attributes={})
|
2009-11-24 15:02:36 -05:00
|
|
|
confirmable = find_or_initialize_by(:confirmation_token, attributes[:confirmation_token])
|
2009-10-18 07:14:52 -04:00
|
|
|
if confirmable.new_record?
|
|
|
|
confirmable.errors.add(:confirmation_token, :invalid)
|
2009-10-18 08:16:30 -04:00
|
|
|
else
|
|
|
|
confirmable.confirm!
|
2009-10-18 07:14:52 -04:00
|
|
|
end
|
|
|
|
confirmable
|
|
|
|
end
|
2009-10-22 17:02:03 -04:00
|
|
|
|
2009-11-22 19:32:54 -05:00
|
|
|
Devise::Models.config(self, :confirm_within)
|
|
|
|
end
|
2009-09-17 10:06:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|