debian-mirror-gitlab/app/models/identity.rb

47 lines
1.3 KiB
Ruby
Raw Normal View History

2015-04-26 12:48:37 +05:30
class Identity < ActiveRecord::Base
include Sortable
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-03-17 18:26:18 +05:30
validates :extern_uid, allow_blank: true, uniqueness: { scope: :provider, case_sensitive: false }
2015-04-26 12:48:37 +05:30
validates :user_id, uniqueness: { scope: :provider }
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
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
end
2015-04-26 12:48:37 +05:30
end