debian-mirror-gitlab/app/models/email.rb

51 lines
1.3 KiB
Ruby
Raw Normal View History

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