2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class Email < ApplicationRecord
|
2015-02-05 17:20:55 -05:00
|
|
|
include Sortable
|
2017-11-24 06:24:24 -05:00
|
|
|
include Gitlab::SQL::Pattern
|
2015-02-05 17:20:55 -05:00
|
|
|
|
2019-06-25 13:54:42 -04:00
|
|
|
belongs_to :user, optional: false
|
2014-04-02 14:02:02 -04:00
|
|
|
|
2020-05-06 05:10:02 -04:00
|
|
|
validates :email, presence: true, uniqueness: true
|
|
|
|
validate :validate_email_format
|
2014-02-08 22:08:49 -05:00
|
|
|
validate :unique_email, if: ->(email) { email.email_changed? }
|
2014-04-02 14:02:02 -04:00
|
|
|
|
2017-09-20 07:24:10 -04:00
|
|
|
scope :confirmed, -> { where.not(confirmed_at: nil) }
|
|
|
|
|
2017-09-18 08:50:32 -04:00
|
|
|
after_commit :update_invalid_gpg_signatures, if: -> { previous_changes.key?('confirmed_at') }
|
|
|
|
|
2017-09-04 13:23:33 -04:00
|
|
|
devise :confirmable
|
2020-05-18 05:08:12 -04:00
|
|
|
|
|
|
|
# This module adds async behaviour to Devise emails
|
|
|
|
# and should be added after Devise modules are initialized.
|
|
|
|
include AsyncDeviseEmail
|
|
|
|
|
2019-01-16 07:09:29 -05:00
|
|
|
self.reconfirmable = false # currently email can't be changed, no need to reconfirm
|
2017-09-04 13:23:33 -04:00
|
|
|
|
2021-05-11 14:10:36 -04:00
|
|
|
delegate :username, :can?, :pending_invitations, :accept_pending_invitations!, to: :user
|
2017-10-27 10:32:48 -04:00
|
|
|
|
2016-05-23 05:19:27 -04:00
|
|
|
def email=(value)
|
|
|
|
write_attribute(:email, value.downcase.strip)
|
2014-02-08 22:08:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def unique_email
|
2021-11-03 20:12:36 -04:00
|
|
|
self.errors.add(:email, 'has already been taken') if primary_email_of_another_user?
|
2014-02-08 22:08:49 -05:00
|
|
|
end
|
2017-09-24 13:52:49 -04:00
|
|
|
|
2020-05-06 05:10:02 -04:00
|
|
|
def validate_email_format
|
|
|
|
self.errors.add(:email, I18n.t(:invalid, scope: 'valid_email.validations.email')) unless ValidateEmail.valid?(self.email)
|
|
|
|
end
|
|
|
|
|
2017-09-18 08:50:32 -04:00
|
|
|
# once email is confirmed, update the gpg signatures
|
|
|
|
def update_invalid_gpg_signatures
|
|
|
|
user.update_invalid_gpg_signatures if confirmed?
|
|
|
|
end
|
2021-11-03 20:12:36 -04:00
|
|
|
|
|
|
|
def user_primary_email?
|
|
|
|
email.casecmp?(user.email)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def primary_email_of_another_user?
|
|
|
|
User.where(email: email).where.not(id: user_id).exists?
|
|
|
|
end
|
2014-04-02 14:02:02 -04:00
|
|
|
end
|