Guest User

Untitled

a guest
Mar 6th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. require 'digest/sha1'
  2.  
  3. class User < ActiveRecord::Base
  4. # Virtual attribute for the unencrypted password
  5. attr_accessor :password
  6.  
  7. validates_uniqueness_of :username, :email, :case_sensitive => false
  8. before_save :encrypt_password
  9.  
  10. # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
  11. def self.authenticate(username, password)
  12. crypted_password = encrypt( password )
  13. user = User.find(:first, :conditions => ["username = ? AND crypted_password = ?", username, crypted_password])
  14. end
  15.  
  16. def self.encrypt( password )
  17. Digest::SHA1.hexdigest( password )
  18. end
  19.  
  20. protected
  21. #before filter
  22. def encrypt_password
  23. self.crypted_password = encrypt( password )
  24. end
  25.  
  26. end
Add Comment
Please, Sign In to add comment