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

getAllNamedRoles returning null #38

Closed
lucaslrolim opened this issue Jan 3, 2019 · 6 comments
Closed

getAllNamedRoles returning null #38

lucaslrolim opened this issue Jan 3, 2019 · 6 comments
Assignees
Labels
invalid This doesn't seem right

Comments

@lucaslrolim
Copy link

lucaslrolim commented Jan 3, 2019

I have implemented these two methods:

    async getAllUserRoles(){
        let roles = this.enforcer.getAllNamedRoles('g');
        return roles;
    }

    async getAllResourceRoles(){
        let roles = this.enforcer.getAllNamedRoles('g2');
        return roles;
    }

And some of my policies are:

g, test_role, admin
g, sub_role_test1, test_role
g, sub_role_test2, test_role
g, sub_role_test2, test_role
g, test_role_2, admin
g, user_test, just_test_role
g2, glosas, plataforma
g2, credentials, plataforma

In both methods the result is null. Is this a bug?

@nodece
Copy link
Member

nodece commented Jan 4, 2019

Can you provide model conf.

@hsluoyz
Copy link
Member

hsluoyz commented Jan 4, 2019

@lucaslrolim We have added some tests here: 43e2654 . And it shows e.getAllRoles() works. Can you try this test and see if there's anything wrong with your code.

@hsluoyz hsluoyz added the bug Something isn't working label Jan 4, 2019
@lucaslrolim
Copy link
Author

lucaslrolim commented Jan 4, 2019

@nodece
My model is:

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

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

[role_definition]
g = _, _
g2 = _, _

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

[matchers]
m = g(r.sub, p.sub) && g2(r.obj, p.obj) && (regexMatch(r.dom, p.dom) || r.dom ===  '') && (regexMatch(r.act, p.act) || p.act ===  'all')

@hsluoyz I'm using getAllNamedRoles, maybe this method has differences of getAllRoles. I will double check my code.

@nodece
Copy link
Member

nodece commented Jan 5, 2019

dev env

node: v8.11.2
casbin: v1.1.9

simple test

async function test() {
  const modelText = `
[request_definition]
r = sub, obj, dom, act

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

[role_definition]
g = _, _
g2 = _, _

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

[matchers]
m = g(r.sub, p.sub) && g2(r.obj, p.obj) && (regexMatch(r.dom, p.dom) || r.dom ===  '') && (regexMatch(r.act, p.act) || p.act ===  'all')
`;
  const m = newModel(modelText);
  const e = await newEnforcer(m);
  await e.addNamedGroupingPolicy('g', 'test_role', 'admin');
  await e.addNamedGroupingPolicy('g', 'sub_role_test1', 'test_role');
  await e.addNamedGroupingPolicy('g', 'sub_role_test2', 'test_role');
  await e.addNamedGroupingPolicy('g', 'sub_role_test2', 'test_role');
  await e.addNamedGroupingPolicy('g', 'test_role_2', 'admin');
  await e.addNamedGroupingPolicy('g', 'user_test', 'just_test_role');
  await e.addNamedGroupingPolicy('g2', 'glosas', 'plataforma');
  await e.addNamedGroupingPolicy('g2', 'credentials', 'plataforma');
  const g = e.getAllNamedRoles('g');
  console.log(g, g.length !== 0 && g.some(n => n === 'admin'));
  const g2 = e.getAllNamedRoles('g2');
  console.log(g2, g2.length !== 0 && g2.some(n => n === 'plataforma'));
}

print result

[ 'admin', 'test_role', 'just_test_role' ] true
[ 'plataforma' ] true

You can try using the above test :)

@hsluoyz
Copy link
Member

hsluoyz commented Jan 5, 2019

@lucaslrolim getAllRoles() calls getAllNamedRoles() internally, so it proves this function works for at least for the example.

@lucaslrolim
Copy link
Author

The error was really related to the order of my Express routes. Sorry about the mistake e thank you for the help @hsluoyz and @nodece.

@hsluoyz hsluoyz self-assigned this Jul 5, 2019
@hsluoyz hsluoyz added invalid This doesn't seem right and removed bug Something isn't working labels Jul 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

3 participants