identity.rb 763 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
  scope :with_provider, ->(provider) { where(provider: provider) }
12
  scope :with_extern_uid, ->(provider, extern_uid) do
13
    iwhere(extern_uid: normalize_uid(provider, extern_uid)).with_provider(provider)
14
  end
15

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

  def self.normalize_uid(provider, uid)
21
    if Gitlab::OAuth::Provider.ldap_provider?(provider)
22 23 24 25 26
      Gitlab::LDAP::Person.normalize_dn(uid)
    else
      uid.to_s
    end
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
27
end