2009-09-17 14:06:46 +00:00
|
|
|
module Devise
|
|
|
|
module Confirmable
|
2009-10-08 01:11:04 +00:00
|
|
|
require 'devise/models/perishable'
|
2009-09-17 14:06:46 +00:00
|
|
|
|
|
|
|
def self.included(base)
|
|
|
|
base.class_eval do
|
2009-09-20 13:35:26 +00:00
|
|
|
include ::Devise::Perishable
|
2009-10-08 01:59:55 +00:00
|
|
|
extend ClassMethods
|
2009-09-17 14:06:46 +00:00
|
|
|
|
2009-09-17 22:54:19 +00:00
|
|
|
after_create :send_confirmation_instructions
|
2009-10-08 22:50:46 +00:00
|
|
|
before_update :reset_confirmation, :if => :email_changed?
|
|
|
|
after_update :send_confirmation_instructions, :if => :email_changed?
|
2009-09-17 14:06:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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!
|
|
|
|
unless confirmed?
|
|
|
|
update_attribute(:confirmed_at, Time.now)
|
|
|
|
else
|
|
|
|
errors.add(:email, :already_confirmed, :default => 'already confirmed')
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Verifies whether a user is confirmed or not
|
|
|
|
#
|
|
|
|
def confirmed?
|
|
|
|
!new_record? && confirmed_at?
|
|
|
|
end
|
|
|
|
|
2009-10-08 03:53:04 +00:00
|
|
|
# Send confirmation instructions by email
|
|
|
|
#
|
|
|
|
def send_confirmation_instructions
|
|
|
|
::Notifier.deliver_confirmation_instructions(self)
|
|
|
|
end
|
2009-09-17 22:54:19 +00:00
|
|
|
|
2009-10-08 22:50:46 +00: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!
|
|
|
|
reset_confirmation
|
|
|
|
reset_perishable_token!
|
|
|
|
send_confirmation_instructions
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Remove confirmation date from the user, ensuring after a user update it's
|
|
|
|
# email, it won't be able to sign in without confirming it.
|
|
|
|
#
|
|
|
|
def reset_confirmation
|
|
|
|
self.confirmed_at = nil
|
|
|
|
end
|
|
|
|
|
2009-09-17 14:06:46 +00:00
|
|
|
module ClassMethods
|
|
|
|
|
2009-09-18 00:29:13 +00:00
|
|
|
# Hook default authenticate to test whether the account is confirmed or not
|
2009-09-17 22:54:19 +00:00
|
|
|
# Returns the authenticated_user if it's confirmed, otherwise returns nil
|
2009-09-18 00:29:13 +00:00
|
|
|
#
|
2009-09-17 23:03:38 +00:00
|
|
|
def authenticate(email, password)
|
|
|
|
confirmable = super
|
|
|
|
confirmable if confirmable.confirmed? unless confirmable.nil?
|
|
|
|
end
|
2009-09-17 22:54:19 +00:00
|
|
|
|
2009-10-08 03:53:04 +00:00
|
|
|
# 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
|
|
|
|
#
|
|
|
|
def send_confirmation_instructions(options={})
|
2009-10-08 21:43:29 +00:00
|
|
|
confirmable = find_or_initialize_with_error_by_email(options[:email])
|
2009-10-08 22:50:46 +00:00
|
|
|
confirmable.reset_confirmation! unless confirmable.new_record?
|
2009-10-08 03:53:04 +00:00
|
|
|
confirmable
|
|
|
|
end
|
|
|
|
|
2009-09-17 14:06:46 +00:00
|
|
|
# Find a user by it's confirmation token and try to confirm it.
|
|
|
|
# If no user is found, returns a new user
|
|
|
|
# If the user is already confirmed, create an error for the user
|
2009-10-08 02:12:48 +00:00
|
|
|
# Options must have the perishable_token
|
2009-09-18 00:29:13 +00:00
|
|
|
#
|
2009-10-08 02:12:48 +00:00
|
|
|
def confirm!(options={})
|
|
|
|
confirmable = find_or_initialize_with_error_by_perishable_token(options[:perishable_token])
|
2009-10-08 01:59:55 +00:00
|
|
|
confirmable.confirm! unless confirmable.new_record?
|
2009-09-17 14:27:45 +00:00
|
|
|
confirmable
|
2009-09-17 14:06:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|