Skip to content

Instantly share code, notes, and snippets.

@pjb3
Created September 24, 2008 11:48
Show Gist options
  • Save pjb3/12536 to your computer and use it in GitHub Desktop.
Save pjb3/12536 to your computer and use it in GitHub Desktop.
require 'email_re'
#I assume this is monkey patching a BasicBackend class
#that is already defined in Django?
#Or else you would just define both methods in EmailBackend?
class BasicBackend
def self.get_user(id)
User.find(id)
rescue ActiveRecord::RecordNotFound
nil
end
end
class EmailBackend < BasicBackend
def self.authenticate(username, password)
if EMAIL_RE.match(username)
user = find_by_email(username)
return nil unless user
else
user = find_by_username(username)
return nil unless user
end
user if user.check_password(password)
end
end
EMAIL_RE = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
require 'email_re'
class User < ActiveRecord::Base
def self.authenticate(login, password)
user = if EMAIL_RE.match(login)
find_by_email(login)
else
find_by_username(login)
end
return user if user and user.check_password(password)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment