user.rb 6.8 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_profile if sync_profile_from_provider?
16
        add_or_update_user_identities
17
      end
18

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

23
      def new?
24
        !persisted?
25
      end
26

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

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

34 35
        block_after_save = needs_blocking?

J
James Lopez 已提交
36
        Users::UpdateService.new(gl_user, user: gl_user).execute!
37

38
        gl_user.block if block_after_save
39

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

      def gl_user
48
        return @gl_user if defined?(@gl_user)
49

50 51
        @gl_user = find_user
      end
52

53 54
      def find_user
        user = find_by_uid_and_provider
55

56 57
        user ||= find_or_build_ldap_user if auto_link_ldap_user?
        user ||= build_new_user if signup_enabled?
58

59
        user.external = true if external_provider? && user
60

61
        user
62
      end
63

64
      protected
65

66 67 68 69 70 71 72 73 74 75 76 77 78 79
      def add_or_update_user_identities
        # find_or_initialize_by doesn't update `gl_user.identities`, and isn't autosaved.
        identity = gl_user.identities.find { |identity| identity.provider == auth_hash.provider }

        identity ||= gl_user.identities.build(provider: auth_hash.provider)
        identity.extern_uid = auth_hash.uid

        if auto_link_ldap_user? && !gl_user.ldap_user? && ldap_person
          log.info "Correct LDAP account has been found. identity to user: #{gl_user.username}."
          gl_user.identities.build(provider: ldap_person.provider, extern_uid: ldap_person.dn)
        end
      end

      def find_or_build_ldap_user
80
        return unless ldap_person
D
Douwe Maan 已提交
81

82 83 84
        user = Gitlab::LDAP::User.find_by_uid_and_provider(ldap_person.dn, ldap_person.provider)
        if user
          log.info "LDAP account found for user #{user.username}. Building new #{auth_hash.provider} identity."
85
          return user
86
        end
D
Douwe Maan 已提交
87

88 89 90 91 92 93 94 95
        log.info "No user found using #{auth_hash.provider} provider. Creating a new one."
        build_new_user
      end

      def find_by_email
        return unless auth_hash.has_attribute?(:email)

        ::User.find_by(email: auth_hash.email.downcase)
96 97 98 99 100 101 102 103 104 105 106 107 108
      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)

109 110
        # Look for a corresponding person with same uid in any of the configured LDAP providers
        Gitlab::LDAP::Config.providers.each do |provider|
111
          adapter = Gitlab::LDAP::Adapter.new(provider)
112
          @ldap_person = find_ldap_person(auth_hash, adapter)
113
          break if @ldap_person
114
        end
115
        @ldap_person
116 117
      end

118
      def find_ldap_person(auth_hash, adapter)
119 120 121
        Gitlab::LDAP::Person.find_by_uid(auth_hash.uid, adapter) ||
          Gitlab::LDAP::Person.find_by_email(auth_hash.uid, adapter) ||
          Gitlab::LDAP::Person.find_by_dn(auth_hash.uid, adapter)
122 123
      end

124
      def ldap_config
D
Douwe Maan 已提交
125
        Gitlab::LDAP::Config.new(ldap_person.provider) if ldap_person
126 127
      end

128
      def needs_blocking?
D
Douwe Maan 已提交
129
        new? && block_after_signup?
130 131 132
      end

      def signup_enabled?
133
        providers = Gitlab.config.omniauth.allow_single_sign_on
134 135 136 137 138
        if providers.is_a?(Array)
          providers.include?(auth_hash.provider)
        else
          providers
        end
139 140
      end

141 142 143 144
      def external_provider?
        Gitlab.config.omniauth.external_providers.include?(auth_hash.provider)
      end

145
      def block_after_signup?
D
Douwe Maan 已提交
146 147
        if creating_linked_ldap_user?
          ldap_config.block_auto_created_users
148
        else
D
Douwe Maan 已提交
149 150
          Gitlab.config.omniauth.block_auto_created_users
        end
151 152
      end

153 154 155 156
      def auth_hash=(auth_hash)
        @auth_hash = AuthHash.new(auth_hash)
      end

157
      def find_by_uid_and_provider
158 159
        identity = Identity.find_by(provider: auth_hash.provider, extern_uid: auth_hash.uid)
        identity && identity.user
160
      end
161

162
      def build_new_user
163
        user_params = user_attributes.merge(skip_confirmation: true)
164
        Users::BuildService.new(nil, user_params).execute(skip_authorization: true)
165 166
      end

167
      def user_attributes
168
        # Give preference to LDAP for sensitive information when creating a linked account
D
Douwe Maan 已提交
169
        if creating_linked_ldap_user?
170 171
          username = ldap_person.username.presence
          email = ldap_person.email.first.presence
172
        end
173

174 175 176
        username ||= auth_hash.username
        email ||= auth_hash.email

177 178 179 180 181
        valid_username = ::Namespace.clean_path(username)

        uniquify = Uniquify.new
        valid_username = uniquify.string(valid_username) { |s| !DynamicPathValidator.valid_user_path?(s) }

182
        name = auth_hash.name
183
        name = valid_username if name.strip.empty?
184

D
Douwe Maan 已提交
185
        {
186
          name:                       name,
187
          username:                   valid_username,
188
          email:                      email,
189 190 191
          password:                   auth_hash.password,
          password_confirmation:      auth_hash.password,
          password_automatically_set: true
192 193
        }
      end
194

195 196 197 198 199 200 201 202
      def sync_profile_from_provider?
        providers = Gitlab.config.omniauth.sync_profile_from_provider

        if providers.is_a?(Array)
          providers.include?(auth_hash.provider)
        else
          providers
        end
203 204
      end

205 206
      def update_profile
        user_synced_attributes_metadata = gl_user.user_synced_attributes_metadata || gl_user.build_user_synced_attributes_metadata
207

208 209 210 211 212 213 214
        UserSyncedAttributesMetadata::SYNCABLE_ATTRIBUTES.each do |key|
          if auth_hash.has_attribute?(key) && gl_user.sync_attribute?(key)
            gl_user[key] = auth_hash.public_send(key) # rubocop:disable GitlabSecurity/PublicSend
            user_synced_attributes_metadata.set_attribute_synced(key, true)
          else
            user_synced_attributes_metadata.set_attribute_synced(key, false)
          end
215
        end
216 217 218

        user_synced_attributes_metadata.provider = auth_hash.provider
        gl_user.user_synced_attributes_metadata = user_synced_attributes_metadata
219 220
      end

221 222 223 224
      def log
        Gitlab::AppLogger
      end

225
      def unauthorized_to_create
D
Douwe Maan 已提交
226
        raise SignupDisabledError
227
      end
228 229 230
    end
  end
end