diff --git a/app/models/user.rb b/app/models/user.rb index d0c3b45..22b5177 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -134,6 +134,10 @@ class User < ActiveRecord::Base password end + add_encryption_method :crypt do |password, salt| + password.crypt(salt) + end + def authenticated?(password) result = (crypted_password == encrypt(password)) if (encryption_method != User.system_encryption_method) && result @@ -209,7 +213,7 @@ class User < ActiveRecord::Base # before filter def encrypt_password return if password.blank? - self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record? + self.salt ||= Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record? self.password_type ||= User.system_encryption_method.to_s self.crypted_password = encrypt(password) end diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index b6821aa..165dda0 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -165,7 +165,7 @@ class UserTest < Test::Unit::TestCase def test_should_encrypt_password_with_salted_sha1 user = User.new(:login => 'lalala', :email => 'lalala@example.com', :password => 'test', :password_confirmation => 'test') user.build_person(person_data) - user.expects(:salt).returns('testsalt') + user.stubs(:salt).returns('testsalt') user.save! # SHA1+salt crypted form for password 'test', and salt 'testsalt', @@ -182,6 +182,11 @@ class UserTest < Test::Unit::TestCase assert_equal '098f6bcd4621d373cade4e832627b4f6', user.crypted_password end + def test_should_support_crypt_passwords + user = new_user(:login => 'lalala', :email => 'lalala@example.com', :password => 'test', :password_confirmation => 'test', :password_type => 'crypt', :salt => 'test') + assert_equal 'teH0wLIpW0gyQ', user.crypted_password + end + def test_should_support_clear_passwords assert_equal 'test', new_user(:password => 'test', :password_confirmation => 'test', :password_type => 'clear').crypted_password end -- libgit2 0.21.2