node-casbin
node-casbin copied to clipboard
How to add implicit role for user
Hi,
We have below policy for RBAC
g, monoSamples_p3, monoSamples_p1 g, monoSamples_p3, monoSamples_p2 p, monoSamples_p1, monoSamples, p1 p, monoSamples_p2, monoSamples, p2
We use below model: [request_definition] r = sub, obj, act
[policy_definition] p = sub, obj, act
[role_definition] g = _, _
[policy_effect] e = some(where (p.eft == allow))
[matchers] m = g(r.sub, p.sub) && keyMatch(r.obj, p.obj) && regexMatch(r.act, p.act)
How can I add user Alice to role monoSamples_p3 which is implicit role ?
I have tried e.addRoleForuser, rolemanager.AddLink etc, which does not work. Please let me know the API
End result I want to achieve is to have below line added to my policy g, Alice, monoSamples_p3
@nodece @Zxilly @Shivansh-yadav13
@shridhar-purandare, I have checked, await e.addRoleForUser('alice', 'monoSamples_p3');
and rm.addLink('alice', 'monoSamples_p3');
both are working and adding the role. Can you give code how you tried? Maybe a problem is there?
@Shivansh-yadav13 : I am calling this as well. await e.addRoleForUser('alice', 'monoSamples_p3'); But it does not work. Because monoSamples_p3 this is kind of user as well because it has g, line. and not p line. It's an inherited role.
Can you please confirm where you used model and policy described above ?
@shridhar-purandare I have tested locally with the policies and the model you have given.
And it should not be a problem even if monoSamples_p3
is a user.
code
import { newEnforcer } from "casbin";
const e = await newEnforcer('./model.conf', './policy.csv');
await e.addRoleForUser('alice', 'monoSamples_p3');
console.log(await e.getImplicitRolesForUser('alice'));
output
[ 'monoSamples_p3', 'monoSamples_p1', 'monoSamples_p2' ]
@Shivansh-yadav13 : can you please paste your generated policy ?
@shridhar-purandare the update in the policy is only in the instance of the enforcer and it does not reflects in the policy.csv
file, this features has been disabled (in-case you are expecting that).
This is what the policy looks in the enforcer after adding the role.
[ 'monoSamples_p3', 'monoSamples_p1', 'monoSamples_p2' ] // g
[
[ 'monoSamples_p1', 'monoSamples', 'p1' ],
[ 'monoSamples_p2', 'monoSamples', 'p2' ]
]
Closed as stale