Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'digest/sha1'
- class User < ActiveRecord::Base
- validates_presence_of :username, :password
- attr_accessor :password
- attr_protected :password_hash
- def password=(passwd)
- @password = passwd
- self.password_hash = User.hash_password(passwd)
- end
- def after_save
- @password = nil
- end
- def authenticated?(passwd = '')
- self.password_hash == User.hash_password(passwd)
- end
- def self.authenticate(username, passwd)
- user = User.find(:first, :conditions => { :username => username })
- return nil if user.nil?
- return user if user.password_hash == User.hash_password(passwd)
- nil
- end
- def self.hash_password(passwd)
- Digest::SHA1::hexdigest(passwd)
- end
- end
Add Comment
Please, Sign In to add comment