diff --git a/lib/gitlab/ldap/user.rb b/lib/gitlab/ldap/user.rb
index 1606210aafc1d7a677dab0fd665a3a83aa778a84..260bacfeeb093d79509978496ee684d7fb46e7e8 100644
--- a/lib/gitlab/ldap/user.rb
+++ b/lib/gitlab/ldap/user.rb
@@ -44,11 +44,15 @@ module Gitlab
         end
 
         def find_user(email)
-          if user = model.find_by_email(email)
-          elsif ldap_conf['allow_username_or_email_login']
-            uname = (email.partition('@').first) unless email.nil?
+          user = model.find_by_email(email)
+
+          # If no user found and allow_username_or_email_login is true
+          # we look for user by extracting part of his email
+          if !user && email && ldap_conf['allow_username_or_email_login']
+            uname = email.partition('@').first
             user = model.find_by_username(uname)
           end
+
           user
         end
 
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index 2b42226ecafe95911ba2d5b78105395a9af6dd82..c879900f8fd806a331601d5be6525f5560fc737f 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -233,7 +233,7 @@ describe User do
         it "should apply defaults to user" do
           Gitlab.config.gitlab.default_projects_limit.should_not == 123
           Gitlab.config.gitlab.default_can_create_group.should_not be_true
-          Gitlab.config.gitlab.default_theme.should_not == Gitlab::Theme::MARS
+          Gitlab.config.gitlab.default_theme.should_not == Gitlab::Theme::BASIC
           user.projects_limit.should == 123
           user.can_create_group.should be_true
           user.theme_id.should == Gitlab::Theme::BASIC