diff --git a/lib/gitlab/backend/grack_auth.rb b/lib/gitlab/backend/grack_auth.rb
index 9de68283dc09d295312bc8f251e38e48965fba4c..f46455f4735562daaf94034c3be1f496ff3e6bdf 100644
--- a/lib/gitlab/backend/grack_auth.rb
+++ b/lib/gitlab/backend/grack_auth.rb
@@ -34,11 +34,15 @@ module Grack
         login, password = @auth.credentials
         self.user = User.find_by_email(login) || User.find_by_username(login)
 
-        if user.nil?
+        # If the provided login was not a known email or username
+        # then user is nil
+        if user.nil? 
+          # Second chance - try LDAP authentication
+          return false unless Gitlab.config.ldap.enabled         
           ldap_auth(login,password)
           return false unless !user.nil?
         else
-          return false unless user.valid_password?(password);
+          return false unless user.valid_password?(password)
         end
            
         Gitlab::ShellEnv.set_env(user)