summaryrefslogtreecommitdiff
path: root/lib/gitlab/auth/omniauth_identity_linker_base.rb
blob: 253445570f2bc7d9723b019338c641c23bdcf34b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# frozen_string_literal: true

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
        save if identity.new_record?
      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

      # rubocop: disable CodeReuse/ActiveRecord
      def identity
        @identity ||= current_user.identities
                                  .with_extern_uid(provider, uid)
                                  .first_or_initialize(extern_uid: uid)
      end
      # rubocop: enable CodeReuse/ActiveRecord

      def provider
        oauth['provider']
      end

      def uid
        oauth['uid']
      end
    end
  end
end