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