2013-02-05 14:16:51 -05:00
|
|
|
class EmailToken < ActiveRecord::Base
|
|
|
|
belongs_to :user
|
|
|
|
|
|
|
|
validates_presence_of :token
|
|
|
|
validates_presence_of :user_id
|
|
|
|
validates_presence_of :email
|
|
|
|
|
2013-02-28 13:54:12 -05:00
|
|
|
before_validation(on: :create) do
|
2013-02-05 14:16:51 -05:00
|
|
|
self.token = EmailToken.generate_token
|
|
|
|
end
|
|
|
|
|
|
|
|
after_create do
|
|
|
|
# Expire the previous tokens
|
2013-02-07 10:45:24 -05:00
|
|
|
EmailToken.update_all 'expired = true', ['user_id = ? and id != ?', self.user_id, self.id]
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.token_length
|
|
|
|
16
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.valid_after
|
|
|
|
1.week.ago
|
|
|
|
end
|
|
|
|
|
2013-02-22 11:49:48 -05:00
|
|
|
def self.unconfirmed
|
|
|
|
where(confirmed: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.active
|
2013-02-22 15:19:44 -05:00
|
|
|
where(expired: false).where('created_at > ?', valid_after)
|
2013-02-22 11:49:48 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def self.generate_token
|
|
|
|
SecureRandom.hex(EmailToken.token_length)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.confirm(token)
|
|
|
|
return unless token.present?
|
|
|
|
return unless token.length/2 == EmailToken.token_length
|
|
|
|
|
2013-02-28 13:54:12 -05:00
|
|
|
email_token = EmailToken.where("token = ? and expired = FALSE and created_at >= ?", token, EmailToken.valid_after).includes(:user).first
|
2013-02-05 14:16:51 -05:00
|
|
|
return if email_token.blank?
|
|
|
|
|
|
|
|
user = email_token.user
|
|
|
|
User.transaction do
|
2013-02-28 13:54:12 -05:00
|
|
|
row_count = EmailToken.update_all 'confirmed = true', id: email_token.id, expired: false
|
2013-02-05 14:16:51 -05:00
|
|
|
if row_count == 1
|
|
|
|
# If we are activating the user, send the welcome message
|
|
|
|
user.send_welcome_message = !user.active?
|
|
|
|
|
|
|
|
user.active = true
|
|
|
|
user.email = email_token.email
|
|
|
|
user.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
user
|
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
# If the user's email is already taken, just return nil (failure)
|
|
|
|
end
|
|
|
|
end
|