Guest User

Untitled

a guest
Mar 15th, 2018
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. class User < Sequel::Model
  2.  
  3. plugin :validation_helpers
  4.  
  5. attr_accessor :password
  6. attr_accessor :password_confirmation
  7.  
  8. def validate
  9. validates_presence [:email, :username]
  10. validates_unique [:email, :username]
  11. end
  12.  
  13. def after_create
  14. self.crypted_password = encrypt(password)
  15. self.created_at = Time.now
  16. @new = false
  17. save
  18. end
  19.  
  20. def authenticated?(pass)
  21. crypted_password == encrypt(pass)
  22. end
  23.  
  24. def self.authenticate(hash)
  25. mail, pass = hash['login'], hash['password']
  26. if user = User[:email => mail]
  27. return user unless pass
  28. user if user.authenticated?(pass)
  29. end
  30. end
  31.  
  32. def encrypt(password)
  33. self.class.encrypt(password, salt)
  34. end
  35.  
  36. def self.encrypt(password, salt)
  37. Digest::SHA1.hexdigest("--#{salt}--#{password}--")
  38. end
  39.  
  40. end
Add Comment
Please, Sign In to add comment