Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'digest/sha1'
- class User < ActiveRecord::Base
- # Virtual attribute for the unencrypted password
- attr_accessor :password
- validates_uniqueness_of :username, :email, :case_sensitive => false
- before_save :encrypt_password
- # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
- def self.authenticate(username, password)
- crypted_password = encrypt( password )
- user = User.find(:first, :conditions => ["username = ? AND crypted_password = ?", username, crypted_password])
- end
- def self.encrypt( password )
- Digest::SHA1.hexdigest( password )
- end
- protected
- #before filter
- def encrypt_password
- self.crypted_password = encrypt( password )
- end
- end
Add Comment
Please, Sign In to add comment