user.rb 6.0 KB
Newer Older
1 2 3 4 5 6 7
# OAuth extension for User model
#
# * Find GitLab user based on omniauth uid and provider
# * Create new user from omniauth data
#
module Gitlab
  module OAuth
8
    SignupDisabledError = Class.new(StandardError)
9

10
    class User
11
      attr_accessor :auth_hash, :gl_user
12

13 14
      def initialize(auth_hash)
        self.auth_hash = auth_hash
15
        update_email
16
      end
17

18
      def persisted?
19
        gl_user.try(:persisted?)
20
      end
21

22
      def new?
23
        !persisted?
24
      end
25

26
      def valid?
27
        gl_user.try(:valid?)
28
      end
29

30
      def save(provider = 'OAuth')
31 32
        unauthorized_to_create unless gl_user

33 34
        block_after_save = needs_blocking?

35
        Users::UpdateService.new(gl_user).execute!
36

37
        gl_user.block if block_after_save
38

39
        log.info "(#{provider}) saving user #{auth_hash.email} from login with extern_uid => #{auth_hash.uid}"
40 41
        gl_user
      rescue ActiveRecord::RecordInvalid => e
42
        log.info "(#{provider}) Error saving user #{auth_hash.uid} (#{auth_hash.email}): #{gl_user.errors.full_messages}"
43 44 45 46
        return self, e.record.errors
      end

      def gl_user
47 48
        @user ||= find_by_uid_and_provider

49 50 51 52
        if auto_link_ldap_user?
          @user ||= find_or_create_ldap_user
        end

53
        if signup_enabled?
54 55
          @user ||= build_new_user
        end
56

P
Patricio Cano 已提交
57 58
        if external_provider? && @user
          @user.external = true
59 60
        end

61
        @user
62
      end
63

64
      protected
65

66 67
      def find_or_create_ldap_user
        return unless ldap_person
D
Douwe Maan 已提交
68 69

        # If a corresponding person exists with same uid in a LDAP server,
70
        # check if the user already has a GitLab account.
71 72
        user = Gitlab::LDAP::User.find_by_uid_and_provider(ldap_person.dn, ldap_person.provider)
        if user
73
          # Case when a LDAP user already exists in Gitlab. Add the OAuth identity to existing account.
74
          log.info "LDAP account found for user #{user.username}. Building new #{auth_hash.provider} identity."
D
Drew Blessing 已提交
75
          user.identities.find_or_initialize_by(extern_uid: auth_hash.uid, provider: auth_hash.provider)
76
        else
77
          log.info "No existing LDAP account was found in GitLab. Checking for #{auth_hash.provider} account."
78 79
          user = find_by_uid_and_provider
          if user.nil?
80
            log.info "No user found using #{auth_hash.provider} provider. Creating a new one."
81 82 83
            user = build_new_user
          end
          log.info "Correct account has been found. Adding LDAP identity to user: #{user.username}."
84 85
          user.identities.new(provider: ldap_person.provider, extern_uid: ldap_person.dn)
        end
D
Douwe Maan 已提交
86

87 88 89 90 91 92 93 94 95 96 97 98 99 100
        user
      end

      def auto_link_ldap_user?
        Gitlab.config.omniauth.auto_link_ldap_user
      end

      def creating_linked_ldap_user?
        auto_link_ldap_user? && ldap_person
      end

      def ldap_person
        return @ldap_person if defined?(@ldap_person)

101 102
        # Look for a corresponding person with same uid in any of the configured LDAP providers
        Gitlab::LDAP::Config.providers.each do |provider|
103
          adapter = Gitlab::LDAP::Adapter.new(provider)
104
          @ldap_person = find_ldap_person(auth_hash, adapter)
105
          break if @ldap_person
106
        end
107
        @ldap_person
108 109
      end

110 111 112 113 114 115
      def find_ldap_person(auth_hash, adapter)
        by_uid = Gitlab::LDAP::Person.find_by_uid(auth_hash.uid, adapter)
        # The `uid` might actually be a DN. Try it next.
        by_uid || Gitlab::LDAP::Person.find_by_dn(auth_hash.uid, adapter)
      end

116
      def ldap_config
D
Douwe Maan 已提交
117
        Gitlab::LDAP::Config.new(ldap_person.provider) if ldap_person
118 119
      end

120
      def needs_blocking?
D
Douwe Maan 已提交
121
        new? && block_after_signup?
122 123 124
      end

      def signup_enabled?
125
        providers = Gitlab.config.omniauth.allow_single_sign_on
126 127 128 129 130
        if providers.is_a?(Array)
          providers.include?(auth_hash.provider)
        else
          providers
        end
131 132
      end

133 134 135 136
      def external_provider?
        Gitlab.config.omniauth.external_providers.include?(auth_hash.provider)
      end

137
      def block_after_signup?
D
Douwe Maan 已提交
138 139
        if creating_linked_ldap_user?
          ldap_config.block_auto_created_users
140
        else
D
Douwe Maan 已提交
141 142
          Gitlab.config.omniauth.block_auto_created_users
        end
143 144
      end

145 146 147 148
      def auth_hash=(auth_hash)
        @auth_hash = AuthHash.new(auth_hash)
      end

149
      def find_by_uid_and_provider
150 151
        identity = Identity.find_by(provider: auth_hash.provider, extern_uid: auth_hash.uid)
        identity && identity.user
152
      end
153

154
      def build_new_user
155
        user_params = user_attributes.merge(extern_uid: auth_hash.uid, provider: auth_hash.provider, skip_confirmation: true)
156
        Users::BuildService.new(nil, user_params).execute(skip_authorization: true)
157 158
      end

159
      def user_attributes
160
        # Give preference to LDAP for sensitive information when creating a linked account
D
Douwe Maan 已提交
161
        if creating_linked_ldap_user?
162 163
          username = ldap_person.username.presence
          email = ldap_person.email.first.presence
164
        end
165

166 167 168
        username ||= auth_hash.username
        email ||= auth_hash.email

169 170
        name = auth_hash.name
        name = ::Namespace.clean_path(username) if name.strip.empty?
171

D
Douwe Maan 已提交
172
        {
173
          name:                       name,
174 175
          username:                   ::Namespace.clean_path(username),
          email:                      email,
176 177 178
          password:                   auth_hash.password,
          password_confirmation:      auth_hash.password,
          password_automatically_set: true
179 180
        }
      end
181

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
      def sync_email_from_provider?
        auth_hash.provider.to_s == Gitlab.config.omniauth.sync_email_from_provider.to_s
      end

      def update_email
        if auth_hash.has_email? && sync_email_from_provider?
          if persisted?
            gl_user.skip_reconfirmation!
            gl_user.email = auth_hash.email
          end

          gl_user.external_email = true
          gl_user.email_provider = auth_hash.provider
        end
      end

198 199 200 201
      def log
        Gitlab::AppLogger
      end

202
      def unauthorized_to_create
D
Douwe Maan 已提交
203
        raise SignupDisabledError
204
      end
205 206 207
    end
  end
end