Advertisement
Guest User

Untitled

a guest
Jul 30th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. !! post.rb
  2.  
  3. class Post < ActiveRecord::Base
  4. has_many :comments
  5. has_one :author, :class_name => "Account"
  6. validates_presence_of :title, :entry, :author_id
  7.  
  8. validates_uniqueness_of :title
  9. validates_length_of :title, :within => 3..100
  10. attr_protected :id
  11.  
  12. end
  13.  
  14. !! account.rb
  15.  
  16. require 'digest/sha1'
  17. class Account < ActiveRecord::Base
  18. validates_presence_of :login, :password, :name, :pen_name
  19. validates_length_of :login, :within => 3..20
  20. validates_length_of :pen_name, :within => 3..30
  21. validates_length_of :password, :within => 3..20
  22. validates_confirmation_of :password, :confirm_password
  23. attr_protected :id, :salt
  24. def forgot_password
  25. salt = random_string(10)
  26. self.update_attributes[:password => Account.sha1(Account.random_string(8) salt), :salt => salt]
  27. self.save
  28. # send email to Account...
  29. end
  30. def self.authenticate(params)
  31. user = Account.find_by_login(params[:login])
  32. return nil if user.nil?
  33. return user if user.password == Account.sha1(params[:password] user.salt)
  34. nil
  35. end
  36. def password=(pass)
  37. self[:salt] = Account.random_string(10) if self[:salt].empty?
  38. self[:password] = Account.sha1(pass self[:salt])
  39. end
  40. def self.sha1(text)
  41. Digest::SHA1.hexdigest(text)
  42. end
  43. def self.random_string(length)
  44. chars = ("a".."z").to_a ("A".."Z").to_a ("0".."9").to_a
  45. random = ""
  46. 1.upto(length) { |i|
  47. random << chars[rand(chars.size-1)]
  48. }
  49. return random
  50. end
  51. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement