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) {