summaryrefslogtreecommitdiff
path: root/lib/gitlab/oauth/user.rb
blob: b768eda185f2a8bcbec8923041693a7474b7c9ca (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# OAuth extension for User model
#
# * Find GitLab user based on omniauth uid and provider
# * Create new user from omniauth data
#
module Gitlab
  module OAuth
    class User
      class << self
        attr_reader :auth_hash

        def find(auth_hash)
          self.auth_hash = auth_hash
          find_by_uid_and_provider
        end

        def create(auth_hash)
          user = new(auth_hash)
          user.save_and_trigger_callbacks
        end

        def model
          ::User
        end

        def auth_hash=(auth_hash)
          @auth_hash = AuthHash.new(auth_hash)
        end

        protected
        def find_by_uid_and_provider
          model.where(provider: auth_hash.provider, extern_uid: auth_hash.uid).last
        end
      end

      # Instance methods
      attr_accessor :auth_hash, :user

      def initialize(auth_hash)
        self.auth_hash = auth_hash
        self.user = self.class.model.new(user_attributes)
        user.skip_confirmation!
      end

      def auth_hash=(auth_hash)
        @auth_hash = AuthHash.new(auth_hash)
      end

      def save_and_trigger_callbacks
        user.save!
        log.info "(OAuth) Creating user #{auth_hash.email} from login with extern_uid => #{auth_hash.uid}"
        user.block if needs_blocking?

        user
      rescue ActiveRecord::RecordInvalid => e
        log.info "(OAuth) Email #{e.record.errors[:email]}. Username #{e.record.errors[:username]}"
        return nil, e.record.errors
      end

      def user_attributes
        {
          extern_uid: auth_hash.uid,
          provider: auth_hash.provider,
          name: auth_hash.name,
          username: auth_hash.username,
          email: auth_hash.email,
          password: auth_hash.password,
          password_confirmation: auth_hash.password,
        }
      end

      def log
        Gitlab::AppLogger
      end

      def raise_error(message)
        raise OmniAuth::Error, "(OAuth) " + message
      end

      def needs_blocking?
        Gitlab.config.omniauth['block_auto_created_users']
      end
    end
  end
end