Advertisement
Guest User

Untitled

a guest
Jul 31st, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. require 'digest/sha1'
  2.  
  3. # this model expects a certain database layout and its based on the name/login pattern.
  4. class User < ActiveRecord::Base
  5.  
  6. set_primary_key "cst_user"
  7. set_table_name "cst_user"
  8.  
  9. def login
  10. username
  11. end
  12.  
  13. def login=(user)
  14. username = user
  15. end
  16.  
  17. def password
  18. passw
  19. end
  20.  
  21. def password=(foo)
  22. passw = foo
  23. end
  24. def self.authenticate(login, pass)
  25. find_first(["username = ? AND passw = ?", login, sha1(pass)])
  26. end
  27.  
  28. def change_password(pass)
  29. update_attribute "password", self.class.sha1(pass)
  30. end
  31.  
  32. protected
  33.  
  34. def self.sha1(pass)
  35. Digest::SHA1.hexdigest("change-me--#{pass}--")
  36. end
  37.  
  38. before_create :crypt_password
  39.  
  40. def crypt_password
  41. write_attribute("password", self.class.sha1(password))
  42. end
  43.  
  44. validates_length_of :login, :within => 3..40
  45. validates_length_of :password, :within => 5..40
  46. validates_presence_of :login, :password, :password_confirmation
  47. validates_uniqueness_of :login, :on => :create
  48. validates_confirmation_of :password, :on => :create
  49. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement