user.rb 5.9 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 36
        gl_user.save!

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 = Gitlab::LDAP::Person.find_by_uid(auth_hash.uid, adapter)
105 106
          # The `uid` might actually be a DN. Try it next.
          @ldap_person ||= Gitlab::LDAP::Person.find_by_dn(auth_hash.uid, adapter)
107
          break if @ldap_person
108
        end
109
        @ldap_person
110 111 112
      end

      def ldap_config
D
Douwe Maan 已提交
113
        Gitlab::LDAP::Config.new(ldap_person.provider) if ldap_person
114 115
      end

116
      def needs_blocking?
D
Douwe Maan 已提交
117
        new? && block_after_signup?
118 119 120
      end

      def signup_enabled?
121
        providers = Gitlab.config.omniauth.allow_single_sign_on
122 123 124 125 126
        if providers.is_a?(Array)
          providers.include?(auth_hash.provider)
        else
          providers
        end
127 128
      end

129 130 131 132
      def external_provider?
        Gitlab.config.omniauth.external_providers.include?(auth_hash.provider)
      end

133
      def block_after_signup?
D
Douwe Maan 已提交
134 135
        if creating_linked_ldap_user?
          ldap_config.block_auto_created_users
136
        else
D
Douwe Maan 已提交
137 138
          Gitlab.config.omniauth.block_auto_created_users
        end
139 140
      end

141 142 143 144
      def auth_hash=(auth_hash)
        @auth_hash = AuthHash.new(auth_hash)
      end

145
      def find_by_uid_and_provider
146 147
        identity = Identity.find_by(provider: auth_hash.provider, extern_uid: auth_hash.uid)
        identity && identity.user
148
      end
149

150
      def build_new_user
151
        user_params = user_attributes.merge(extern_uid: auth_hash.uid, provider: auth_hash.provider, skip_confirmation: true)
152
        Users::BuildService.new(nil, user_params).execute(skip_authorization: true)
153 154
      end

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

162 163 164
        username ||= auth_hash.username
        email ||= auth_hash.email

165 166
        name = auth_hash.name
        name = ::Namespace.clean_path(username) if name.strip.empty?
167

D
Douwe Maan 已提交
168
        {
169
          name:                       name,
170 171
          username:                   ::Namespace.clean_path(username),
          email:                      email,
172 173 174
          password:                   auth_hash.password,
          password_confirmation:      auth_hash.password,
          password_automatically_set: true
175 176
        }
      end
177

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
      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

194 195 196 197
      def log
        Gitlab::AppLogger
      end

198
      def unauthorized_to_create
D
Douwe Maan 已提交
199
        raise SignupDisabledError
200
      end
201 202 203
    end
  end
end