Skip to content

Commit

Permalink
testing ctx options injection
Browse files Browse the repository at this point in the history
  • Loading branch information
ebarault committed Dec 13, 2016
1 parent baa5614 commit 09b9a8c
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 10 deletions.
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@
"compression": "^1.0.3",
"cors": "^2.5.2",
"helmet": "^1.3.0",
"loopback-boot": "^2.6.5",
"loopback-component-explorer": "^2.4.0",
"loopback-boot": "^2.23.0",
"loopback-component-explorer": "^2.7.0",
"serve-favicon": "^2.0.1",
"strong-error-handler": "^1.0.1",
"loopback-datasource-juggler": "^2.39.0",
"loopback": "^2.22.0"
"strong-error-handler": "^1.1.1",
"loopback": "git+https://github.com/strongloop/loopback.git#feature/options-from-context-v2"
},
"devDependencies": {
"eslint": "^2.13.1",
"eslint": "^3.7.1",
"eslint-config-loopback": "^4.0.0",
"nsp": "^2.1.0"
"nsp": "^2.6.2"
},
"repository": {
"type": "",
Expand Down
3 changes: 2 additions & 1 deletion server/boot/authentication.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

module.exports = function enableAuthentication(server) {
module.exports = function enableAuthentication(server, next) {
// enable authentication
server.enableAuth();
next();
};
3 changes: 2 additions & 1 deletion server/boot/root.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict';

module.exports = function(server) {
module.exports = function(server, next) {
// Install a `/` route that returns server status
var router = server.loopback.Router();
router.get('/', server.loopback.status());
server.use(router);
next();
};
17 changes: 17 additions & 0 deletions server/boot/zzz-createUser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

module.exports = function(server, next) {
var user = server.models.user;
user.create({email: '[email protected]', password: 'a1b2c3'})
.then(function(user) {
return user.accessTokens.create();
})
.then(function(token) {
console.log(token);
next();
})
.catch(function(err) {
console.log(err);
next();
});
};
2 changes: 1 addition & 1 deletion server/model-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"./mixins"
]
},
"User": {
"user": {
"dataSource": "db"
},
"AccessToken": {
Expand Down
61 changes: 61 additions & 0 deletions server/models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';

var Promise = require('bluebird');
var extend = require('util')._extend;

module.exports = function(user) {
user.on('attached', function(app) {
app.on('started', function() {
var Role = user.app.models.Role;
Role.registerResolver('$dynamicRole', function(role, ctx, next) {
ctx.remotingContext.args.options = ctx.remotingContext.args.options || {};
ctx.remotingContext.args.options.canInjectCtxOptionsInCustomRoleResolver = true;
next();
});
});
});

user.createOptionsFromRemotingContext = function(ctx) {
return extend(this.base.createOptionsFromRemotingContext(ctx), {
currentUserId: ctx.req.accessToken && ctx.req.accessToken.userId,
});
};

user.beforeRemote('testInjectedOptions', function(ctx, unused, next) {
if (!ctx.args.options.accessToken) return next();
ctx.args.options.canReadCtxOptionsInBeforeRemote = ctx.args.options.canInjectCtxOptionsInCustomRoleResolver ? true : false;
user.findById(ctx.args.options.accessToken.userId, function(err, user) {
if (err) return next(err);
ctx.args.options.currentUser = user;
next();
});
});

user.observe('loaded', function(ctx, next) {
ctx.options = ctx.options || {};
ctx.options.canReadCtxOptionsInObserveLoaded = ctx.options.canInjectCtxOptionsInCustomRoleResolver ? true : false;
ctx.options.canInjectCtxOptionsInObserveLoaded = true;
next();
});

user.remoteMethod('testInjectedOptions', {
http: {verb: 'get', path: '/:id/testInjectedOptions'},
accepts: [
{arg: 'id', type: 'string', required: true},
{arg: 'options', type: 'object', http: 'optionsFromRequest'},
],
returns: {arg: 'data', type: 'object', root: true},
});

user.testInjectedOptions = function(id, ctx) {
return Promise.resolve({
accessToken: ctx.accessToken,
user: ctx.currentUser,
currentUserId: ctx.currentUserId,
canInjectCtxOptionsInCustomRoleResolver: ctx.canInjectCtxOptionsInCustomRoleResolver || false,
canReadCtxOptionsInObserveLoaded: ctx.canReadCtxOptionsInObserveLoaded || false,
canInjectCtxOptionsInObserveLoaded: ctx.canInjectCtxOptionsInObserveLoaded || false,
canReadCtxOptionsInBeforeRemote: ctx.canReadCtxOptionsInBeforeRemote || false,
});
};
};
37 changes: 37 additions & 0 deletions server/models/user.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "user",
"plural": "users",
"base": "User",
"idInjection": true,
"strict": "true",
"options": {
"validateUpsert": true
},
"mixins": {},
"properties": {},
"hidden": [],
"protected": [],
"validations": [],
"relations": {},
"acls": [
{
"accessType": "EXECUTE",
"principalType": "ROLE",
"principalId": "$owner",
"permission": "ALLOW",
"property": [
"testInjectedOptions"
]
},
{
"accessType": "EXECUTE",
"principalType": "ROLE",
"principalId": "$dynamicRole",
"permission": "ALLOW",
"property": [
"testInjectedOptions"
]
}
],
"methods": {}
}

0 comments on commit 09b9a8c

Please sign in to comment.