From bf79c17bd04e3f57522e8f354f99a8dbdaaf583f Mon Sep 17 00:00:00 2001 From: Liran Tal Date: Sat, 11 Jul 2015 12:24:32 +0300 Subject: [PATCH] updating the schema save pre hook so that it checks for a modified version of the password field before it tries to re-calculate the new password to save for the user model --- modules/users/server/models/user.server.model.js | 2 +- modules/users/tests/server/user.server.model.tests.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/modules/users/server/models/user.server.model.js b/modules/users/server/models/user.server.model.js index ac86e3031e..bf5743c4fb 100644 --- a/modules/users/server/models/user.server.model.js +++ b/modules/users/server/models/user.server.model.js @@ -99,7 +99,7 @@ var UserSchema = new Schema({ * Hook a pre save method to hash the password */ UserSchema.pre('save', function(next) { - if (this.password && this.password.length > 6) { + if (this.password && this.isModified('password') && this.password.length > 6) { this.salt = crypto.randomBytes(16).toString('base64'); this.password = this.hashPassword(this.password); } diff --git a/modules/users/tests/server/user.server.model.tests.js b/modules/users/tests/server/user.server.model.tests.js index e43848764d..5e2a46e8bd 100644 --- a/modules/users/tests/server/user.server.model.tests.js +++ b/modules/users/tests/server/user.server.model.tests.js @@ -67,6 +67,16 @@ describe('User Model Unit Tests:', function() { done(); }); }); + + it('should confirm that saving user model doesnt change the password', function(done) { + user.firstName = 'test'; + var passwordBefore = user.password; + return user.save(function(err) { + var passwordAfter = user.password + passwordBefore.should.equal(passwordAfter); + done(); + }); + }); }); after(function(done) {