Skip to content

Commit 1583970

Browse files
author
Tom Kirkpatrick
committed
Fix bug resolving related groups
1 parent a4ba06d commit 1583970

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

lib/utils.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -272,34 +272,39 @@ module.exports = class AccessUtils {
272272
const Role = this.app.models[this.options.roleModel];
273273

274274
Role.registerResolver(accessGroup, (role, context, cb) => {
275+
const modelClass = context.model;
276+
const modelId = context.modelId;
277+
const userId = context.getUserId();
278+
const roleName = this.extractRoleName(role);
279+
const GroupAccess = this.app.models[this.options.groupAccessModel];
280+
const scope = { };
281+
282+
if (userId) {
283+
this.app.loopback.getCurrentContext().set('groupAccessApplied', true);
284+
}
285+
286+
debug(`Role resolver for ${role}: evaluate ${modelClass.modelName} with id: ${modelId} for user: ${userId}`);
287+
275288
if (!context || !context.model || !context.modelId) {
276289
process.nextTick(() => {
277290
debug('Allow passthrough access (context: %s, context.model: %s, context.modelId: %s)',
278291
!!context, !!context.model, !!context.modelId);
292+
if (cb) cb(null, true);
293+
});
294+
return;
295+
}
279296

280-
const currentUser = this.getCurrentUser();
281-
282-
if (currentUser) {
283-
this.app.loopback.getCurrentContext().set('groupAccessApplied', true);
284-
}
285-
297+
// No userId is present
298+
if (!userId) {
299+
process.nextTick(() => {
300+
debug('Deny access for anonymous user');
286301
if (cb) cb(null, false);
287302
});
288303
return;
289304
}
290305

291-
const modelClass = context.model;
292-
const modelId = context.modelId;
293-
const userId = context.getUserId();
294-
const roleName = this.extractRoleName(role);
295-
const GroupAccess = this.app.models[this.options.groupAccessModel];
296-
const scope = { };
297-
298-
debug(`Role resolver for ${role}: evaluate ${modelClass.modelName} with id: ${modelId} for user: ${userId}`);
299-
300306
return this.isGroupMemberWithRole(modelClass, modelId, userId, roleName)
301307
.then(res => {
302-
debug('Resolved to', res);
303308
cb(null, res);
304309
})
305310
.catch(cb);
@@ -369,7 +374,7 @@ module.exports = class AccessUtils {
369374

370375
// Is the modelClass GroupModel or a subclass of GroupModel?
371376
if (this.isGroupModel(modelClass)) {
372-
this.hasRoleInGroup(userId, roleId, modelId, context)
377+
this.hasRoleInGroup(userId, roleId, modelId)
373378
.then(res => cb(null, res));
374379
return cb.promise;
375380
}
@@ -385,7 +390,7 @@ module.exports = class AccessUtils {
385390
// Ensure groupId exists and is not a function/relation
386391
if (groupId && 'function' !== typeof groupId) {
387392
if (cb) {
388-
return this.hasRoleInGroup(userId, roleId, groupId, context)
393+
return this.hasRoleInGroup(userId, roleId, groupId)
389394
.then(res => cb(null, res));
390395
}
391396
} else {
@@ -405,7 +410,7 @@ module.exports = class AccessUtils {
405410
function processRelatedGroup(err, group) {
406411
if (!err && group) {
407412
debug('Group found: %j', group.getId());
408-
if (cb) cb(null, this.hasRoleInGroup(userId, roleId, group.getId(), context, cb));
413+
if (cb) cb(null, this.hasRoleInGroup(userId, roleId, group.getId()));
409414
} else {
410415
if (cb) cb(err, false);
411416
}
@@ -414,7 +419,7 @@ module.exports = class AccessUtils {
414419
return cb.promise;
415420
};
416421

417-
hasRoleInGroup(userId, role, group, context, cb) {
422+
hasRoleInGroup(userId, role, group, cb) {
418423
debug('hasRoleInGroup: role: %o, group: %o, userId: %o', role, group, userId);
419424
cb = cb || createPromiseCallback();
420425
const GroupAccess = this.app.models[this.options.groupAccessModel];

0 commit comments

Comments
 (0)