user.rb 5.4 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
D
Douwe Maan 已提交
8
    class SignupDisabledError < StandardError; end
9

10
    class User
11
      attr_accessor :auth_hash, :gl_user
12

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

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

21
      def new?
22
        !persisted?
23
      end
24

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

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

32
        if needs_blocking?
33
          gl_user.save!
34
          gl_user.block
35 36
        else
          gl_user.save!
37
        end
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: #{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 = ::User.new(user_attributes)
152 153
        user.skip_confirmation!
        user.identities.new(extern_uid: auth_hash.uid, provider: auth_hash.provider)
154
        user
155 156
      end

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

164 165 166
        username ||= auth_hash.username
        email ||= auth_hash.email

167 168
        name = auth_hash.name
        name = ::Namespace.clean_path(username) if name.strip.empty?
169

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

180 181 182 183
      def log
        Gitlab::AppLogger
      end

184
      def unauthorized_to_create
D
Douwe Maan 已提交
185
        raise SignupDisabledError
186
      end
187 188 189
    end
  end
end