From 659e9ce09b4699ed700ff462a3baacc440bc3d05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=95=EB=8C=80=EC=84=A0?= Date: Fri, 23 Dec 2016 14:04:44 +0900 Subject: [PATCH] Fix false emailVerified on user model update We noticed that every time the user model updates, the emailVerified column would change to false, even though the email was not changed at all. I took a look and realized there might be an error in https://github.com/strongloop/loopback/commit/eb640d8 The intent of the commit just mention is to make emailVerified false when the email gets changed, but notice that ctx.data.email is null on updates, so the condition is always met and emailVerified always becomes false. This commit fixes the issue just mentioned. --- common/models/user.js | 2 +- test/user.test.js | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/common/models/user.js b/common/models/user.js index c89c34e9..dadc6f90 100644 --- a/common/models/user.js +++ b/common/models/user.js @@ -845,7 +845,7 @@ module.exports = function(User) { if (emailChanged && ctx.Model.settings.emailVerificationRequired) { ctx.instance.emailVerified = false; } - } else { + } else if (ctx.data.email) { emailChanged = ctx.hookState.originalUserData.some(function(data) { return data.email != ctx.data.email; }); diff --git a/test/user.test.js b/test/user.test.js index c9d0f7df..f58ce330 100644 --- a/test/user.test.js +++ b/test/user.test.js @@ -2358,6 +2358,28 @@ describe('User', function() { ], done); }); + it('should not set verification to false after something other than email is updated', + function(done) { + User.settings.emailVerificationRequired = true; + async.series([ + function updateUser(next) { + userInstance.updateAttribute('realm', 'test', function(err, info) { + if (err) return next(err); + assert.equal(info.realm, 'test'); + next(); + }); + }, + function findUser(next) { + User.findById(userInstance.id, function(err, info) { + if (err) return next(err); + assert.equal(info.realm, 'test'); + assert.equal(info.emailVerified, true); + next(); + }); + }, + ], done); + }); + function createOriginalUser(done) { var userData = { email: 'original@example.com',