@@ -272,34 +272,39 @@ module.exports = class AccessUtils {
272
272
const Role = this . app . models [ this . options . roleModel ] ;
273
273
274
274
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
+
275
288
if ( ! context || ! context . model || ! context . modelId ) {
276
289
process . nextTick ( ( ) => {
277
290
debug ( 'Allow passthrough access (context: %s, context.model: %s, context.modelId: %s)' ,
278
291
! ! context , ! ! context . model , ! ! context . modelId ) ;
292
+ if ( cb ) cb ( null , true ) ;
293
+ } ) ;
294
+ return ;
295
+ }
279
296
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' ) ;
286
301
if ( cb ) cb ( null , false ) ;
287
302
} ) ;
288
303
return ;
289
304
}
290
305
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
-
300
306
return this . isGroupMemberWithRole ( modelClass , modelId , userId , roleName )
301
307
. then ( res => {
302
- debug ( 'Resolved to' , res ) ;
303
308
cb ( null , res ) ;
304
309
} )
305
310
. catch ( cb ) ;
@@ -369,7 +374,7 @@ module.exports = class AccessUtils {
369
374
370
375
// Is the modelClass GroupModel or a subclass of GroupModel?
371
376
if ( this . isGroupModel ( modelClass ) ) {
372
- this . hasRoleInGroup ( userId , roleId , modelId , context )
377
+ this . hasRoleInGroup ( userId , roleId , modelId )
373
378
. then ( res => cb ( null , res ) ) ;
374
379
return cb . promise ;
375
380
}
@@ -385,7 +390,7 @@ module.exports = class AccessUtils {
385
390
// Ensure groupId exists and is not a function/relation
386
391
if ( groupId && 'function' !== typeof groupId ) {
387
392
if ( cb ) {
388
- return this . hasRoleInGroup ( userId , roleId , groupId , context )
393
+ return this . hasRoleInGroup ( userId , roleId , groupId )
389
394
. then ( res => cb ( null , res ) ) ;
390
395
}
391
396
} else {
@@ -405,7 +410,7 @@ module.exports = class AccessUtils {
405
410
function processRelatedGroup ( err , group ) {
406
411
if ( ! err && group ) {
407
412
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 ( ) ) ) ;
409
414
} else {
410
415
if ( cb ) cb ( err , false ) ;
411
416
}
@@ -414,7 +419,7 @@ module.exports = class AccessUtils {
414
419
return cb . promise ;
415
420
} ;
416
421
417
- hasRoleInGroup ( userId , role , group , context , cb ) {
422
+ hasRoleInGroup ( userId , role , group , cb ) {
418
423
debug ( 'hasRoleInGroup: role: %o, group: %o, userId: %o' , role , group , userId ) ;
419
424
cb = cb || createPromiseCallback ( ) ;
420
425
const GroupAccess = this . app . models [ this . options . groupAccessModel ] ;
0 commit comments