@@ -5,6 +5,7 @@ const { createPromiseCallback } = require('loopback-datasource-juggler/lib/utils
55const _defaults = require ( 'lodash' ) . defaults
66const _get = require ( 'lodash' ) . get
77const Promise = require ( 'bluebird' )
8+ const LoopBackContext = require ( 'loopback-context' )
89
910module . exports = class AccessUtils {
1011 constructor ( app , options ) {
@@ -58,7 +59,7 @@ module.exports = class AccessUtils {
5859 if ( typeof Model . observe === 'function' ) {
5960 debug ( 'Attaching access observer to %s' , modelName )
6061 Model . observe ( 'access' , ( ctx , next ) => {
61- const currentUser = this . getCurrentUser ( )
62+ const currentUser = AccessUtils . getCurrentUser ( )
6263
6364 if ( currentUser ) {
6465 // Do not filter if options.skipAccess has been set.
@@ -74,7 +75,7 @@ module.exports = class AccessUtils {
7475 }
7576
7677 // Do not apply filters if no group access acls were applied.
77- const loopbackContext = this . app . loopback . getCurrentContext ( )
78+ const loopbackContext = LoopBackContext . getCurrentContext ( )
7879 const groupAccessApplied = Boolean ( loopbackContext && loopbackContext . get ( 'groupAccessApplied' ) )
7980
8081 if ( ! groupAccessApplied ) {
@@ -197,8 +198,8 @@ module.exports = class AccessUtils {
197198 getUserGroups ( userId , force , cb ) {
198199 force = force || false
199200 cb = cb || createPromiseCallback ( )
200- const currentUser = this . getCurrentUser ( )
201- const currentUserGroups = this . getCurrentUserGroups ( )
201+ const currentUser = AccessUtils . getCurrentUser ( )
202+ const currentUserGroups = AccessUtils . getCurrentUserGroups ( )
202203
203204 // Return from the context cache if exists.
204205 if ( ! force && currentUser && currentUser . getId ( ) === userId ) {
@@ -227,8 +228,8 @@ module.exports = class AccessUtils {
227228 *
228229 * @returns {Object } Returns the currently logged in user.
229230 */
230- getCurrentUser ( ) {
231- const ctx = this . app . loopback . getCurrentContext ( )
231+ static getCurrentUser ( ) {
232+ const ctx = LoopBackContext . getCurrentContext ( )
232233 const currentUser = ( ctx && ctx . get ( 'currentUser' ) ) || null
233234
234235 return currentUser
@@ -239,8 +240,8 @@ module.exports = class AccessUtils {
239240 *
240241 * @returns {Array } Returnds a list of access groups the user is a member of.
241242 */
242- getCurrentUserGroups ( ) {
243- const ctx = this . app . loopback . getCurrentContext ( )
243+ static getCurrentUserGroups ( ) {
244+ const ctx = LoopBackContext . getCurrentContext ( )
244245 const currentUserGroups = ( ctx && ctx . get ( 'currentUserGroups' ) ) || [ ]
245246
246247 return currentUserGroups
@@ -295,7 +296,7 @@ module.exports = class AccessUtils {
295296 return cb . promise
296297 }
297298
298- this . app . loopback . getCurrentContext ( ) . set ( 'groupAccessApplied' , true )
299+ LoopBackContext . getCurrentContext ( ) . set ( 'groupAccessApplied' , true )
299300
300301 /**
301302 * Basic application that does not cover static methods. Similar to $owner. (RECOMMENDED)
@@ -368,7 +369,7 @@ module.exports = class AccessUtils {
368369
369370 // Note the fact that we are allowing access due to passing an ACL.
370371 if ( res ) {
371- this . app . loopback . getCurrentContext ( ) . set ( 'groupAccessApplied' , true )
372+ LoopBackContext . getCurrentContext ( ) . set ( 'groupAccessApplied' , true )
372373 }
373374
374375 return cb ( null , res )
0 commit comments