node-casbin icon indicating copy to clipboard operation
node-casbin copied to clipboard

enforcer.addGroupingPolicy

Open Murthybhat opened this issue 5 years ago • 6 comments

I am trying to do a basic authorization by adding a policy and a grouping policy. However the api to addGroupingPolicy does not seem to work for me. Can someone help if there is an issue or am I doing something wrong.

casbin version : 5.7.1

// For Node.js:
const { newEnforcer } = require('casbin');
const sub = 'alice';
const obj = 'data';
const act = 'read'; 

async function run() {
    const enforcer = await newEnforcer('./examples/basic_model.conf');

    const p = ['group1', 'data', 'read']
    var added = await enforcer.addPolicy(...p)
    console.log('Is Policy added = ', added);

    const g = ['alice', 'group1']
    var gpAdded = await enforcer.addGroupingPolicy(...g)
    // const gpAdded = await enforcer.addNamedGroupingPolicy('g', 'alice', 'group1');
    console.log('Is GroupingPolicy added = ', gpAdded);

    const allPolicies = await enforcer.getPolicy()
    console.log('All Policies : ', allPolicies);

   // authorize alice
    var res = await enforcer.enforce(sub, obj, act);
    if (res) {
        // permit alice to read data1
        console.log('Access granted');
    } else {
        console.log('Access denied');
        // deny the request, show an error
    }
}

run();

Murthybhat avatar May 31 '21 12:05 Murthybhat

@Murthybhat Are you using jcasbin?

shink avatar May 31 '21 12:05 shink

No. I will move it to Node-Casbin

hsluoyz avatar May 31 '21 12:05 hsluoyz

@Zxilly

hsluoyz avatar May 31 '21 16:05 hsluoyz

working on this

Zxilly avatar Jun 01 '21 18:06 Zxilly

@Murthybhat You should not use ./examples/basic_model.conf. Since it didn't include a so-called g section, which will make RBAC works. You should use rbac_model.conf , which include

[role_definition]
g = _, _

Zxilly avatar Jun 09 '21 04:06 Zxilly

Closed as resolved.

hsluoyz avatar Jun 10 '21 05:06 hsluoyz