2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
class EmailToken < ActiveRecord::Base
|
2021-11-25 02:34:39 -05:00
|
|
|
class TokenAccessError < StandardError; end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
belongs_to :user
|
|
|
|
|
2021-11-25 02:34:39 -05:00
|
|
|
validates :user_id, :email, :token_hash, presence: true
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2021-11-25 02:34:39 -05:00
|
|
|
scope :unconfirmed, -> { where(confirmed: false) }
|
|
|
|
scope :active, -> { where(expired: false).where('created_at >= ?', SiteSetting.email_token_valid_hours.hours.ago) }
|
|
|
|
|
|
|
|
after_initialize do
|
|
|
|
if self.token_hash.blank?
|
|
|
|
@token ||= SecureRandom.hex
|
|
|
|
self.token_hash = self.class.hash_token(@token)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
after_create do
|
2021-11-25 02:34:39 -05:00
|
|
|
EmailToken
|
|
|
|
.where(user_id: self.user_id)
|
|
|
|
.where(scope: [nil, self.scope])
|
|
|
|
.where.not(id: self.id)
|
2016-05-02 17:15:32 -04:00
|
|
|
.update_all(expired: true)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2021-11-25 02:34:39 -05:00
|
|
|
before_validation do
|
|
|
|
self.email = self.email.downcase if self.email
|
2014-03-04 14:03:04 -05:00
|
|
|
end
|
|
|
|
|
2021-11-25 02:34:39 -05:00
|
|
|
before_save do
|
|
|
|
if self.scope.blank?
|
|
|
|
Discourse.deprecate("EmailToken#scope cannot be empty.", output_in_test: true)
|
|
|
|
end
|
2013-02-22 11:49:48 -05:00
|
|
|
end
|
|
|
|
|
2021-12-13 00:29:47 -05:00
|
|
|
# TODO(2022-01-01): Remove
|
|
|
|
self.ignored_columns = %w{token}
|
|
|
|
|
2021-11-25 02:34:39 -05:00
|
|
|
def self.scopes
|
|
|
|
@scopes ||= Enum.new(
|
|
|
|
signup: 1,
|
|
|
|
password_reset: 2,
|
|
|
|
email_login: 3,
|
|
|
|
email_update: 4,
|
|
|
|
)
|
2013-02-22 11:49:48 -05:00
|
|
|
end
|
|
|
|
|
2021-11-25 02:34:39 -05:00
|
|
|
def token
|
|
|
|
raise TokenAccessError.new if @token.blank?
|
2021-12-13 00:29:47 -05:00
|
|
|
|
|
|
|
@token
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2021-11-25 02:34:39 -05:00
|
|
|
def self.confirm(token, scope: nil, skip_reviewable: false)
|
|
|
|
User.transaction do
|
|
|
|
email_token = confirmable(token, scope: scope)
|
|
|
|
return if email_token.blank?
|
|
|
|
|
|
|
|
email_token.update!(confirmed: true)
|
|
|
|
|
|
|
|
user = email_token.user
|
|
|
|
user.send_welcome_message = !user.active?
|
|
|
|
user.email = email_token.email
|
|
|
|
user.active = true
|
|
|
|
user.custom_fields.delete('activation_reminder')
|
|
|
|
user.save!
|
|
|
|
user.create_reviewable if !skip_reviewable
|
|
|
|
user.set_automatic_groups
|
|
|
|
DiscourseEvent.trigger(:user_confirmed_email, user)
|
|
|
|
Invite.redeem_from_email(user.email)
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
end
|
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
# If the user's email is already taken, just return nil (failure)
|
2014-08-25 15:30:52 -04:00
|
|
|
end
|
|
|
|
|
2021-11-25 02:34:39 -05:00
|
|
|
def self.confirmable(token, scope: nil)
|
|
|
|
return nil if token.blank?
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2021-11-25 02:34:39 -05:00
|
|
|
relation = unconfirmed.active
|
|
|
|
.includes(:user)
|
|
|
|
.where(token_hash: hash_token(token))
|
2016-05-02 17:15:32 -04:00
|
|
|
|
2021-11-25 02:34:39 -05:00
|
|
|
# TODO(2022-01-01): All email tokens should have scopes by now
|
|
|
|
if !scope
|
|
|
|
relation.first
|
2016-05-02 17:15:32 -04:00
|
|
|
else
|
2021-11-25 02:34:39 -05:00
|
|
|
relation.where(scope: scope).first || relation.where(scope: nil).first
|
2016-03-07 14:40:11 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-25 02:34:39 -05:00
|
|
|
def self.enqueue_signup_email(email_token, to_address: nil)
|
|
|
|
Jobs.enqueue(
|
|
|
|
:critical_user_email,
|
|
|
|
type: :signup,
|
|
|
|
user_id: email_token.user_id,
|
|
|
|
email_token: email_token.token,
|
|
|
|
to_address: to_address
|
|
|
|
)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2016-01-04 11:48:54 -05:00
|
|
|
|
2021-11-25 02:34:39 -05:00
|
|
|
def self.hash_token(token)
|
|
|
|
Digest::SHA256.hexdigest(token)
|
2016-01-04 11:48:54 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-05-23 22:48:32 -04:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: email_tokens
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# user_id :integer not null
|
2019-01-11 14:29:56 -05:00
|
|
|
# email :string not null
|
2013-05-23 22:48:32 -04:00
|
|
|
# confirmed :boolean default(FALSE), not null
|
|
|
|
# expired :boolean default(FALSE), not null
|
2014-08-27 01:19:25 -04:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2021-11-25 02:34:39 -05:00
|
|
|
# token_hash :string not null
|
|
|
|
# scope :integer
|
2013-05-23 22:48:32 -04:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2021-12-06 12:17:32 -05:00
|
|
|
# index_email_tokens_on_token_hash (token_hash) UNIQUE
|
|
|
|
# index_email_tokens_on_user_id (user_id)
|
2013-05-23 22:48:32 -04:00
|
|
|
#
|