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
|
|
|
|
2020-05-06 09:10:02 +00:00
|
|
|
validates :email, presence: true, uniqueness: true
|
|
|
|
validate :validate_email_format
|
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
|
|
|
|
|
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
|
|
|
|
2020-05-18 09:08:12 +00:00
|
|
|
delegate :username, :can?, 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
|
|
|
|
self.errors.add(:email, 'has already been taken') if User.exists?(email: self.email)
|
|
|
|
end
|
2017-09-24 17:52:49 +00:00
|
|
|
|
2018-07-25 16:15:04 +00:00
|
|
|
def accept_pending_invitations!
|
|
|
|
user.accept_pending_invitations!
|
|
|
|
end
|
|
|
|
|
2020-05-06 09:10:02 +00: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 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
|
2014-04-02 18:02:02 +00:00
|
|
|
end
|