user.rb 2.6 KB
Newer Older
1 2
require 'gitlab/oauth/user'

3 4 5 6
# LDAP extension for User model
#
# * Find or create user from omniauth.auth data
# * Links LDAP account with existing user
7
# * Auth LDAP user with login and password
8 9 10
#
module Gitlab
  module LDAP
11
    class User < Gitlab::OAuth::User
12 13
      class << self
        def find_or_create(auth)
14
          @auth = auth
15

D
Dmitriy Zaporozhets 已提交
16 17
          if uid.blank? || email.blank? || username.blank?
            raise_error("Account must provide a dn, uid and email address")
18 19
          end

20 21 22 23 24 25
          user = find(auth)

          unless user
            # Look for user with same emails
            #
            # Possible cases:
26 27
            # * When user already has account and need to link their LDAP account.
            # * LDAP uid changed for user with same email and we need to update their uid
28
            #
J
Jacob Vosmaer 已提交
29
            user = model.find_by(email: email)
30 31 32 33 34 35 36 37 38 39 40 41

            if user
              user.update_attributes(extern_uid: uid, provider: provider)
              log.info("(LDAP) Updating legacy LDAP user #{email} with extern_uid => #{uid}")
            else
              # Create a new user inside GitLab database
              # based on LDAP credentials
              #
              #
              user = create(auth)
            end
          end
42

43
          user
44 45
        end

46
        def authenticate(login, password)
47 48 49 50 51
          # Check user against LDAP backend if user is not authenticated
          # Only check with valid login and password to prevent anonymous bind results
          return nil unless ldap_conf.enabled && login.present? && password.present?

          ldap = OmniAuth::LDAP::Adaptor.new(ldap_conf)
D
Dmitriy Zaporozhets 已提交
52 53 54 55 56 57 58 59
          filter = Net::LDAP::Filter.eq(ldap.uid, login)

          # Apply LDAP user filter if present
          if ldap_conf['user_filter'].present?
            user_filter = Net::LDAP::Filter.construct(ldap_conf['user_filter'])
            filter = Net::LDAP::Filter.join(filter, user_filter)
          end

60
          ldap_user = ldap.bind_as(
D
Dmitriy Zaporozhets 已提交
61
            filter: filter,
62 63 64 65 66 67 68
            size: 1,
            password: password
          )

          find_by_uid(ldap_user.dn) if ldap_user
        end

69 70
        private

71 72 73 74
        def find_by_uid_and_provider
          find_by_uid(uid)
        end

75
        def find_by_uid(uid)
76 77
          # LDAP distinguished name is case-insensitive
          model.where("provider = ? and lower(extern_uid) = ?", provider, uid.downcase).last
78 79
        end

80 81
        def provider
          'ldap'
82 83 84 85 86 87
        end

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

88 89 90
        def ldap_conf
          Gitlab.config.ldap
        end
91 92 93 94
      end
    end
  end
end