2014-11-25 11:15:30 -05:00
|
|
|
class Identity < ActiveRecord::Base
|
2018-04-22 17:04:56 -04:00
|
|
|
def self.uniqueness_scope
|
|
|
|
:provider
|
|
|
|
end
|
|
|
|
|
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-04-22 17:04:56 -04:00
|
|
|
validates :extern_uid, allow_blank: true, uniqueness: { scope: uniqueness_scope, case_sensitive: false }
|
|
|
|
validates :user_id, uniqueness: { scope: uniqueness_scope }
|
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
|
|
|
|
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
|
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)
|
|
|
|
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
|