2018-10-11 16:12:21 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-04-18 10:03:27 -04:00
|
|
|
module Gitlab
|
|
|
|
module Auth
|
|
|
|
class OmniauthIdentityLinkerBase
|
|
|
|
attr_reader :current_user, :oauth
|
|
|
|
|
|
|
|
def initialize(current_user, oauth)
|
|
|
|
@current_user = current_user
|
|
|
|
@oauth = oauth
|
2018-04-23 06:56:44 -04:00
|
|
|
@changed = false
|
2018-04-18 10:03:27 -04:00
|
|
|
end
|
|
|
|
|
2018-04-23 06:56:44 -04:00
|
|
|
def link
|
|
|
|
save if identity.new_record?
|
|
|
|
end
|
|
|
|
|
|
|
|
def changed?
|
|
|
|
@changed
|
2018-04-18 10:03:27 -04:00
|
|
|
end
|
|
|
|
|
2018-04-22 19:05:12 -04:00
|
|
|
def failed?
|
|
|
|
error_message.present?
|
|
|
|
end
|
|
|
|
|
2018-04-22 14:08:08 -04:00
|
|
|
def error_message
|
2018-04-23 06:56:44 -04:00
|
|
|
identity.validate
|
|
|
|
|
|
|
|
identity.errors.full_messages.join(', ')
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def save
|
|
|
|
@changed = identity.save
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-04-23 06:56:44 -04:00
|
|
|
def identity
|
|
|
|
@identity ||= current_user.identities
|
|
|
|
.with_extern_uid(provider, uid)
|
|
|
|
.first_or_initialize(extern_uid: uid)
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-04-23 06:56:44 -04:00
|
|
|
|
|
|
|
def provider
|
|
|
|
oauth['provider']
|
2018-04-22 14:08:08 -04:00
|
|
|
end
|
|
|
|
|
2018-04-23 06:56:44 -04:00
|
|
|
def uid
|
|
|
|
oauth['uid']
|
2018-04-18 10:03:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|