From 2a5927b4f9ea6fc0e52be69cb5bace5839e38954 Mon Sep 17 00:00:00 2001 From: Katharina Irrgang Date: Mon, 13 Feb 2017 16:36:21 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=20fix=20email=20already=20in=20?= =?UTF-8?q?use=20(#7987)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refs #7981 - usage of data.id was wrong - usage of id comparison was wrong --- core/server/models/user.js | 5 ++--- .../integration/model/model_users_spec.js | 19 ++++++++++--------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/core/server/models/user.js b/core/server/models/user.js index cab11ec44c..16cdefc725 100644 --- a/core/server/models/user.js +++ b/core/server/models/user.js @@ -368,11 +368,10 @@ User = ghostBookshelf.Model.extend({ options = options || {}; options.withRelated = _.union(options.withRelated, options.include); - if (data.email && data.id) { + if (data.email) { ops.push(function checkForDuplicateEmail() { return self.getByEmail(data.email).then(function then(user) { - // don't allow update if another user already uses this email - if (user && user.id !== data.id) { + if (user && user.id !== options.id) { return Promise.reject(new errors.ValidationError({message: i18n.t('errors.models.user.userUpdateError.emailIsAlreadyInUse')})); } }); diff --git a/core/test/integration/model/model_users_spec.js b/core/test/integration/model/model_users_spec.js index e3bf7d396d..bff73cf454 100644 --- a/core/test/integration/model/model_users_spec.js +++ b/core/test/integration/model/model_users_spec.js @@ -416,16 +416,17 @@ describe('User Model', function run() { }); it('can NOT set an already existing email address', function (done) { - var firstUser = testUtils.DataGenerator.Content.users[0].id, - secondEmail = testUtils.DataGenerator.Content.users[1].email; + var firstUser = testUtils.DataGenerator.Content.users[0], + secondUser = testUtils.DataGenerator.Content.users[1]; - UserModel.findOne({id: firstUser}).then(function (user) { - return user.edit({email: secondEmail}); - }).then(function () { - done(new Error('Already existing email address was accepted')); - }).catch(function () { - done(); - }); + UserModel.edit({email: secondUser.email}, {id: firstUser.id}) + .then(function () { + done(new Error('Already existing email address was accepted')); + }) + .catch(function (err) { + (err instanceof errors.ValidationError).should.eql(true); + done(); + }); }); it('can edit invited user', function (done) {