user.rb 1.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
# LDAP extension for User model
#
# * Find or create user from omniauth.auth data
# * Links LDAP account with existing user
# * Auth LDAP user with login and password
#
module Gitlab
  module Auth
    module LDAP
      class User < Gitlab::Auth::OAuth::User
11 12
        extend ::Gitlab::Utils::Override

13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
        class << self
          def find_by_uid_and_provider(uid, provider)
            identity = ::Identity.with_extern_uid(provider, uid).take

            identity && identity.user
          end
        end

        def save
          super('LDAP')
        end

        # instance methods
        def find_user
          find_by_uid_and_provider || find_by_email || build_new_user
        end

        def find_by_uid_and_provider
          self.class.find_by_uid_and_provider(auth_hash.uid, auth_hash.provider)
        end

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

38 39 40 41 42
        override :omniauth_should_save?
        def omniauth_should_save?
          changed? && super
        end

43 44 45 46 47 48 49 50
        def block_after_signup?
          ldap_config.block_auto_created_users
        end

        def allowed?
          Gitlab::Auth::LDAP::Access.allowed?(gl_user)
        end

51
        def valid_sign_in?
52
          allowed? && super
53 54
        end

55 56 57 58 59 60 61 62 63 64 65
        def ldap_config
          Gitlab::Auth::LDAP::Config.new(auth_hash.provider)
        end

        def auth_hash=(auth_hash)
          @auth_hash = Gitlab::Auth::LDAP::AuthHash.new(auth_hash)
        end
      end
    end
  end
end