Guest User

Untitled

a guest
Apr 27th, 2018
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. class User < Sequel::Model
  2. validates do
  3. presence_of :email_address
  4. presence_of :password_salt
  5. presence_of :password_hash
  6. end
  7.  
  8. def password_mismatch
  9. @password_mismatch ||= false
  10. end
  11. def password_mismatch=(val)
  12. @password_mismatch = val
  13. end
  14.  
  15. validates_each :password_mismatch do |object,attribute,value|
  16. raise @password_mismatch.inspect
  17. if @password_mismatch
  18. object.errors[attribute] << "does not match"
  19. end
  20. end
  21.  
  22. before_create do
  23. self.signed_up_on = Time.now
  24. end
  25.  
  26. def password=(pass)
  27. @password = pass
  28. # This is for generating the keys
  29. salt = [Array.new(6){rand(256).chr}.join].pack("m").chomp
  30. self.password_salt, self.password_hash = salt, Digest::SHA256.hexdigest(pass+salt)
  31. end
  32.  
  33. def self.authenticate(email_address, password)
  34. user = User.where{:email_address == email_address}.first
  35. if user.nil? || Digest::SHA256.hexdigest(password + user.password_salt) != user.password_hash
  36. raise "Username or password invalid"
  37. end
  38. user
  39. end
  40. end
  41.  
  42.  
  43. >> u = User.new(:email_address => "hey@hey.com", :password_mismatch => true); u.valid?
  44. RuntimeError: nil
Add Comment
Please, Sign In to add comment