@@ -111,7 +111,7 @@ module.exports = class AccessUtils {
111
111
*/
112
112
buildFilter ( userId , Model ) {
113
113
const filter = { } ;
114
- const key = this . isGroupModel ( Model ) ? Model . getIdName ( ) : this . options . foreignKey ;
114
+ const key = this . isGroupModel ( Model ) ? Model . getIdName ( ) : this . options . foreignKey ;
115
115
// TODO: Support key determination based on the belongsTo relationship.
116
116
117
117
return this . getUserGroups ( userId )
@@ -177,7 +177,7 @@ module.exports = class AccessUtils {
177
177
rel = modelClass . relations [ rel ] ;
178
178
// debug('Checking relation %s to %s: %j', r, rel.modelTo.modelName, rel);
179
179
if ( rel . type === 'belongsTo' && this . isGroupModel ( rel . modelTo ) ) {
180
- return models . push ( modelName ) ;
180
+ models . push ( modelName ) ;
181
181
}
182
182
}
183
183
} ) ;
@@ -230,6 +230,7 @@ module.exports = class AccessUtils {
230
230
getCurrentUser ( ) {
231
231
const ctx = this . app . loopback . getCurrentContext ( ) ;
232
232
const currentUser = ctx && ctx . get ( 'currentUser' ) || null ;
233
+
233
234
return currentUser ;
234
235
}
235
236
@@ -241,6 +242,7 @@ module.exports = class AccessUtils {
241
242
getCurrentUserGroups ( ) {
242
243
const ctx = this . app . loopback . getCurrentContext ( ) ;
243
244
const currentUserGroups = ctx && ctx . get ( 'currentUserGroups' ) || [ ] ;
245
+
244
246
return currentUserGroups ;
245
247
}
246
248
@@ -274,6 +276,7 @@ module.exports = class AccessUtils {
274
276
const Role = this . app . models [ this . options . roleModel ] ;
275
277
276
278
Role . registerResolver ( accessGroup , ( role , context , cb ) => {
279
+ cb = cb || createPromiseCallback ( ) ;
277
280
const modelClass = context . model ;
278
281
const modelId = context . modelId ;
279
282
const userId = context . getUserId ( ) ;
@@ -287,9 +290,9 @@ module.exports = class AccessUtils {
287
290
if ( ! userId ) {
288
291
process . nextTick ( ( ) => {
289
292
debug ( 'Deny access for anonymous user' ) ;
290
- if ( cb ) cb ( null , false ) ;
293
+ cb ( null , false ) ;
291
294
} ) ;
292
- return ;
295
+ return cb . promise ;
293
296
}
294
297
295
298
this . app . loopback . getCurrentContext ( ) . set ( 'groupAccessApplied' , true ) ;
@@ -302,23 +305,23 @@ module.exports = class AccessUtils {
302
305
if ( ! context || ! context . model || ! context . modelId ) {
303
306
process . nextTick ( ( ) => {
304
307
debug ( 'Deny access (context: %s, context.model: %s, context.modelId: %s)' ,
305
- ! ! context , ! ! context . model , ! ! context . modelId ) ;
306
- if ( cb ) cb ( null , false ) ;
308
+ Boolean ( context ) , Boolean ( context . model ) , Boolean ( context . modelId ) ) ;
309
+ cb ( null , false ) ;
307
310
} ) ;
308
- return ;
311
+ return cb . promise ;
309
312
}
310
313
311
- return this . isGroupMemberWithRole ( modelClass , modelId , userId , roleName )
312
- . then ( res => {
313
- cb ( null , res ) ;
314
- } )
314
+ this . isGroupMemberWithRole ( modelClass , modelId , userId , roleName )
315
+ . then ( res => cb ( null , res ) )
315
316
. catch ( cb ) ;
317
+
318
+ return cb . promise ;
316
319
}
317
320
318
321
/**
319
322
* More complex application that also covers static methods.
320
323
*/
321
- return Promise . join ( this . getCurrentGroupId ( context ) , this . getTargetGroupId ( context ) ,
324
+ Promise . join ( this . getCurrentGroupId ( context ) , this . getTargetGroupId ( context ) ,
322
325
( currentGroupId , targetGroupId ) => {
323
326
if ( ! currentGroupId ) {
324
327
// TODO: Use promise cancellation to abort the chain early.
@@ -329,10 +332,7 @@ module.exports = class AccessUtils {
329
332
scope . currentGroupId = currentGroupId ;
330
333
scope . targetGroupId = targetGroupId ;
331
334
const actions = [ ] ;
332
- const conditions = {
333
- userId : userId ,
334
- role : roleName
335
- } ;
335
+ const conditions = { userId, role : roleName } ;
336
336
337
337
conditions [ this . options . foreignKey ] = currentGroupId ;
338
338
actions . push ( GroupAccess . count ( conditions ) ) ;
@@ -375,9 +375,9 @@ module.exports = class AccessUtils {
375
375
return cb ( null , res ) ;
376
376
} )
377
377
. catch ( cb ) ;
378
-
379
- } ) ;
380
- } ;
378
+ return cb . promise ;
379
+ } ) ;
380
+ }
381
381
382
382
/**
383
383
* Check if a given user ID has a given role in the model instances group.
@@ -410,59 +410,51 @@ module.exports = class AccessUtils {
410
410
modelClass . findById ( modelId , ( err , inst ) => {
411
411
if ( err || ! inst ) {
412
412
debug ( 'Model not found for id %j' , modelId ) ;
413
- if ( cb ) cb ( err , false ) ;
414
- return ;
413
+ return cb ( err , false ) ;
415
414
}
416
415
debug ( 'Model found: %j' , inst ) ;
417
- var groupId = inst [ this . options . foreignKey ] ;
416
+ const groupId = inst [ this . options . foreignKey ] ;
417
+
418
418
// Ensure groupId exists and is not a function/relation
419
- if ( groupId && 'function' !== typeof groupId ) {
420
- if ( cb ) {
421
- return this . hasRoleInGroup ( userId , roleId , groupId )
422
- . then ( res => cb ( null , res ) ) ;
423
- }
424
- } else {
425
- // Try to follow belongsTo
426
- for ( var r in modelClass . relations ) {
427
- var rel = modelClass . relations [ r ] ;
428
- if ( rel . type === 'belongsTo' && isGroupModel ( rel . modelTo ) ) {
429
- debug ( 'Checking relation %s to %s: %j' , r , rel . modelTo . modelName , rel ) ;
430
- inst [ r ] ( processRelatedGroup ) ;
431
- return ;
432
- }
433
- }
434
- debug ( 'No matching belongsTo relation found for model %j and group: %j' , modelId , groupId ) ;
435
- if ( cb ) cb ( null , false ) ;
419
+ if ( groupId && typeof groupId !== 'function' ) {
420
+ return this . hasRoleInGroup ( userId , roleId , groupId )
421
+ . then ( res => cb ( null , res ) ) ;
436
422
}
423
+ // Try to follow belongsTo
424
+ for ( const relName in modelClass . relations ) {
425
+ const rel = modelClass . relations [ relName ] ;
437
426
438
- function processRelatedGroup ( err , group ) {
439
- if ( ! err && group ) {
440
- debug ( 'Group found: %j' , group . getId ( ) ) ;
441
- if ( cb ) cb ( null , this . hasRoleInGroup ( userId , roleId , group . getId ( ) ) ) ;
442
- } else {
443
- if ( cb ) cb ( err , false ) ;
427
+ if ( rel . type === 'belongsTo' && this . isGroupModel ( rel . modelTo ) ) {
428
+ debug ( 'Checking relation %s to %s: %j' , relName , rel . modelTo . modelName , rel ) ;
429
+ return inst [ relName ] ( function processRelatedGroup ( error , group ) {
430
+ if ( ! error && group ) {
431
+ debug ( 'Group found: %j' , group . getId ( ) ) ;
432
+ return cb ( null , this . hasRoleInGroup ( userId , roleId , group . getId ( ) ) ) ;
433
+ }
434
+ return cb ( error , false ) ;
435
+ } ) ;
444
436
}
445
437
}
438
+ debug ( 'No matching belongsTo relation found for model %j and group: %j' , modelId , groupId ) ;
439
+ return cb ( null , false ) ;
446
440
} ) ;
447
441
return cb . promise ;
448
- } ;
442
+ }
449
443
450
444
hasRoleInGroup ( userId , role , group , cb ) {
451
445
debug ( 'hasRoleInGroup: role: %o, group: %o, userId: %o' , role , group , userId ) ;
452
446
cb = cb || createPromiseCallback ( ) ;
453
447
const GroupAccess = this . app . models [ this . options . groupAccessModel ] ;
454
- const conditions = {
455
- userId,
456
- role,
457
- }
448
+ const conditions = { userId, role } ;
449
+
458
450
conditions [ this . options . foreignKey ] = group ;
459
451
GroupAccess . count ( conditions )
460
452
. then ( count => {
461
453
const res = count > 0 ;
462
454
463
455
debug ( `user ${ userId } has role ${ role } in group ${ group } : ${ res } ` ) ;
464
456
cb ( null , res ) ;
465
- } )
457
+ } ) ;
466
458
return cb . promise ;
467
459
}
468
460
0 commit comments