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