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

fix: field_index is incorrect in RBAC with domains mode (#345) #346

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions casbin/management_enforcer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,26 @@ def get_all_subjects(self):

def get_all_named_subjects(self, ptype):
"""gets the list of subjects that show up in the current named policy."""
return self.model.get_values_for_field_in_policy("p", ptype, 0)
field_index = self.model.get_field_index(ptype, "sub")
return self.model.get_values_for_field_in_policy("p", ptype, field_index)

def get_all_objects(self):
"""gets the list of objects that show up in the current policy."""
return self.get_all_named_objects("p")

def get_all_named_objects(self, ptype):
"""gets the list of objects that show up in the current named policy."""
return self.model.get_values_for_field_in_policy("p", ptype, 1)
field_index = self.model.get_field_index(ptype, "obj")
return self.model.get_values_for_field_in_policy("p", ptype, field_index)

def get_all_actions(self):
"""gets the list of actions that show up in the current policy."""
return self.get_all_named_actions("p")

def get_all_named_actions(self, ptype):
"""gets the list of actions that show up in the current named policy."""
return self.model.get_values_for_field_in_policy("p", ptype, 2)
field_index = self.model.get_field_index(ptype, "act")
return self.model.get_values_for_field_in_policy("p", ptype, field_index)
hsluoyz marked this conversation as resolved.
Show resolved Hide resolved

def get_all_roles(self):
"""gets the list of roles that show up in the current named policy."""
Expand Down
12 changes: 12 additions & 0 deletions tests/test_management_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ def test_get_list(self):
self.assertEqual(e.get_all_actions(), ["read", "write"])
self.assertEqual(e.get_all_roles(), ["data2_admin"])

def test_get_list_with_domains(self):
e = self.get_enforcer(
get_examples("rbac_with_domains_model.conf"),
get_examples("rbac_with_domains_policy.csv"),
# True,
)

self.assertEqual(e.get_all_subjects(), ["admin"])
self.assertEqual(e.get_all_objects(), ["data1", "data2"])
self.assertEqual(e.get_all_actions(), ["read", "write"])
self.assertEqual(e.get_all_roles(), ["admin"])

def test_get_policy_api(self):
e = self.get_enforcer(
get_examples("rbac_model.conf"),
Expand Down