require 'digest/sha1' class User < ActiveRecord::Base # Virtual attribute for the unencrypted password attr_accessor :password validates_presence_of :login validates_presence_of :password, :if => :password_required? validates_presence_of :password_confirmation, :if => :password_required? validates_length_of :password, :within => 5..40, :if => :password_required? validates_confirmation_of :password, :if => :password_required? validates_length_of :login, :within => 3..40 validates_uniqueness_of :login, :salt before_save :encrypt_password has_many :projects # Authenticates a user by their login name and unencrypted password. Returns the user or nil. def self.authenticate(login, password) # use this instead if you want user activation # u = find :first, :select => 'id, salt', :conditions => ['login = ? and activated_at IS NOT NULL', login] u = find_by_login(login) # need to get the salt return nil unless u find :first, :conditions => ["id = ? AND crypted_password = ?", u.id, u.encrypt(password)] end # Encrypts some data with the salt. def self.encrypt(password, salt) Digest::SHA1.hexdigest("--#{salt}--#{password}--") end # Encrypts the password with the user salt def encrypt(password) self.class.encrypt(password, salt) end protected # before filter def encrypt_password return if password.blank? self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record? self.crypted_password = encrypt(password) end def password_required? crypted_password.blank? or not password.blank? end end