Guest User

Untitled

a guest
Jul 17th, 2018
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. require 'digest/sha1'
  2.  
  3. class User < ActiveRecord::Base
  4. validates_presence_of :username, :password
  5.  
  6. attr_accessor :password
  7. attr_protected :password_hash
  8.  
  9. def password=(passwd)
  10. @password = passwd
  11. self.password_hash = User.hash_password(passwd)
  12. end
  13.  
  14. def after_save
  15. @password = nil
  16. end
  17.  
  18. def authenticated?(passwd = '')
  19. self.password_hash == User.hash_password(passwd)
  20. end
  21.  
  22. def self.authenticate(username, passwd)
  23. user = User.find(:first, :conditions => { :username => username })
  24. return nil if user.nil?
  25. return user if user.password_hash == User.hash_password(passwd)
  26. nil
  27. end
  28.  
  29. def self.hash_password(passwd)
  30. Digest::SHA1::hexdigest(passwd)
  31. end
  32. end
Add Comment
Please, Sign In to add comment