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