koa-rbac icon indicating copy to clipboard operation
koa-rbac copied to clipboard

expose rbac-a instance

Open azhang opened this issue 8 years ago • 5 comments

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)

azhang avatar Mar 16 '16 01:03 azhang

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 avatar Mar 16 '16 04:03 yanickrochon

@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. :)

bbqsrc avatar Jun 22 '16 11:06 bbqsrc

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!

yanickrochon avatar Jun 22 '16 11:06 yanickrochon

Ping. I just stay in stuck with the problem how to move that to koa 2. Is there some news?

Thanks.

ValeriyDP avatar Feb 27 '17 09:02 ValeriyDP

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!

yanickrochon avatar Jul 24 '17 03:07 yanickrochon