Skip to content

Commit

Permalink
Added case-insensitive fail-over for user login names.
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Mefford committed Jun 20, 2010
1 parent 8269a9f commit 022c8ad
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
11 changes: 10 additions & 1 deletion app/models/user.rb
Expand Up @@ -96,7 +96,7 @@ def identity_url=(url)
def self.try_to_login(login, password)
# Make sure no one can sign in with an empty password
return nil if password.to_s.empty?
user = find(:first, :conditions => ["login=?", login])
user = find_by_login(login)
if user
# user is already in local database
return nil if !user.active?
Expand Down Expand Up @@ -222,6 +222,15 @@ def notified_project_ids=(ids)
notified_projects_ids
end

# case-insensitive fall-over
def self.find_by_login(login)
# First look for an exact match
user = find(:first, :conditions => ["login = ?", login])
# Fail over to case-insensitive if none was found
user = find(:first, :conditions => ["LOWER(login) = ?", login.to_s.downcase]) if user.nil?
return user
end

def self.find_by_rss_key(key)
token = Token.find_by_value(key)
token && token.user.active? ? token.user : nil
Expand Down
8 changes: 8 additions & 0 deletions test/unit/user_test.rb
Expand Up @@ -103,6 +103,14 @@ def test_validate
assert_equal 1, @admin.errors.count
end

context "User#try_to_login" do
should "fall-back to case-insensitive if user login is not found as-typed." do
user = User.try_to_login("AdMin", "admin")
assert_kind_of User, user
assert_equal "admin", user.login
end
end

def test_password
user = User.try_to_login("admin", "admin")
assert_kind_of User, user
Expand Down

0 comments on commit 022c8ad

Please sign in to comment.