2018-07-25 09:30:33 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 13:17:42 +00:00
|
|
|
class Identity < ApplicationRecord
|
2015-02-05 22:20:55 +00:00
|
|
|
include Sortable
|
2015-12-22 18:00:41 +00:00
|
|
|
include CaseSensitivity
|
2017-11-17 14:24:25 +00:00
|
|
|
|
2014-11-25 16:15:30 +00:00
|
|
|
belongs_to :user
|
|
|
|
|
2015-06-23 09:21:23 +00:00
|
|
|
validates :provider, presence: true
|
2018-11-12 13:14:13 +00:00
|
|
|
validates :extern_uid, allow_blank: true, uniqueness: { scope: UniquenessScopes.scopes, case_sensitive: false }
|
2019-01-29 06:35:25 +00:00
|
|
|
validates :user, uniqueness: { scope: UniquenessScopes.scopes }
|
2016-01-06 07:38:52 +00:00
|
|
|
|
2017-12-21 14:31:15 +00:00
|
|
|
before_save :ensure_normalized_extern_uid, if: :extern_uid_changed?
|
2018-02-15 09:27:38 +00:00
|
|
|
after_destroy :clear_user_synced_attributes, if: :user_synced_attributes_metadata_from_provider?
|
2017-12-21 14:31:15 +00:00
|
|
|
|
2017-11-17 14:24:25 +00:00
|
|
|
scope :with_provider, ->(provider) { where(provider: provider) }
|
2017-10-31 09:52:44 +00:00
|
|
|
scope :with_extern_uid, ->(provider, extern_uid) do
|
2017-11-17 14:24:25 +00:00
|
|
|
iwhere(extern_uid: normalize_uid(provider, extern_uid)).with_provider(provider)
|
2017-10-31 09:52:44 +00:00
|
|
|
end
|
2017-02-16 01:44:36 +00:00
|
|
|
|
2016-01-12 14:29:10 +00:00
|
|
|
def ldap?
|
2018-02-23 12:10:39 +00:00
|
|
|
Gitlab::Auth::OAuth::Provider.ldap_provider?(provider)
|
2016-01-06 07:38:52 +00:00
|
|
|
end
|
2017-11-17 14:24:25 +00:00
|
|
|
|
|
|
|
def self.normalize_uid(provider, uid)
|
2018-02-23 12:10:39 +00:00
|
|
|
if Gitlab::Auth::OAuth::Provider.ldap_provider?(provider)
|
|
|
|
Gitlab::Auth::LDAP::Person.normalize_dn(uid)
|
2017-11-17 14:24:25 +00:00
|
|
|
else
|
|
|
|
uid.to_s
|
|
|
|
end
|
|
|
|
end
|
2017-12-21 14:31:15 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def ensure_normalized_extern_uid
|
|
|
|
return if extern_uid.nil?
|
|
|
|
|
|
|
|
self.extern_uid = Identity.normalize_uid(self.provider, self.extern_uid)
|
|
|
|
end
|
2018-02-15 09:27:38 +00:00
|
|
|
|
|
|
|
def user_synced_attributes_metadata_from_provider?
|
|
|
|
user.user_synced_attributes_metadata&.provider == provider
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear_user_synced_attributes
|
|
|
|
user.user_synced_attributes_metadata&.destroy
|
|
|
|
end
|
2015-01-22 17:40:03 +00:00
|
|
|
end
|