Guest User

Untitled

a guest
Mar 7th, 2018
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. ## MODEL
  2.  
  3. equire 'digest/sha1'
  4.  
  5. class User < ActiveRecord::Base
  6.  
  7. #model relationships
  8. belongs_to :province
  9. belongs_to :country
  10. belongs_to :rank
  11.  
  12. validates_length_of :username, :within => 3..40
  13. validates_length_of :password, :within => 4..40
  14. validates_presence_of :username, :password, :password_confirmation, :salt
  15. validates_uniqueness_of :login, :email
  16. validates_confirmation_of :password
  17. validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :message => 'Invalid email'
  18.  
  19. # Makes sure values cannot set them with a post
  20. attr_protected :id, :salt, :rank_id
  21.  
  22. # always prepend 'self.' in the model method declarations.
  23. # It will help troubleshoot if a migration breaks,
  24.  
  25.  
  26.  
  27. # Anytime a password is assigned to a User this method is called.
  28. # the password assigned is stored as @password, I think
  29. !! def password=(pass)
  30. !! @password=pass
  31. !! # if it doesn't have any salt, give it some
  32. !! self.salt = User.random_string(10) if !self.salt?
  33. !! self.hashed_password = User.encrypt(@password, self.salt)
  34. !! end
  35.  
  36.  
  37.  
  38. def self.authenticate(login, pass)
  39. u=find(:first, :conditions => ["login = ?", login]) #return the first user with that login
  40. return nil if u.nil?
  41. return u if User.encrypt(pass, u.salt) == u.hashed_password
  42. nil
  43. end
  44.  
  45. def send_new_password
  46. new_pass = User.random_string(10)
  47. # store new password as both for model verification
  48. self.password = self.password_confirmation = new_pass
  49. self.save
  50. # make sure to write thisn Notifications mailer!
  51. Notifications.deliver_forgot_password(self.email, self.login, new_pass)
  52. end
  53.  
  54.  
  55.  
  56. protected
  57. #Everything after here not accesible outside of this model
  58.  
  59. def self.random_string(len)
  60. # generate a random alphanumeric string for the password
  61. chars = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
  62. newpass = ''
  63. # for the length or the string, a random character is concat onto the newpass
  64. 1.upto(len) { |i| newpass << chars[rand(chars.size-1)] }
  65. return newpass
  66. end
  67.  
  68. def self.encrypt(pass, salt)
  69. Digest::SHA1.hexdigest(pass+salt)
  70. end
  71.  
  72.  
  73.  
  74. end
  75.  
  76. ## A sample test that fails when the model saves. It fails where highlighted on .save
  77. ##
  78. ## NoMethodError: undefined method `password' for #<User:0x475e690>
  79.  
  80. def test_create
  81. #check create works and we can authenticate after creation
  82. u = User.new
  83. u.username = "nonexistingbob"
  84. u.password = u.password_confirmation = "bobs_secure_password"
  85. u.email="nonexistingbob@mcbob.com"
  86. assert_not_nil u.salt
  87. assert u.save
  88. assert_equal 10, u.salt.length
  89. assert_equal u, User.authenticate(u.username, u.password)
  90.  
  91. u = User.new(:username => "newbob", :password => "newpassword", :password_confirmation => "newpassword", :email => "newbob@mcbob.com" )
  92. assert_not_nil u.salt
  93. assert_not_nil u.password
  94. assert_not_nil u.hashed_password
  95. !! assert u.save
  96. assert_equal u, User.authenticate(u.username, u.password)
  97.  
  98. end
  99.  
  100. ## STACK TRACE
  101.  
  102. 4) Error:
  103. test_create(UserTest):
  104. NoMethodError: undefined method `password' for #<User:0x475e690>
  105. c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1860:in `method_missing'
  106. c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/validations.rb:300:in `send'
  107. c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/validations.rb:300:in `validates_each'
  108. c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/validations.rb:299:in `each'
  109. c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/validations.rb:299:in `validates_each'
  110. c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/validations.rb:822:in `call'
  111. c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/validations.rb:822:in `run_validations'
  112. c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/validations.rb:816:in `each'
  113. c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/validations.rb:816:in `run_validations'
  114. c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/validations.rb:780:in `valid_without_callbacks?'
  115. c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/callbacks.rb:299:in `valid?'
  116. c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/validations.rb:751:in `save_without_transactions'
  117. c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/transactions.rb:129:in `save'
  118. c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract/database_statements.rb:59:in `transaction'
  119. c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/transactions.rb:95:in `transaction'
  120. c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/transactions.rb:121:in `transaction'
  121. c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/transactions.rb:129:in `save'
  122. test/unit/user_test.rb:111:in `test_create'
Add Comment
Please, Sign In to add comment