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
|
2019-08-19 09:19:19 -04:00
|
|
|
attr_reader :current_user, :oauth, :session
|
2018-04-18 10:03:27 -04:00
|
|
|
|
2019-08-19 09:19:19 -04:00
|
|
|
def initialize(current_user, oauth, session = {})
|
2018-04-18 10:03:27 -04:00
|
|
|
@current_user = current_user
|
|
|
|
@oauth = oauth
|
2018-04-23 06:56:44 -04:00
|
|
|
@changed = false
|
2019-08-19 09:19:19 -04:00
|
|
|
@session = session
|
2018-04-18 10:03:27 -04:00
|
|
|
end
|
|
|
|
|
2018-04-23 06:56:44 -04:00
|
|
|
def link
|
2018-12-08 09:12:50 -05:00
|
|
|
save if unlinked?
|
2018-04-23 06:56:44 -04:00
|
|
|
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-12-08 09:12:50 -05:00
|
|
|
def unlinked?
|
|
|
|
identity.new_record?
|
|
|
|
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
|