require 'sha1' class User < ActiveRecord::Base has_many :markets # markets opened by the user has_many :trades has_many :holdings attr_accessor :password attr_protected :hashed_password validates_uniqueness_of :username validates_confirmation_of :password def self.hashed(str) SHA1.new(str).to_s end # If a user matching the credentials is found, returns the user object. # If no matching user is found, returns nil. def self.authenticate(user_info) user = find_by_username(user_info[:username]) if user && user.hashed_password == hashed(user_info[:password]) return user end end private before_save :update_password # Updates the hashed_password if a plain password was provided def update_password if not password.blank? self.hashed_password = self.class.hashed(password) end end end