Skip to content

Commit

Permalink
Merge pull request #1 from voxgig/adding_user_org
Browse files Browse the repository at this point in the history
Add usr and org to msg from `meta.custom`
  • Loading branch information
rjrodger committed Aug 21, 2018
2 parents 9f6f3a7 + 5a96041 commit 373bf2b
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 43 deletions.
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"semi": false,
"singleQuote": true
}
54 changes: 27 additions & 27 deletions owner.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,46 @@ const Optioner = require('optioner')
const Joi = Optioner.Joi

const optioner = Optioner({
userprop: Joi.string().default('user'),
entprop: Joi.string().default('ent'),
inbound: Joi.array().required(),
qprop: Joi.string().default('q'),
annotate: Joi.array().required(),
allowprop: Joi.string().default('allow')
})

module.exports = function owner(options) {
const seneca = this
const opts = optioner.check(options)
const userprop = opts.userprop
const allowprop = opts.allowprop
const entprop = opts.entprop

// console.log(userprop, entprop)

opts.inbound.forEach(function(msgpat) {
seneca.wrap(msgpat, function(msg, reply) {
// TODO: error if userprop not found: configurable
var userdata = msg[userprop]


// TODO: fixedargs should be renamed
this.fixedargs[userprop] = userdata

this.prior(msg, reply)
})
})
const qprop = opts.qprop

opts.annotate.forEach(function(msgpat) {
seneca.add(msgpat, function(msg, reply) {
var userdata = msg[userprop]
var ent = msg[entprop]

// TODO: make these props configurable too
ent.user = userdata.id
ent.org = userdata.org
seneca.add(msgpat, function(msg, reply, meta) {
var usrdata = meta.custom[allowprop]

if (usrdata) {
if (msg.cmd === 'list') {
var q = msg[qprop]
if (!q.usr && usrdata.usr) {
q.usr = usrdata.usr
}
if (!q.org && usrdata.org) {
q.org = usrdata.org
}
} else {
var ent = msg[entprop]
if (!ent.usr && usrdata.usr) {
ent.usr = usrdata.usr
}
if (!ent.org && usrdata.org) {
ent.org = usrdata.org
}
}
}

this.prior(msg, reply)
})
})
}

const intern = (module.exports.intern = {
})
const intern = (module.exports.intern = {})
121 changes: 105 additions & 16 deletions test/owner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,119 @@ const Plugin = require('..')

lab.test('validate', PluginValidator(Plugin, module))


lab.test('happy', fin => {
Seneca()
.test(fin)
.use('entity')
.add('role:foo,add:bar', function (msg, reply) {
this
.make('bar', {who:msg.who})
.save$(reply)
.add('role:foo,add:bar', function(msg, reply) {
this.make('core/act').save$(reply)
})
.use('..', {
inbound: ['role:foo'],
annotate: ['role:entity,cmd:save,name:bar']
annotate: ['role:entity,cmd:save,base:core']
})
.delegate(null, {
custom: {
allow: {
usr: 'alice',
org: 'alice'
}
}
})
.ready(function() {
this.act('role:foo,add:bar,who:zed',
{user:{id:'u0', org:'o0'}},
function(err, out) {
expect(out.who).equal('zed')
expect(out.user).equal('u0')
expect(out.org).equal('o0')
fin()
})
this.act('role:foo,add:bar', function(err, out) {
expect(out.usr).equal('alice')
expect(out.org).equal('alice')
})
})
})

Seneca()
.test(fin)
.use('entity')
.add('role:foo,add:bar', function(msg, reply) {
this.make('core/act').save$(reply)
})
.use('..', {
annotate: ['role:entity,cmd:save,base:core']
})
.delegate(null, {
custom: {
allow: {}
}
})
.ready(function() {
this.act('role:foo,add:bar', function(err, out) {
expect(out.usr).equal(undefined)
expect(out.org).equal(undefined)
})
})

Seneca()
.test(fin)
.use('entity')
.add('role:foo,add:bar', function(msg, reply) {
this.make('core/act', { usr: msg.usr, org: msg.org }).save$(reply)
})
.add('role:foo,query:foo', function(msg, reply) {
this.make('core/act').list$(reply)
})
.add('role:foo,query:bar', function(msg, reply) {
this.make('core/act').list$({ usr: 'john', org: 'john' }, reply)
})
.use('..', {
annotate: [
'role:entity,cmd:save,base:core',
'role:entity,cmd:list,base:core'
]
})
.delegate(null, {
custom: {
allow: {
usr: 'alice',
org: 'alice'
}
}
})
.ready(function() {
this.act('role:foo,add:bar', { usr: 'john', org: 'john' }, function(
err,
out
) {
expect(out.usr).equal('john')
expect(out.org).equal('john')
})
this.act('role:foo,add:bar', function(err, out) {
expect(out.usr).equal('alice')
expect(out.org).equal('alice')
})
this.act('role:foo,query:foo', function(err, query_out) {
expect(query_out.length).equal(1)

var query_out_data = query_out[0]
expect(query_out_data.usr).equal('alice')
expect(query_out_data.org).equal('alice')
})
this.act('role:foo,query:bar', function(err, query_out) {
expect(query_out.length).equal(1)

var query_out_data = query_out[0]
expect(query_out_data.usr).equal('john')
expect(query_out_data.org).equal('john')
})
})

Seneca()
.test(fin)
.use('entity')
.add('role:foo,add:bar', function(msg, reply) {
this.make('core/act').save$(reply)
})
.use('..', {
annotate: ['role:entity,cmd:save,base:core']
})
.ready(function() {
this.act('role:foo,add:bar', function(err, out) {
expect(out.usr).equal(undefined)
fin()
})
})
})

0 comments on commit 373bf2b

Please sign in to comment.