Guest User

Untitled

a guest
Mar 1st, 2018
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. class Manager < ActiveRecord::Base
  2. validates_presence_of :password
  3. validates_presence_of :email
  4. validates_email_format_of :email
  5.  
  6. attr_accessor :password_confirmation
  7. validates_confirmation_of :password
  8.  
  9. def self.verify(email, password)
  10. Manager.find( :first, :conditions => { :email => email, :password => password } )
  11. end
  12.  
  13. def self.authenticate(email, password)
  14. user = self.find_by_name(email)
  15. if user
  16. expected_password = encrypted_password(password, user.salt)
  17. if user.hashed_password != expected_password
  18. user = nil
  19. end
  20. end
  21. user
  22. end
  23.  
  24. def password
  25. @password
  26. end
  27.  
  28. def password=(pwd)
  29. @password = pwd
  30. return if pwd.blank?
  31. create_new_salt
  32. self.hashed_password = Manager.encrypted_password(self.password, self.salt)
  33. end
  34.  
  35. private
  36.  
  37. def self.encrypted_password(password, salt)
  38. string_to_hash = password + "scramble" + salt
  39. Digest::SHA1.hexdigest(string_to_hash)
  40. end
  41.  
  42. def create_new_salt
  43. self.salt = self.object_id.to_s + rand.to_s
  44. end
  45.  
  46. end
Add Comment
Please, Sign In to add comment