Skip to content

Commit 8ca5d8e

Browse files
author
Tom Kirkpatrick
committed
fix: use loopback-context to access context info
BREAKING CHANGE: now requires loopback-context to be installed and configured
1 parent f320c27 commit 8ca5d8e

File tree

7 files changed

+36
-26
lines changed

7 files changed

+36
-26
lines changed

lib/middleware/user-context.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
'use strict'
22

33
const debug = require('debug')('loopback:component:access:context')
4-
const loopback = require('loopback')
54
const Promise = require('bluebird')
5+
const LoopBackContext = require('loopback-context')
66

77
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 = loopback.getCurrentContext()
11+
const loopbackContext = LoopBackContext.getCurrentContext()
1212

1313
if (!loopbackContext) {
1414
debug('No user context (loopback current context not found)')

lib/mixins/get-current-user.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
'use strict'
22

3-
const loopback = require('loopback')
43
const debug = require('debug')('loopback:component:access:utils')
4+
const LoopBackContext = require('loopback-context')
55

66
module.exports = function getCurrentUserMixin(Model) {
77
debug('initializing GetCurrentUser Mixin for model %s', Model.modelName)
88

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

1313
if (ctx) {

lib/utils.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { createPromiseCallback } = require('loopback-datasource-juggler/lib/utils
55
const _defaults = require('lodash').defaults
66
const _get = require('lodash').get
77
const Promise = require('bluebird')
8+
const LoopBackContext = require('loopback-context')
89

910
module.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)

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
"lodash": "^4.17.4"
3939
},
4040
"peerDependencies": {
41-
"loopback": "^2.25.0"
41+
"loopback": "^2.25.0",
42+
"loopback-context": "^3.1.0",
43+
"cls-hooked": "^4.1.5"
4244
},
4345
"optionalDependencies": {
4446
"loopback-component-explorer": "2.3.0"
@@ -54,6 +56,8 @@
5456
"eslint-config-fullcube": "^2.0.2",
5557
"loopback": "^2.25.0",
5658
"loopback-boot": "^2.24.0",
59+
"loopback-context": "^3.1.0",
60+
"cls-hooked": "^4.1.5",
5761
"loopback-component-fixtures": "^1.1.0",
5862
"mocha": "^3.4.1",
5963
"mocha-sinon": "latest",

test/fixtures/simple-app/server/config.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
"host": "0.0.0.0",
44
"port": 3000,
55
"remoting": {
6-
"context": {
7-
"enableHttpContext": false
8-
},
6+
"context": false,
97
"rest": {
108
"normalizeHttpPath": false,
119
"xml": false
@@ -23,5 +21,6 @@
2321
"disableStackTrace": false
2422
}
2523
},
26-
"legacyExplorer": false
24+
"legacyExplorer": false,
25+
"logoutSessionsOnSensitiveChanges": true
2726
}

test/fixtures/simple-app/server/middleware.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
{
2-
"initial:before": {
3-
"loopback#favicon": {},
4-
"loopback#context": {},
5-
"loopback#token": {}
2+
"initial": {
3+
"loopback-context#per-request": {
4+
"params": {
5+
"enableHttpContext": true
6+
}
7+
}
68
},
79
"session": {},
8-
"auth": {},
10+
"auth": {
11+
"loopback#token": {}
12+
},
913
"parse": {},
1014
"routes": {
1115
"loopback#rest": {

test/middleware-test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
const path = require('path')
44
const chai = require('chai')
5+
const LoopBackContext = require('loopback-context')
6+
57
const { expect } = chai
68

79
chai.use(require('dirty-chai'))
@@ -23,8 +25,8 @@ describe('User Context Middleware', function() {
2325

2426
describe('With user in loopback context', function() {
2527
it('should return the user', function() {
26-
app.loopback.runInContext(function() {
27-
const loopbackContext = app.loopback.getCurrentContext()
28+
LoopBackContext.runInContext(function() {
29+
const loopbackContext = LoopBackContext.getCurrentContext()
2830
const user = {
2931
id: 'generalUser',
3032
username: 'generalUser',

0 commit comments

Comments
 (0)