0
@@ -43,6 +43,17 @@ class SecureMagic::Account
0
attribute_set(:login, value.downcase) unless value.nil?
0
+ # Encrypts some data with the salt.
0
+ def self.encrypt(password, salt)
0
+ Digest::SHA1.hexdigest("--#{salt}--#{password}--")
0
+ # Authenticates a account by their login name and unencrypted password. Returns the account or nil.
0
+ def self.authenticate(login, password)
0
+ u = find_activated_authenticated_model_with_login(login) # need to get the salt
0
+ u && u.authenticated?(password) ? u : nil
0
EMAIL_FROM = "info@%s.com"
0
@@ -80,7 +91,100 @@ class SecureMagic::Account
0
+ def authenticated?(password)
0
+ crypted_password == encrypt(password)
0
+ return if password.blank?
0
+ self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
0
+ self.crypted_password = encrypt(password)
0
+ # Encrypts the password with the account salt
0
+ self.class.encrypt(password, salt)
0
+ remember_token_expires_at && DateTime.now < DateTime.parse(remember_token_expires_at.to_s)
0
+ def remember_me_until(time)
0
+ self.remember_token_expires_at = time
0
+ self.remember_token = encrypt("#{email}--#{remember_token_expires_at}")
0
+ def remember_me_for(time)
0
+ remember_me_until (Time.now + time)
0
+ # These create and unset the fields required for remembering accounts between browser closes
0
+ remember_me_for (Merb::Const::WEEK * 2)
0
+ self.remember_token_expires_at = nil
0
+ self.remember_token = nil
0
+ # Returns true if the account has just been activated.
0
+ def recently_activated?
0
+ return false if self.new_record?
0
+ !! activation_code.nil?
0
+ # the existence of an activation code means they have not activated yet
0
+ def self.find_authenticated_model_with_id(id)
0
+ SecureMagic::Account.first(:id => id)
0
+ def self.find_authenticated_model_with_remember_token(rt)
0
+ SecureMagic::Account.first(:remember_token => rt)
0
+ def self.find_activated_authenticated_model_with_login(login)
0
+ if SecureMagic::Account.instance_methods.include?("activated_at")
0
+ SecureMagic::Account.first(:login => login, :activated_at.not => nil)
0
+ SecureMagic::Account.first(:login => login)
0
+ def self.find_activated_authenticated_model(activation_code)
0
+ SecureMagic::Account.first(:activation_code => activation_code)
0
+ def self.find_with_conditions(conditions)
0
+ SecureMagic::Account.first(conditions)
0
+ # A method to assist with specs
0
+ def self.clear_database_table
0
+ def make_activation_code
0
+ self.activation_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )
0
+ def password_required?
0
+ crypted_password.blank? || !password.blank?
0
\ No newline at end of file
Comments
No one has commented yet.