Created
December 9, 2008 10:23
-
-
Save oleganza/33865 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
# | |
# Helps to authenticate person using password and email | |
# | |
class Person | |
Authentication = DeferredModule.new do | |
property :crypted_password, String, :length => 50, :accessor => :protected # DM bug #663, :lazy => [:auth_data] | |
property :salt, String, :length => 50, :accessor => :protected # DM bug #663, :lazy => [:auth_data] | |
attr_accessor :password, :password_confirmation | |
validates_length :password, :min => 4, :if => :password_required? | |
validates_is_confirmed :password, :if => :password_required?, :message => "Password and confirmation do not match" | |
before :save, :encrypt_password | |
# Encrypts the password with the user salt | |
def encrypt(password) | |
self.class.encrypt(password, salt) | |
end | |
def encrypt_password | |
return if password.blank? | |
self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{email}--") if new_record? | |
self.crypted_password = encrypt(password) | |
end | |
def authenticated?(password) | |
crypted_password == encrypt(password) | |
end | |
def password_required? | |
crypted_password.blank? || !password.blank? | |
end | |
def with_random_password | |
self.password = rand(2**64).to_s(16) | |
self.password_confirmation = self.password | |
self | |
end | |
end | |
module Authentication | |
def self.included(base) | |
super(base) | |
base.extend(ClassMethods) | |
end | |
module ClassMethods | |
# Encrypts some data with the salt. | |
def encrypt(password, salt) | |
Digest::SHA1.hexdigest("--#{salt}--#{password}--") | |
end | |
# Authenticates a user by their login field and unencrypted password. Returns the user or nil. | |
def authenticate(given_email, given_password) | |
person = first(:email => given_email) | |
person = person && person.authenticated?(given_password) && person.activated? ? person : nil | |
end | |
end | |
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
# | |
# Helps to authenticate person using password and email | |
# | |
class Person | |
SimplisticAuthentication = DeferredModule.new do | |
property :password, String, :length => 255 | |
attr_accessor :password_confirmation | |
validates_length :password, :min => 4 | |
validates_is_confirmed :password, :if => :new_record?, :message => "Password and confirmation do not match" | |
def authenticated?(password) | |
self.password == password | |
end | |
def with_random_password | |
self.password = rand(2**64).to_s(16) | |
self.password_confirmation = self.password | |
self | |
end | |
end | |
module SimplisticAuthentication | |
def self.included(base) | |
super(base) | |
base.extend(ClassMethods) | |
end | |
module ClassMethods | |
# Authenticates a user by their login field and unencrypted password. Returns the user or nil. | |
def authenticate(given_email, given_password) | |
person = first(:email => given_email) | |
person = person && person.authenticated?(given_password) && person.activated? ? person : nil | |
end | |
end | |
end # SimplisticAuthentication | |
end # Person |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment