File: email.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (64 lines) | stat: -rw-r--r-- 2,002 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true

class Email < ApplicationRecord
  include Sortable
  include Gitlab::SQL::Pattern

  belongs_to :user, optional: false
  belongs_to :banned_user, class_name: '::Users::BannedUser', foreign_key: 'user_id', inverse_of: 'emails'

  validates :email, presence: true, uniqueness: true, devise_email: true

  validate :unique_email, if: ->(email) { email.email_changed? }

  scope :users_by_detumbled_email_count, ->(email) do
    normalized_email = ::Gitlab::Utils::Email.normalize_email(email)

    where(detumbled_email: normalized_email).distinct.count(:user_id)
  end

  scope :confirmed, -> { where.not(confirmed_at: nil) }
  scope :unconfirmed, -> { where(confirmed_at: nil) }
  scope :unconfirmed_and_created_before, ->(created_cut_off) { unconfirmed.where('created_at < ?', created_cut_off) }

  before_save :detumble_email!, if: ->(email) { email.email_changed? }
  after_commit :update_invalid_gpg_signatures, if: -> { previous_changes.key?('confirmed_at') }

  devise :confirmable

  # This module adds async behaviour to Devise emails
  # and should be added after Devise modules are initialized.
  include AsyncDeviseEmail
  include ForcedEmailConfirmation

  self.reconfirmable = false # currently email can't be changed, no need to reconfirm

  delegate :username, :can?, :pending_invitations, :accept_pending_invitations!, to: :user

  def email=(value)
    write_attribute(:email, value.downcase.strip)
  end

  def unique_email
    self.errors.add(:email, 'has already been taken') if primary_email_of_another_user?
  end

  # once email is confirmed, update the gpg signatures
  def update_invalid_gpg_signatures
    user.update_invalid_gpg_signatures if confirmed?
  end

  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

  def detumble_email!
    self.detumbled_email = ::Gitlab::Utils::Email.normalize_email(email)
  end
end