Skip to content

Commit 95bcf03

Browse files
authored
Merge pull request #3917 from mcitdev/master
Fix crash in User model's "before delete" hook
2 parents 9c554fd + 37e57f6 commit 95bcf03

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

common/models/user.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,9 @@ module.exports = function(User) {
346346
};
347347

348348
User.observe('before delete', function(ctx, next) {
349+
// Do nothing when the access control was disabled for this user model.
350+
if (!ctx.Model.relations.accessTokens) return next();
351+
349352
var AccessToken = ctx.Model.relations.accessTokens.modelTo;
350353
var pkName = ctx.Model.definition.idName() || 'id';
351354
ctx.Model.find({where: ctx.where, fields: [pkName]}, function(err, list) {

test/user.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,23 @@ describe('User', function() {
299299
});
300300
});
301301

302+
it('skips token invalidation when the relation is not configured', () => {
303+
const app = loopback({localRegistry: true, loadBuiltinModels: true});
304+
app.dataSource('db', {connector: 'memory'});
305+
306+
const PrivateUser = app.registry.createModel({
307+
name: 'PrivateUser',
308+
base: 'User',
309+
// Speed up the password hashing algorithm for tests
310+
saltWorkFactor: 4,
311+
});
312+
app.model(PrivateUser, {dataSource: 'db'});
313+
314+
return PrivateUser.create({email: 'private@example.com', password: 'pass'})
315+
.then(u => PrivateUser.deleteById(u.id));
316+
// the test passed when the operation did not crash
317+
});
318+
302319
it('invalidates the user\'s accessToken when the user is deleted all', function(done) {
303320
var userIds = [];
304321
var users;

0 commit comments

Comments
 (0)