Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3bf84ba

Browse files
authored
Merge pull request strongloop#3720 from STRML/fix/falsy-id-3.x
Fix handling of falsy model ids
2 parents 1babfcd + 2bfd67c commit 3bf84ba

File tree

4 files changed

+66
-9
lines changed

4 files changed

+66
-9
lines changed

lib/access-context.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,16 @@ function AccessContext(context) {
8080
var principalType = context.principalType || Principal.USER;
8181
var principalId = context.principalId || undefined;
8282
var principalName = context.principalName || undefined;
83-
84-
if (principalId) {
83+
if (principalId != null) {
8584
this.addPrincipal(principalType, principalId, principalName);
8685
}
8786

8887
var token = this.accessToken || {};
8988

90-
if (token.userId) {
89+
if (token.userId != null) {
9190
this.addPrincipal(Principal.USER, token.userId);
9291
}
93-
if (token.appId) {
92+
if (token.appId != null) {
9493
this.addPrincipal(Principal.APPLICATION, token.appId);
9594
}
9695
this.remotingContext = context.remotingContext;
@@ -193,7 +192,7 @@ AccessContext.prototype.getAppId = function() {
193192
* @returns {boolean}
194193
*/
195194
AccessContext.prototype.isAuthenticated = function() {
196-
return !!(this.getUserId() || this.getAppId());
195+
return this.getUserId() != null || this.getAppId() != null;
197196
};
198197

199198
/**

lib/model.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,13 @@ module.exports = function(registry) {
161161
}
162162
}
163163

164-
if (id && data) {
164+
if (id != null && data) {
165165
var model = new ModelCtor(data);
166166
model.id = id;
167167
fn(null, model);
168168
} else if (data) {
169169
fn(null, new ModelCtor(data));
170-
} else if (id) {
170+
} else if (id != null) {
171171
var filter = {};
172172
ModelCtor.findById(id, filter, options, function(err, model) {
173173
if (err) {

lib/persisted-model.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,7 +1710,7 @@ module.exports = function(registry) {
17101710
ctx.instance, ctx.currentInstance, ctx.where, ctx.data);
17111711
}
17121712

1713-
if (id) {
1713+
if (id != null) {
17141714
ctx.Model.rectifyChange(id, reportErrorAndNext);
17151715
} else {
17161716
ctx.Model.rectifyAllChanges(reportErrorAndNext);
@@ -1734,7 +1734,7 @@ module.exports = function(registry) {
17341734
debug('context instance:%j where:%j', ctx.instance, ctx.where);
17351735
}
17361736

1737-
if (id) {
1737+
if (id != null) {
17381738
ctx.Model.rectifyChange(id, reportErrorAndNext);
17391739
} else {
17401740
ctx.Model.rectifyAllChanges(reportErrorAndNext);

test/role.test.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,64 @@ describe('role model', function() {
364364
});
365365
});
366366

367+
it.only('should be properly authenticated with 0 userId', function(done) {
368+
var userData = {name: 'Raymond', email: '[email protected]', password: 'foobar', id: 0};
369+
var TestUser = app.registry.createModel({
370+
name: 'TestUser',
371+
base: 'User',
372+
// forceId is set to false so we can create a user with a known ID,
373+
// in this case 0 - which used to fail the falsy checks.
374+
forceId: false,
375+
});
376+
app.model(TestUser, {dataSource: 'db'});
377+
378+
TestUser.create(userData, function(err, user) {
379+
if (err) return done(err);
380+
Role.create({name: 'userRole'}, function(err, role) {
381+
if (err) return done(err);
382+
role.principals.create({principalType: RoleMapping.USER, principalId: user.id},
383+
function(err, p) {
384+
if (err) return done(err);
385+
async.series([
386+
function(next) {
387+
Role.isInRole(
388+
'userRole',
389+
{principalType: RoleMapping.USER, principalId: user.id},
390+
function(err, inRole) {
391+
if (err) return next(err);
392+
assert(!!inRole);
393+
next();
394+
});
395+
},
396+
function(next) {
397+
Role.isInRole(
398+
'userRole',
399+
{principalType: RoleMapping.APP, principalId: user.id},
400+
function(err, inRole) {
401+
if (err) return next(err);
402+
assert(!inRole);
403+
next();
404+
});
405+
},
406+
function(next) {
407+
Role.getRoles(
408+
{principalType: RoleMapping.USER, principalId: user.id},
409+
function(err, roles) {
410+
if (err) return next(err);
411+
expect(roles).to.eql([
412+
Role.AUTHENTICATED,
413+
Role.EVERYONE,
414+
role.id,
415+
]);
416+
next();
417+
});
418+
},
419+
], done);
420+
});
421+
});
422+
});
423+
});
424+
367425
// this test should be split to address one resolver at a time
368426
it('supports built-in role resolvers', function(done) {
369427
Role.registerResolver('returnPromise', function(role, context) {

0 commit comments

Comments
 (0)