Skip to content

Commit bdeaf65

Browse files
committed
Validate non-email property partial update
1 parent 46f0c06 commit bdeaf65

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

common/models/user.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,9 @@ module.exports = function(User) {
681681
UserModel.observe('after save', function afterEmailUpdate(ctx, next) {
682682
if (!ctx.Model.relations.accessTokens) return next();
683683
var AccessToken = ctx.Model.relations.accessTokens.modelTo;
684+
if (!ctx.instance && !ctx.data) return next();
684685
var newEmail = (ctx.instance || ctx.data).email;
686+
if (!newEmail) return next();
685687
if (!ctx.hookState.originalUserData) return next();
686688
var idsToExpire = ctx.hookState.originalUserData.filter(function(u) {
687689
return u.email !== newEmail;

test/user.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1972,6 +1972,43 @@ describe('User', function() {
19721972
], done);
19731973
});
19741974

1975+
it('keeps sessions AS IS if non-email property is changed using updateAll', function(done) {
1976+
var userPartial;
1977+
async.series([
1978+
function createPartialUser(next) {
1979+
User.create(
1980+
{ email: '[email protected]', password: 'pass1', age: 25 },
1981+
function(err, partialInstance) {
1982+
if (err) return next(err);
1983+
userPartial = partialInstance;
1984+
next();
1985+
});
1986+
},
1987+
function loginPartiallUser(next) {
1988+
User.login({ email: '[email protected]', password: 'pass1' }, function(err, ats) {
1989+
if (err) return next(err);
1990+
next();
1991+
});
1992+
},
1993+
function updatePartialUser(next) {
1994+
User.updateAll(
1995+
{ id: userPartial.id },
1996+
{ age: userPartial.age + 1 },
1997+
function(err, info) {
1998+
if (err) return next(err);
1999+
next();
2000+
});
2001+
},
2002+
function verifyTokensOfPartialUser(next) {
2003+
AccessToken.find({ where: { userId: userPartial.id }}, function(err, tokens1) {
2004+
if (err) return next(err);
2005+
expect(tokens1.length).to.equal(1);
2006+
next();
2007+
});
2008+
},
2009+
], done);
2010+
});
2011+
19752012
function assertPreservedToken(done) {
19762013
AccessToken.find({ where: { userId: user.id }}, function(err, tokens) {
19772014
if (err) return done(err);

0 commit comments

Comments
 (0)