Skip to content

Commit

Permalink
fix: field_index is incorrect in RBAC with domains mode (#345)
Browse files Browse the repository at this point in the history
  • Loading branch information
truc0 committed Jun 11, 2024
1 parent 846cf24 commit 54bc5a0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
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)

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

0 comments on commit 54bc5a0

Please sign in to comment.