koa-rbac
koa-rbac copied to clipboard
expose rbac-a instance
It'd be nice to expose an rbac-a instance so that its config can be applied in one place to use both rbac-a and koa-rbac. Alternatively, exposing some methods similar to the .allow, .deny, .check in koa-rbac but return a Boolean so it can be used inline too.
for example, let's say I have a PostGroup, which includes PostA and PostB, all authorized by rbac. When I share a PostGroup, I want to automatically share a Post to that user as well, but only if I have the 'share:post' permission on that Post. Otherwise, just share the PostGroup and the Posts where I have that permission.
I'd use koa-rbac middleware to check for 'share:postgroup', but I'd need to use rbac .check (share:post) somewhere inside the handler as well. I'd prefer it use the same rbac-a instance to ensure the config is identical.
(hope that example makes sense, I realize it's a bit complicated)
Sure, I think I understand your example. And this is already implemented, actually. This module already exposes rbac-a
, so you can just define your instance and preserve a copy somewhere and re-use it whenever you need it.
const rbac = require('koa-rbac');
const permissions = new rbac.RBAC( /* rbac-a options here */ );
...
app.use(rbac.middleware({
rbac: permissions,
...
});
app.use(function * (next) {
let allowed = permissions.check(this.user, 'share:postgroup');
...
});
Note: I will release a next major version sometime to support the next koa@2. Will update the docs as well. It's on the todo list :stuck_out_tongue:
@yanickrochon I may spend a little bit of time porting it to Koa v2 if you don't have time in the next couple of weeks, as I need to use it for a project I'm doing. :)
Sure! I will be starting a new project with koa@2 in a few weeks, but I am still in the process of moving, so I don't have time at the moment. If you want, the changes should be trivial. Thanks!
Ping. I just stay in stuck with the problem how to move that to koa 2. Is there some news?
Thanks.
I will update RBAC-A (hopefully soon!) and will also update this module to close this issue. I know it has been a while, but this is not dead!