diff --git a/app/models/topic.rb b/app/models/topic.rb index 861dfc8..1c36f28 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -68,7 +68,7 @@ def mark_by(user) {"$addToSet" => {:marker_ids => user.id}}) marker_ids.push user.id end - end + end def unmark_by(user) if marked_by? user diff --git a/app/models/user.rb b/app/models/user.rb index 9dd3b71..1494b82 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -18,9 +18,9 @@ class User validates :email, :format => {:with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/} validates :password, :password_confirmation, :presence => true, :on => :create validates :password, :length => {:minimum => 6, :allow_nil => true} - validates :current_password, :current_password => {:fields => [:name, :email, :password]}, :on => :update + validates :current_password, :current_password => {:fields => [:name, :email, :password_digest]}, :on => :update validates :locale, :inclusion => {:in => AllowLocale} - + attr_accessor :current_password attr_accessible :name, :email, :password, :password_confirmation, :current_password, :locale @@ -118,7 +118,7 @@ def encrypt_password(password) 20.times { digest = User.secure_digest(digest) } digest end - + def self.secure_digest(*args) Digest::SHA512.hexdigest(args.flatten.join) end diff --git a/app/validators/current_password_validator.rb b/app/validators/current_password_validator.rb index 1c39184..97ee9cd 100644 --- a/app/validators/current_password_validator.rb +++ b/app/validators/current_password_validator.rb @@ -1,6 +1,6 @@ class CurrentPasswordValidator < ActiveModel::EachValidator def initialize(options) - options[:column] ||= [] + options[:fields] ||= [] super end @@ -9,7 +9,7 @@ def validate_each(record, attribute, value) if (record.changed & options[:fields].map(&:to_s)).any? if record.current_password.blank? record.errors.add(attribute, :blank) - elsif BCrypt::Password.new(record.password_digest) != record.current_password + elsif BCrypt::Password.new(record.password_digest_was) != record.current_password record.errors.add(attribute, :current_password_no_match) end end diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index 8c02f74..511c8ff 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -10,12 +10,13 @@ class UserTest < ActiveSupport::TestCase end test "should generate remember token" do - user = create :user + password = '123456' + user = create :user, :password => password, :password_confirmation => password assert_not_nil user.remember_token token = user.remember_token assert_equal user, User.find_by_remember_token(token) - user.update_attributes :password => 'change', :password_confirmation => 'change' + user.update_attributes :password => 'change', :password_confirmation => 'change', :current_password => password assert_not_equal token, user.remember_token assert_nil User.find_by_remember_token(token) end