Created
September 24, 2008 11:48
-
-
Save pjb3/12536 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
EMAIL_RE = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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