Skip to content

Commit

Permalink
fix current_password valid bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
chloerei committed Nov 25, 2012
1 parent d2d44b6 commit 10a2202
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/models/topic.rb
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions app/models/user.rb
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/validators/current_password_validator.rb
@@ -1,6 +1,6 @@
class CurrentPasswordValidator < ActiveModel::EachValidator
def initialize(options)
options[:column] ||= []
options[:fields] ||= []

super
end
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions test/unit/user_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit 10a2202

Please sign in to comment.