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