Project

General

Profile

Patch #4732 ยป case-insensitive-login.patch

Marcel Waldvogel, 2010-02-04 00:26

View differences:

app/models/user.rb (revision 50)
96 96
  def self.try_to_login(login, password)
97 97
    # Make sure no one can sign in with an empty password
98 98
    return nil if password.to_s.empty?
99
    user = find(:first, :conditions => ["login=?", login])
99
    # This does a case-insensitive lookup:
100
    # - Uses the index (avoids table walks) if the user exists (maybe causes table walks on MySQL for non-existant users?)
101
    # - Does not depend on the database adapter
102
    #   (MySQL will always match on the first, as it does not support function indexes;
103
    #   PostgreSQL will match on first or second, both times with index (standard or the one created by the 20100203232731 migration);
104
    #   SQLite should always be small enough for table walks)
105
    user = find(:first, :conditions => ["login=?", login]) || find(:first, :conditions => ["LOWER(login)=LOWER(?)", login])
100 106
    if user
101 107
      # user is already in local database
102 108
      return nil if !user.active?
db/migrate/20100203232731_add_case_insensitive_user_index.rb (revision 50)
1
class AddCaseInsensitiveUserIndex < ActiveRecord::Migration
2
  def self.up
3
    # Migrates PostgreSQL databases only
4
    # MySQL compares case-insensitive by default
5
    if ActiveRecord::Base.connection.adapter_name =~ /postgresql/i
6
      execute "CREATE INDEX index_users_on_lower_login ON users (LOWER(login));"
7
    end
8
  end
9

  
10
  def self.down
11
    if ActiveRecord::Base.connection.adapter_name =~ /postgresql/i
12
      execute "DROP INDEX index_users_on_lower_login;"
13
    end
14
  end
15
end
    (1-1/1)