2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
module Gitlab
|
|
|
|
module Auth
|
|
|
|
class OmniauthIdentityLinkerBase
|
|
|
|
attr_reader :current_user, :oauth
|
|
|
|
|
|
|
|
def initialize(current_user, oauth)
|
|
|
|
@current_user = current_user
|
|
|
|
@oauth = oauth
|
|
|
|
@changed = false
|
|
|
|
end
|
|
|
|
|
|
|
|
def link
|
2019-05-18 00:54:41 +05:30
|
|
|
save if unlinked?
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def changed?
|
|
|
|
@changed
|
|
|
|
end
|
|
|
|
|
|
|
|
def failed?
|
|
|
|
error_message.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def error_message
|
|
|
|
identity.validate
|
|
|
|
|
|
|
|
identity.errors.full_messages.join(', ')
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def save
|
|
|
|
@changed = identity.save
|
|
|
|
end
|
|
|
|
|
2019-05-18 00:54:41 +05:30
|
|
|
def unlinked?
|
|
|
|
identity.new_record?
|
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-10-15 14:42:47 +05:30
|
|
|
def identity
|
|
|
|
@identity ||= current_user.identities
|
|
|
|
.with_extern_uid(provider, uid)
|
|
|
|
.first_or_initialize(extern_uid: uid)
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-10-15 14:42:47 +05:30
|
|
|
|
|
|
|
def provider
|
|
|
|
oauth['provider']
|
|
|
|
end
|
|
|
|
|
|
|
|
def uid
|
|
|
|
oauth['uid']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|