2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-27 02:47:36 +08:00
|
|
|
class UserEmail < ActiveRecord::Base
|
|
|
|
belongs_to :user
|
|
|
|
|
2017-10-25 13:02:18 +08:00
|
|
|
attr_accessor :skip_validate_email
|
2021-02-22 19:42:37 +08:00
|
|
|
attr_accessor :skip_validate_unique_email
|
2024-11-25 11:55:27 +08:00
|
|
|
attr_accessor :skip_normalize_email
|
2017-08-09 10:56:08 +08:00
|
|
|
|
2017-04-27 02:47:36 +08:00
|
|
|
before_validation :strip_downcase_email
|
2021-11-24 17:30:06 +08:00
|
|
|
before_validation :normalize_email
|
2017-04-27 02:47:36 +08:00
|
|
|
|
2018-03-20 10:22:06 +08:00
|
|
|
validates :email, presence: true
|
2020-06-03 10:13:25 +08:00
|
|
|
validates :email, email: true, if: :validate_email?
|
2017-04-27 02:47:36 +08:00
|
|
|
|
2017-12-21 20:30:26 +08:00
|
|
|
validates :primary, uniqueness: { scope: [:user_id] }, if: %i[user_id primary]
|
2018-01-04 15:37:26 +08:00
|
|
|
validate :user_id_not_changed, if: :primary
|
2021-02-22 19:42:37 +08:00
|
|
|
validate :unique_email, if: :validate_unique_email?
|
2017-04-27 02:47:36 +08:00
|
|
|
|
2018-07-03 19:51:22 +08:00
|
|
|
scope :secondary, -> { where(primary: false) }
|
|
|
|
|
2023-01-05 06:08:55 +08:00
|
|
|
before_save -> { destroy_email_tokens(self.email_was) }, if: :will_save_change_to_email?
|
|
|
|
|
|
|
|
after_destroy { destroy_email_tokens(self.email) }
|
|
|
|
|
2021-11-24 17:30:06 +08:00
|
|
|
def normalize_email
|
|
|
|
self.normalized_email =
|
|
|
|
if self.email.present?
|
|
|
|
username, domain = self.email.split("@", 2)
|
|
|
|
username = username.gsub(".", "").gsub(/\+.*/, "")
|
|
|
|
"#{username}@#{domain}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-27 02:47:36 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def strip_downcase_email
|
|
|
|
if self.email
|
|
|
|
self.email = self.email.strip
|
|
|
|
self.email = self.email.downcase
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-09 10:56:08 +08:00
|
|
|
def validate_email?
|
2017-10-25 13:02:18 +08:00
|
|
|
return false if self.skip_validate_email
|
2017-04-27 02:47:36 +08:00
|
|
|
email_changed?
|
|
|
|
end
|
2018-01-04 15:37:26 +08:00
|
|
|
|
2021-02-22 19:42:37 +08:00
|
|
|
def validate_unique_email?
|
|
|
|
return false if self.skip_validate_unique_email
|
|
|
|
will_save_change_to_email?
|
|
|
|
end
|
|
|
|
|
2024-11-25 11:55:27 +08:00
|
|
|
def normalize_emails?
|
|
|
|
return false if self.skip_normalize_email
|
|
|
|
|
|
|
|
SiteSetting.normalize_emails?
|
|
|
|
end
|
|
|
|
|
2018-03-20 10:22:06 +08:00
|
|
|
def unique_email
|
2021-11-24 17:30:06 +08:00
|
|
|
email_exists =
|
2024-11-25 11:55:27 +08:00
|
|
|
if self.normalize_emails?
|
2021-11-24 17:30:06 +08:00
|
|
|
self
|
|
|
|
.class
|
|
|
|
.where("lower(email) = ? OR lower(normalized_email) = ?", email, normalized_email)
|
|
|
|
.exists?
|
|
|
|
else
|
|
|
|
self.class.where("lower(email) = ?", email).exists?
|
2018-03-20 10:22:06 +08:00
|
|
|
end
|
2021-11-24 17:30:06 +08:00
|
|
|
|
|
|
|
self.errors.add(:email, :taken) if email_exists
|
2018-03-20 10:22:06 +08:00
|
|
|
end
|
|
|
|
|
2018-01-04 15:37:26 +08:00
|
|
|
def user_id_not_changed
|
|
|
|
if self.will_save_change_to_user_id? && self.persisted?
|
|
|
|
self.errors.add(
|
|
|
|
:user_id,
|
|
|
|
I18n.t(
|
2024-05-24 22:15:53 +08:00
|
|
|
"activerecord.errors.models.user_email.attributes.user_id.reassigning_primary_email",
|
2023-01-09 20:20:10 +08:00
|
|
|
),
|
2018-01-04 15:37:26 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2023-01-05 06:08:55 +08:00
|
|
|
|
|
|
|
def destroy_email_tokens(email)
|
|
|
|
EmailToken.where(email: email).destroy_all
|
|
|
|
end
|
2017-04-27 02:47:36 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: user_emails
|
|
|
|
#
|
2021-11-24 17:30:06 +08:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# user_id :integer not null
|
|
|
|
# email :string(513) not null
|
|
|
|
# primary :boolean default(FALSE), not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# normalized_email :string
|
2017-04-27 02:47:36 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2017-10-06 11:13:01 +08:00
|
|
|
# index_user_emails_on_email (lower((email)::text)) UNIQUE
|
2021-11-24 17:30:06 +08:00
|
|
|
# index_user_emails_on_normalized_email (lower((normalized_email)::text))
|
2017-04-27 02:47:36 +08:00
|
|
|
# index_user_emails_on_user_id (user_id)
|
2018-07-16 14:18:07 +08:00
|
|
|
# index_user_emails_on_user_id_and_primary (user_id,primary) UNIQUE WHERE "primary"
|
2017-04-27 02:47:36 +08:00
|
|
|
#
|