🐛 fix email already in use (#7987)

refs #7981

- usage of data.id was wrong
- usage of id comparison was wrong
This commit is contained in:
Katharina Irrgang 2017-02-13 16:36:21 +01:00 committed by Kevin Ansfield
parent d9e87aa793
commit 2a5927b4f9
2 changed files with 12 additions and 12 deletions

View File

@ -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')}));
}
});

View File

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