2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-26 13:47:36 -05:00
|
|
|
class UserEmail < ActiveRecord::Base
|
|
|
|
belongs_to :user
|
|
|
|
|
2017-10-25 00:02:18 -05:00
|
|
|
attr_accessor :skip_validate_email
|
2021-02-22 05:42:37 -06:00
|
|
|
attr_accessor :skip_validate_unique_email
|
2017-08-08 21:56:08 -05:00
|
|
|
|
2017-04-26 13:47:36 -05:00
|
|
|
before_validation :strip_downcase_email
|
2021-11-24 03:30:06 -06:00
|
|
|
before_validation :normalize_email
|
2017-04-26 13:47:36 -05:00
|
|
|
|
2018-03-19 21:22:06 -05:00
|
|
|
validates :email, presence: true
|
2020-06-02 21:13:25 -05:00
|
|
|
validates :email, email: true, if: :validate_email?
|
2017-04-26 13:47:36 -05:00
|
|
|
|
2017-12-21 06:30:26 -06:00
|
|
|
validates :primary, uniqueness: { scope: [:user_id] }, if: %i[user_id primary]
|
2018-01-04 01:37:26 -06:00
|
|
|
validate :user_id_not_changed, if: :primary
|
2021-02-22 05:42:37 -06:00
|
|
|
validate :unique_email, if: :validate_unique_email?
|
2017-04-26 13:47:36 -05:00
|
|
|
|
2018-07-03 06:51:22 -05:00
|
|
|
scope :secondary, -> { where(primary: false) }
|
|
|
|
|
2023-01-04 16:08:55 -06: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 03:30:06 -06: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-26 13:47:36 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def strip_downcase_email
|
|
|
|
if self.email
|
|
|
|
self.email = self.email.strip
|
|
|
|
self.email = self.email.downcase
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-08 21:56:08 -05:00
|
|
|
def validate_email?
|
2017-10-25 00:02:18 -05:00
|
|
|
return false if self.skip_validate_email
|
2017-04-26 13:47:36 -05:00
|
|
|
email_changed?
|
|
|
|
end
|
2018-01-04 01:37:26 -06:00
|
|
|
|
2021-02-22 05:42:37 -06:00
|
|
|
def validate_unique_email?
|
|
|
|
return false if self.skip_validate_unique_email
|
|
|
|
will_save_change_to_email?
|
|
|
|
end
|
|
|
|
|
2018-03-19 21:22:06 -05:00
|
|
|
def unique_email
|
2021-11-24 03:30:06 -06:00
|
|
|
email_exists =
|
|
|
|
if SiteSetting.normalize_emails?
|
|
|
|
self
|
|
|
|
.class
|
|
|
|
.where("lower(email) = ? OR lower(normalized_email) = ?", email, normalized_email)
|
|
|
|
.exists?
|
|
|
|
else
|
|
|
|
self.class.where("lower(email) = ?", email).exists?
|
2018-03-19 21:22:06 -05:00
|
|
|
end
|
2021-11-24 03:30:06 -06:00
|
|
|
|
|
|
|
self.errors.add(:email, :taken) if email_exists
|
2018-03-19 21:22:06 -05:00
|
|
|
end
|
|
|
|
|
2018-01-04 01:37:26 -06:00
|
|
|
def user_id_not_changed
|
|
|
|
if self.will_save_change_to_user_id? && self.persisted?
|
|
|
|
self.errors.add(
|
|
|
|
:user_id,
|
|
|
|
I18n.t(
|
|
|
|
"active_record.errors.model.user_email.attributes.user_id.reassigning_primary_email",
|
2023-01-09 06:20:10 -06:00
|
|
|
),
|
2018-01-04 01:37:26 -06:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2023-01-04 16:08:55 -06:00
|
|
|
|
|
|
|
def destroy_email_tokens(email)
|
|
|
|
EmailToken.where(email: email).destroy_all
|
|
|
|
end
|
2017-04-26 13:47:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: user_emails
|
|
|
|
#
|
2021-11-24 03:30:06 -06: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-26 13:47:36 -05:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2017-10-05 22:13:01 -05:00
|
|
|
# index_user_emails_on_email (lower((email)::text)) UNIQUE
|
2021-11-24 03:30:06 -06:00
|
|
|
# index_user_emails_on_normalized_email (lower((normalized_email)::text))
|
2017-04-26 13:47:36 -05:00
|
|
|
# index_user_emails_on_user_id (user_id)
|
2018-07-16 01:18:07 -05:00
|
|
|
# index_user_emails_on_user_id_and_primary (user_id,primary) UNIQUE WHERE "primary"
|
2017-04-26 13:47:36 -05:00
|
|
|
#
|