user.rb 1.3 KB
Newer Older
1 2 3 4
# LDAP extension for User model
#
# * Find or create user from omniauth.auth data
# * Links LDAP account with existing user
5
# * Auth LDAP user with login and password
6 7 8
#
module Gitlab
  module LDAP
9
    class User < Gitlab::OAuth::User
10
      class << self
11
        def find_by_uid_and_provider(uid, provider)
12
          # LDAP distinguished name is case-insensitive
13 14 15
          identity = ::Identity
            .where(provider: provider)
            .iwhere(extern_uid: uid).last
16
          identity && identity.user
17
        end
18
      end
19

20 21 22 23
      def save
        super('LDAP')
      end

24
      # instance methods
25 26
      def find_user
        find_by_uid_and_provider || find_by_email || build_new_user
27
      end
28

29
      def find_by_uid_and_provider
30
        self.class.find_by_uid_and_provider(auth_hash.uid, auth_hash.provider)
31 32 33
      end

      def changed?
34
        gl_user.changed? || gl_user.identities.any?(&:changed?)
35
      end
36

37 38
      def block_after_signup?
        ldap_config.block_auto_created_users
39
      end
40

41
      def sync_profile_from_provider?
42 43 44
        true
      end

45 46 47
      def allowed?
        Gitlab::LDAP::Access.allowed?(gl_user)
      end
48

D
Douwe Maan 已提交
49 50
      def ldap_config
        Gitlab::LDAP::Config.new(auth_hash.provider)
51
      end
52 53

      def auth_hash=(auth_hash)
D
Douwe Maan 已提交
54
        @auth_hash = Gitlab::LDAP::AuthHash.new(auth_hash)
55
      end
56 57 58
    end
  end
end