Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- !!I am adding a method to reset a users password and then send them their temporary password.
- !!Right now, it generates and emails a password correctly, but I cannot get it to save the password in the user.
- !!I used LoginEngine and here is my User model.
- require 'digest/sha1'
- class User < ActiveRecord::Base
- acts_as_blog
- # Virtual attribute for the unencrypted password
- attr_accessor :password
- file_column :image
- validates_presence_of :login, :email, :first_name, :last_name
- validates_presence_of :password, :if => :password_required?
- validates_presence_of :password_confirmation, :if => :password_required?
- validates_length_of :password, :within => 5..40, :if => :password_required?
- validates_confirmation_of :password, :if => :password_required?
- validates_length_of :login, :within => 3..40
- validates_length_of :email, :within => 3..100
- validates_uniqueness_of :login, :email
- validates_format_of :email, :with => %r{^[a-zA-Z0-9@\.]*}i
- before_save :encrypt_password, :transform_resume
- searches_on :first_name, :last_name, :company_name, :email
- def transform_resume
- self.education_formatted = User.convert_to_html(self.education, 'textile')
- self.affiliations_formatted = User.convert_to_html(self.affiliations, 'textile')
- self.significant_formatted = User.convert_to_html(self.significant_transactions, 'textile')
- self.professional_formatted = User.convert_to_html(self.professional_experience, 'textile')
- self.achievements_formatted = User.convert_to_html(self.achievements, 'textile')
- end
- # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
- def self.authenticate(login, password)
- u = find_by_login(login) # need to get the salt
- u && u.authenticated?(password) ? u : nil
- end
- # Encrypts some data with the salt.
- def self.encrypt(password, salt)
- Digest::SHA1.hexdigest("--#{salt}--#{password}--")
- end
- # Encrypts the password with the user salt
- def encrypt(password)
- self.class.encrypt(password, salt)
- end
- def authenticated?(password)
- crypted_password == encrypt(password)
- end
- def change_password(password)
- new_pass = encrypt(password)
- write_attribute("crypted_password", new_pass)
- new_pass
- end
- protected
- # before filter
- def encrypt_password
- return if password.blank?
- self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
- write_attribute("crypted_password", encrypt(password))
- end
- def password_required?
- crypted_password.blank? or not password.blank?
- end
- end
- !!Here is my controller code:
- def send_password
- @error_text = "Your email address was not found in the system. Please register for an account."
- if params[:email].any?
- if @user = User.find(:first, :conditions => ["email = ?", params[:email]])
- @new_pass = @user.change_password(generate_password)
- Notifier.deliver_password_reminder(@user, @new_pass)
- flash[:notice] = "A new password has been sent to your email..."
- redirect_back(2)
- else
- flash[:error] = "Your email address was not found in the system. Please register for an account."
- redirect_back(1)
- end
- end
- end
- !!I get this error:
- NoMethodError (undefined method `change_password' for #<User:0x40871f54>):
- /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1792:in `method_missing'
- /app/controllers/account_controller.rb:129:in `send_password'
- !!Anyone?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement