2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class Identity < ApplicationRecord
|
2015-02-05 17:20:55 -05:00
|
|
|
include Sortable
|
2015-12-22 13:00:41 -05:00
|
|
|
include CaseSensitivity
|
2017-11-17 09:24:25 -05:00
|
|
|
|
2014-11-25 11:15:30 -05:00
|
|
|
belongs_to :user
|
|
|
|
|
2015-06-23 05:21:23 -04:00
|
|
|
validates :provider, presence: true
|
2018-11-12 08:14:13 -05:00
|
|
|
validates :extern_uid, allow_blank: true, uniqueness: { scope: UniquenessScopes.scopes, case_sensitive: false }
|
2019-01-29 01:35:25 -05:00
|
|
|
validates :user, uniqueness: { scope: UniquenessScopes.scopes }
|
2016-01-06 02:38:52 -05:00
|
|
|
|
2017-12-21 09:31:15 -05:00
|
|
|
before_save :ensure_normalized_extern_uid, if: :extern_uid_changed?
|
2018-02-15 04:27:38 -05:00
|
|
|
after_destroy :clear_user_synced_attributes, if: :user_synced_attributes_metadata_from_provider?
|
2017-12-21 09:31:15 -05:00
|
|
|
|
2019-05-24 04:14:19 -04:00
|
|
|
scope :for_user, ->(user) { where(user: user) }
|
2017-11-17 09:24:25 -05:00
|
|
|
scope :with_provider, ->(provider) { where(provider: provider) }
|
2017-10-31 05:52:44 -04:00
|
|
|
scope :with_extern_uid, ->(provider, extern_uid) do
|
2017-11-17 09:24:25 -05:00
|
|
|
iwhere(extern_uid: normalize_uid(provider, extern_uid)).with_provider(provider)
|
2017-10-31 05:52:44 -04:00
|
|
|
end
|
2020-12-08 07:09:53 -05:00
|
|
|
scope :with_any_extern_uid, ->(provider) do
|
|
|
|
where.not(extern_uid: nil).with_provider(provider)
|
|
|
|
end
|
2017-02-15 20:44:36 -05:00
|
|
|
|
2016-01-12 09:29:10 -05:00
|
|
|
def ldap?
|
2018-02-23 07:10:39 -05:00
|
|
|
Gitlab::Auth::OAuth::Provider.ldap_provider?(provider)
|
2016-01-06 02:38:52 -05:00
|
|
|
end
|
2017-11-17 09:24:25 -05:00
|
|
|
|
|
|
|
def self.normalize_uid(provider, uid)
|
2018-02-23 07:10:39 -05:00
|
|
|
if Gitlab::Auth::OAuth::Provider.ldap_provider?(provider)
|
2020-03-12 11:09:39 -04:00
|
|
|
Gitlab::Auth::Ldap::Person.normalize_dn(uid)
|
2017-11-17 09:24:25 -05:00
|
|
|
else
|
|
|
|
uid.to_s
|
|
|
|
end
|
|
|
|
end
|
2017-12-21 09:31:15 -05: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 04:27:38 -05: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 12:40:03 -05:00
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
Identity.prepend_if_ee('EE::Identity')
|