2009-09-17 11:06:46 -03:00
|
|
|
module Devise
|
2009-10-09 08:30:25 -03:00
|
|
|
module Models
|
2009-10-09 09:27:44 -03:00
|
|
|
# Confirmable is responsible to verify if an account is already confirmed to
|
2009-10-13 17:01:42 -03:00
|
|
|
# sign in, and to send emails with confirmation instructions.
|
2009-10-09 09:27:44 -03: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 09:49:19 -02:00
|
|
|
#
|
|
|
|
# Configuration:
|
|
|
|
#
|
2009-10-30 07:23:47 -02: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 09:49:19 -02:00
|
|
|
#
|
2009-10-09 09:27:44 -03: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
|
2009-12-15 00:16:22 +01:00
|
|
|
# User.find(1).resend_confirmation! # generates a new token and resent it
|
2009-10-09 08:30:25 -03:00
|
|
|
module Confirmable
|
2010-02-17 12:35:38 +01:00
|
|
|
extend ActiveSupport::Concern
|
2009-09-17 11:06:46 -03:00
|
|
|
|
2010-02-17 12:35:38 +01:00
|
|
|
included do
|
|
|
|
before_create :generate_confirmation_token, :if => :confirmation_required?
|
|
|
|
after_create :send_confirmation_instructions, :if => :confirmation_required?
|
2009-09-17 11:06:46 -03:00
|
|
|
end
|
|
|
|
|
2009-10-09 08:30:25 -03: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 16:43:30 -03:00
|
|
|
unless_confirmed do
|
2009-10-23 00:54:42 -02:00
|
|
|
self.confirmation_token = nil
|
|
|
|
self.confirmed_at = Time.now
|
2010-02-16 14:31:49 +01:00
|
|
|
save(:validate => false)
|
2009-10-09 08:30:25 -03:00
|
|
|
end
|
2009-09-17 11:06:46 -03:00
|
|
|
end
|
2009-10-08 19:50:46 -03:00
|
|
|
|
2009-10-09 08:30:25 -03:00
|
|
|
# Verifies whether a user is confirmed or not
|
|
|
|
def confirmed?
|
2010-03-28 13:26:07 -07:00
|
|
|
persisted? && !confirmed_at.nil?
|
2009-10-08 19:50:46 -03:00
|
|
|
end
|
|
|
|
|
2009-10-09 08:30:25 -03:00
|
|
|
# Send confirmation instructions by email
|
|
|
|
def send_confirmation_instructions
|
2010-05-06 07:48:26 -05:00
|
|
|
generate_confirmation_token if self.confirmation_token.nil?
|
2010-06-12 20:56:55 +02:00
|
|
|
::Devise.mailer.confirmation_instructions(self).deliver
|
2009-09-17 20:03:38 -03:00
|
|
|
end
|
2009-09-17 19:54:19 -03:00
|
|
|
|
2010-03-28 23:09:28 +02:00
|
|
|
# Resend confirmation token. This method does not need to generate a new token.
|
2010-03-10 16:13:54 +01:00
|
|
|
def resend_confirmation_token
|
2010-03-28 23:09:28 +02:00
|
|
|
unless_confirmed { send_confirmation_instructions }
|
2009-10-08 00:53:04 -03:00
|
|
|
end
|
|
|
|
|
2009-12-20 13:53:53 +01:00
|
|
|
# Overwrites active? from Devise::Models::Activatable for confirmation
|
|
|
|
# by verifying whether an 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.
|
2009-10-20 21:32:30 -02:00
|
|
|
def active?
|
2010-05-05 16:31:11 -05:00
|
|
|
super && (!confirmation_required? || confirmed? || confirmation_period_valid?)
|
2009-10-20 21:32:30 -02:00
|
|
|
end
|
|
|
|
|
2009-12-20 13:53:53 +01:00
|
|
|
# The message to be shown if the account is inactive.
|
|
|
|
def inactive_message
|
2010-03-10 16:13:54 +01:00
|
|
|
!confirmed? ? :unconfirmed : super
|
2009-12-20 13:53:53 +01:00
|
|
|
end
|
|
|
|
|
2009-12-15 00:30:28 +01:00
|
|
|
# If you don't want confirmation to be sent on create, neither a code
|
|
|
|
# to be generated, call skip_confirmation!
|
|
|
|
def skip_confirmation!
|
|
|
|
self.confirmed_at = Time.now
|
|
|
|
@skip_confirmation = true
|
|
|
|
end
|
|
|
|
|
2009-10-20 11:08:40 -02:00
|
|
|
protected
|
2009-10-09 08:30:25 -03:00
|
|
|
|
2009-12-15 00:30:28 +01:00
|
|
|
# Callback to overwrite if confirmation is required or not.
|
|
|
|
def confirmation_required?
|
|
|
|
!@skip_confirmation
|
|
|
|
end
|
|
|
|
|
2009-10-20 21:32:30 -02: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-23 00:54:42 -02:00
|
|
|
#
|
2009-10-20 21:32:30 -02:00
|
|
|
# Example:
|
2009-10-23 00:54:42 -02:00
|
|
|
#
|
2009-10-30 07:23:47 -02:00
|
|
|
# # confirm_within = 1.day and confirmation_sent_at = today
|
2009-10-20 21:32:30 -02:00
|
|
|
# confirmation_period_valid? # returns true
|
2009-10-23 00:54:42 -02:00
|
|
|
#
|
2009-10-30 07:23:47 -02:00
|
|
|
# # confirm_within = 5.days and confirmation_sent_at = 4.days.ago
|
2009-10-20 21:32:30 -02:00
|
|
|
# confirmation_period_valid? # returns true
|
2009-10-23 00:54:42 -02:00
|
|
|
#
|
2009-10-30 07:23:47 -02:00
|
|
|
# # confirm_within = 5.days and confirmation_sent_at = 5.days.ago
|
2009-10-20 21:32:30 -02:00
|
|
|
# confirmation_period_valid? # returns false
|
2009-10-23 00:54:42 -02:00
|
|
|
#
|
2009-10-30 07:23:47 -02:00
|
|
|
# # confirm_within = 0.days
|
2009-10-20 21:32:30 -02:00
|
|
|
# confirmation_period_valid? # will always return false
|
2009-10-23 00:54:42 -02:00
|
|
|
#
|
2009-10-20 21:32:30 -02:00
|
|
|
def confirmation_period_valid?
|
2009-11-24 11:00:35 -02:00
|
|
|
confirmation_sent_at && confirmation_sent_at.utc >= self.class.confirm_within.ago
|
2009-10-09 08:30:25 -03:00
|
|
|
end
|
|
|
|
|
2009-10-18 09:14:52 -02: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 16:43:30 -03:00
|
|
|
def unless_confirmed
|
|
|
|
unless confirmed?
|
|
|
|
yield
|
|
|
|
else
|
2010-02-16 17:00:36 +01:00
|
|
|
self.errors.add(:email, :already_confirmed)
|
2009-10-15 16:43:30 -03:00
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-18 09:14:52 -02:00
|
|
|
# Generates a new random token for confirmation, and stores the time
|
|
|
|
# this token is being generated
|
|
|
|
def generate_confirmation_token
|
2009-10-30 07:49:18 -02:00
|
|
|
self.confirmed_at = nil
|
2010-03-29 16:13:19 +02:00
|
|
|
self.confirmation_token = self.class.confirmation_token
|
2009-10-18 09:14:52 -02:00
|
|
|
self.confirmation_sent_at = Time.now.utc
|
|
|
|
end
|
|
|
|
|
2009-10-09 08:30:25 -03: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 16:20:23 -03:00
|
|
|
def send_confirmation_instructions(attributes={})
|
2009-11-24 21:35:07 -02:00
|
|
|
confirmable = find_or_initialize_with_error_by(:email, attributes[:email], :not_found)
|
2010-03-28 13:26:07 -07:00
|
|
|
confirmable.resend_confirmation_token if confirmable.persisted?
|
2009-10-09 08:30:25 -03:00
|
|
|
confirmable
|
|
|
|
end
|
|
|
|
|
|
|
|
# Find a user by it's confirmation token and try to confirm it.
|
2009-10-18 10:16:30 -02:00
|
|
|
# If no user is found, returns a new user with an error.
|
2009-10-09 08:30:25 -03:00
|
|
|
# If the user is already confirmed, create an error for the user
|
2009-10-18 10:16:30 -02:00
|
|
|
# Options must have the confirmation_token
|
2010-03-10 16:13:54 +01:00
|
|
|
def confirm_by_token(confirmation_token)
|
|
|
|
confirmable = find_or_initialize_with_error_by(:confirmation_token, confirmation_token)
|
2010-03-28 13:26:07 -07:00
|
|
|
confirmable.confirm! if confirmable.persisted?
|
2009-10-18 09:14:52 -02:00
|
|
|
confirmable
|
|
|
|
end
|
2009-10-22 19:02:03 -02:00
|
|
|
|
2010-03-29 16:13:19 +02:00
|
|
|
def confirmation_token
|
|
|
|
Devise.friendly_token
|
|
|
|
end
|
|
|
|
|
2009-11-22 22:32:54 -02:00
|
|
|
Devise::Models.config(self, :confirm_within)
|
|
|
|
end
|
2009-09-17 11:06:46 -03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|