Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple policies config? #311

Open
yamilfrich opened this issue Aug 12, 2021 · 24 comments
Open

Multiple policies config? #311

yamilfrich opened this issue Aug 12, 2021 · 24 comments
Assignees
Labels
enhancement New feature or request

Comments

@yamilfrich
Copy link

Hi, I'm new with Casbin and I'm trying to configure it with multiple policies like:

  • Feature policy (for each subscription plan, it will contain or not certain features):
    For example, for the subscription plan "professional" it will have the feature "insights"
    My policy config for this would be probably just:
    p = sub, obj

  • Roles policy (basic ACL, like: admin, users, write):
    My policy config would be:
    p2 = sub, obj, act

I read on https://casbin.org/docs/en/syntax-for-models#policy-definition that it's actually possible to define multiple policies and you have some examples below, but I can't find the way to make it work on node-casbin.

When adding the policy in the DB (I'm using casbin-sequelize-adapter) it allows you to use "named" policies by using: e.addNamedPolicies.

But when you want to enforce and choose which namedPolicy you want to use, I can't find how to do so.

This is the config I'm trying (I'm repeating all just in case based on the docs):

[request_definition]
r = sub, obj
r2 = sub, obj, act

[policy_definition]
p = sub, act # features
p2 = sub, obj, act # roles

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = r.sub == p.sub && r.obj == p.obj
m2 = r.sub == p.sub && r.obj == p.obj && r.act == p.act

Hope this is clear, thanks in advance.

@hsluoyz
Copy link
Member

hsluoyz commented Aug 12, 2021

@Zxilly @Gabriel-403 @closetool @tangyang9464

@hsluoyz hsluoyz self-assigned this Aug 12, 2021
@hsluoyz hsluoyz added the question Further information is requested label Aug 12, 2021
@tangyang9464
Copy link
Member

It seems that node-casbin does not support this feature now.

@tangyang9464
Copy link
Member

tangyang9464 commented Aug 12, 2021

@yamilfrich your m2 have an error.
Should it be r2

@Gabriel-403
Copy link
Contributor

It seems that node-casbin does not support this feature now.

I think so,and this seems that I wrote this part

@yamilfrich
Copy link
Author

@yamilfrich your m2 have an error.
Should it be r2

Not sure, I couldn't make it work anyway, so I couldn't validate if I need to reference to r2, p2, etc.

It seems that node-casbin does not support this feature now.

I think so,and this seems that I wrote this part

So, how would you solve this? Different configs + Different Enforcers?

If so, can they still share the same adapter + db_table?

@tangyang9464
Copy link
Member

@yamilfrich r-p-e-m four type must one-to-one correspondence. In Go-casbin you can pass in a EnforceContext to specify which types you need to use for your request. See Multiple sections type

@yamilfrich
Copy link
Author

@yamilfrich r-p-e-m four type must one-to-one correspondence. In Go-casbin you can pass in a EnforceContext to specify which types you need to use for your request. See Multiple sections type

Ok thank you 😄 , but I have node, so I need to find a way to make this work with node. Should it be with multiple configs and multiple enforcers? Can they share the same db_table?

@tangyang9464
Copy link
Member

@yamilfrich r-p-e-m four type must one-to-one correspondence. In Go-casbin you can pass in a EnforceContext to specify which types you need to use for your request. See Multiple sections type

Ok thank you 😄 , but I have node, so I need to find a way to make this work with node. Should it be with multiple configs and multiple enforcers? Can they share the same db_table?

node-casbin is implementing this feature. It should be consistent with go-casbin

@hsluoyz hsluoyz added enhancement New feature or request and removed question Further information is requested labels Aug 13, 2021
@hsluoyz
Copy link
Member

hsluoyz commented Aug 13, 2021

@Zxilly @Gabriel-403 plz implement it

@Gabriel-403
Copy link
Contributor

@Zxilly @Gabriel-403 plz implement it

ok!

@Gabriel-403
Copy link
Contributor

@yamilfrich

This function has been added and can be used later

@yamilfrich
Copy link
Author

yamilfrich commented Aug 26, 2021

Thank you, how can we implement it? Is there any code example? 🙏🏻 I'll look in the changes and the code and see if I can manage my way.

@Zxilly
Copy link
Contributor

Zxilly commented Aug 26, 2021

@yamilfrich Full discussion can be found at casbin/casbin.js#172 casbin/casbin.js#176. If that PR got merged, we will apply it to node-casbin immediately.

@rashid301
Copy link

any ETA on this? We have a need for this soon.

@Zxilly
Copy link
Contributor

Zxilly commented Aug 31, 2021

@rashid301 An early bird version is available at casbin.js@next, but it has some limitation right now. The migration will take place when it is functionally complete.
You can find casbin.js@next at https://github.com/casbin/casbin.js/tree/v1

@Shivansh-yadav13
Copy link
Member

Shivansh-yadav13 commented Mar 18, 2022

hey, can I work on this? it is in the docs but not present.

@vaseala
Copy link

vaseala commented Jun 25, 2022

how's the progress?

@vaseala
Copy link

vaseala commented Jun 25, 2022

@Gabriel-403

It seems that node-casbin does not support this feature now.

I think so,and this seems that I wrote this part

https://github.com/Gabriel-403/node-casbin/blob/master/src/coreEnforcer.ts#L378

I find you fixed it, but why not merged it to master branch?

@hsluoyz
Copy link
Member

hsluoyz commented Jun 25, 2022

Can we merge this code to origin? @Gabriel-403 @Shivansh-yadav13

/cc @nodece @Zxilly

@Shivansh-yadav13
Copy link
Member

Shivansh-yadav13 commented Jun 26, 2022

I'm not sure of it's working
how is this returning true

import { newEnforcer, newModel, MemoryAdapter, EnforceContext } from 'casbin';

const ec = new EnforceContext('r2', 'p2', 'e', 'm2');

const model = newModel(`
[request_definition]
r = sub, obj
r2 = sub, obj, act

[policy_definition]
p = sub, obj
p2 = sub, obj, act

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = r.sub == p.sub && r.obj == p.obj
m2 = r.sub == p.sub && r.obj == p.obj && r.act == p.act
`);

const adapter = new MemoryAdapter(`
p2, alice, data1, read
p2, bob, data2, write
`);

const enforcer = await newEnforcer(model, adapter);

const res = await enforcer.enforce(ec, 'alice', 'data2', 'read');
console.log(res);

@nodece
Copy link
Member

nodece commented Jun 26, 2022

@Shivansh-yadav13 Could you cherry-pick the cc58c57 and 33c784c to your branch, then make a PR?

@nodece
Copy link
Member

nodece commented Jun 26, 2022

This should be returned false, could you work on this?

I'm not sure of it's working how is this returning true

import { newEnforcer, newModel, MemoryAdapter, EnforceContext } from 'casbin';

const ec = new EnforceContext('r2', 'p2', 'e', 'm2');

const model = newModel(`
[request_definition]
r = sub, obj
r2 = sub, obj, act

[policy_definition]
p = sub, obj
p2 = sub, obj, act

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = r.sub == p.sub && r.obj == p.obj
m2 = r.sub == p.sub && r.obj == p.obj && r.act == p.act
`);

const adapter = new MemoryAdapter(`
p2, alice, data1, read
p2, bob, data2, write
`);

const enforcer = await newEnforcer(model, adapter);

const res = await enforcer.enforce(ec, 'alice', 'data2', 'read');
console.log(res);

also I think we should set default values here

export class EnforceContext {

@Shivansh-yadav13
Copy link
Member

This should be returned false, could you work on this?

@nodece sorry it should be m2 = r2.sub == p2.sub && r2.obj == p2.obj && r2.act == p2.act

@hsluoyz
Copy link
Member

hsluoyz commented Jun 28, 2022

@yamilfrich

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Development

Successfully merging a pull request may close this issue.

9 participants