2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
class Identity < ActiveRecord::Base
|
2018-10-15 14:42:47 +05:30
|
|
|
def self.uniqueness_scope
|
|
|
|
:provider
|
|
|
|
end
|
|
|
|
|
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
|
2018-10-15 14:42:47 +05:30
|
|
|
validates :extern_uid, allow_blank: true, uniqueness: { scope: uniqueness_scope, case_sensitive: false }
|
|
|
|
validates :user_id, uniqueness: { scope: uniqueness_scope }
|
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?
|
|
|
|
|
|
|
|
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
|
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)
|
|
|
|
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
|