Advertisement
Guest User

Untitled

a guest
Jul 30th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. !!I am adding a method to reset a users password and then send them their temporary password.
  2. !!Right now, it generates and emails a password correctly, but I cannot get it to save the password in the user.
  3. !!I used LoginEngine and here is my User model.
  4.  
  5. require 'digest/sha1'
  6. class User < ActiveRecord::Base
  7.  
  8. acts_as_blog
  9.  
  10. # Virtual attribute for the unencrypted password
  11. attr_accessor :password
  12. file_column :image
  13. validates_presence_of :login, :email, :first_name, :last_name
  14. validates_presence_of :password, :if => :password_required?
  15. validates_presence_of :password_confirmation, :if => :password_required?
  16. validates_length_of :password, :within => 5..40, :if => :password_required?
  17. validates_confirmation_of :password, :if => :password_required?
  18. validates_length_of :login, :within => 3..40
  19. validates_length_of :email, :within => 3..100
  20. validates_uniqueness_of :login, :email
  21. validates_format_of :email, :with => %r{^[a-zA-Z0-9@\.]*}i
  22. before_save :encrypt_password, :transform_resume
  23. searches_on :first_name, :last_name, :company_name, :email
  24.  
  25. def transform_resume
  26. self.education_formatted = User.convert_to_html(self.education, 'textile')
  27. self.affiliations_formatted = User.convert_to_html(self.affiliations, 'textile')
  28. self.significant_formatted = User.convert_to_html(self.significant_transactions, 'textile')
  29. self.professional_formatted = User.convert_to_html(self.professional_experience, 'textile')
  30. self.achievements_formatted = User.convert_to_html(self.achievements, 'textile')
  31. end
  32.  
  33. # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
  34. def self.authenticate(login, password)
  35. u = find_by_login(login) # need to get the salt
  36. u && u.authenticated?(password) ? u : nil
  37. end
  38.  
  39. # Encrypts some data with the salt.
  40. def self.encrypt(password, salt)
  41. Digest::SHA1.hexdigest("--#{salt}--#{password}--")
  42. end
  43.  
  44. # Encrypts the password with the user salt
  45. def encrypt(password)
  46. self.class.encrypt(password, salt)
  47. end
  48.  
  49. def authenticated?(password)
  50. crypted_password == encrypt(password)
  51. end
  52.  
  53. def change_password(password)
  54. new_pass = encrypt(password)
  55. write_attribute("crypted_password", new_pass)
  56. new_pass
  57. end
  58.  
  59. protected
  60. # before filter
  61. def encrypt_password
  62. return if password.blank?
  63. self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
  64. write_attribute("crypted_password", encrypt(password))
  65. end
  66.  
  67. def password_required?
  68. crypted_password.blank? or not password.blank?
  69. end
  70.  
  71. end
  72.  
  73. !!Here is my controller code:
  74.  
  75. def send_password
  76. @error_text = "Your email address was not found in the system. Please register for an account."
  77. if params[:email].any?
  78. if @user = User.find(:first, :conditions => ["email = ?", params[:email]])
  79. @new_pass = @user.change_password(generate_password)
  80. Notifier.deliver_password_reminder(@user, @new_pass)
  81. flash[:notice] = "A new password has been sent to your email..."
  82. redirect_back(2)
  83. else
  84. flash[:error] = "Your email address was not found in the system. Please register for an account."
  85. redirect_back(1)
  86. end
  87. end
  88. end
  89.  
  90. !!I get this error:
  91.  
  92. NoMethodError (undefined method `change_password' for #<User:0x40871f54>):
  93. /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1792:in `method_missing'
  94. /app/controllers/account_controller.rb:129:in `send_password'
  95.  
  96. !!Anyone?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement