identity.rb 995 Bytes
Newer Older
1
class Identity < ActiveRecord::Base
2
  include Sortable
3
  include CaseSensitivity
4

5 6
  belongs_to :user

7
  validates :provider, presence: true
8
  validates :extern_uid, allow_blank: true, uniqueness: { scope: :provider, case_sensitive: false }
Valery Sizov's avatar
Valery Sizov committed
9
  validates :user_id, uniqueness: { scope: :provider }
10

11 12
  before_save :ensure_normalized_extern_uid, if: :extern_uid_changed?

13
  scope :with_provider, ->(provider) { where(provider: provider) }
14
  scope :with_extern_uid, ->(provider, extern_uid) do
15
    iwhere(extern_uid: normalize_uid(provider, extern_uid)).with_provider(provider)
16
  end
17

Gabriel Mazetto's avatar
Gabriel Mazetto committed
18
  def ldap?
19
    Gitlab::OAuth::Provider.ldap_provider?(provider)
20
  end
21 22

  def self.normalize_uid(provider, uid)
23
    if Gitlab::OAuth::Provider.ldap_provider?(provider)
24 25 26 27 28
      Gitlab::LDAP::Person.normalize_dn(uid)
    else
      uid.to_s
    end
  end
29 30 31 32 33 34 35 36

  private

  def ensure_normalized_extern_uid
    return if extern_uid.nil?

    self.extern_uid = Identity.normalize_uid(self.provider, self.extern_uid)
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
37
end