Skip to content

Commit f2d2219

Browse files
author
Tom Kirkpatrick
committed
fix: bind loopback-context for concurrency
1 parent e0c926a commit f2d2219

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

lib/middleware/access-logger.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
'use strict'
22

3+
const LoopBackContext = require('loopback-context')
34
const debug = require('debug')('loopback:component:access:logger')
45

56
module.exports = function accessLoggerMiddleware() {
67
debug('initializing access logger middleware')
78
return function accessLogger(req, res, next) {
9+
const loopbackContext = LoopBackContext.getCurrentContext({ bind: true })
10+
11+
next = loopbackContext.bind(next)
12+
813
if (req.accessToken) {
914
debug('req: %s %s, token: %o', req.method, req.originalUrl, req.accessToken)
1015
}

lib/middleware/user-context.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ module.exports = function userContextMiddleware() {
88
debug('initializing user context middleware')
99
// set current user to enable user access for remote methods
1010
return function userContext(req, res, next) {
11-
const loopbackContext = LoopBackContext.getCurrentContext()
11+
const loopbackContext = LoopBackContext.getCurrentContext({ bind: true })
12+
13+
next = loopbackContext.bind(next)
1214

1315
if (!loopbackContext) {
1416
debug('No user context (loopback current context not found)')

lib/mixins/get-current-user.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = function getCurrentUserMixin(Model) {
77
debug('initializing GetCurrentUser Mixin for model %s', Model.modelName)
88

99
Model.getCurrentUser = function getCurrentUser() {
10-
const ctx = LoopBackContext.getCurrentContext()
10+
const ctx = LoopBackContext.getCurrentContext({ bind: true })
1111
const currentUser = (ctx && ctx.get('currentUser')) || null
1212

1313
if (ctx) {

lib/utils.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ module.exports = class AccessUtils {
7575
}
7676

7777
// Do not apply filters if no group access acls were applied.
78-
const loopbackContext = LoopBackContext.getCurrentContext()
78+
const loopbackContext = LoopBackContext.getCurrentContext({ bind: true })
7979
const groupAccessApplied = Boolean(loopbackContext && loopbackContext.get('groupAccessApplied'))
8080

8181
if (!groupAccessApplied) {
@@ -229,7 +229,7 @@ module.exports = class AccessUtils {
229229
* @returns {Object} Returns the currently logged in user.
230230
*/
231231
getCurrentUser() {
232-
const ctx = LoopBackContext.getCurrentContext()
232+
const ctx = LoopBackContext.getCurrentContext({ bind: true })
233233
const currentUser = (ctx && ctx.get('currentUser')) || null
234234

235235
return currentUser
@@ -241,7 +241,7 @@ module.exports = class AccessUtils {
241241
* @returns {Array} Returnds a list of access groups the user is a member of.
242242
*/
243243
getCurrentUserGroups() {
244-
const ctx = LoopBackContext.getCurrentContext()
244+
const ctx = LoopBackContext.getCurrentContext({ bind: true })
245245
const currentUserGroups = (ctx && ctx.get('currentUserGroups')) || []
246246

247247
return currentUserGroups
@@ -296,7 +296,7 @@ module.exports = class AccessUtils {
296296
return cb.promise
297297
}
298298

299-
LoopBackContext.getCurrentContext().set('groupAccessApplied', true)
299+
LoopBackContext.getCurrentContext({ bind: true }).set('groupAccessApplied', true)
300300

301301
/**
302302
* Basic application that does not cover static methods. Similar to $owner. (RECOMMENDED)
@@ -369,7 +369,7 @@ module.exports = class AccessUtils {
369369

370370
// Note the fact that we are allowing access due to passing an ACL.
371371
if (res) {
372-
LoopBackContext.getCurrentContext().set('groupAccessApplied', true)
372+
LoopBackContext.getCurrentContext({ bind: true }).set('groupAccessApplied', true)
373373
}
374374

375375
return cb(null, res)

0 commit comments

Comments
 (0)