Skip to content

Instantly share code, notes, and snippets.

@scottwater
Created July 7, 2010 21:27
Show Gist options
  • Save scottwater/467312 to your computer and use it in GitHub Desktop.
Save scottwater/467312 to your computer and use it in GitHub Desktop.
require 'digest/sha1'
class Account
include MongoMapper::Document
attr_accessor :password
# Keys
key :name, String
key :surname, String
key :email, String
key :crypted_password, String
key :salt, String
key :role, String
# Validations
validates_presence_of :email, :role
validates_presence_of :password, :if => :password_required
validates_presence_of :password_confirmation, :if => :password_required
validates_length_of :password, :within => 4..40, :if => :password_required
validates_confirmation_of :password, :if => :password_required
validates_length_of :email, :within => 3..100
validates_uniqueness_of :email, :case_sensitive => false
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
validates_format_of :role, :with => /[A-Za-z]/
# Callbacks
before_save :generate_password
##
# This method is for authentication purpose
#
def self.authenticate(email, password)
account = first(:email => email) if email.present?
account && account.verify_password(password) ? account : nil
end
def verify_password(password)
hash_password(password, self.salt) == self.crypted_password
end
private
def generate_password
return if password.blank?
self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{email}--") if new_record?
self.crypted_password = hash_password(password, self.salt)
end
def password_required
crypted_password.blank? || !password.blank?
end
def hash_password(password, salt)
Digest::SHA512.hexdigest("#{password}:#{salt}")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment