Preserve sessions on User.save() making no changes
This commit is contained in:
parent
50743e94be
commit
05db4337cf
|
@ -866,30 +866,16 @@ module.exports = function(User) {
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Delete old sessions once email is updated
|
User.observe('before save', function prepareForTokenInvalidation(ctx, next) {
|
||||||
User.observe('before save', function beforeEmailUpdate(ctx, next) {
|
|
||||||
if (!ctx.Model.app.get('logoutSessionsOnSensitiveChanges')) return next();
|
if (!ctx.Model.app.get('logoutSessionsOnSensitiveChanges')) return next();
|
||||||
|
|
||||||
var emailChanged;
|
|
||||||
if (ctx.isNewInstance) return next();
|
if (ctx.isNewInstance) return next();
|
||||||
if (!ctx.where && !ctx.instance) return next();
|
if (!ctx.where && !ctx.instance) return next();
|
||||||
|
|
||||||
var pkName = ctx.Model.definition.idName() || 'id';
|
var pkName = ctx.Model.definition.idName() || 'id';
|
||||||
|
|
||||||
var isPartialUpdateChangingPassword = ctx.data && 'password' in ctx.data;
|
var where = ctx.where;
|
||||||
|
if (!where) {
|
||||||
// Full replace of User instance => assume password change.
|
|
||||||
// HashPassword returns a different value for each invocation,
|
|
||||||
// therefore we cannot tell whether ctx.instance.password is the same
|
|
||||||
// or not.
|
|
||||||
var isFullReplaceChangingPassword = !!ctx.instance;
|
|
||||||
|
|
||||||
ctx.hookState.isPasswordChange = isPartialUpdateChangingPassword ||
|
|
||||||
isFullReplaceChangingPassword;
|
|
||||||
|
|
||||||
var where;
|
|
||||||
if (ctx.where) {
|
|
||||||
where = ctx.where;
|
|
||||||
} else {
|
|
||||||
where = {};
|
where = {};
|
||||||
where[pkName] = ctx.instance[pkName];
|
where[pkName] = ctx.instance[pkName];
|
||||||
}
|
}
|
||||||
|
@ -899,9 +885,11 @@ module.exports = function(User) {
|
||||||
ctx.hookState.originalUserData = userInstances.map(function(u) {
|
ctx.hookState.originalUserData = userInstances.map(function(u) {
|
||||||
var user = {};
|
var user = {};
|
||||||
user[pkName] = u[pkName];
|
user[pkName] = u[pkName];
|
||||||
user['email'] = u['email'];
|
user.email = u.email;
|
||||||
|
user.password = u.password;
|
||||||
return user;
|
return user;
|
||||||
});
|
});
|
||||||
|
var emailChanged;
|
||||||
if (ctx.instance) {
|
if (ctx.instance) {
|
||||||
emailChanged = ctx.instance.email !== ctx.hookState.originalUserData[0].email;
|
emailChanged = ctx.instance.email !== ctx.hookState.originalUserData[0].email;
|
||||||
if (emailChanged && ctx.Model.settings.emailVerificationRequired) {
|
if (emailChanged && ctx.Model.settings.emailVerificationRequired) {
|
||||||
|
@ -920,7 +908,7 @@ module.exports = function(User) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
User.observe('after save', function afterEmailUpdate(ctx, next) {
|
User.observe('after save', function invalidateOtherTokens(ctx, next) {
|
||||||
if (!ctx.Model.app.get('logoutSessionsOnSensitiveChanges')) return next();
|
if (!ctx.Model.app.get('logoutSessionsOnSensitiveChanges')) return next();
|
||||||
|
|
||||||
if (!ctx.instance && !ctx.data) return next();
|
if (!ctx.instance && !ctx.data) return next();
|
||||||
|
@ -928,12 +916,13 @@ module.exports = function(User) {
|
||||||
|
|
||||||
var pkName = ctx.Model.definition.idName() || 'id';
|
var pkName = ctx.Model.definition.idName() || 'id';
|
||||||
var newEmail = (ctx.instance || ctx.data).email;
|
var newEmail = (ctx.instance || ctx.data).email;
|
||||||
var isPasswordChange = ctx.hookState.isPasswordChange;
|
var newPassword = (ctx.instance || ctx.data).password;
|
||||||
|
|
||||||
if (!newEmail && !isPasswordChange) return next();
|
if (!newEmail && !newPassword) return next();
|
||||||
|
|
||||||
var userIdsToExpire = ctx.hookState.originalUserData.filter(function(u) {
|
var userIdsToExpire = ctx.hookState.originalUserData.filter(function(u) {
|
||||||
return (newEmail && u.email !== newEmail) || isPasswordChange;
|
return (newEmail && u.email !== newEmail) ||
|
||||||
|
(newPassword && u.password !== newPassword);
|
||||||
}).map(function(u) {
|
}).map(function(u) {
|
||||||
return u[pkName];
|
return u[pkName];
|
||||||
});
|
});
|
||||||
|
|
|
@ -2097,6 +2097,13 @@ describe('User', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('keeps sessions AS IS when calling save() with no changes', function(done) {
|
||||||
|
user.save(function(err) {
|
||||||
|
if (err) return done(err);
|
||||||
|
assertPreservedTokens(done);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('keeps sessions AS IS if firstName is added using `updateOrCreate`', function(done) {
|
it('keeps sessions AS IS if firstName is added using `updateOrCreate`', function(done) {
|
||||||
User.updateOrCreate({
|
User.updateOrCreate({
|
||||||
pk: user.pk,
|
pk: user.pk,
|
||||||
|
|
Loading…
Reference in New Issue